/** * Method tryReadPacket2. * * @param key SelectionKey * @param con MMOConnection<T> * @param buf ByteBuffer * @return boolean */ protected boolean tryReadPacket2(SelectionKey key, MMOConnection<T> con, ByteBuffer buf) { if (con.isClosed()) { return false; } int pos = buf.position(); if (buf.remaining() > _sc.HEADER_SIZE) { int size = buf.getShort() & 0xffff; if ((size <= _sc.HEADER_SIZE) || (size > _sc.PACKET_SIZE)) { _log.error( "Incorrect packet size : " + size + "! Client : " + con.getClient() + ". Closing connection."); closeConnectionImpl(con); return false; } size -= _sc.HEADER_SIZE; if (size <= buf.remaining()) { stats.increaseIncomingPacketsCount(); parseClientPacket(getPacketHandler(), buf, size, con); buf.position(pos + size + _sc.HEADER_SIZE); if (!buf.hasRemaining()) { freeBuffer(buf, con); return false; } return true; } buf.position(pos); } if (pos == buf.capacity()) { _log.warn( "Read buffer exhausted for client : " + con.getClient() + ", try to adjust buffer size, current : " + buf.capacity() + ", primary : " + (buf == READ_BUFFER) + "."); } if (buf == READ_BUFFER) { allocateReadBuffer(con); } else { buf.compact(); } return false; }
/** * Method parseClientPacket. * * @param handler IPacketHandler<T> * @param buf ByteBuffer * @param dataSize int * @param con MMOConnection<T> * @return boolean */ protected boolean parseClientPacket( IPacketHandler<T> handler, ByteBuffer buf, int dataSize, MMOConnection<T> con) { T client = con.getClient(); int pos = buf.position(); client.decrypt(buf, dataSize); buf.position(pos); if (buf.hasRemaining()) { // apply limit int limit = buf.limit(); buf.limit(pos + dataSize); ReceivablePacket<T> rp = handler.handlePacket(buf, client); if (rp != null) { rp.setByteBuffer(buf); rp.setClient(client); if (rp.read()) { con.recvPacket(rp); } rp.setByteBuffer(null); } buf.limit(limit); } return true; }
/** * Method readPacket. * * @param key SelectionKey */ protected void readPacket(SelectionKey key) { MMOConnection<T> con = (MMOConnection<T>) key.attachment(); if (con.isClosed()) { return; } ByteBuffer buf; int result = -2; if ((buf = con.getReadBuffer()) == null) { buf = READ_BUFFER; } // if we try to to do a read with no space in the buffer it will read 0 bytes // going into infinite loop if (buf.position() == buf.limit()) { _log.error( "Read buffer exhausted for client : " + con.getClient() + ", try to adjust buffer size, current : " + buf.capacity() + ", primary : " + (buf == READ_BUFFER) + ". Closing connection."); closeConnectionImpl(con); } else { try { result = con.getReadableByteChannel().read(buf); } catch (IOException e) { // error handling goes bellow } if (result > 0) { buf.flip(); stats.increaseIncomingBytes(result); @SuppressWarnings("unused") int i; for (i = 0; this.tryReadPacket2(key, con, buf); i++) {} } else if (result == 0) { closeConnectionImpl(con); } else if (result == -1) { closeConnectionImpl(con); } else { con.onForcedDisconnection(); closeConnectionImpl(con); } } if (buf == READ_BUFFER) { buf.clear(); } }
/** * Method finishConnection. * * @param key SelectionKey */ protected void finishConnection(SelectionKey key) { try { ((SocketChannel) key.channel()).finishConnect(); } catch (IOException e) { MMOConnection<T> con = (MMOConnection<T>) key.attachment(); T client = con.getClient(); client.getConnection().onForcedDisconnection(); closeConnectionImpl(client.getConnection()); } }
/** * Method prepareWriteBuffer. * * @param con MMOConnection<T> */ protected void prepareWriteBuffer(MMOConnection<T> con) { WRITE_CLIENT = con.getClient(); DIRECT_WRITE_BUFFER.clear(); if (con.hasPendingWriteBuffer()) { con.movePendingWriteBufferTo(DIRECT_WRITE_BUFFER); } if (DIRECT_WRITE_BUFFER.hasRemaining() && !con.hasPendingWriteBuffer()) { int i; Queue<SendablePacket<T>> sendQueue = con.getSendQueue(); SendablePacket<T> sp; for (i = 0; i < _sc.MAX_SEND_PER_PASS; i++) { synchronized (con) { if ((sp = sendQueue.poll()) == null) { break; } } try { stats.increaseOutgoingPacketsCount(); putPacketIntoWriteBuffer(sp, true); WRITE_BUFFER.flip(); if (DIRECT_WRITE_BUFFER.remaining() >= WRITE_BUFFER.limit()) { DIRECT_WRITE_BUFFER.put(WRITE_BUFFER); } else { con.createWriteBuffer(WRITE_BUFFER); break; } } catch (Exception e) { _log.error("Error in " + getName(), e); break; } } } WRITE_BUFFER.clear(); WRITE_CLIENT = null; }
/** * Method closeConnectionImpl. * * @param con MMOConnection<T> */ protected void closeConnectionImpl(MMOConnection<T> con) { try { // notify connection con.onDisconnection(); } finally { try { // close socket and the SocketChannel con.close(); } catch (IOException e) { // ignore, we are closing anyway } finally { con.releaseBuffers(); con.clearQueues(); con.getClient().setConnection(null); con.getSelectionKey().attach(null); con.getSelectionKey().cancel(); _connections.remove(con); stats.decreseOpenedConnections(); } } }