use of io.xol.chunkstories.api.exceptions.net.UnknowPacketException in project chunkstories by Hugobros3.
the class StreamGobbler method run.
@Override
public void run() {
try {
while (connection.isOpen()) {
LogicalPacketDatagram datagram = connection.getPacketsContext().digestIncommingPacket(in);
connection.handleDatagram(datagram);
}
} catch (SocketException e) {
// Natural
if (connection.isOpen()) {
logger.info("Closing socket.");
}
connection.close();
} catch (EOFException e) {
// Natural too
connection.close();
logger.info("Connection closed");
} catch (IOException e) {
connection.close();
logger.info("Connection error", e);
} catch (UnknowPacketException e) {
logger.error("Unknown packet", e);
connection.close();
} catch (PacketProcessingException e) {
connection.close();
logger.error("Error processing packet", e);
} catch (IllegalPacketException e) {
logger.error("Illegal packet", e);
connection.close();
}
}
use of io.xol.chunkstories.api.exceptions.net.UnknowPacketException in project chunkstories by Hugobros3.
the class PacketsContextCommon method buildOutgoingPacket.
public PacketOutgoing buildOutgoingPacket(Packet packet) throws UnknowPacketException, IOException {
try {
short packet_id = findIdForPacket(packet);
if (packet instanceof PacketSendFile) {
return new PacketOutgoing() {
@Override
public void write(DataOutputStream out) throws IOException {
writePacketIdHeader(out, packet_id);
packet.send(getInterlocutor(), out, PacketsContextCommon.this);
}
};
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
packet.send(getInterlocutor(), dos, this);
PacketOutgoingBuffered buffered = new PacketOutgoingBuffered(this, packet_id, baos.size(), baos.toByteArray());
return buffered;
} catch (IOException | UnknowPacketException e) {
logger().error("Error : unable to buffer Packet " + packet, e);
// e.printStackTrace(logger().getPrintWriter());
}
return null;
}
use of io.xol.chunkstories.api.exceptions.net.UnknowPacketException in project chunkstories by Hugobros3.
the class PacketsContextCommon method findIdForPacket.
private short findIdForPacket(Packet packet) throws UnknowPacketException {
WorldNetworked world = this.getWorld();
if (world == null) {
if ((packet instanceof PacketText))
return 0x00;
else if ((packet instanceof PacketSendFile))
return 0x01;
else
// Throw an error if sending a packet while not within a world
throw new UnknowPacketException(0xFF);
} else {
PacketDefinition def = world.getContent().packets().getPacketFromInstance(packet);
if (def == null)
logger.error("Could not find the definition of packet " + packet);
short id = (short) world.getContentTranslator().getIdForPacket(def);
if (id == -1) {
logger.error("Could not find the id of packet definition " + def.getName());
// ((AbstractContentTranslator)world.getContentTranslator()).test();
throw new UnknowPacketException(packet);
}
return id;
}
}
Aggregations