|
|
@ -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,14 +116,18 @@ 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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 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 ); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|