Pārlūkot izejas kodu

MMOCore: Cleanup & Code Formating using L2J Default Formater.

nBd 15 gadi atpakaļ
vecāks
revīzija
44ab123d8f

+ 3 - 4
MMOCore/src/org/mmocore/network/AbstractPacket.java

@@ -19,17 +19,16 @@ package org.mmocore.network;
 
 import java.nio.ByteBuffer;
 
-
 /**
  * @author KenM
- *
+ * 
  */
 public abstract class AbstractPacket<T extends MMOClient<?>>
 {
     protected ByteBuffer _buf;
-    
+
     T _client;
-    
+
     public final T getClient()
     {
         return _client;

+ 1 - 1
MMOCore/src/org/mmocore/network/IAcceptFilter.java

@@ -21,7 +21,7 @@ import java.nio.channels.SocketChannel;
 
 /**
  * @author KenM
- *
+ * 
  */
 public interface IAcceptFilter
 {

+ 1 - 1
MMOCore/src/org/mmocore/network/IClientFactory.java

@@ -19,7 +19,7 @@ package org.mmocore.network;
 
 /**
  * @author KenM
- *
+ * 
  */
 public interface IClientFactory<T extends MMOClient<?>>
 {

+ 1 - 2
MMOCore/src/org/mmocore/network/IMMOExecutor.java

@@ -17,10 +17,9 @@
  */
 package org.mmocore.network;
 
-
 /**
  * @author KenM
- *
+ * 
  */
 public interface IMMOExecutor<T extends MMOClient<?>>
 {

+ 1 - 2
MMOCore/src/org/mmocore/network/IPacketHandler.java

@@ -19,10 +19,9 @@ package org.mmocore.network;
 
 import java.nio.ByteBuffer;
 
-
 /**
  * @author KenM
- *
+ * 
  */
 public interface IPacketHandler<T extends MMOClient<?>>
 {

+ 8 - 8
MMOCore/src/org/mmocore/network/MMOClient.java

@@ -21,27 +21,27 @@ import java.nio.ByteBuffer;
 
 /**
  * @author KenM
- *
+ * 
  */
 public abstract class MMOClient<T extends MMOConnection>
 {
     private final T _con;
-    
+
     public MMOClient(final T con)
     {
-    	_con = con;
+        _con = con;
     }
-    
+
     public T getConnection()
     {
         return _con;
     }
-    
+
     public abstract boolean decrypt(final ByteBuffer buf, final int size);
-    
+
     public abstract boolean encrypt(final ByteBuffer buf, final int size);
-    
+
     protected abstract void onDisconnection();
-    
+
     protected abstract void onForcedDisconnection();
 }

+ 76 - 63
MMOCore/src/org/mmocore/network/MMOConnection.java

@@ -28,29 +28,40 @@ import java.nio.channels.WritableByteChannel;
 
 /**
  * @author KenM
- *
+ * 
  */
 public final class MMOConnection<T extends MMOClient<?>>
 {
     private final SelectorThread<T> _selectorThread;
-    
+
     private final Socket _socket;
+
     private final InetAddress _address;
+
     private final ReadableByteChannel _readableByteChannel;
+
     private final WritableByteChannel _writableByteChannel;
+
     private final int _port;
-    
+
     private final NioNetStackList<SendablePacket<T>> _sendQueue;
+
     private final SelectionKey _selectionKey;
+
     private SendablePacket<T> _closePacket;
-    
+
     private ByteBuffer _readBuffer;
+
     private ByteBuffer _primaryWriteBuffer;
+
     private ByteBuffer _secondaryWriteBuffer;
+
     private boolean _pendingClose;
+
     private T _client;
-    
-	public MMOConnection(final SelectorThread<T> selectorThread, final Socket socket, final SelectionKey key)
+
+    public MMOConnection(final SelectorThread<T> selectorThread,
+            final Socket socket, final SelectionKey key)
     {
         _selectorThread = selectorThread;
         _socket = socket;
@@ -59,37 +70,38 @@ public final class MMOConnection<T extends MMOClient<?>>
         _writableByteChannel = socket.getChannel();
         _port = socket.getPort();
         _selectionKey = key;
-        
+
         _sendQueue = new NioNetStackList<SendablePacket<T>>();
     }
-    
+
     final void setClient(final T client)
     {
-    	_client = client;
+        _client = client;
     }
-    
+
     public final T getClient()
     {
         return _client;
     }
-    
+
     public final void sendPacket(final SendablePacket<T> sp)
     {
         sp._client = _client;
-        
+
         synchronized (getSendQueue())
-    	{
-    		if (!_pendingClose)
-    		{
-    			_sendQueue.addLast(sp);
-    		}
-    	}
-        
+        {
+            if (!_pendingClose)
+            {
+                _sendQueue.addLast(sp);
+            }
+        }
+
         if (!_sendQueue.isEmpty())
         {
-        	try
+            try
             {
-            	_selectionKey.interestOps(_selectionKey.interestOps() | SelectionKey.OP_WRITE);
+                _selectionKey.interestOps(_selectionKey.interestOps()
+                        | SelectionKey.OP_WRITE);
             }
             catch (CancelledKeyException e)
             {
@@ -97,37 +109,37 @@ public final class MMOConnection<T extends MMOClient<?>>
             }
         }
     }
-    
+
     final SelectionKey getSelectionKey()
     {
         return _selectionKey;
     }
-    
+
     public final InetAddress getInetAddress()
     {
-    	return _address;
+        return _address;
     }
-    
+
     public final int getPort()
     {
-    	return _port;
+        return _port;
     }
-    
+
     final void close() throws IOException
     {
-    	_socket.close();
+        _socket.close();
     }
-    
+
     final int read(final ByteBuffer buf) throws IOException
     {
-    	return _readableByteChannel.read(buf);
+        return _readableByteChannel.read(buf);
     }
-    
+
     final int write(final ByteBuffer buf) throws IOException
     {
-    	return _writableByteChannel.write(buf);
+        return _writableByteChannel.write(buf);
     }
-    
+
     final void createWriteBuffer(final ByteBuffer buf)
     {
         if (_primaryWriteBuffer == null)
@@ -139,11 +151,11 @@ public final class MMOConnection<T extends MMOClient<?>>
         {
             final ByteBuffer temp = _selectorThread.getPooledBuffer();
             temp.put(buf);
-            
+
             final int remaining = temp.remaining();
             _primaryWriteBuffer.flip();
             final int limit = _primaryWriteBuffer.limit();
-            
+
             if (remaining >= _primaryWriteBuffer.remaining())
             {
                 temp.put(_primaryWriteBuffer);
@@ -161,12 +173,12 @@ public final class MMOConnection<T extends MMOClient<?>>
             }
         }
     }
-    
+
     final boolean hasPendingWriteBuffer()
     {
         return _primaryWriteBuffer != null;
     }
-    
+
     final void movePendingWriteBufferTo(final ByteBuffer dest)
     {
         _primaryWriteBuffer.flip();
@@ -175,72 +187,73 @@ public final class MMOConnection<T extends MMOClient<?>>
         _primaryWriteBuffer = _secondaryWriteBuffer;
         _secondaryWriteBuffer = null;
     }
-    
+
     final void setReadBuffer(final ByteBuffer buf)
     {
         _readBuffer = buf;
     }
-    
+
     final ByteBuffer getReadBuffer()
     {
         return _readBuffer;
     }
-    
+
     public final boolean isClosed()
     {
         return _pendingClose;
     }
-    
+
     final NioNetStackList<SendablePacket<T>> getSendQueue()
     {
-    	return _sendQueue;
+        return _sendQueue;
     }
-    
+
     final SendablePacket<T> getClosePacket()
     {
-    	return _closePacket;
+        return _closePacket;
     }
-    
+
     public final void close(final SendablePacket<T> sp)
     {
-    	synchronized (getSendQueue())
-    	{
-    		if (!_pendingClose)
-    			_pendingClose = true;
-    		
-    		_sendQueue.clear();
-    	}
-    	
-    	try
+        synchronized (getSendQueue())
+        {
+            if (!_pendingClose)
+                _pendingClose = true;
+
+            _sendQueue.clear();
+        }
+
+        try
         {
-			_selectionKey.interestOps(_selectionKey.interestOps() & ~SelectionKey.OP_WRITE);
+            _selectionKey.interestOps(_selectionKey.interestOps()
+                    & ~SelectionKey.OP_WRITE);
         }
         catch (CancelledKeyException e)
         {
             // ignore
         }
-        
+
         _closePacket = sp;
-    	_selectorThread.closeConnection(this);
+        _selectorThread.closeConnection(this);
     }
-    
+
     final void releaseBuffers()
     {
         if (_primaryWriteBuffer != null)
         {
-        	_selectorThread.recycleBuffer(_primaryWriteBuffer);
+            _selectorThread.recycleBuffer(_primaryWriteBuffer);
             _primaryWriteBuffer = null;
-            
+
             if (_secondaryWriteBuffer != null)
             {
-            	_selectorThread.recycleBuffer(_secondaryWriteBuffer);
+                _selectorThread.recycleBuffer(_secondaryWriteBuffer);
                 _secondaryWriteBuffer = null;
             }
         }
-        
+
         if (_readBuffer != null)
         {
-        	_selectorThread.recycleBuffer(_readBuffer);
+            _selectorThread.recycleBuffer(_readBuffer);
             _readBuffer = null;
         }
     }

+ 34 - 30
MMOCore/src/org/mmocore/network/NioNetStackList.java

@@ -23,77 +23,81 @@ package org.mmocore.network;
 public final class NioNetStackList<E>
 {
     private final NioNetStackNode _start = new NioNetStackNode();
+
     private final NioNetStackNodeBuf _buf = new NioNetStackNodeBuf();
+
     private NioNetStackNode _end = new NioNetStackNode();
-    
+
     public NioNetStackList()
     {
         clear();
     }
-    
+
     public final void addLast(final E elem)
     {
-    	 final NioNetStackNode newEndNode = _buf.removeFirst();
-    	_end._value = elem;
+        final NioNetStackNode newEndNode = _buf.removeFirst();
+        _end._value = elem;
         _end._next = newEndNode;
         _end = newEndNode;
     }
-    
+
     public final E removeFirst()
     {
-    	final NioNetStackNode old = _start._next;
-    	final E value = old._value;
-    	_start._next = old._next;
-    	_buf.addLast(old);
+        final NioNetStackNode old = _start._next;
+        final E value = old._value;
+        _start._next = old._next;
+        _buf.addLast(old);
         return value;
     }
-    
+
     public final boolean isEmpty()
     {
         return _start._next == _end;
     }
-    
+
     public final void clear()
     {
         _start._next = _end;
     }
-    
+
     private final class NioNetStackNode
     {
-    	private NioNetStackNode _next;
-    	private E _value;
-        
-    	private NioNetStackNode()
+        private NioNetStackNode _next;
+
+        private E _value;
+
+        private NioNetStackNode()
         {
-        	
+
         }
     }
-    
+
     private final class NioNetStackNodeBuf
     {
-    	private final NioNetStackNode _start = new NioNetStackNode();
+        private final NioNetStackNode _start = new NioNetStackNode();
+
         private NioNetStackNode _end = new NioNetStackNode();
-        
+
         NioNetStackNodeBuf()
         {
-        	_start._next = _end;
+            _start._next = _end;
         }
-        
+
         final void addLast(final NioNetStackNode node)
         {
-        	node._next = null;
-        	node._value = null;
+            node._next = null;
+            node._value = null;
             _end._next = node;
             _end = node;
         }
-        
+
         final NioNetStackNode removeFirst()
         {
-        	if (_start._next == _end)
-        		return new NioNetStackNode();
-        	
-        	final NioNetStackNode old = _start._next;
-        	_start._next = old._next;
+            if (_start._next == _end)
+                return new NioNetStackNode();
+
+            final NioNetStackNode old = _start._next;
+            _start._next = old._next;
             return old;
         }
     }

+ 31 - 29
MMOCore/src/org/mmocore/network/NioNetStringBuffer.java

@@ -7,33 +7,35 @@ import java.nio.BufferOverflowException;
  */
 public final class NioNetStringBuffer
 {
-	private final char[] _buf;
-	private final int _size;
-	private int _len;
-	
-	public NioNetStringBuffer(final int size)
-	{
-		_buf = new char[size];
-		_size = size;
-		_len = 0;
-	}
-	
-	public final void clear()
-	{
-		_len = 0;
-	}
-	
-	public final void append(final char c)
-	{
-		if (_len < _size)
-			_buf[_len++] = c;
-		else
-			throw new BufferOverflowException();
-	}
-	
-	@Override
-	public final String toString()
-	{
-		return new String(_buf, 0, _len);
-	}
+    private final char[] _buf;
+
+    private final int _size;
+
+    private int _len;
+
+    public NioNetStringBuffer(final int size)
+    {
+        _buf = new char[size];
+        _size = size;
+        _len = 0;
+    }
+
+    public final void clear()
+    {
+        _len = 0;
+    }
+
+    public final void append(final char c)
+    {
+        if (_len < _size)
+            _buf[_len++] = c;
+        else
+            throw new BufferOverflowException();
+    }
+
+    @Override
+    public final String toString()
+    {
+        return new String(_buf, 0, _len);
+    }
 }

+ 19 - 19
MMOCore/src/org/mmocore/network/ReceivablePacket.java

@@ -19,66 +19,66 @@ package org.mmocore.network;
 
 /**
  * @author KenM
- *
+ * 
  */
 public abstract class ReceivablePacket<T extends MMOClient<?>> extends AbstractPacket<T> implements Runnable
 {
-	NioNetStringBuffer _sbuf;
-	
+    NioNetStringBuffer _sbuf;
+
     protected ReceivablePacket()
     {
-        
+
     }
-    
+
     protected abstract boolean read();
-    
+
     public abstract void run();
-    
+
     protected final void readB(final byte[] dst)
     {
         _buf.get(dst);
     }
-    
+
     protected final void readB(final byte[] dst, final int offset, final int len)
     {
-    	_buf.get(dst, offset, len);
+        _buf.get(dst, offset, len);
     }
-    
+
     protected final int readC()
     {
         return _buf.get() & 0xFF;
     }
-    
+
     protected final int readH()
     {
         return _buf.getShort() & 0xFFFF;
     }
-    
+
     protected final int readD()
     {
         return _buf.getInt();
     }
-    
+
     protected final long readQ()
     {
         return _buf.getLong();
     }
-    
+
     protected final double readF()
     {
         return _buf.getDouble();
     }
-    
+
     protected final String readS()
     {
-    	_sbuf.clear();
-    	
+        _sbuf.clear();
+
         char ch;
         while ((ch = _buf.getChar()) != 0)
         {
-        	_sbuf.append(ch);
+            _sbuf.append(ch);
         }
-        
+
         return _sbuf.toString();
     }
 }

+ 21 - 13
MMOCore/src/org/mmocore/network/SelectorConfig.java

@@ -19,31 +19,39 @@ package org.mmocore.network;
 
 /**
  * @author KenM
- *
+ * 
  */
 public final class SelectorConfig
 {
-	public int READ_BUFFER_SIZE = 64 * 1024;
-	public int WRITE_BUFFER_SIZE = 64 * 1024;
-	
-	public int HELPER_BUFFER_COUNT = 20;
-	public int HELPER_BUFFER_SIZE = 64 * 1024;
-	
+    public int READ_BUFFER_SIZE = 64 * 1024;
+
+    public int WRITE_BUFFER_SIZE = 64 * 1024;
+
+    public int HELPER_BUFFER_COUNT = 20;
+
+    public int HELPER_BUFFER_SIZE = 64 * 1024;
+
     /**
      * Server will try to send MAX_SEND_PER_PASS packets per socket write call<br>
-     * however it may send less if the write buffer was filled before achieving this value.
+     * however it may send less if the write buffer was filled before achieving
+     * this value.
      */
     public int MAX_SEND_PER_PASS = 10;
-    
+
     /**
      * Server will try to read MAX_READ_PER_PASS packets per socket read call<br>
-     * however it may read less if the read buffer was empty before achieving this value.
+     * however it may read less if the read buffer was empty before achieving
+     * this value.
      */
     public int MAX_READ_PER_PASS = 10;
-    
+
     /**
-     * Defines how much time (in milis) should the selector sleep, an higher value increases throughput but also increases latency(to a max of the sleep value itself).<BR>
-     * Also an extremely high value(usually > 100) will decrease throughput due to the server not doing enough sends per second (depends on max sends per pass).<BR>
+     * Defines how much time (in milis) should the selector sleep, an higher
+     * value increases throughput but also increases latency(to a max of the
+     * sleep value itself).<BR>
+     * Also an extremely high value(usually > 100) will decrease throughput due
+     * to the server not doing enough sends per second (depends on max sends per
+     * pass).<BR>
      * <BR>
      * Recommended values:<BR>
      * 1 for minimal latency.<BR>

+ 188 - 253
MMOCore/src/org/mmocore/network/SelectorThread.java

@@ -33,42 +33,35 @@ import javolution.util.FastList;
 
 /**
  * @author KenM<BR>
- * Parts of design based on networkcore from WoodenGil
+ *         Parts of design based on networkcore from WoodenGil
  */
 public final class SelectorThread<T extends MMOClient<?>> extends Thread
 {
-	// default BYTE_ORDER
-	private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
-	// default HEADER_SIZE
-	private static final int HEADER_SIZE = 2;
-    
-	// Selector
-	private final Selector _selector;
-	
+    // default BYTE_ORDER
+    private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
+    // default HEADER_SIZE
+    private static final int HEADER_SIZE = 2;
+    // Selector
+    private final Selector _selector;
     // Implementations
     private final IPacketHandler<T> _packetHandler;
     private final IMMOExecutor<T> _executor;
     private final IClientFactory<T> _clientFactory;
     private final IAcceptFilter _acceptFilter;
-    
     // Configurations
     private final int HELPER_BUFFER_SIZE;
     private final int HELPER_BUFFER_COUNT;
     private final int MAX_SEND_PER_PASS;
     private final int MAX_READ_PER_PASS;
     private final long SLEEP_TIME;
-    
     // Main Buffers
     private final ByteBuffer DIRECT_WRITE_BUFFER;
     private final ByteBuffer WRITE_BUFFER;
     private final ByteBuffer READ_BUFFER;
-    
     // String Buffer
     private final NioNetStringBuffer STRING_BUFFER;
-
     // ByteBuffers General Purpose Pool
     private final FastList<ByteBuffer> _bufferPool;
-    
     // Pending Close
     private final NioNetStackList<MMOConnection<T>> _pendingClose;
     
@@ -76,28 +69,29 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     
     public SelectorThread(final SelectorConfig sc, final IMMOExecutor<T> executor, final IPacketHandler<T> packetHandler, final IClientFactory<T> clientFactory, final IAcceptFilter acceptFilter) throws IOException
     {
-    	super.setName("SelectorThread-" + super.getId());
-    	
+        super.setName("SelectorThread-" + super.getId());
+
         HELPER_BUFFER_SIZE = sc.HELPER_BUFFER_SIZE;
         HELPER_BUFFER_COUNT = sc.HELPER_BUFFER_COUNT;
         MAX_SEND_PER_PASS = sc.MAX_SEND_PER_PASS;
         MAX_READ_PER_PASS = sc.MAX_READ_PER_PASS;
-        
+
         SLEEP_TIME = sc.SLEEP_TIME;
-        
-        DIRECT_WRITE_BUFFER = ByteBuffer.allocateDirect(sc.WRITE_BUFFER_SIZE).order(BYTE_ORDER);
+
+        DIRECT_WRITE_BUFFER = ByteBuffer.allocateDirect(sc.WRITE_BUFFER_SIZE) .order(BYTE_ORDER);
         WRITE_BUFFER = ByteBuffer.wrap(new byte[sc.WRITE_BUFFER_SIZE]).order(BYTE_ORDER);
         READ_BUFFER = ByteBuffer.wrap(new byte[sc.READ_BUFFER_SIZE]).order(BYTE_ORDER);
-        
+
         STRING_BUFFER = new NioNetStringBuffer(64 * 1024);
-        
+
         _pendingClose = new NioNetStackList<MMOConnection<T>>();
         _bufferPool = new FastList<ByteBuffer>();
+        
         for (int i = 0; i < HELPER_BUFFER_COUNT; i++)
         {
-        	_bufferPool.addLast(ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER));
+            _bufferPool.addLast(ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER));
         }
-        
+
         _acceptFilter = acceptFilter;
         _packetHandler = packetHandler;
         _clientFactory = clientFactory;
@@ -111,29 +105,23 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
         selectable.configureBlocking(false);
 
         ServerSocket ss = selectable.socket();
+        
         if (address == null)
-        {
             ss.bind(new InetSocketAddress(tcpPort));
-        }
         else
-        {
             ss.bind(new InetSocketAddress(address, tcpPort));
-        }
+        
         selectable.register(_selector, SelectionKey.OP_ACCEPT);
     }
     
     final ByteBuffer getPooledBuffer()
     {
         if (_bufferPool.isEmpty())
-        {
             return ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER);
-        }
-        else
-        {
-            return _bufferPool.removeFirst();
-        }
+        
+        return _bufferPool.removeFirst();
     }
-
+    
     final void recycleBuffer(final ByteBuffer buf)
     {
         if (_bufferPool.size() < HELPER_BUFFER_COUNT)
@@ -148,17 +136,17 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     public final void run()
     {
         int selectedKeysCount = 0;
-        
+
         SelectionKey key;
         MMOConnection<T> con;
-        
+
         Iterator<SelectionKey> selectedKeys;
-        
+
         while (!_shutdown)
         {
             try
             {
-            	selectedKeysCount = _selector.selectNow();
+                selectedKeysCount = _selector.selectNow();
             }
             catch (IOException e)
             {
@@ -167,63 +155,48 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
 
             if (selectedKeysCount > 0)
             {
-            	selectedKeys = _selector.selectedKeys().iterator();
-            	
-            	while (selectedKeys.hasNext())
-            	{
-            		key = selectedKeys.next();
-            		selectedKeys.remove();
-            		
-            		con = (MMOConnection<T>)key.attachment();
-            		
-            		switch (key.readyOps())
+                selectedKeys = _selector.selectedKeys().iterator();
+
+                while (selectedKeys.hasNext())
+                {
+                    key = selectedKeys.next();
+                    selectedKeys.remove();
+
+                    con = (MMOConnection<T>) key.attachment();
+
+                    switch (key.readyOps())
                     {
                         case SelectionKey.OP_CONNECT:
-                        {
-                        	finishConnection(key, con);
+                            finishConnection(key, con);
                             break;
-                        }
-                        
                         case SelectionKey.OP_ACCEPT:
-                        {
-                        	acceptConnection(key, con);
+                            acceptConnection(key, con);
                             break;
-                        }
-                        
                         case SelectionKey.OP_READ:
-                        {
-                        	readPacket(key, con);
+                            readPacket(key, con);
                             break;
-                        }
-                        
                         case SelectionKey.OP_WRITE:
-                        {
-                        	writePacket(key, con);
+                            writePacket(key, con);
                             break;
-                        }
-                        
                         case SelectionKey.OP_READ | SelectionKey.OP_WRITE:
-                        {
-                        	writePacket(key, con);
+                            writePacket(key, con);
                             if (key.isValid())
                                 readPacket(key, con);
-                            
                             break;
-                        }
                     }
-            	}
+                }
             }
-            
+
             synchronized (_pendingClose)
             {
-            	while (!_pendingClose.isEmpty())
-            	{
-            		con = _pendingClose.removeFirst();
-            		writeClosePacket(con);
-            		closeConnectionImpl(con.getSelectionKey(), con);
-            	}
+                while (!_pendingClose.isEmpty())
+                {
+                    con = _pendingClose.removeFirst();
+                    writeClosePacket(con);
+                    closeConnectionImpl(con.getSelectionKey(), con);
+                }
             }
-            
+
             try
             {
                 Thread.sleep(SLEEP_TIME);
@@ -233,7 +206,6 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                 e.printStackTrace();
             }
         }
-        
         closeSelectorThread();
     }
     
@@ -241,7 +213,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     {
         try
         {
-            ((SocketChannel)key.channel()).finishConnect();
+            ((SocketChannel) key.channel()).finishConnect();
         }
         catch (IOException e)
         {
@@ -256,12 +228,12 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
             key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT);
         }
     }
-
+    
     private final void acceptConnection(final SelectionKey key, MMOConnection<T> con)
     {
-    	ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
+        ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
         SocketChannel sc;
-        
+
         try
         {
             while ((sc = ssc.accept()) != null)
@@ -270,15 +242,12 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                 {
                     sc.configureBlocking(false);
                     SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
-                    
                     con = new MMOConnection<T>(this, sc.socket(), clientKey);
                     con.setClient(_clientFactory.create(con));
                     clientKey.attach(con);
                 }
                 else
-                {
                     sc.socket().close();
-                }
             }
         }
         catch (IOException e)
@@ -289,123 +258,98 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     
     private final void readPacket(final SelectionKey key, final MMOConnection<T> con)
     {
-    	if (!con.isClosed())
-    	{
-    		ByteBuffer buf;
+        if (!con.isClosed())
+        {
+            ByteBuffer buf;
             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
+
+            // 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())
-            {
-                // should never happen
-                System.err.println("POS ANTES SC.READ(): "+buf.position()+" limit: "+buf.limit());
-                System.err.println("NOOBISH ERROR "+( buf == READ_BUFFER ? "READ_BUFFER" : "temp"));
                 System.exit(0);
-            }
-            
+
             int result = -2;
-            
+
             try
             {
                 result = con.read(buf);
             }
             catch (IOException e)
             {
-                //error handling goes bellow
+                // error handling goes bellow
             }
-            
+
             if (result > 0)
             {
-            	buf.flip();
-            	
-            	final T client = con.getClient();
-            	
-            	for (int i = 0; i < MAX_READ_PER_PASS; i++)
-            	{
-            		 if (!tryReadPacket(key, client, buf, con))
-            			return;
-            	}
-            	
-            	// only reachable if MAX_READ_PER_PASS has been reached
-            	// check if there are some more bytes in buffer
-            	// and allocate/compact to prevent content lose.
-            	if (buf.remaining() > 0)
-            	{
-            		// did we use the READ_BUFFER ?
+                buf.flip();
+
+                final T client = con.getClient();
+
+                for (int i = 0; i < MAX_READ_PER_PASS; i++)
+                {
+                    if (!tryReadPacket(key, client, buf, con))
+                        return;
+                }
+
+                // only reachable if MAX_READ_PER_PASS has been reached
+                // check if there are some more bytes in buffer
+                // and allocate/compact to prevent content lose.
+                if (buf.remaining() > 0)
+                {
+                    // did we use the READ_BUFFER ?
                     if (buf == READ_BUFFER)
-                    {
-                    	// move the pending byte to the connections READ_BUFFER
+                        // move the pending byte to the connections READ_BUFFER
                         allocateReadBuffer(con);
-                    }
                     else
-                    {
-                    	// move the first byte to the beginning :)
+                        // move the first byte to the beginning :)
                         buf.compact();
-                    }
-            	}
+                }
             }
             else
             {
-            	switch (result)
-            	{
-            		case 0:
-            		case -1:
-            		{
-            			closeConnectionImpl(key, con);
-            			break;
-            		}
-            		case -2:
-            		{
-            			con.getClient().onForcedDisconnection();
-            			closeConnectionImpl(key, con);
-            			break;
-            		}
-            	}
-                
+                switch (result)
+                {
+                    case 0:
+                    case -1:
+                        closeConnectionImpl(key, con);
+                        break;
+                    case -2:
+                        con.getClient().onForcedDisconnection();
+                        closeConnectionImpl(key, con);
+                        break;
+                }
             }
-    	}
+        }
     }
     
     private final boolean tryReadPacket(final SelectionKey key, final T client, final ByteBuffer buf, final MMOConnection<T> con)
     {
-    	switch (buf.remaining())
-    	{
-    		case 0:
-    		{
-    			// buffer is full
-    			// nothing to read
-    			return false;
-    		}
-    		
-    		case 1:
-    		{
-    			// we don`t have enough data for header so we need to read
-            	key.interestOps(key.interestOps() | SelectionKey.OP_READ);
-                
-            	// did we use the READ_BUFFER ?
+        switch (buf.remaining())
+        {
+            case 0:
+                // buffer is full
+                // nothing to read
+                return false;
+            case 1:
+                // we don`t have enough data for header so we need to read
+                key.interestOps(key.interestOps() | SelectionKey.OP_READ);
+
+                // did we use the READ_BUFFER ?
                 if (buf == READ_BUFFER)
-                {
-                	// move the pending byte to the connections READ_BUFFER
+                    // move the pending byte to the connections READ_BUFFER
                     allocateReadBuffer(con);
-                }
                 else
-                {
-                	// move the first byte to the beginning :)
+                    // move the first byte to the beginning :)
                     buf.compact();
-                }
-                
                 return false;
-    		}
-    		
-    		default:
-    		{
-    			// data size excluding header size :>
-    			final int dataPending = (buf.getShort() & 0xFFFF) - HEADER_SIZE;
-                
+            default:
+                // data size excluding header size :>
+                final int dataPending = (buf.getShort() & 0xFFFF) - HEADER_SIZE;
+
                 // do we got enough bytes for the packet?
                 if (dataPending <= buf.remaining())
                 {
@@ -416,7 +360,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                         parseClientPacket(pos, buf, dataPending, client);
                         buf.position(pos + dataPending);
                     }
-                    
+
                     // if we are done with this buffer
                     if (!buf.hasRemaining())
                     {
@@ -429,21 +373,20 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                         {
                             READ_BUFFER.clear();
                         }
-                        
                         return false;
                     }
-                    
                     return true;
                 }
                 else
                 {
-                    // we don`t have enough bytes for the dataPacket so we need to read
-                	key.interestOps(key.interestOps() | SelectionKey.OP_READ);
+                    // we don`t have enough bytes for the dataPacket so we need
+                    // to read
+                    key.interestOps(key.interestOps() | SelectionKey.OP_READ);
                     
-                	// did we use the READ_BUFFER ?
+                    // did we use the READ_BUFFER ?
                     if (buf == READ_BUFFER)
                     {
-                    	// move it`s position 
+                        // move it`s position
                         buf.position(buf.position() - HEADER_SIZE);
                         // move the pending byte to the connections READ_BUFFER
                         allocateReadBuffer(con);
@@ -453,11 +396,9 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                         buf.position(buf.position() - HEADER_SIZE);
                         buf.compact();
                     }
-                    
                     return false;
                 }
-    		}
-    	}
+        }
     }
     
     private final void allocateReadBuffer(final MMOConnection<T> con)
@@ -465,14 +406,14 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
         con.setReadBuffer(getPooledBuffer().put(READ_BUFFER));
         READ_BUFFER.clear();
     }
-
+    
     private final void parseClientPacket(final int pos, final ByteBuffer buf, final int dataSize, final T client)
     {
         final boolean ret = client.decrypt(buf, dataSize);
-        
+
         if (ret && buf.hasRemaining())
         {
-            //  apply limit
+            // apply limit
             final int limit = buf.limit();
             buf.limit(pos + dataSize);
             final ReceivablePacket<T> cp = _packetHandler.handlePacket(buf, client);
@@ -482,15 +423,12 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                 cp._buf = buf;
                 cp._sbuf = STRING_BUFFER;
                 cp._client = client;
-                
+
                 if (cp.read())
-                {
-                	_executor.execute(cp);
-                }
-                
+                    _executor.execute(cp);
+
                 cp._buf = null;
                 cp._sbuf = null;
-                //cp._client = null;
             }
             buf.limit(limit);
         }
@@ -498,16 +436,17 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     
     private final void writeClosePacket(final MMOConnection<T> con)
     {
-    	final SendablePacket<T> sp = con.getClosePacket();
-    	if (sp != null)
-    	{
-    		WRITE_BUFFER.clear();
-    		
-    		putPacketIntoWriteBuffer(con.getClient(), sp);
-    		
-    		WRITE_BUFFER.flip();
-    		
-    		try
+        final SendablePacket<T> sp = con.getClosePacket();
+        
+        if (sp != null)
+        {
+            WRITE_BUFFER.clear();
+
+            putPacketIntoWriteBuffer(con.getClient(), sp);
+
+            WRITE_BUFFER.flip();
+
+            try
             {
                 con.write(WRITE_BUFFER);
             }
@@ -515,23 +454,23 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
             {
                 // error handling goes on the if bellow
             }
-    	}
+        }
     }
     
     protected final void writePacket(final SelectionKey key, final MMOConnection<T> con)
     {
         if (!prepareWriteBuffer(con))
         {
-        	key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
-        	return;
+            key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
+            return;
         }
-        
+
         DIRECT_WRITE_BUFFER.flip();
-        
+
         final int size = DIRECT_WRITE_BUFFER.remaining();
-        
+
         int result = -1;
-        
+
         try
         {
             result = con.write(DIRECT_WRITE_BUFFER);
@@ -540,7 +479,7 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
         {
             // error handling goes on the if bellow
         }
-        
+
         // check if no error happened
         if (result >= 0)
         {
@@ -552,14 +491,13 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
                 {
                     if (con.getSendQueue().isEmpty() && !con.hasPendingWriteBuffer())
                     {
-                    	key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
+                        key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
                     }
                 }
             }
-            else //incomplete write
-            {               
+            else
+                // incomplete write
                 con.createWriteBuffer(DIRECT_WRITE_BUFFER);
-            }
         }
         else
         {
@@ -567,61 +505,58 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
             closeConnectionImpl(key, con);
         }
     }
-    
+
     private final boolean prepareWriteBuffer(final MMOConnection<T> con)
     {
-    	boolean hasPending = false;
+        boolean hasPending = false;
         DIRECT_WRITE_BUFFER.clear();
-        
+
         // if there is pending content add it
         if (con.hasPendingWriteBuffer())
         {
             con.movePendingWriteBufferTo(DIRECT_WRITE_BUFFER);
             hasPending = true;
         }
-        
+
         if (DIRECT_WRITE_BUFFER.remaining() > 1 && !con.hasPendingWriteBuffer())
         {
             final NioNetStackList<SendablePacket<T>> sendQueue = con.getSendQueue();
             final T client = con.getClient();
             SendablePacket<T> sp;
-            
+
             for (int i = 0; i < MAX_SEND_PER_PASS; i++)
-        	{
-        		synchronized (con.getSendQueue())
-        		{
-        			if (sendQueue.isEmpty())
-        				sp = null;
-        			else
-        				sp = sendQueue.removeFirst();
-        		}
-        		
-        		if (sp == null)
-        			break;
-        		
-        		hasPending = true;
-        		
-        		// put into WriteBuffer
-        		putPacketIntoWriteBuffer(client, sp);
-        		
-        		WRITE_BUFFER.flip();
-        		
-        		if (DIRECT_WRITE_BUFFER.remaining() >= WRITE_BUFFER.limit())
+            {
+                synchronized (con.getSendQueue())
                 {
-                    DIRECT_WRITE_BUFFER.put(WRITE_BUFFER);
+                    if (sendQueue.isEmpty())
+                        sp = null;
+                    else
+                        sp = sendQueue.removeFirst();
                 }
+
+                if (sp == null)
+                    break;
+
+                hasPending = true;
+
+                // put into WriteBuffer
+                putPacketIntoWriteBuffer(client, sp);
+
+                WRITE_BUFFER.flip();
+
+                if (DIRECT_WRITE_BUFFER.remaining() >= WRITE_BUFFER.limit())
+                    DIRECT_WRITE_BUFFER.put(WRITE_BUFFER);
                 else
                 {
                     con.createWriteBuffer(WRITE_BUFFER);
                     break;
                 }
-        	}
+            }
         }
-        
         return hasPending;
     }
     
-    private final void putPacketIntoWriteBuffer(final T client, final SendablePacket<T> sp)
+    private final void putPacketIntoWriteBuffer(final T client,final SendablePacket<T> sp)
     {
         WRITE_BUFFER.clear();
 
@@ -629,25 +564,25 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
         final int headerPos = WRITE_BUFFER.position();
         final int dataPos = headerPos + HEADER_SIZE;
         WRITE_BUFFER.position(dataPos);
-        
+
         // set the write buffer
         sp._buf = WRITE_BUFFER;
         // write content to buffer
         sp.write();
         // delete the write buffer
         sp._buf = null;
-        
+
         // size (inclusive header)
         int dataSize = WRITE_BUFFER.position() - dataPos;
         WRITE_BUFFER.position(dataPos);
         client.encrypt(WRITE_BUFFER, dataSize);
-        
+
         // recalculate size after encryption
         dataSize = WRITE_BUFFER.position() - dataPos;
-        
+
         WRITE_BUFFER.position(headerPos);
         // write header
-        WRITE_BUFFER.putShort((short)(dataSize + HEADER_SIZE));
+        WRITE_BUFFER.putShort((short) (dataSize + HEADER_SIZE));
         WRITE_BUFFER.position(dataPos + dataSize);
     }
     
@@ -655,13 +590,13 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     {
         synchronized (_pendingClose)
         {
-        	_pendingClose.addLast(con);
+            _pendingClose.addLast(con);
         }
     }
     
     private final void closeConnectionImpl(final SelectionKey key, final MMOConnection<T> con)
     {
-    	try
+        try
         {
             // notify connection
             con.getClient().onDisconnection();
@@ -692,12 +627,12 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
     {
         _shutdown = true;
     }
-
-    protected void closeSelectorThread() 
+    
+    protected void closeSelectorThread()
     {
-    	for (final SelectionKey key : _selector.keys())
-    	{
-    		try
+        for (final SelectionKey key : _selector.keys())
+        {
+            try
             {
                 key.channel().close();
             }
@@ -705,11 +640,11 @@ public final class SelectorThread<T extends MMOClient<?>> extends Thread
             {
                 // ignore
             }
-    	}
-    	
+        }
+
         try
         {
-        	_selector.close();
+            _selector.close();
         }
         catch (IOException e)
         {

+ 32 - 31
MMOCore/src/org/mmocore/network/SendablePacket.java

@@ -19,68 +19,69 @@ package org.mmocore.network;
 
 /**
  * @author KenM
- *
+ * 
  */
-public abstract class SendablePacket<T extends MMOClient<?>> extends AbstractPacket<T>
+public abstract class SendablePacket<T extends MMOClient<?>> extends
+        AbstractPacket<T>
 {
     protected final void putInt(final int value)
     {
-    	_buf.putInt(value);
+        _buf.putInt(value);
     }
-    
+
     protected final void putDouble(final double value)
     {
-    	_buf.putDouble(value);
+        _buf.putDouble(value);
     }
-    
+
     protected final void putFloat(final float value)
     {
-    	_buf.putFloat(value);
+        _buf.putFloat(value);
     }
-    
+
     protected final void writeC(final int data)
     {
-    	_buf.put((byte)data);
+        _buf.put((byte) data);
     }
-    
+
     protected final void writeF(final double value)
     {
-    	_buf.putDouble(value);
+        _buf.putDouble(value);
     }
-    
+
     protected final void writeH(final int value)
     {
-    	_buf.putShort((short)value);
+        _buf.putShort((short) value);
     }
-    
+
     protected final void writeD(final int value)
     {
-    	_buf.putInt(value);
+        _buf.putInt(value);
     }
-    
+
     protected final void writeQ(final long value)
     {
-    	_buf.putLong(value);
+        _buf.putLong(value);
     }
-    
+
     protected final void writeB(final byte[] data)
     {
-    	_buf.put(data);
+        _buf.put(data);
     }
-    
+
     protected final void writeS(final String text)
     {
-    	if (text != null)
-    	{
-    		final int len = text.length();
-    		for (int i = 0; i < len; i++)
-    		{
-    			_buf.putChar(text.charAt(i));
-    		}
-    	}
-    	
-    	_buf.putChar('\000');
+        if (text != null)
+        {
+            final int len = text.length();
+            for (int i = 0; i < len; i++)
+            {
+                _buf.putChar(text.charAt(i));
+            }
+        }
+
+        _buf.putChar('\000');
     }
-    
+
     protected abstract void write();
 }