public void run() { MessageProtocol protocol = new MessageProtocol(); try { while( go.get() ) { ByteBuffer buffer = connector.read(); if( buffer == null ) { if( go.get() ) { throw new ConnectorException( "Connector closed." ); } else { // Just dump out because a null buffer is expected // from a closed/closing connector break; } } protocol.addBuffer( buffer ); Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable( reliable ); dispatch( m ); } } } catch( Exception e ) { handleError( e ); } }
createMessage( current );
@Override public void send( int channel, Message message ) { if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "send({0}, {1})", new Object[]{channel, message}); } checkChannel(channel); ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); channels[channel+CH_FIRST].send(buffer); }
protected void createAndDispatch( Envelope env ) { MessageProtocol protocol = getMessageBuffer(env.getSource()); byte[] data = env.getData(); ByteBuffer buffer = ByteBuffer.wrap(data); int count = protocol.addBuffer( buffer ); if( count == 0 ) { // This can happen if there was only a partial message // received. However, this should never happen for unreliable // connections. if( !reliable ) { // Log some additional information about the packet. int len = Math.min( 10, data.length ); StringBuilder sb = new StringBuilder(); for( int i = 0; i < len; i++ ) { sb.append( "[" + Integer.toHexString(data[i]) + "]" ); } log.log( Level.FINE, "First 10 bytes of incomplete nessage:" + sb ); throw new RuntimeException( "Envelope contained incomplete data:" + env ); } } // Should be complete... and maybe we should check but we don't Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable(reliable); dispatch( env.getSource(), m ); } }
protected MessageProtocol getMessageBuffer( Endpoint p ) { if( !reliable ) { // Since UDP comes in packets and they aren't split // up, there is no reason to buffer. In fact, there would // be a down side because there is no way for us to reliably // clean these up later since we'd create another one for // any random UDP packet that comes to the port. return new MessageProtocol(); } else { // See if we already have one MessageProtocol result = messageBuffers.get(p); if( result == null ) { result = new MessageProtocol(); messageBuffers.put(p, result); } return result; } }
protected void createAndDispatch( Envelope env ) { MessageProtocol protocol = getMessageBuffer(env.getSource()); byte[] data = env.getData(); ByteBuffer buffer = ByteBuffer.wrap(data); int count = protocol.addBuffer( buffer ); if( count == 0 ) { // This can happen if there was only a partial message // received. However, this should never happen for unreliable // connections. if( !reliable ) { // Log some additional information about the packet. int len = Math.min( 10, data.length ); StringBuilder sb = new StringBuilder(); for( int i = 0; i < len; i++ ) { sb.append( "[" + Integer.toHexString(data[i]) + "]" ); } log.log( Level.INFO, "First 10 bytes of incomplete nessage:" + sb ); throw new RuntimeException( "Envelope contained incomplete data:" + env ); } } // Should be complete... and maybe we should check but we don't Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable(reliable); dispatch( env.getSource(), m ); } }
protected MessageProtocol getMessageBuffer( Endpoint p ) { if( !reliable ) { // Since UDP comes in packets and they aren't split // up, there is no reason to buffer. In fact, there would // be a down side because there is no way for us to reliably // clean these up later since we'd create another one for // any random UDP packet that comes to the port. return new MessageProtocol(); } else { // See if we already have one MessageProtocol result = messageBuffers.get(p); if( result == null ) { result = new MessageProtocol(); messageBuffers.put(p, result); } return result; } }
public void run() { MessageProtocol protocol = new MessageProtocol(); try { while( go.get() ) { ByteBuffer buffer = connector.read(); if( buffer == null ) { if( go.get() ) { throw new ConnectorException( "Connector closed." ); } else { // Just dump out because a null buffer is expected // from a closed/closing connector break; } } protocol.addBuffer( buffer ); Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable( reliable ); dispatch( m ); } } } catch( Exception e ) { handleError( e ); } }
@Override public void broadcast( int channel, Filter<? super HostedConnection> filter, Message message ) { if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "broadcast({0}, {1}. {2})", new Object[]{channel, filter, message}); } if( connections.isEmpty() ) return; checkChannel(channel); ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); FilterAdapter adapter = filter == null ? null : new FilterAdapter(filter); channels.get(channel+CH_FIRST).broadcast( adapter, buffer, true, false ); }
protected void createAndDispatch( Envelope env ) { MessageProtocol protocol = getMessageBuffer(env.getSource()); byte[] data = env.getData(); ByteBuffer buffer = ByteBuffer.wrap(data); int count = protocol.addBuffer( buffer ); if( count == 0 ) { // This can happen if there was only a partial message // received. However, this should never happen for unreliable // connections. if( !reliable ) { // Log some additional information about the packet. int len = Math.min( 10, data.length ); StringBuilder sb = new StringBuilder(); for( int i = 0; i < len; i++ ) { sb.append( "[" + Integer.toHexString(data[i]) + "]" ); } log.log( Level.FINE, "First 10 bytes of incomplete nessage:" + sb ); throw new RuntimeException( "Envelope contained incomplete data:" + env ); } } // Should be complete... and maybe we should check but we don't Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable(reliable); dispatch( env.getSource(), m ); } }
protected MessageProtocol getMessageBuffer( Endpoint p ) { if( !reliable ) { // Since UDP comes in packets and they aren't split // up, there is no reason to buffer. In fact, there would // be a down side because there is no way for us to reliably // clean these up later since we'd create another one for // any random UDP packet that comes to the port. return new MessageProtocol(); } else { // See if we already have one MessageProtocol result = messageBuffers.get(p); if( result == null ) { result = new MessageProtocol(); messageBuffers.put(p, result); } return result; } }
public void run() { MessageProtocol protocol = new MessageProtocol(); try { while( go.get() ) { ByteBuffer buffer = connector.read(); if( buffer == null ) { if( go.get() ) { throw new ConnectorException( "Connector closed." ); } else { // Just dump out because a null buffer is expected // from a closed/closing connector break; } } protocol.addBuffer( buffer ); Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable( reliable ); dispatch( m ); } } } catch( Exception e ) { handleError( e ); } }
protected void send( int channel, Message message, boolean waitForConnected ) { checkRunning(); if( waitForConnected ) { // Make sure we aren't still connecting waitForConnected(); } ByteBuffer buffer = dataBuffer.get(); if( buffer == null ) { buffer = ByteBuffer.allocate( 65536 + 2 ); dataBuffer.set(buffer); } buffer.clear(); // Convert the message to bytes 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); channels.get(channel).write(buffer); }
@Override public void send( Message message ) { if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "send({0})", message); } ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); if( message.isReliable() || channels[CH_UNRELIABLE] == null ) { channels[CH_RELIABLE].send( buffer ); } else { channels[CH_UNRELIABLE].send( buffer ); } }
@Override public void broadcast( Filter<? super HostedConnection> filter, Message message ) { if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "broadcast({0}, {1})", new Object[]{filter, message}); } if( connections.isEmpty() ) return; ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); FilterAdapter adapter = filter == null ? null : new FilterAdapter(filter); if( message.isReliable() || fastAdapter == null ) { // Don't need to copy the data because message protocol is already // giving us a fresh buffer reliableAdapter.broadcast( adapter, buffer, true, false ); } else { fastAdapter.broadcast( adapter, buffer, false, false ); } }
@Override public void send( int channel, Message message ) { if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "send({0}, {1})", new Object[]{channel, message}); } checkChannel(channel); ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); channels[channel+CH_FIRST].send(buffer); }
@Override public void broadcast( int channel, Filter<? super HostedConnection> filter, Message message ) { if( log.isLoggable(Level.FINER) ) { log.log(Level.FINER, "broadcast({0}, {1}. {2})", new Object[]{channel, filter, message}); } if( connections.isEmpty() ) return; checkChannel(channel); ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null); FilterAdapter adapter = filter == null ? null : new FilterAdapter(filter); channels.get(channel+CH_FIRST).broadcast( adapter, buffer, true, false ); }
protected void send( int channel, Message message, boolean waitForConnected ) { checkRunning(); if( waitForConnected ) { // Make sure we aren't still connecting waitForConnected(); } ByteBuffer buffer = dataBuffer.get(); if( buffer == null ) { buffer = ByteBuffer.allocate( 65536 + 2 ); dataBuffer.set(buffer); } buffer.clear(); // Convert the message to bytes 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); channels.get(channel).write(buffer); }