public void run System out println Selector Started int totalKeys Iter

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public void run()
{
System.out.println("Selector Started");
int totalKeys = 0;
Iterator<SelectionKey> iter;
SelectionKey key;
MMOConnection<T> con;
FastList.Node<MMOConnection<T>> 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<SelectionKey> 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<T>(this, sc, clientKey)));
}
else
{
sc.socket().close();
}
}
}
catch (IOException e) // <------- exc
{
e.printStackTrace();
}
}