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

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#getUnsigned() . 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: IntegerDecodingState.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    while (in.hasRemaining()) {
        switch (counter) {
        case 0:
            firstByte = in.getUnsigned();
            break;
        case 1:
            secondByte = in.getUnsigned();
            break;
        case 2:
            thirdByte = in.getUnsigned();
            break;
        case 3:
            counter = 0;
            return finishDecode((firstByte << 24) | (secondByte << 16) | (thirdByte << 8) | in.getUnsigned(), out);
        default:
            throw new InternalError();
        }
        counter++;
    }

    return this;
}
 
Example 2
Source File: ShortIntegerDecodingState.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {

    while (in.hasRemaining()) {
        switch (counter) {
        case 0:
            highByte = in.getUnsigned();
            break;
        case 1:
            counter = 0;
            return finishDecode((short) ((highByte << 8) | in.getUnsigned()), out);
        default:
            throw new InternalError();
        }

        counter++;
    }
    return this;
}
 
Example 3
Source File: DaveFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private DaveMessage decodeDataReadReply ( final IoBuffer parameters, final IoBuffer data )
{
    parameters.get (); // command code

    final short count = parameters.getUnsigned ();

    final Collection<Result> result = new LinkedList<Result> ();

    for ( int i = 0; i < count; i++ )
    {
        final short q = data.getUnsigned ();
        if ( q == 0xFF && data.remaining () > 4 )
        {
            final byte type = data.get ();
            int len = data.getUnsignedShort ();
            if ( type == 4 )
            {
                len >>= 3;
            }

            final IoBuffer valueData = IoBuffer.allocate ( len );
            data.get ( valueData.array () );
            result.add ( new Result ( valueData ) );

            if ( len % 2 != 0 && data.remaining () > 0 )
            {
                data.get ();
            }
        }
        else if ( q != 0xFF )
        {
            result.add ( new Result ( q ) );
        }
    }

    return new DaveReadResult ( result );
}
 
Example 4
Source File: ModbusProtocol.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Read a set of bytes with the first byte being the number of bytes to read
 *
 * @param buffer
 *            the buffer to read from
 * @return the result read
 */
private static byte[] readBytesArrayWithPrefix ( final IoBuffer buffer )
{
    final short numOfBytes = buffer.getUnsigned ();

    logger.trace ( "Prepare to read {} bytes", numOfBytes );

    final byte[] result = new byte[numOfBytes];
    buffer.get ( result, 0, numOfBytes );
    return result;
}
 
Example 5
Source File: COTPFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void messageReceived ( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception
{
    logger.debug ( "Message received: {} - {}", new Object[] { session, message } );
    if ( ! ( message instanceof IoBuffer ) )
    {
        return;
    }
    final IoBuffer buffer = (IoBuffer)message;

    final short command = (short) ( buffer.getUnsigned ( 1 ) >> 4 );

    logger.debug ( "Command: {}", command );

    if ( command == COMMAND_CONNECT_CONFIRM )
    {
        handleConnectConfirm ( nextFilter, session, buffer );
    }
    else if ( command == COMMAND_DATA )
    {
        handleData ( nextFilter, session, buffer );
    }
    else
    {
        logger.warn ( "Unknown command: {}", command );
    }
}
 
Example 6
Source File: COTPFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void handleData ( final NextFilter nextFilter, final IoSession session, final IoBuffer buffer )
{
    final short len = buffer.getUnsigned ();
    final short command = buffer.getUnsigned ();
    final short nr = buffer.getUnsigned ();

    // FIXME: append data TPDUs if there is more than one 

    nextFilter.messageReceived ( session, new DataTPDU ( buffer ) );
}
 
Example 7
Source File: UInt8Accessor.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Short get ( final IoBuffer data, final int index )
{
    return data.getUnsigned ( index );
}
 
Example 8
Source File: SumkProtocolDecoder.java    From sumk with Apache License 2.0 4 votes vote down vote up
protected boolean innerDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
		throws CharacterCodingException, ProtocolDecoderException {
	int protocol = in.getInt();
	if ((protocol & 0xFF000000) != Protocols.MAGIC) {
		if (Logs.rpc().isTraceEnabled()) {
			Logs.rpc().trace(in.getString(Profile.UTF8.newDecoder()));
		}
		throw new ProtocolDecoderException("error magic," + Integer.toHexString(protocol));
	}
	int prefixLength = 0, maxDataLength = 0;
	if ((protocol & Protocols.ONE) != 0) {
		prefixLength = 1;
		maxDataLength = 0xFF;
	} else if ((protocol & Protocols.TWO) != 0) {
		prefixLength = 2;
		maxDataLength = 0xFFFF;
	} else if ((protocol & Protocols.FOUR) != 0) {
		prefixLength = 4;
		maxDataLength = Protocols.MAX_LENGTH;
	} else {
		if (AppInfo.getBoolean("sumk.rpc.log.code.error", true)) {
			Logs.rpc().error("error byte length protocol," + Integer.toHexString(protocol));
		}
		throw new ProtocolDecoderException("error byte length protocol," + Integer.toHexString(protocol));
	}

	if (in.remaining() < prefixLength) {
		return false;
	}
	int dataSize = 0;
	switch (prefixLength) {
	case 1:
		dataSize = in.getUnsigned();
		break;
	case 2:
		dataSize = in.getUnsignedShort();
		break;
	case 4:
		dataSize = in.getInt();
		break;
	}
	if (dataSize < 0 || dataSize > maxDataLength) {
		throw new BufferDataException("dataLength: " + dataSize);
	}
	if (in.remaining() < dataSize) {
		return false;
	}

	byte[] bs = new byte[dataSize];
	in.get(bs);
	out.write(new ProtocolObject(protocol, bs));
	return true;
}