Procházet zdrojové kódy

More graceful shutdown of client-initiated close of
the Client. Way easier to take care of this stuff with
working code... thanks, MonkeyZone.


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7033 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

PSp..om před 14 roky
rodič
revize
5d525edc59

+ 10 - 0
engine/src/networking/com/jme3/network/base/ConnectorAdapter.java

@@ -39,6 +39,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import com.jme3.network.Message;
 import com.jme3.network.MessageListener;
 import com.jme3.network.kernel.Connector;
+import com.jme3.network.kernel.ConnectorException;
 import com.jme3.network.serializing.Serializer;
 
 /**
@@ -90,6 +91,15 @@ public class ConnectorAdapter extends Thread
         
         while( go.get() ) {
             ByteBuffer buffer = connector.read();
+            if( buffer == null ) {
+                if( go.get() ) {
+                    throw new ConnectorException( "Connector closed." ); 
+                } else {
+                    // Just dump out because a null buffer is expected
+                    // from a closed/closing connector
+                    break;
+                }
+            }
             
             protocol.addBuffer( buffer );
             

+ 11 - 2
engine/src/networking/com/jme3/network/kernel/tcp/SocketConnector.java

@@ -35,6 +35,7 @@ package com.jme3.network.kernel.tcp;
 import java.io.*;
 import java.net.*;
 import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.jme3.network.kernel.*;
 
@@ -54,6 +55,7 @@ public class SocketConnector implements Connector
     private OutputStream out;
     private SocketAddress remoteAddress;
     private byte[] buffer = new byte[65535];
+    private AtomicBoolean connected = new AtomicBoolean(false);
 
     public SocketConnector( InetAddress address, int port ) throws IOException
     {
@@ -66,6 +68,8 @@ public class SocketConnector implements Connector
         
         in = sock.getInputStream();
         out = sock.getOutputStream();
+        
+        connected.set(true);
     }
  
     protected void checkClosed()
@@ -87,6 +91,7 @@ public class SocketConnector implements Connector
         try {
             Socket temp = sock;
             sock = null;            
+            connected.set(false);
             temp.close();
         } catch( IOException e ) {            
             throw new ConnectorException( "Error closing socket for:" + remoteAddress, e );
@@ -111,14 +116,18 @@ public class SocketConnector implements Connector
             // Read what we can
             int count = in.read(buffer);
             if( count < 0 ) {
-                // Socket it closed
+                // Socket is closed
                 close();
                 return null;
             }
 
             // Wrap it in a ByteBuffer for the caller
             return ByteBuffer.wrap( buffer, 0, count ); 
-        } catch( IOException e ) {        
+        } catch( IOException e ) {
+            if( !connected.get() ) {
+                // Nothing to see here... just move along
+                return null;
+            }        
             throw new ConnectorException( "Error reading from connection to:" + remoteAddress, e );    
         }                
     }

+ 11 - 2
engine/src/networking/com/jme3/network/kernel/udp/UdpConnector.java

@@ -35,6 +35,7 @@ package com.jme3.network.kernel.udp;
 import java.io.*;
 import java.net.*;
 import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.jme3.network.kernel.*;
 
@@ -51,6 +52,7 @@ public class UdpConnector implements Connector
     private DatagramSocket sock = new DatagramSocket();
     private SocketAddress remoteAddress;
     private byte[] buffer = new byte[65535];
+    private AtomicBoolean connected = new AtomicBoolean(false);
 
     /**
      *  In order to provide proper available() checking, we
@@ -70,6 +72,8 @@ public class UdpConnector implements Connector
         
         // Setup to receive only from the remote address
         sock.connect( remoteAddress );
+        
+        connected.set(true);
     }
  
     protected void checkClosed()
@@ -89,7 +93,8 @@ public class UdpConnector implements Connector
     {
         checkClosed();
         DatagramSocket temp = sock;
-        sock = null;            
+        sock = null;
+        connected.set(false);            
         temp.close();
     }     
 
@@ -116,7 +121,11 @@ public class UdpConnector implements Connector
             
             // Wrap it in a ByteBuffer for the caller
             return ByteBuffer.wrap( buffer, 0, packet.getLength() ); 
-        } catch( IOException e ) {        
+        } catch( IOException e ) {
+            if( !connected.get() ) {
+                // Nothing to see here... just move along
+                return null;
+            }        
             throw new ConnectorException( "Error reading from connection to:" + remoteAddress, e );    
         }                
     }