Added code that will wake up the blocked kernel-caller

if there are events to deal with.  This wasn't a big
deal for connects because there's always an accompanying
message but for disconnects we wouldn't get events until
the next message came through.  Always meant to go back
and fix it and now I have.  Note: this is because our
adapters use a single thread for pulling envelopes off
and for dispatching connection events.


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7123 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
PSp..om 14 years ago
parent 3fc0d506f4
commit e58ec4b8a0
  1. 6
      engine/src/networking/com/jme3/network/base/KernelAdapter.java
  2. 7
      engine/src/networking/com/jme3/network/kernel/Kernel.java
  3. 15
      engine/src/networking/com/jme3/network/kernel/tcp/SelectorKernel.java

@ -140,7 +140,9 @@ public class KernelAdapter extends Thread
byte[] data = env.getData(); byte[] data = env.getData();
ByteBuffer buffer = ByteBuffer.wrap(data); ByteBuffer buffer = ByteBuffer.wrap(data);
protocol.addBuffer( buffer ); int count = protocol.addBuffer( buffer );
if( count == 0 )
throw new RuntimeException( "Envelope contained incomplete data:" + env );
// Should be complete... and maybe we should check but we don't // Should be complete... and maybe we should check but we don't
Message m = null; Message m = null;
@ -177,6 +179,8 @@ public class KernelAdapter extends Thread
// Grab the next envelope // Grab the next envelope
Envelope e = kernel.read(); Envelope e = kernel.read();
if( e == Kernel.EVENTS_PENDING )
continue; // We'll catch it up above
// Check for pending events that might have // Check for pending events that might have
// come in while we were blocking. This is usually // come in while we were blocking. This is usually

@ -46,6 +46,13 @@ import com.jme3.network.Filter;
*/ */
public interface Kernel public interface Kernel
{ {
/**
* A marker envelope returned from read() that indicates that
* there are events pending. This allows a single thread to
* more easily process the envelopes and endpoint events.
*/
public static final Envelope EVENTS_PENDING = new Envelope( null, new byte[0], false );
/** /**
* Initializes the kernel and starts any internal processing. * Initializes the kernel and starts any internal processing.
*/ */

@ -160,10 +160,21 @@ public class SelectorKernel extends AbstractKernel
protected void removeEndpoint( NioEndpoint p, SocketChannel c ) protected void removeEndpoint( NioEndpoint p, SocketChannel c )
{ {
System.out.println( "removeEndpoint(" + p + ", " + c + ")" );
endpoints.remove( p.getId() ); endpoints.remove( p.getId() );
// Enqueue an endpoint event for the listeners // Enqueue an endpoint event for the listeners
addEvent( EndpointEvent.createRemove( this, p ) ); addEvent( EndpointEvent.createRemove( this, p ) );
// If there are no pending messages then add one so that the
// kernel-user knows to wake up if it is only listening for
// envelopes.
if( !hasEnvelopes() ) {
// Note: this is not really a race condition. At worst, our
// event has already been handled by now and it does no harm
// to check again.
addEnvelope( EVENTS_PENDING );
}
} }
/** /**
@ -393,7 +404,11 @@ public class SelectorKernel extends AbstractKernel
i.remove(); i.remove();
if( !key.isValid() ) if( !key.isValid() )
{
// When does this happen?
log.log( Level.INFO, "Key is not valid:{0}.", key );
continue; continue;
}
if( key.isAcceptable() ) if( key.isAcceptable() )
accept(key); accept(key);

Loading…
Cancel
Save