public void run() { System.out.println("Selector Started"); int totalKeys = 0; Iterator iter; SelectionKey key; MMOConnection con; FastList.Node> n, end, temp; // main loop; check shutdown while(!this.isShuttingDown()) // <----- optimiz { try { totalKeys = this.getSelector().selectNow(); } catch (IOException e) { e.printStackTrace(); } if (totalKeys > 0) { Set keys = this.getSelector().selectedKeys(); iter = keys.iterator(); while (iter.hasNext()) { key = iter.next(); iter.remove(); switch (key.readyOps()) { // ADD DEBUG INFO case SelectionKey.OP_CONNECT: System.out.println("OP_CONNECT"); this.finishConnection(key); break; case SelectionKey.OP_ACCEPT: // WARN!!! System.out.println("OP_ACCEPT <----- pro devine"); this.acceptConnection(key); break; case SelectionKey.OP_READ: System.out.println("OP_READ"); this.readPacket(key); break; case SelectionKey.OP_WRITE: System.out.println("OP_WRITE"); this.writePacket(key); break; case SelectionKey.OP_READ | SelectionKey.OP_WRITE: System.out.println("OP_READ | OP_WRITE"); this.writePacket(key); if (key.isValid()) this.readPacket(key); break; } } } for (n = this.getPendingClose().head(), end = this.getPendingClose().tail(); (n = n.getNext()) != end;) { con = n.getValue(); if (con.getSendQueue().isEmpty()) { temp = n.getPrevious(); this.getPendingClose().delete(n); n = temp; this.closeConnectionImpl(con); } } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } protected void acceptConnection(SelectionKey key) { ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); SocketChannel sc; try { sc = ssc.accept(); // <----- exc -> use divane :( while (sc != null) { if(this.getAcceptFilter() == null || this.getAcceptFilter().accept(sc)) { //configure sc.configureBlocking(false); sc.socket().setTrafficClass(0); sc.socket().setTcpNoDelay(false); sc.socket().setKeepAlive(false); SelectionKey clientKey = sc.register(this.getSelector(), SelectionKey.OP_READ); clientKey.attach(this.getClientFactory().create(new MMOConnection(this, sc, clientKey))); } else { sc.socket().close(); } } } catch (IOException e) // <------- exc { e.printStackTrace(); } }