mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-29 08:34:37 +01:00
Separate life of socket from that of selector: close socket if must
but don't exit the thread and be ready to reopen. Fixes bug where wouldn't reconnect to relay if died and then came back.
This commit is contained in:
parent
cb5cc975b3
commit
13b46297e0
1 changed files with 38 additions and 13 deletions
|
@ -82,16 +82,10 @@ public class CommsTransport extends Thread implements TransportProcs {
|
|||
{
|
||||
try {
|
||||
m_selector = Selector.open();
|
||||
m_socketChannel = SocketChannel.open();
|
||||
m_socketChannel.configureBlocking( false );
|
||||
InetSocketAddress isa
|
||||
= new InetSocketAddress( m_addr.ip_relay_hostName,
|
||||
m_addr.ip_relay_port );
|
||||
m_socketChannel.connect( isa );
|
||||
|
||||
loop();
|
||||
|
||||
m_socketChannel.close();
|
||||
closeSocket();
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
} catch ( UnresolvedAddressException uae ) {
|
||||
|
@ -105,19 +99,23 @@ public class CommsTransport extends Thread implements TransportProcs {
|
|||
{
|
||||
while ( !m_done ) {
|
||||
try {
|
||||
int ops = figureOps();
|
||||
Utils.logf( "calling with ops=" + ops );
|
||||
m_socketChannel.register( m_selector, ops );
|
||||
synchronized( this ) {
|
||||
if ( null != m_socketChannel ) {
|
||||
int ops = figureOps();
|
||||
Utils.logf( "calling with ops=%x", ops );
|
||||
m_socketChannel.register( m_selector, ops );
|
||||
}
|
||||
}
|
||||
m_selector.select();
|
||||
} catch ( ClosedChannelException cce ) {
|
||||
// we get this when relay goes down. Need to notify!
|
||||
m_jniThread.handle( JNICmd.CMD_TRANSFAIL );
|
||||
closeSocket();
|
||||
Utils.logf( "exiting: " + cce.toString() );
|
||||
break;
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
closeSocket();
|
||||
Utils.logf( "exiting: " + ioe.toString() );
|
||||
Utils.logf( ioe.toString() );
|
||||
break;
|
||||
}
|
||||
|
||||
Iterator<SelectionKey> iter = m_selector.selectedKeys().iterator();
|
||||
|
@ -168,11 +166,36 @@ public class CommsTransport extends Thread implements TransportProcs {
|
|||
m_buffersOut.add( netbuf );
|
||||
Assert.assertEquals( netbuf.remaining(), 0 );
|
||||
|
||||
if ( null == m_socketChannel ) {
|
||||
try {
|
||||
m_socketChannel = SocketChannel.open();
|
||||
m_socketChannel.configureBlocking( false );
|
||||
InetSocketAddress isa
|
||||
= new InetSocketAddress( m_addr.ip_relay_hostName,
|
||||
m_addr.ip_relay_port );
|
||||
m_socketChannel.connect( isa );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Utils.logf( ioe.toString() );
|
||||
}
|
||||
}
|
||||
|
||||
if ( null != m_selector ) {
|
||||
m_selector.wakeup(); // tell it it's got some writing to do
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void closeSocket()
|
||||
{
|
||||
if ( null != m_socketChannel ) {
|
||||
try {
|
||||
m_socketChannel.close();
|
||||
} catch ( Exception e ) {
|
||||
Utils.logf( "closing socket: %s", e.toString() );
|
||||
}
|
||||
m_socketChannel = null;
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void getOut()
|
||||
{
|
||||
if ( null != m_bytesOut && m_bytesOut.remaining() == 0 ) {
|
||||
|
@ -187,7 +210,9 @@ public class CommsTransport extends Thread implements TransportProcs {
|
|||
|
||||
private synchronized int figureOps() {
|
||||
int ops;
|
||||
if ( m_socketChannel.isConnected() ) {
|
||||
if ( null == m_socketChannel ) {
|
||||
ops = 0;
|
||||
} else if ( m_socketChannel.isConnected() ) {
|
||||
ops = SelectionKey.OP_READ;
|
||||
if ( (null != m_bytesOut && m_bytesOut.hasRemaining())
|
||||
|| m_buffersOut.size() > 0 ) {
|
||||
|
|
Loading…
Add table
Reference in a new issue