Modified the DefaultClient (really its ConnectionAdapters)

to send their outbound data on a background thread.


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7257 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
PSp..om 14 years ago
parent e298a4357e
commit 7d6d513fa5
  1. 31
      engine/src/networking/com/jme3/network/base/ConnectorAdapter.java
  2. 7
      engine/src/networking/com/jme3/network/base/DefaultClient.java

@ -35,6 +35,8 @@ package com.jme3.network.base;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import com.jme3.network.Message; import com.jme3.network.Message;
import com.jme3.network.MessageListener; import com.jme3.network.MessageListener;
@ -64,6 +66,9 @@ public class ConnectorAdapter extends Thread
private MessageListener<Object> dispatcher; private MessageListener<Object> dispatcher;
private AtomicBoolean go = new AtomicBoolean(true); private AtomicBoolean go = new AtomicBoolean(true);
// Writes messages out on a background thread
private ExecutorService writer = Executors.newFixedThreadPool(1);
// Marks the messages as reliable or not if they came // Marks the messages as reliable or not if they came
// through this connector. // through this connector.
private boolean reliable; private boolean reliable;
@ -81,6 +86,9 @@ public class ConnectorAdapter extends Thread
{ {
go.set(false); go.set(false);
// Kill the writer service
writer.shutdown();
// Kill the connector // Kill the connector
connector.close(); connector.close();
} }
@ -90,6 +98,11 @@ public class ConnectorAdapter extends Thread
dispatcher.messageReceived( null, m ); dispatcher.messageReceived( null, m );
} }
public void write( ByteBuffer data )
{
writer.execute( new MessageWriter(data) );
}
public void run() public void run()
{ {
MessageProtocol protocol = new MessageProtocol(); MessageProtocol protocol = new MessageProtocol();
@ -116,4 +129,22 @@ public class ConnectorAdapter extends Thread
} }
} }
protected class MessageWriter implements Runnable
{
private ByteBuffer data;
public MessageWriter( ByteBuffer data )
{
this.data = data;
}
public void run()
{
if( !go.get() )
return;
connector.write(data);
}
}
} }

@ -204,13 +204,16 @@ public class DefaultClient implements Client
// be called from multiple threads. If writing // be called from multiple threads. If writing
// is queued into its own thread then that could // is queued into its own thread then that could
// be shared. // be shared.
// Writing is now done on a background thread.
// If we ever share a ByteBuffer then it will need to be
// copied before handing off.
ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null);
if( message.isReliable() || fast == null ) { if( message.isReliable() || fast == null ) {
if( reliable == null ) if( reliable == null )
throw new RuntimeException( "No reliable connector configured" ); throw new RuntimeException( "No reliable connector configured" );
reliable.write(buffer); reliableAdapter.write(buffer);
} else { } else {
fast.write(buffer); fastAdapter.write(buffer);
} }
} }

Loading…
Cancel
Save