Java Code Examples for org.apache.mina.core.buffer.IoBuffer#skip()

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#skip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RTMPProtocolDecoder.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
public ISharedObjectMessage decodeFlexSharedObject(IoBuffer in) {
    byte encoding = in.get();
    Input input;
    if (encoding == 0) {
        input = new org.red5.io.amf.Input(in);
    } else if (encoding == 3) {
        input = new org.red5.io.amf3.Input(in);
    } else {
        throw new RuntimeException("Unknown SO encoding: " + encoding);
    }
    String name = input.getString();
    // Read version of SO to modify
    int version = in.getInt();
    // Read persistence informations
    boolean persistent = in.getInt() == 2;
    // Skip unknown bytes
    in.skip(4);
    // create our shared object message
    final SharedObjectMessage so = new FlexSharedObjectMessage(null, name, version, persistent);
    doDecodeSharedObject(so, in, input);
    return so;
}
 
Example 2
Source File: ProtocolDecoderImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private int messageLength ( final IoBuffer data )
{
    final int len = data.getUnsignedShort ( data.position () + 3 );
    if ( data.remaining () < 5 + len )
    {
        return -1;
    }

    data.skip ( 5 );

    return len;
}
 
Example 3
Source File: TestReloadClassLoader.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(IoSession session, Object message)
			throws Exception{
	
		IoBuffer buf = (IoBuffer)message;
    buf.skip(4);
    int index = buf.getInt();
    System.out.println("index = " + index);
    session.close(false);
}
 
Example 4
Source File: RTMPProtocolDecoder.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public ISharedObjectMessage decodeSharedObject(IoBuffer in) {
    final Input input = new org.red5.io.amf.Input(in);
    String name = input.getString();
    // Read version of SO to modify
    int version = in.getInt();
    // Read persistence informations
    boolean persistent = in.getInt() == 2;
    // Skip unknown bytes
    in.skip(4);
    // create our shared object message
    final SharedObjectMessage so = new SharedObjectMessage(null, name, version, persistent);
    doDecodeSharedObject(so, in, input);
    return so;
}
 
Example 5
Source File: ProtocolDecoderImpl.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean doDecode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws Exception
{
    byte marker1;
    byte marker2;

    do
    {
        if ( data.remaining () < 2 ) // we may only eat the start when there could be a packet after it
        {
            return false;
        }

        // peek marker
        marker1 = data.get ( data.position () + 0 );
        marker2 = data.get ( data.position () + 1 );

        // TODO: re-think the idea of just skipping

        if ( marker1 != 0x12 || marker2 != 0x02 )
        {
            data.skip ( 2 ); // eat garbage
        }
    } while ( marker1 != 0x12 || marker2 != 0x02 );

    if ( data.remaining () < 3 )
    {
        return false;
    }

    data.order ( Sessions.isLittleEndian ( session ) ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN );

    final byte command = data.get ( data.position () + 2 );
    switch ( command )
    {
        case Messages.MC_HELLO:
            return processHello ( session, data, out );
        case Messages.MC_WELCOME:
            return processWelcome ( session, data, out );
        case Messages.MC_READ_ALL:
            out.write ( new ReadAll () );
            return true;
        case Messages.MC_DATA_UPDATE:
            return processDataUpdate ( session, data, out );
        case Messages.MC_START_BROWSE:
            out.write ( new SubscribeBrowse () );
            return true;
        case Messages.MC_STOP_BROWSE:
            out.write ( new UnsubscribeBrowse () );
            return true;
        case Messages.MC_NS_ADDED:
            return processBrowseAdded ( session, data, out );
        case Messages.MC_WRITE_COMMAND:
            return processWriteCommand ( session, data, out );
        case Messages.MC_WRITE_RESULT:
            return processWriteResult ( session, data, out );
    }

    throw new ProtocolCodecException ( String.format ( "Message code %02x is unkown", command ) );
}
 
Example 6
Source File: SessionMessageDecoder.java    From gameserver with Apache License 2.0 4 votes vote down vote up
/**
 * Decode the message.
 */
@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
	SessionMessage sessionMessage = (SessionMessage) session.getAttribute(DECODER_STATE_KEY);
	if ( sessionMessage == null ) {
		sessionMessage = new SessionMessage();
		session.setAttribute(DECODER_STATE_KEY, sessionMessage);
	}
	
	// Make sure the sessionkey in the header bytes are ready.
	if ( !in.prefixedDataAvailable(HEADER_LENGTH, MAX_LENGTH) ) {
    return false;
	}
	int sessionKeyLength = in.getShort();
	byte[] sessionRawBytes = new byte[sessionKeyLength];
	in.get(sessionRawBytes);
	SessionKey sessionKey = SessionKey.createSessionKey(sessionRawBytes);
	sessionMessage.setSessionkey(sessionKey);
	
	// Make sure the xinqimessage in the header bytes are ready.
	if ( !in.prefixedDataAvailable(HEADER_LENGTH, MAX_LENGTH) ) {
    return false;
	}
	
	int xinqiMessageLength = in.getShort()-6;
	XinqiMessage message = new XinqiMessage();
	
	message.type = in.getShort();
	message.index = in.getInt();
	if ( log.isDebugEnabled() ) {
		log.debug("XinqiMessage length:"+xinqiMessageLength+", type:"+message.type+", index:"+message.index);
	}
	
	MessageLite request = IdToMessage.idToMessage(message.type);
	if ( request == null ) {
		if ( log.isWarnEnabled() ) {
			log.warn("No id found for message type. return null. ");
		}
		return false;
	}
	
	request = request.newBuilderForType().mergeFrom(in.slice().array(), sessionKeyLength + 10 , xinqiMessageLength).build();

	in.skip(xinqiMessageLength);
	if ( log.isDebugEnabled() ) {
		log.debug("Message:"+request.getClass().getName()+"["+request+"]");
	}
	message.payload = request;
	if ( log.isDebugEnabled() ) {
		log.debug("Response["+message.toString()+"]");
	}
	
	sessionMessage.setMessage(message);
	
	out.write(sessionMessage);
	return true;
}