wrap a bunch of tcp-connection code in !XWApp.UDP_ENABLED so compiler

can make it go away.
This commit is contained in:
Eric House 2013-09-23 07:00:05 -07:00
parent 107ba4c940
commit 8ab832a136

View file

@ -82,129 +82,133 @@ public class CommsTransport implements TransportProcs,
@Override @Override
public void run() public void run()
{ {
m_done = false; if ( !XWApp.UDP_ENABLED ) {
boolean failed = true; m_done = false;
try { boolean failed = true;
if ( Build.PRODUCT.contains("sdk") ) { try {
System.setProperty("java.net.preferIPv6Addresses", "false"); if ( Build.PRODUCT.contains("sdk") ) {
System.setProperty("java.net.preferIPv6Addresses", "false");
}
m_selector = Selector.open();
failed = loop();
closeSocket();
} catch ( java.io.IOException ioe ) {
DbgUtils.loge( ioe );
} catch ( UnresolvedAddressException uae ) {
DbgUtils.logf( "bad address: name: %s; port: %s; exception: %s",
m_relayAddr.ip_relay_hostName,
m_relayAddr.ip_relay_port,
uae.toString() );
} }
m_selector = Selector.open(); m_thread = null;
if ( failed ) {
failed = loop(); m_jniThread.handle( JNICmd.CMD_TRANSFAIL );
}
closeSocket();
} catch ( java.io.IOException ioe ) {
DbgUtils.loge( ioe );
} catch ( UnresolvedAddressException uae ) {
DbgUtils.logf( "bad address: name: %s; port: %s; exception: %s",
m_relayAddr.ip_relay_hostName,
m_relayAddr.ip_relay_port,
uae.toString() );
}
m_thread = null;
if ( failed ) {
m_jniThread.handle( JNICmd.CMD_TRANSFAIL );
} }
} }
private boolean loop() private boolean loop()
{ {
boolean failed = false; boolean failed = false;
outer_loop: if ( !XWApp.UDP_ENABLED ) {
while ( !m_done ) { outer_loop:
try { while ( !m_done ) {
synchronized( this ) { try {
synchronized( this ) {
// if we have data and no socket, try to connect. // if we have data and no socket, try to connect.
if ( null == m_socketChannel if ( null == m_socketChannel
&& 0 < m_buffersOut.size() ) { && 0 < m_buffersOut.size() ) {
try { try {
m_socketChannel = SocketChannel.open(); m_socketChannel = SocketChannel.open();
m_socketChannel.configureBlocking( false ); m_socketChannel.configureBlocking( false );
DbgUtils.logf( "connecting to %s:%d", DbgUtils.logf( "connecting to %s:%d",
m_relayAddr.ip_relay_hostName, m_relayAddr.ip_relay_hostName,
m_relayAddr.ip_relay_port ); m_relayAddr.ip_relay_port );
InetSocketAddress isa = new InetSocketAddress isa = new
InetSocketAddress(m_relayAddr.ip_relay_hostName, InetSocketAddress(m_relayAddr.ip_relay_hostName,
m_relayAddr.ip_relay_port ); m_relayAddr.ip_relay_port );
m_socketChannel.connect( isa ); m_socketChannel.connect( isa );
} catch ( java.io.IOException ioe ) { } catch ( java.io.IOException ioe ) {
DbgUtils.loge( ioe ); DbgUtils.loge( ioe );
failed = true; failed = true;
break outer_loop; break outer_loop;
}
} }
}
if ( null != m_socketChannel ) { if ( null != m_socketChannel ) {
int ops = figureOps(); int ops = figureOps();
// DbgUtils.logf( "calling with ops=%x", ops ); // DbgUtils.logf( "calling with ops=%x", ops );
m_socketChannel.register( m_selector, ops ); m_socketChannel.register( m_selector, ops );
}
}
m_selector.select();
} catch ( ClosedChannelException cce ) {
// we get this when relay goes down. Need to notify!
failed = true;
closeSocket();
DbgUtils.logf( "exiting: %s", cce.toString() );
break; // don't try again
} catch ( java.io.IOException ioe ) {
closeSocket();
DbgUtils.logf( "exiting: %s", ioe.toString() );
DbgUtils.logf( ioe.toString() );
} catch ( java.nio.channels.NoConnectionPendingException ncp ) {
DbgUtils.loge( ncp );
closeSocket();
break;
}
Iterator<SelectionKey> iter = m_selector.selectedKeys().iterator();
while ( iter.hasNext() ) {
SelectionKey key = iter.next();
SocketChannel channel = (SocketChannel)key.channel();
iter.remove();
try {
if (key.isValid() && key.isConnectable()) {
if ( !channel.finishConnect() ) {
key.cancel();
} }
} }
if (key.isValid() && key.isReadable()) { m_selector.select();
m_bytesIn.clear(); // will wipe any pending data! } catch ( ClosedChannelException cce ) {
// DbgUtils.logf( "socket is readable; buffer has space for " // we get this when relay goes down. Need to notify!
// + m_bytesIn.remaining() );
int nRead = channel.read( m_bytesIn );
if ( nRead == -1 ) {
channel.close();
} else {
addIncoming();
}
ConnStatusHandler.
updateStatusIn( m_context, null,
CommsConnType.COMMS_CONN_RELAY,
0 <= nRead );
}
if (key.isValid() && key.isWritable()) {
getOut();
if ( null != m_bytesOut ) {
int nWritten = channel.write( m_bytesOut );
ConnStatusHandler.
updateStatusOut( m_context, null,
CommsConnType.COMMS_CONN_RELAY,
0 < nWritten );
}
}
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "%s: cancelling key", ioe.toString() );
key.cancel();
failed = true; failed = true;
break outer_loop; closeSocket();
} catch ( java.nio.channels. DbgUtils.logf( "exiting: %s", cce.toString() );
NoConnectionPendingException ncp ) { break; // don't try again
} catch ( java.io.IOException ioe ) {
closeSocket();
DbgUtils.logf( "exiting: %s", ioe.toString() );
DbgUtils.logf( ioe.toString() );
} catch ( java.nio.channels.NoConnectionPendingException ncp ) {
DbgUtils.loge( ncp ); DbgUtils.loge( ncp );
break outer_loop; closeSocket();
break;
}
Iterator<SelectionKey> iter = m_selector.selectedKeys().iterator();
while ( iter.hasNext() ) {
SelectionKey key = iter.next();
SocketChannel channel = (SocketChannel)key.channel();
iter.remove();
try {
if (key.isValid() && key.isConnectable()) {
if ( !channel.finishConnect() ) {
key.cancel();
}
}
if (key.isValid() && key.isReadable()) {
m_bytesIn.clear(); // will wipe any pending data!
// DbgUtils.logf( "socket is readable; buffer has space for "
// + m_bytesIn.remaining() );
int nRead = channel.read( m_bytesIn );
if ( nRead == -1 ) {
channel.close();
} else {
addIncoming();
}
ConnStatusHandler.
updateStatusIn( m_context, null,
CommsConnType.COMMS_CONN_RELAY,
0 <= nRead );
}
if (key.isValid() && key.isWritable()) {
getOut();
if ( null != m_bytesOut ) {
int nWritten = channel.write( m_bytesOut );
ConnStatusHandler.
updateStatusOut( m_context, null,
CommsConnType.COMMS_CONN_RELAY,
0 < nWritten );
}
}
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "%s: cancelling key", ioe.toString() );
key.cancel();
failed = true;
break outer_loop;
} catch ( java.nio.channels.
NoConnectionPendingException ncp ) {
DbgUtils.loge( ncp );
break outer_loop;
}
} }
} }
} }
@ -254,22 +258,24 @@ public class CommsTransport implements TransportProcs,
private synchronized void putOut( final byte[] buf ) private synchronized void putOut( final byte[] buf )
{ {
int len = buf.length; if ( !XWApp.UDP_ENABLED ) {
ByteBuffer netbuf = ByteBuffer.allocate( len + 2 ); int len = buf.length;
netbuf.putShort( (short)len ); ByteBuffer netbuf = ByteBuffer.allocate( len + 2 );
netbuf.put( buf ); netbuf.putShort( (short)len );
m_buffersOut.add( netbuf ); netbuf.put( buf );
Assert.assertEquals( netbuf.remaining(), 0 ); m_buffersOut.add( netbuf );
Assert.assertEquals( netbuf.remaining(), 0 );
if ( null != m_selector ) { if ( null != m_selector ) {
// tell it it's got some writing to do // tell it it's got some writing to do
m_selector.wakeup(); // getting NPE inside here -- see below m_selector.wakeup(); // getting NPE inside here -- see below
}
} }
} }
private synchronized void closeSocket() private synchronized void closeSocket()
{ {
if ( null != m_socketChannel ) { if ( !XWApp.UDP_ENABLED && null != m_socketChannel ) {
try { try {
m_socketChannel.close(); m_socketChannel.close();
} catch ( Exception e ) { } catch ( Exception e ) {
@ -309,33 +315,35 @@ public class CommsTransport implements TransportProcs,
private void addIncoming( ) private void addIncoming( )
{ {
m_bytesIn.flip(); if ( !XWApp.UDP_ENABLED ) {
m_bytesIn.flip();
for ( ; ; ) { for ( ; ; ) {
int len = m_bytesIn.remaining(); int len = m_bytesIn.remaining();
if ( len <= 0 ) { if ( len <= 0 ) {
break; break;
} }
if ( null == m_packetIn ) { // we're not mid-packet if ( null == m_packetIn ) { // we're not mid-packet
Assert.assertTrue( len > 1 ); // tell me if I see this case Assert.assertTrue( len > 1 ); // tell me if I see this case
if ( len == 1 ) { // half a length byte... if ( len == 1 ) { // half a length byte...
break; // can I leave it in the buffer? break; // can I leave it in the buffer?
} else { } else {
m_packetIn = new byte[m_bytesIn.getShort()]; m_packetIn = new byte[m_bytesIn.getShort()];
m_haveLen = 0; m_haveLen = 0;
} }
} else { // we're mid-packet } else { // we're mid-packet
int wantLen = m_packetIn.length - m_haveLen; int wantLen = m_packetIn.length - m_haveLen;
if ( wantLen > len ) { if ( wantLen > len ) {
wantLen = len; wantLen = len;
} }
m_bytesIn.get( m_packetIn, m_haveLen, wantLen ); m_bytesIn.get( m_packetIn, m_haveLen, wantLen );
m_haveLen += wantLen; m_haveLen += wantLen;
if ( m_haveLen == m_packetIn.length ) { if ( m_haveLen == m_packetIn.length ) {
// send completed packet // send completed packet
m_jniThread.handle( JNICmd.CMD_RECEIVE, m_packetIn, null ); m_jniThread.handle( JNICmd.CMD_RECEIVE, m_packetIn, null );
m_packetIn = null; m_packetIn = null;
}
} }
} }
} }
@ -343,17 +351,19 @@ public class CommsTransport implements TransportProcs,
private void waitToStopImpl() private void waitToStopImpl()
{ {
m_done = true; // this is in a race! if ( !XWApp.UDP_ENABLED ) {
if ( null != m_selector ) { m_done = true; // this is in a race!
m_selector.wakeup(); // getting NPE inside here -- see below if ( null != m_selector ) {
} m_selector.wakeup(); // getting NPE inside here -- see below
if ( null != m_thread ) { // synchronized this? Or use Thread method }
try { if ( null != m_thread ) { // synchronized this? Or use Thread method
m_thread.join(100); // wait up to 1/10 second try {
} catch ( java.lang.InterruptedException ie ) { m_thread.join(100); // wait up to 1/10 second
DbgUtils.loge( ie ); } catch ( java.lang.InterruptedException ie ) {
DbgUtils.loge( ie );
}
m_thread = null;
} }
m_thread = null;
} }
} }
@ -373,7 +383,7 @@ public class CommsTransport implements TransportProcs,
addr = faddr; addr = faddr;
} }
if ( null == m_relayAddr ) { if ( !XWApp.UDP_ENABLED && null == m_relayAddr ) {
m_relayAddr = new CommsAddrRec( addr ); m_relayAddr = new CommsAddrRec( addr );
} }