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;
}
}
}
}
Aggregations