Browse Source

MMOCORE: multiple close packets support.

JIV 14 năm trước cách đây
mục cha
commit
8fae79be07

+ 26 - 13
MMOCore/src/org/mmocore/network/MMOConnection.java

@@ -48,7 +48,7 @@ public final class MMOConnection<T extends MMOClient<?>>
 
     private final SelectionKey _selectionKey;
 
-    private SendablePacket<T> _closePacket;
+    //private SendablePacket<T> _closePacket;
 
     private ByteBuffer _readBuffer;
 
@@ -56,7 +56,7 @@ public final class MMOConnection<T extends MMOClient<?>>
 
     private ByteBuffer _secondaryWriteBuffer;
 
-    private boolean _pendingClose;
+    private volatile boolean _pendingClose;
 
     private T _client;
 
@@ -87,15 +87,15 @@ public final class MMOConnection<T extends MMOClient<?>>
     public final void sendPacket(final SendablePacket<T> sp)
     {
         sp._client = _client;
-
+        
+        if (_pendingClose)
+            return;
+        
         synchronized (getSendQueue())
         {
-            if (!_pendingClose)
-            {
-                _sendQueue.addLast(sp);
-            }
+            _sendQueue.addLast(sp);
         }
-
+        
         if (!_sendQueue.isEmpty())
         {
             try
@@ -208,19 +208,32 @@ public final class MMOConnection<T extends MMOClient<?>>
         return _sendQueue;
     }
 
-    final SendablePacket<T> getClosePacket()
+    /*final SendablePacket<T> getClosePacket()
     {
         return _closePacket;
-    }
+    }*/
 
+    @SuppressWarnings("unchecked")
     public final void close(final SendablePacket<T> sp)
     {
+        
+        close(new SendablePacket[] {sp});
+    }
+    
+    public final void close(final SendablePacket<T>[] closeList)
+    {
+        if (_pendingClose)
+            return;
+        
         synchronized (getSendQueue())
         {
             if (!_pendingClose)
+            {
                 _pendingClose = true;
-
-            _sendQueue.clear();
+                _sendQueue.clear();
+                for (SendablePacket<T> sp : closeList)
+                    _sendQueue.addLast(sp);
+            }
         }
 
         try
@@ -233,7 +246,7 @@ public final class MMOConnection<T extends MMOClient<?>>
             // ignore
         }
 
-        _closePacket = sp;
+        //_closePacket = sp;
         _selectorThread.closeConnection(this);
     }
 

+ 2 - 3
MMOCore/src/org/mmocore/network/SelectorThread.java

@@ -436,9 +436,8 @@ 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)
+        SendablePacket<T> sp;
+        while ((sp = con.getSendQueue().removeFirst()) != null)
         {
             WRITE_BUFFER.clear();