Search in sources :

Example 1 with PacketWorld

use of io.xol.chunkstories.api.net.PacketWorld 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 2 with PacketWorld

use of io.xol.chunkstories.api.net.PacketWorld 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)2 Packet (io.xol.chunkstories.api.net.Packet)2 PacketWorld (io.xol.chunkstories.api.net.PacketWorld)2 LogicalPacketDatagram (io.xol.chunkstories.net.LogicalPacketDatagram)2 PacketDefinitionImplementation (io.xol.chunkstories.net.PacketDefinitionImplementation)2 IOException (java.io.IOException)2 ServerPlayer (io.xol.chunkstories.server.player.ServerPlayer)1