Search in sources :

Example 1 with OPEN_CONNECTION_REPLY_2

use of org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REPLY_2 in project Dragonet-Legacy by DragonetMC.

the class Session method handlePacket.

public void handlePacket(Packet packet) throws Exception {
    this.isActive = true;
    this.lastUpdate = System.currentTimeMillis();
    if (this.state == STATE_CONNECTED || this.state == STATE_CONNECTING_2) {
        if (((packet.buffer[0] & 0xff) >= 0x80 || (packet.buffer[0] & 0xff) <= 0x8f) && packet instanceof DataPacket) {
            DataPacket dp = (DataPacket) packet;
            dp.decode();
            if (dp.seqNumber < this.windowStart || dp.seqNumber > this.windowEnd || this.receivedWindow.containsKey(dp.seqNumber)) {
                return;
            }
            int diff = dp.seqNumber - this.lastSeqNumber;
            this.NACKQueue.remove(dp.seqNumber);
            this.ACKQueue.put(dp.seqNumber, dp.seqNumber);
            this.receivedWindow.put(dp.seqNumber, dp.seqNumber);
            if (diff != 1) {
                for (int i = this.lastSeqNumber + 1; i < dp.seqNumber; i++) {
                    if (!this.receivedWindow.containsKey(i)) {
                        this.NACKQueue.put(i, i);
                    }
                }
            }
            if (diff >= 1) {
                this.lastSeqNumber = dp.seqNumber;
                this.windowStart += diff;
                this.windowEnd += diff;
            }
            for (Object pk : dp.packets) {
                if (pk instanceof EncapsulatedPacket) {
                    this.handleEncapsulatedPacket((EncapsulatedPacket) pk);
                }
            }
        } else {
            if (packet instanceof ACK) {
                packet.decode();
                for (int seq : new ArrayList<>(((ACK) packet).packets.values())) {
                    if (this.recoveryQueue.containsKey(seq)) {
                        for (Object pk : this.recoveryQueue.get(seq).packets) {
                            if (pk instanceof EncapsulatedPacket && ((EncapsulatedPacket) pk).needACK && ((EncapsulatedPacket) pk).messageIndex != null) {
                                if (this.needACK.containsKey(((EncapsulatedPacket) pk).identifierACK)) {
                                    this.needACK.get(((EncapsulatedPacket) pk).identifierACK).remove(((EncapsulatedPacket) pk).messageIndex);
                                }
                            }
                        }
                        this.recoveryQueue.remove(seq);
                    }
                }
            } else if (packet instanceof NACK) {
                packet.decode();
                for (int seq : new ArrayList<>(((NACK) packet).packets.values())) {
                    if (this.recoveryQueue.containsKey(seq)) {
                        DataPacket pk = this.recoveryQueue.get(seq);
                        pk.seqNumber = this.sendSeqNumber++;
                        this.packetToSend.add(pk);
                        this.recoveryQueue.remove(seq);
                    }
                }
            }
        }
    } else if ((packet.buffer[0] & 0xff) > 0x00 || (packet.buffer[0] & 0xff) < (byte) 0x80) {
        //Not Data packet :)
        packet.decode();
        if (packet instanceof OPEN_CONNECTION_REQUEST_1) {
            //TODO: check protocol number and refuse connections
            OPEN_CONNECTION_REPLY_1 pk = new OPEN_CONNECTION_REPLY_1();
            pk.mtuSize = ((OPEN_CONNECTION_REQUEST_1) packet).mtuSize;
            pk.serverID = sessionManager.getID();
            this.sendPacket(pk);
            this.state = STATE_CONNECTING_1;
        } else if (this.state == STATE_CONNECTING_1 && packet instanceof OPEN_CONNECTION_REQUEST_2) {
            this.id = ((OPEN_CONNECTION_REQUEST_2) packet).clientID;
            if (((OPEN_CONNECTION_REQUEST_2) packet).serverPort == this.sessionManager.getPort() || !this.sessionManager.portChecking) {
                //Max size, do not allow creating large buffers to fill server memory
                this.mtuSize = Math.min(Math.abs(((OPEN_CONNECTION_REQUEST_2) packet).mtuSize), 1464);
                OPEN_CONNECTION_REPLY_2 pk = new OPEN_CONNECTION_REPLY_2();
                pk.mtuSize = (short) this.mtuSize;
                pk.serverID = this.sessionManager.getID();
                pk.clientAddress = this.address;
                pk.clientPort = this.port;
                this.sendPacket(pk);
                this.state = STATE_CONNECTING_2;
            }
        }
    }
}
Also used : OPEN_CONNECTION_REPLY_2(org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REPLY_2) OPEN_CONNECTION_REPLY_1(org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REPLY_1) ACK(org.dragonet.raknet.protocol.packet.ACK) NACK(org.dragonet.raknet.protocol.packet.NACK) OPEN_CONNECTION_REQUEST_2(org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REQUEST_2) OPEN_CONNECTION_REQUEST_1(org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REQUEST_1) EncapsulatedPacket(org.dragonet.raknet.protocol.EncapsulatedPacket) CLIENT_DISCONNECT_DataPacket(org.dragonet.raknet.protocol.packet.CLIENT_DISCONNECT_DataPacket) DataPacket(org.dragonet.raknet.protocol.DataPacket) PONG_DataPacket(org.dragonet.raknet.protocol.packet.PONG_DataPacket) SERVER_HANDSHAKE_DataPacket(org.dragonet.raknet.protocol.packet.SERVER_HANDSHAKE_DataPacket) CLIENT_CONNECT_DataPacket(org.dragonet.raknet.protocol.packet.CLIENT_CONNECT_DataPacket) CLIENT_HANDSHAKE_DataPacket(org.dragonet.raknet.protocol.packet.CLIENT_HANDSHAKE_DataPacket) PING_DataPacket(org.dragonet.raknet.protocol.packet.PING_DataPacket) NACK(org.dragonet.raknet.protocol.packet.NACK)

Aggregations

DataPacket (org.dragonet.raknet.protocol.DataPacket)1 EncapsulatedPacket (org.dragonet.raknet.protocol.EncapsulatedPacket)1 ACK (org.dragonet.raknet.protocol.packet.ACK)1 CLIENT_CONNECT_DataPacket (org.dragonet.raknet.protocol.packet.CLIENT_CONNECT_DataPacket)1 CLIENT_DISCONNECT_DataPacket (org.dragonet.raknet.protocol.packet.CLIENT_DISCONNECT_DataPacket)1 CLIENT_HANDSHAKE_DataPacket (org.dragonet.raknet.protocol.packet.CLIENT_HANDSHAKE_DataPacket)1 NACK (org.dragonet.raknet.protocol.packet.NACK)1 OPEN_CONNECTION_REPLY_1 (org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REPLY_1)1 OPEN_CONNECTION_REPLY_2 (org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REPLY_2)1 OPEN_CONNECTION_REQUEST_1 (org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REQUEST_1)1 OPEN_CONNECTION_REQUEST_2 (org.dragonet.raknet.protocol.packet.OPEN_CONNECTION_REQUEST_2)1 PING_DataPacket (org.dragonet.raknet.protocol.packet.PING_DataPacket)1 PONG_DataPacket (org.dragonet.raknet.protocol.packet.PONG_DataPacket)1 SERVER_HANDSHAKE_DataPacket (org.dragonet.raknet.protocol.packet.SERVER_HANDSHAKE_DataPacket)1