/** * 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 writePacket. * * @param key SelectionKey */ protected void writePacket(SelectionKey key) { MMOConnection<T> con = (MMOConnection<T>) key.attachment(); prepareWriteBuffer(con); DIRECT_WRITE_BUFFER.flip(); int size = DIRECT_WRITE_BUFFER.remaining(); int result = -1; try { result = con.getWritableChannel().write(DIRECT_WRITE_BUFFER); } catch (IOException e) { // error handling goes on the if bellow } // check if no error happened if (result >= 0) { stats.increaseOutgoingBytes(result); // check if we written everything if (result != size) { con.createWriteBuffer(DIRECT_WRITE_BUFFER); } if (!con.getSendQueue().isEmpty() || con.hasPendingWriteBuffer()) { con.scheduleWriteInterest(); } } else { con.onForcedDisconnection(); closeConnectionImpl(con); } }
/** * 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 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 run. * * @see java.lang.Runnable#run() */ @Override public void run() { int totalKeys = 0; Set<SelectionKey> keys = null; Iterator<SelectionKey> itr = null; Iterator<MMOConnection<T>> conItr = null; SelectionKey key = null; MMOConnection<T> con = null; long currentMillis = 0L; // main loop for (; ; ) { try { if (isShuttingDown()) { closeSelectorThread(); break; } currentMillis = System.currentTimeMillis(); conItr = _connections.iterator(); while (conItr.hasNext()) { con = conItr.next(); if (con.isPengingClose()) { if (!con.isPendingWrite() || ((currentMillis - con.getPendingCloseTime()) >= 10000L)) { closeConnectionImpl(con); continue; } } if (con.isPendingWrite()) { if ((currentMillis - con.getPendingWriteTime()) >= _sc.INTEREST_DELAY) { con.enableWriteInterest(); } } } totalKeys = getSelector().selectNow(); if (totalKeys > 0) { keys = getSelector().selectedKeys(); itr = keys.iterator(); while (itr.hasNext()) { key = itr.next(); itr.remove(); if (key.isValid()) { try { if (key.isAcceptable()) { acceptConnection(key); continue; } else if (key.isConnectable()) { finishConnection(key); continue; } if (key.isReadable()) { readPacket(key); } if (key.isValid()) { if (key.isWritable()) { writePacket(key); } } } catch (CancelledKeyException cke) { } } } } try { Thread.sleep(_sc.SLEEP_TIME); } catch (InterruptedException ie) { } } catch (IOException e) { _log.error("Error in " + getName(), e); try { Thread.sleep(1000L); } catch (InterruptedException ie) { } } } }