use of io.xol.chunkstories.client.net.ClientPacketsContext in project chunkstories by Hugobros3.
the class PacketInitializeRemoteWorld method process.
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
String initializationString = in.readUTF();
ByteArrayInputStream bais = new ByteArrayInputStream(initializationString.getBytes("UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(bais, "UTF-8"));
info = new WorldInfoImplementation(reader);
if (processor instanceof ClientPacketsContext) {
processor.logger().info("Received World initialization packet");
ClientPacketsContext cpp = (ClientPacketsContext) processor;
OnlineContentTranslator contentTranslator = cpp.getContentTranslator();
if (contentTranslator == null) {
processor.logger().error("Can't initialize a world without a ContentTranslator initialized first!");
return;
}
// TODO should we expose this to the interface ?
Client client = (Client) cpp.getContext();
Fence fence = client.getGameWindow().queueSynchronousTask(new Runnable() {
@Override
public void run() {
WorldClientRemote world;
try {
world = new WorldClientRemote(client, info, contentTranslator, cpp.getConnection());
client.changeWorld(world);
cpp.getConnection().handleSystemRequest("world/ok");
} catch (WorldLoadingException e) {
client.exitToMainMenu(e.getMessage());
}
}
});
fence.traverse();
}
}
use of io.xol.chunkstories.client.net.ClientPacketsContext in project chunkstories by Hugobros3.
the class PacketReceiveFile method process.
@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, PacketProcessingException {
if (!(processor instanceof ClientPacketsContext))
return;
// ClientPacketsContext cppi = (ClientPacketsContext)processor;
String fileTag = in.readUTF();
long fileLength = in.readLong();
if (fileLength > 0) {
PacketsContextCommon context = (PacketsContextCommon) processor;
PendingDownload pendingDownload = context.getConnection().getLocationForExpectedFile(fileTag);
if (pendingDownload == null)
throw new IOException("Received unexpected PacketFile with tag: " + fileTag);
// Class to report back the download status to whoever requested it via the onStart callback
PacketFileDownloadStatus status = new PacketFileDownloadStatus((int) fileLength);
if (pendingDownload.a != null)
pendingDownload.a.onStart(status);
long remaining = fileLength;
FileOutputStream fos = new FileOutputStream(pendingDownload.f);
byte[] buffer = new byte[4096];
while (remaining > 0) {
long toRead = Math.min(4096, remaining);
System.out.println("Working ! ...");
// cppi.getConnection().getCurrentlyDownloadedFileProgress().setStepText("Downloading "+fileTag+", "+(fileLength - remaining)/1024+"/"+fileLength/1024+"kb");
int actuallyRead = in.read(buffer, 0, (int) toRead);
fos.write(buffer, 0, (int) actuallyRead);
remaining -= actuallyRead;
status.downloaded += actuallyRead;
}
status.end.release();
fos.close();
}
}
Aggregations