Reuse one big buffer per thread and copy the outbound

smaller buffers.  Way faster and way cheaper overall.


git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7259 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
PSp..om 14 years ago
parent 85b4eea8d7
commit 9073820361
  1. 28
      engine/src/networking/com/jme3/network/base/DefaultClient.java

@ -57,6 +57,8 @@ public class DefaultClient implements Client
{ {
static Logger log = Logger.getLogger(DefaultClient.class.getName()); static Logger log = Logger.getLogger(DefaultClient.class.getName());
private ThreadLocal<ByteBuffer> dataBuffer = new ThreadLocal<ByteBuffer>();
private int id = -1; private int id = -1;
private boolean isRunning = false; private boolean isRunning = false;
private CountDownLatch connecting = new CountDownLatch(1); private CountDownLatch connecting = new CountDownLatch(1);
@ -199,15 +201,23 @@ public class DefaultClient implements Client
waitForConnected(); waitForConnected();
} }
// For now just send direclty. We allocate our ByteBuffer buffer = dataBuffer.get();
// own buffer each time because this method might if( buffer == null ) {
// be called from multiple threads. If writing buffer = ByteBuffer.allocate( 65536 + 2 );
// is queued into its own thread then that could dataBuffer.set(buffer);
// be shared. }
// Writing is now done on a background thread. buffer.clear();
// If we ever share a ByteBuffer then it will need to be
// copied before handing off. // Convert the message to bytes
ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); buffer = MessageProtocol.messageToBuffer(message, buffer);
// Since we share the buffer between invocations, we will need to
// copy this message's part out of it. This is because we actually
// do the send on a background thread.
byte[] temp = new byte[buffer.remaining()];
System.arraycopy(buffer.array(), buffer.position(), temp, 0, buffer.remaining());
buffer = ByteBuffer.wrap(temp);
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" );

Loading…
Cancel
Save