Search in sources :

Example 1 with MessageType

use of com.b3dgs.lionengine.network.MessageType in project lionengine by b3dgs.

the class ClientUdp method taskReceive.

private void taskReceive() {
    while (running) {
        try {
            final DatagramPacket packet = UtilNetwork.receive(socket);
            final ByteBuffer buffer = UtilNetwork.getBuffer(packet);
            final MessageType type = MessageType.from(buffer);
            if (MessageType.DIRECT == type) {
                channel.write(Direct.decode(buffer, clientId));
            } else if (MessageType.DATA == type) {
                channel.write(Data.decode(buffer, clientId));
            } else if (MessageType.CLIENTS_LIST == type) {
                handleClientsList(buffer);
            } else if (MessageType.DISCONNECT == type) {
                handleDisconnected(buffer);
            } else if (MessageType.PING == type) {
                handlePing(buffer);
            }
        } catch (final IOException exception) {
            if (running) {
                Verbose.exception(exception);
            }
        }
    }
}
Also used : DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MessageType(com.b3dgs.lionengine.network.MessageType)

Example 2 with MessageType

use of com.b3dgs.lionengine.network.MessageType in project lionengine by b3dgs.

the class ServerUdp method handleType.

private void handleType(DatagramPacket packet) throws IOException {
    final ByteBuffer buffer = UtilNetwork.getBuffer(packet);
    bandwidthDownSum.addAndGet(buffer.capacity());
    final MessageType type = MessageType.from(buffer);
    switch(type) {
        case CONNECT:
            connect(packet, buffer);
            break;
        case DISCONNECT:
            disconnect(packet, buffer);
            break;
        case ALIVE:
            alive(packet, buffer);
            break;
        case PING:
            ping(packet, buffer);
            break;
        case DIRECT:
            direct(packet, buffer);
            break;
        case DATA:
            data(packet, buffer);
            break;
        case CLIENTS_LIST:
        case UNKNOWN:
            break;
        default:
            throw new LionEngineException(type);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) ByteBuffer(java.nio.ByteBuffer) MessageType(com.b3dgs.lionengine.network.MessageType)

Aggregations

MessageType (com.b3dgs.lionengine.network.MessageType)2 ByteBuffer (java.nio.ByteBuffer)2 LionEngineException (com.b3dgs.lionengine.LionEngineException)1 IOException (java.io.IOException)1 DatagramPacket (java.net.DatagramPacket)1