本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.getUnsignedShort方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.getUnsignedShort方法的具體用法?Java IoBuffer.getUnsignedShort怎麽用?Java IoBuffer.getUnsignedShort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.getUnsignedShort方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: processWriteCommand
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private boolean processWriteCommand ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
final int len = messageLength ( data );
if ( len < 0 )
{
return false;
}
final int registerNumber = data.getUnsignedShort ();
final int operationId = data.getInt ();
final Variant value = decodeVariant ( session, data );
out.write ( new WriteCommand ( registerNumber, value, operationId ) );
return true;
}
示例2: processWriteResult
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private boolean processWriteResult ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
final int len = messageLength ( data );
if ( len < 0 )
{
return false;
}
try
{
final int operationId = data.getInt ();
final int errorCode = data.getUnsignedShort ();
final String errorMessage = decodeString ( session, data );
out.write ( new WriteResult ( operationId, errorCode, errorMessage ) );
}
catch ( final CharacterCodingException e )
{
throw new ProtocolCodecException ( e );
}
return true;
}
示例3: processDataUpdate
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private boolean processDataUpdate ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
final int len = messageLength ( data );
if ( len < 0 )
{
return false;
}
final int count = data.getUnsignedShort ();
final List<DataUpdate.Entry> entries = new ArrayList<DataUpdate.Entry> ( count );
for ( int i = 0; i < count; i++ )
{
entries.add ( decodeDataUpdateEntry ( data, session ) );
}
out.write ( new DataUpdate ( entries ) );
return true;
}
示例4: processBrowseAdded
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private boolean processBrowseAdded ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
final int len = messageLength ( data );
if ( len < 0 )
{
return false;
}
final int count = data.getUnsignedShort ();
final List<BrowseAdded.Entry> entries = new ArrayList<BrowseAdded.Entry> ( count );
for ( int i = 0; i < count; i++ )
{
entries.add ( decodeBrowserAddEntry ( data, session ) );
}
out.write ( new BrowseAdded ( entries ) );
return true;
}
示例5: writeData
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void writeData ( final int blockAddress, byte[] data )
{
if ( this.request.getType () != RequestType.HOLDING )
{
throw new IllegalStateException ( String.format ( "Modbus can only write data when the block is of type %s", RequestType.HOLDING ) );
}
if ( data.length == 2 )
{
final IoBuffer buffer = IoBuffer.wrap ( data );
buffer.order ( this.dataOrder );
final int value = buffer.getUnsignedShort ();
this.slave.writeCommand ( new WriteSingleDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER, toWriteAddress ( blockAddress ), value ), this.request.getTimeout () );
}
else
{
data = ModbusProtocol.encodeData ( data, this.dataOrder ); // apply requested byte order
this.slave.writeCommand ( new WriteMultiDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS, toWriteAddress ( blockAddress ), data, data.length / 2 ), this.request.getTimeout () );
}
requestUpdate ();
}
示例6: decodeDataReadReply
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
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 );
}
示例7: processAnalogWrite
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void processAnalogWrite ( final int startAddress, final int numRegisters, final byte[] rawData )
{
final int[] regs = new int[numRegisters];
final IoBuffer data = IoBuffer.wrap ( rawData );
data.order ( this.order );
for ( int i = 0; i < numRegisters; i++ )
{
regs[i] = data.getUnsignedShort ();
}
handleAnalogWrite ( startAddress, regs );
}
示例8: decodeAsMaster
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Decode a PDU as a master
*
* @param message
* the message PDU
* @return the decoded messages
* @throws IllegalStateException
* if the function code is not supported
*/
public static Object decodeAsMaster ( final Pdu message )
{
final IoBuffer data = message.getData ();
final byte functionCode = data.get ();
logger.trace ( "Decoding functionCode = {}", functionCode );
if ( ( functionCode & Constants.FUNCTION_CODE_ERROR_FLAG ) != 0 )
{
final byte exceptionCode = data.get ();
final byte originalFunctionCode = (byte) ( functionCode & ~Constants.FUNCTION_CODE_ERROR_FLAG );
logger.trace ( "EC = {}, FC = {}", exceptionCode, functionCode );
return new ErrorResponse ( message.getTransactionId (), message.getUnitIdentifier (), originalFunctionCode, exceptionCode );
}
switch ( functionCode )
{
case Constants.FUNCTION_CODE_READ_COILS:
case Constants.FUNCTION_CODE_READ_DISCRETE_INPUTS:
case Constants.FUNCTION_CODE_READ_HOLDING_REGISTERS:
case Constants.FUNCTION_CODE_READ_INPUT_REGISTERS:
return new ReadResponse ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, readBytes ( data ) );
case Constants.FUNCTION_CODE_WRITE_SINGLE_COIL:
case Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER:
return new WriteSingleDataResponse ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), readBytes ( data, 2 ) );
case Constants.FUNCTION_CODE_WRITE_MULTIPLE_COILS:
case Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS:
return new WriteMultiDataResponse ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), data.getUnsignedShort () );
default:
throw new IllegalStateException ( String.format ( "Function code %02x is not supported", functionCode ) );
}
}
示例9: decodeAsSlave
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Decode a PDU as a slave
*
* @param message
* the message PDU
* @return the decoded messages
* @throws IllegalStateException
* if the function code is not supported
*/
public static Object decodeAsSlave ( final Pdu message ) throws ProtocolCodecException
{
final IoBuffer data = message.getData ();
final byte functionCode = data.get ();
switch ( functionCode )
{
case Constants.FUNCTION_CODE_READ_COILS:
case Constants.FUNCTION_CODE_READ_DISCRETE_INPUTS:
case Constants.FUNCTION_CODE_READ_HOLDING_REGISTERS:
case Constants.FUNCTION_CODE_READ_INPUT_REGISTERS:
return new ReadRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), data.getUnsignedShort () );
case Constants.FUNCTION_CODE_WRITE_SINGLE_COIL:
case Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER:
return new WriteSingleDataRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), readBytes ( data, 2 ) );
case Constants.FUNCTION_CODE_WRITE_MULTIPLE_COILS:
case Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS:
final int startAddress = data.getUnsignedShort ();
final int numRegisters = data.getUnsignedShort (); /* number of registers */
final byte num = data.get ();
if ( data.remaining () != num )
{
throw new ProtocolCodecException ( String.format ( "Wrong amount of data. Announced %s (bytes), found %s (bytes)", num, data.remaining () ) );
}
final byte[] b = new byte[data.remaining ()];
data.get ( b );
return new WriteMultiDataRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, startAddress, b, numRegisters );
default:
throw new IllegalStateException ( String.format ( "Function code %02x is not supported", functionCode ) );
}
}
示例10: decodeTimeoutFrame
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void decodeTimeoutFrame ( final IoSession session, final IoBuffer currentFrame, final ProtocolDecoderOutput out )
{
logger.trace ( "timeout () frame = {}", currentFrame.getHexDump () );
if ( currentFrame.limit () <= Constants.RTU_HEADER_SIZE )
{
throw new ModbusProtocolError ( "frame must be at least 4 bytes long (address + data[] + crc low + crc high" );
}
currentFrame.order ( ByteOrder.LITTLE_ENDIAN );
final int receivedCrc = currentFrame.getUnsignedShort ( currentFrame.limit () - 2 );
currentFrame.order ( ByteOrder.BIG_ENDIAN );
final int actualCrc = Checksum.crc16 ( currentFrame.array (), 0, currentFrame.limit () - 2 );
if ( receivedCrc != actualCrc )
{
final String hd = currentFrame.getHexDump ();
logger.info ( "CRC error - received: {}, calculated: {} - data: {}", receivedCrc, actualCrc, hd );
throw new ChecksumProtocolException ( String.format ( "CRC error. received: %04x, but actually was: %04x - Data: %s", receivedCrc, actualCrc, hd ) );
}
currentFrame.position ( 0 );
// get unit id
final byte unitIdentifier = currentFrame.get ();
final int len = currentFrame.limit () - ( 2 /*crc*/+ 1/*unit id*/);
final IoBuffer pdu = IoBuffer.allocate ( len );
for ( int i = 0; i < len; i++ )
{
pdu.put ( currentFrame.get () );
}
pdu.flip ();
// decode and send
logger.trace ( "Decoded PDU - data: {}", pdu.getHexDump () );
out.write ( new Pdu ( 0, unitIdentifier, pdu ) );
}
示例11: decodeDataUpdateEntry
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private DataUpdate.Entry decodeDataUpdateEntry ( final IoBuffer data, final IoSession session ) throws ProtocolCodecException
{
final int register = data.getUnsignedShort ();
final byte missedUpdates = data.get ();
final long timestamp = data.getLong ();
final Set<DataUpdate.State> states = data.getEnumSetShort ( DataUpdate.State.class );
final Variant value = decodeVariant ( session, data );
return new DataUpdate.Entry ( register, value, timestamp, states, missedUpdates );
}
示例12: decodeBrowserAddEntry
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private BrowseAdded.Entry decodeBrowserAddEntry ( final IoBuffer data, final IoSession session ) throws ProtocolCodecException
{
final short register = (short)data.getUnsignedShort ();
// FIXME: validate if short works
final byte b = data.get ();
final DataType dataType = DataType.fromByte ( b );
if ( dataType == null )
{
throw new ProtocolCodecException ( String.format ( "Data type %s is unkown", b ) );
}
final Set<BrowseAdded.Entry.Flags> flags = data.getEnumSet ( BrowseAdded.Entry.Flags.class );
final CharsetDecoder decoder = Sessions.getCharsetDecoder ( session );
try
{
final String name = data.getPrefixedString ( decoder );
final String description = data.getPrefixedString ( decoder );
final String unit = data.getPrefixedString ( decoder );
return new BrowseAdded.Entry ( register, name, description, unit, dataType, flags );
}
catch ( final CharacterCodingException e )
{
throw new ProtocolCodecException ( e );
}
}
示例13: messageLength
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
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;
}
示例14: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private DaveMessage decode ( final IoBuffer buffer )
{
final byte start = buffer.get ();
if ( start != PACKET_START_MAGIC )
{
throw new IllegalStateException ( String.format ( "packet header must be 0x32 but is %02x", start ) );
}
final byte type = buffer.get ();
logger.debug ( "Type: {}" );
buffer.get (); // unknown
buffer.get (); // unknown
buffer.getUnsignedShort (); // request id
final int plen = buffer.getUnsignedShort ();
final int dlen = buffer.getUnsignedShort ();
int errorInfo;
if ( type == 2 || type == 3 )
{
errorInfo = buffer.getUnsignedShort ();
logger.debug ( "Error info: {}", errorInfo );
}
else
{
errorInfo = 0;
}
final IoBuffer parameters = IoBuffer.allocate ( plen );
final IoBuffer data = IoBuffer.allocate ( dlen );
buffer.get ( parameters.array () );
buffer.get ( data.array () );
logger.debug ( "plen: {}, dlen: {}, remain: {}", new Object[] { plen, dlen, buffer.remaining () } );
logger.debug ( "Parameters: {}", parameters );
logger.debug ( "Data: {}", data );
if ( parameters.remaining () == 2 && parameters.get ( 0 ) == (byte)4 )
{
// Data read reply
return decodeDataReadReply ( parameters, data );
}
else if ( parameters.remaining () == 8 && parameters.get ( 0 ) == (byte)0xF0 )
{
// FIXME: better detection
final int maxPDU = parameters.getUnsignedShort ( 6 );
return new DaveConnectionEstablishedMessage ( maxPDU );
}
else
{
return new DaveGenericMessage ( parameters, data );
}
}
示例15: messageReceived
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void messageReceived ( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception
{
if ( ! ( message instanceof IoBuffer ) )
{
nextFilter.messageReceived ( session, message );
return;
}
final IoBuffer in = (IoBuffer)message;
final IoBuffer sessionBuffer = getSessionBuffer ( session );
// first add to the session buffer (may be optimized later)
sessionBuffer.position ( sessionBuffer.limit () );
sessionBuffer.put ( in );
sessionBuffer.flip ();
while ( sessionBuffer.remaining () >= 4 )
{
final int len = sessionBuffer.getUnsignedShort ( 2 );
if ( sessionBuffer.remaining () < len )
{
logger.debug ( "Next packet requires {} bytes", new Object[] { len } );
// not enough data for body
return;
}
// convert
final IoBuffer data = IoBuffer.allocate ( len - 4 );
sessionBuffer.get (); // version
sessionBuffer.get (); // reserved
sessionBuffer.getUnsignedShort (); // length
sessionBuffer.get ( data.array () );
nextFilter.messageReceived ( session, data );
logger.debug ( "{} bytes left in session buffer", sessionBuffer.remaining () );
}
if ( sessionBuffer.hasRemaining () )
{
sessionBuffer.compact ();
}
else
{
sessionBuffer.clear ().flip ();
}
}