Search in sources :

Example 1 with UnknowPacketException

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();
    }
}
Also used : PacketProcessingException(io.xol.chunkstories.api.exceptions.PacketProcessingException) SocketException(java.net.SocketException) EOFException(java.io.EOFException) IllegalPacketException(io.xol.chunkstories.api.exceptions.net.IllegalPacketException) IOException(java.io.IOException) UnknowPacketException(io.xol.chunkstories.api.exceptions.net.UnknowPacketException) LogicalPacketDatagram(io.xol.chunkstories.net.LogicalPacketDatagram)

Example 2 with UnknowPacketException

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;
}
Also used : PacketSendFile(io.xol.chunkstories.net.packets.PacketSendFile) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UnknowPacketException(io.xol.chunkstories.api.exceptions.net.UnknowPacketException)

Example 3 with UnknowPacketException

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;
    }
}
Also used : PacketText(io.xol.chunkstories.api.net.packets.PacketText) PacketSendFile(io.xol.chunkstories.net.packets.PacketSendFile) WorldNetworked(io.xol.chunkstories.api.world.WorldNetworked) PacketDefinition(io.xol.chunkstories.api.net.PacketDefinition) UnknowPacketException(io.xol.chunkstories.api.exceptions.net.UnknowPacketException)

Aggregations

UnknowPacketException (io.xol.chunkstories.api.exceptions.net.UnknowPacketException)3 PacketSendFile (io.xol.chunkstories.net.packets.PacketSendFile)2 IOException (java.io.IOException)2 PacketProcessingException (io.xol.chunkstories.api.exceptions.PacketProcessingException)1 IllegalPacketException (io.xol.chunkstories.api.exceptions.net.IllegalPacketException)1 PacketDefinition (io.xol.chunkstories.api.net.PacketDefinition)1 PacketText (io.xol.chunkstories.api.net.packets.PacketText)1 WorldNetworked (io.xol.chunkstories.api.world.WorldNetworked)1 LogicalPacketDatagram (io.xol.chunkstories.net.LogicalPacketDatagram)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 SocketException (java.net.SocketException)1