Search in sources :

Example 1 with Packet

use of io.xol.chunkstories.api.net.Packet in project chunkstories by Hugobros3.

the class TCPServerConnection method handleDatagram.

@Override
public void handleDatagram(LogicalPacketDatagram datagram) throws IOException, PacketProcessingException, IllegalPacketException {
    // (PacketDefinitionImpl) getPacketsContext().getContentTranslator().getPacketForId(datagram.packetTypeId);
    PacketDefinitionImplementation definition = (PacketDefinitionImplementation) datagram.packetDefinition;
    if (definition.getGenre() == PacketGenre.GENERAL_PURPOSE) {
        Packet packet = definition.createNew(true, null);
        packet.process(getRemoteServer(), datagram.getData(), getPacketsContext());
        datagram.dispose();
    } else if (definition.getGenre() == PacketGenre.SYSTEM) {
        Packet packet = definition.createNew(true, null);
        packet.process(getRemoteServer(), datagram.getData(), getPacketsContext());
        if (packet instanceof PacketText) {
            handleSystemRequest(((PacketText) packet).text);
        }
        datagram.dispose();
    } else if (definition.getGenre() == PacketGenre.WORLD) {
        WorldClientRemote world = getPacketsContext().getWorld();
        world.queueDatagram(datagram);
    } else if (definition.getGenre() == PacketGenre.WORLD_STREAMING) {
        WorldClientRemote world = getPacketsContext().getWorld();
        PacketWorldStreaming packet = (PacketWorldStreaming) definition.createNew(true, world);
        packet.process(getRemoteServer(), datagram.getData(), getPacketsContext());
        world.ioHandler().handlePacketWorldStreaming(packet);
        datagram.dispose();
    } else {
        throw new RuntimeException("whut");
    }
}
Also used : Packet(io.xol.chunkstories.api.net.Packet) PacketText(io.xol.chunkstories.api.net.packets.PacketText) PacketWorldStreaming(io.xol.chunkstories.api.net.PacketWorldStreaming) PacketDefinitionImplementation(io.xol.chunkstories.net.PacketDefinitionImplementation) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote)

Example 2 with Packet

use of io.xol.chunkstories.api.net.Packet in project chunkstories by Hugobros3.

the class SocketedClientConnection method handleDatagram.

@Override
public void handleDatagram(LogicalPacketDatagram datagram) throws IOException, PacketProcessingException, IllegalPacketException {
    // getPacketsContext().getContentTranslator().getPacketForId(datagram.packetTypeId);
    PacketDefinitionImplementation definition = (PacketDefinitionImplementation) datagram.packetDefinition;
    if (definition.getGenre() == PacketGenre.GENERAL_PURPOSE) {
        Packet packet = definition.createNew(true, null);
        packet.process(packetsProcessor.getInterlocutor(), datagram.getData(), getPacketsContext());
        datagram.dispose();
    } else if (definition.getGenre() == PacketGenre.SYSTEM) {
        Packet packet = definition.createNew(true, null);
        packet.process(packetsProcessor.getInterlocutor(), datagram.getData(), getPacketsContext());
        if (packet instanceof PacketText) {
            handleSystemRequest(((PacketText) packet).text);
        }
        datagram.dispose();
    } else if (definition.getGenre() == PacketGenre.WORLD) {
        // Queue packets for a specific world
        if (player != null) {
            WorldServer world = player.getWorld();
            if (world != null) {
                world.queueDatagram(datagram, player);
            }
        }
    } else if (definition.getGenre() == PacketGenre.WORLD_STREAMING) {
        // Server doesn't expect world streaming updates from the client
        // it does, however, listen to world_user_requests packets to keep
        // track of the client's world data
        WorldServer world = getPacketsContext().getWorld();
        PacketWorldStreaming packet = (PacketWorldStreaming) definition.createNew(false, world);
        packet.process(packetsProcessor.getInterlocutor(), datagram.getData(), getPacketsContext());
        datagram.dispose();
    } else {
        throw new RuntimeException("whut");
    }
}
Also used : Packet(io.xol.chunkstories.api.net.Packet) PacketText(io.xol.chunkstories.api.net.packets.PacketText) PacketWorldStreaming(io.xol.chunkstories.api.net.PacketWorldStreaming) PacketDefinitionImplementation(io.xol.chunkstories.net.PacketDefinitionImplementation) WorldServer(io.xol.chunkstories.world.WorldServer)

Example 3 with Packet

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

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

Packet (io.xol.chunkstories.api.net.Packet)4 PacketDefinitionImplementation (io.xol.chunkstories.net.PacketDefinitionImplementation)4 PacketProcessingException (io.xol.chunkstories.api.exceptions.PacketProcessingException)2 PacketWorld (io.xol.chunkstories.api.net.PacketWorld)2 PacketWorldStreaming (io.xol.chunkstories.api.net.PacketWorldStreaming)2 PacketText (io.xol.chunkstories.api.net.packets.PacketText)2 LogicalPacketDatagram (io.xol.chunkstories.net.LogicalPacketDatagram)2 IOException (java.io.IOException)2 ServerPlayer (io.xol.chunkstories.server.player.ServerPlayer)1 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)1 WorldServer (io.xol.chunkstories.world.WorldServer)1