Search in sources :

Example 1 with LogicalPacketDatagram

use of io.xol.chunkstories.net.LogicalPacketDatagram 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 LogicalPacketDatagram

use of io.xol.chunkstories.net.LogicalPacketDatagram in project chunkstories by Hugobros3.

the class WorldServer method processIncommingPackets.

public void processIncommingPackets() {
    try {
        entitiesLock.writeLock().lock();
        Iterator<PendingPlayerDatagram> iterator = packetsQueue.iterator();
        while (iterator.hasNext()) {
            PendingPlayerDatagram incomming = iterator.next();
            iterator.remove();
            ServerPlayer player = incomming.player;
            LogicalPacketDatagram datagram = incomming.datagram;
            try {
                // this.getContentTranslator().getPacketForId(datagram.packetTypeId);
                PacketDefinitionImplementation definition = (PacketDefinitionImplementation) datagram.packetDefinition;
                Packet packet = definition.createNew(false, this);
                if (definition.getGenre() != PacketGenre.WORLD || !(packet instanceof PacketWorld)) {
                    logger().error(definition + " isn't a PacketWorld");
                } else {
                    PacketWorld packetWorld = (PacketWorld) packet;
                    // packetsProcessor.getSender() is equivalent to player here
                    packetWorld.process(player, datagram.getData(), player.getPlayerConnection().getPacketsContext());
                }
            } catch (IOException | PacketProcessingException e) {
                logger().warn("Networking Exception while processing datagram: " + e.getMessage());
            } catch (Exception e) {
                logger().warn("Exception while processing datagram: " + e.getMessage());
            }
            datagram.dispose();
        }
    } finally {
        entitiesLock.writeLock().unlock();
    }
}
Also used : PacketProcessingException(io.xol.chunkstories.api.exceptions.PacketProcessingException) Packet(io.xol.chunkstories.api.net.Packet) ServerPlayer(io.xol.chunkstories.server.player.ServerPlayer) PacketDefinitionImplementation(io.xol.chunkstories.net.PacketDefinitionImplementation) IOException(java.io.IOException) LogicalPacketDatagram(io.xol.chunkstories.net.LogicalPacketDatagram) PacketWorld(io.xol.chunkstories.api.net.PacketWorld) IOException(java.io.IOException) PacketProcessingException(io.xol.chunkstories.api.exceptions.PacketProcessingException)

Example 3 with LogicalPacketDatagram

use of io.xol.chunkstories.net.LogicalPacketDatagram in project chunkstories by Hugobros3.

the class WorldClientRemote method processIncommingPackets.

// Accepts and processes synched packets
public void processIncommingPackets() {
    try {
        entitiesLock.writeLock().lock();
        @SuppressWarnings("unused") int packetsThisTick = 0;
        Iterator<LogicalPacketDatagram> i = incommingDatagrams.iterator();
        while (i.hasNext()) {
            LogicalPacketDatagram datagram = i.next();
            try {
                // this.getContentTranslator().getPacketForId(datagram.packetTypeId);
                PacketDefinitionImplementation definition = (PacketDefinitionImplementation) datagram.packetDefinition;
                Packet packet = definition.createNew(true, this);
                if (definition.getGenre() != PacketGenre.WORLD || !(packet instanceof PacketWorld)) {
                    logger().error(definition + " isn't a PacketWorld");
                } else {
                    PacketWorld packetWorld = (PacketWorld) packet;
                    // packetsProcessor.getSender() is equivalent to getRemoteServer() here
                    packetWorld.process(packetsProcessor.getInterlocutor(), datagram.getData(), packetsProcessor);
                }
            } catch (IOException | PacketProcessingException e) {
                logger().warn("Networking Exception while processing datagram: " + e.getMessage());
            } catch (Exception e) {
                logger().warn("Exception while processing datagram: " + e.getMessage());
            }
            datagram.dispose();
            i.remove();
            packetsThisTick++;
        }
    } finally {
        entitiesLock.writeLock().unlock();
    }
}
Also used : PacketProcessingException(io.xol.chunkstories.api.exceptions.PacketProcessingException) Packet(io.xol.chunkstories.api.net.Packet) PacketDefinitionImplementation(io.xol.chunkstories.net.PacketDefinitionImplementation) IOException(java.io.IOException) LogicalPacketDatagram(io.xol.chunkstories.net.LogicalPacketDatagram) PacketWorld(io.xol.chunkstories.api.net.PacketWorld) IOException(java.io.IOException) PacketProcessingException(io.xol.chunkstories.api.exceptions.PacketProcessingException)

Aggregations

PacketProcessingException (io.xol.chunkstories.api.exceptions.PacketProcessingException)3 LogicalPacketDatagram (io.xol.chunkstories.net.LogicalPacketDatagram)3 IOException (java.io.IOException)3 Packet (io.xol.chunkstories.api.net.Packet)2 PacketWorld (io.xol.chunkstories.api.net.PacketWorld)2 PacketDefinitionImplementation (io.xol.chunkstories.net.PacketDefinitionImplementation)2 IllegalPacketException (io.xol.chunkstories.api.exceptions.net.IllegalPacketException)1 UnknowPacketException (io.xol.chunkstories.api.exceptions.net.UnknowPacketException)1 ServerPlayer (io.xol.chunkstories.server.player.ServerPlayer)1 EOFException (java.io.EOFException)1 SocketException (java.net.SocketException)1