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
3.0
PSp..om 14 years ago
parent 312e26aeca
commit 5d525edc59
  1. 10
      engine/src/networking/com/jme3/network/base/ConnectorAdapter.java
  2. 11
      engine/src/networking/com/jme3/network/kernel/tcp/SocketConnector.java
  3. 9
      engine/src/networking/com/jme3/network/kernel/udp/UdpConnector.java

@ -39,6 +39,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import com.jme3.network.Message; import com.jme3.network.Message;
import com.jme3.network.MessageListener; import com.jme3.network.MessageListener;
import com.jme3.network.kernel.Connector; import com.jme3.network.kernel.Connector;
import com.jme3.network.kernel.ConnectorException;
import com.jme3.network.serializing.Serializer; import com.jme3.network.serializing.Serializer;
/** /**
@ -90,6 +91,15 @@ public class ConnectorAdapter extends Thread
while( go.get() ) { while( go.get() ) {
ByteBuffer buffer = connector.read(); 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 ); protocol.addBuffer( buffer );

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

@ -35,6 +35,7 @@ package com.jme3.network.kernel.udp;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
import com.jme3.network.kernel.*; import com.jme3.network.kernel.*;
@ -51,6 +52,7 @@ public class UdpConnector implements Connector
private DatagramSocket sock = new DatagramSocket(); private DatagramSocket sock = new DatagramSocket();
private SocketAddress remoteAddress; private SocketAddress remoteAddress;
private byte[] buffer = new byte[65535]; private byte[] buffer = new byte[65535];
private AtomicBoolean connected = new AtomicBoolean(false);
/** /**
* In order to provide proper available() checking, we * 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 // Setup to receive only from the remote address
sock.connect( remoteAddress ); sock.connect( remoteAddress );
connected.set(true);
} }
protected void checkClosed() protected void checkClosed()
@ -90,6 +94,7 @@ public class UdpConnector implements Connector
checkClosed(); checkClosed();
DatagramSocket temp = sock; DatagramSocket temp = sock;
sock = null; sock = null;
connected.set(false);
temp.close(); temp.close();
} }
@ -117,6 +122,10 @@ public class UdpConnector implements Connector
// Wrap it in a ByteBuffer for the caller // Wrap it in a ByteBuffer for the caller
return ByteBuffer.wrap( buffer, 0, packet.getLength() ); 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 ); throw new ConnectorException( "Error reading from connection to:" + remoteAddress, e );
} }
} }

Loading…
Cancel
Save