List of usage examples for io.netty.buffer ByteBuf getInt
public abstract int getInt(int index);
From source file:IncommingPacketFSMHandler.java
License:Open Source License
@Override protected void messageReceived(ChannelHandlerContext channelHandlerContext, DatagramPacket packet) throws Exception { final InetAddress srcAddr = packet.sender().getAddress(); final ByteBuf buf = packet.content(); try {/* w w w .j av a2 s . co m*/ System.out.printf("Inside incomming packet handler %s wtpId %d \n", ODLCapwapConsts.msgTypetoString(buf.getInt(0)), buf.getInt(4)); CapwapEvent event = new CapwapEvent(CapwapEventType.DECODE); int wtpId = buf.getInt(4); Integer WTPID = new Integer(wtpId); event.setWtpID("wtpID" + WTPID.toString()); msgProcessor.tell(event, this.msgProcessor); } catch (Exception e) { e.printStackTrace(); } }
From source file:bftsmart.communication.client.netty.NettyTOMMessageDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> list) throws Exception { // Wait until the length prefix is available. if (buffer.readableBytes() < Integer.BYTES) { return;//from www .j ava 2 s . co m } int dataLength = buffer.getInt(buffer.readerIndex()); //Logger.println("Receiving message with "+dataLength+" bytes."); // Wait until the whole data is available. if (buffer.readableBytes() < dataLength + Integer.BYTES) { return; } // Skip the length field because we know it already. buffer.skipBytes(Integer.BYTES); int size = buffer.readInt(); byte[] data = new byte[size]; buffer.readBytes(data); byte[] signature = null; size = buffer.readInt(); if (size > 0) { signature = new byte[size]; buffer.readBytes(signature); } DataInputStream dis = null; TOMMessage sm = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(data); dis = new DataInputStream(bais); sm = new TOMMessage(); sm.rExternal(dis); sm.serializedMessage = data; if (signature != null) { sm.serializedMessageSignature = signature; sm.signed = true; } if (!isClient) { rl.readLock().lock(); if (!sessionTable.containsKey(sm.getSender())) { rl.readLock().unlock(); NettyClientServerSession cs = new NettyClientServerSession(context.channel(), sm.getSender()); rl.writeLock().lock(); sessionTable.put(sm.getSender(), cs); logger.debug("Active clients: " + sessionTable.size()); rl.writeLock().unlock(); } else { rl.readLock().unlock(); } } logger.debug("Decoded reply from " + sm.getSender() + " with sequence number " + sm.getSequence()); list.add(sm); } catch (Exception ex) { logger.error("Failed to decode TOMMessage", ex); } return; }
From source file:com.cloudhopper.smpp.transcoder.DefaultPduTranscoder.java
License:Apache License
@Override public Pdu decode(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException { // wait until the length prefix is available if (buffer.readableBytes() < SmppConstants.PDU_INT_LENGTH) { return null; }/* w ww. j a va 2 s. c o m*/ // parse the command length (first 4 bytes) int commandLength = buffer.getInt(buffer.readerIndex()); //logger.trace("PDU commandLength [" + commandLength + "]"); // valid command length is >= 16 bytes if (commandLength < SmppConstants.PDU_HEADER_LENGTH) { throw new UnrecoverablePduException( "Invalid PDU length [0x" + HexUtil.toHexString(commandLength) + "] parsed"); } // wait until the whole pdu is available (entire pdu) if (buffer.readableBytes() < commandLength) { return null; } // at this point, we have the entire PDU and length already in the buffer // we'll create a new "view" of this PDU and read the data from the actual buffer // NOTE: this should be super fast since the underlying byte array doesn't get copied ByteBuf buffer0 = buffer.readSlice(commandLength); return doDecode(commandLength, buffer0); }
From source file:com.couchbase.client.core.endpoint.binary.BinaryHandler.java
License:Open Source License
@Override protected CouchbaseResponse decodeResponse(final ChannelHandlerContext ctx, final FullBinaryMemcacheResponse msg) throws Exception { BinaryRequest request = currentRequest(); ResponseStatus status = convertStatus(msg.getStatus()); CouchbaseResponse response;//www . ja v a 2 s .c om ByteBuf content = msg.content().copy(); long cas = msg.getCAS(); String bucket = request.bucket(); if (request instanceof GetRequest || request instanceof ReplicaGetRequest) { int flags = 0; if (msg.getExtrasLength() > 0) { final ByteBuf extrasReleased = msg.getExtras(); final ByteBuf extras = ctx.alloc().buffer(msg.getExtrasLength()); extras.writeBytes(extrasReleased, extrasReleased.readerIndex(), extrasReleased.readableBytes()); flags = extras.getInt(0); extras.release(); } response = new GetResponse(status, cas, flags, bucket, content, request); } else if (request instanceof GetBucketConfigRequest) { response = new GetBucketConfigResponse(status, bucket, content, ((GetBucketConfigRequest) request).hostname()); } else if (request instanceof InsertRequest) { response = new InsertResponse(status, cas, bucket, content, request); } else if (request instanceof UpsertRequest) { response = new UpsertResponse(status, cas, bucket, content, request); } else if (request instanceof ReplaceRequest) { response = new ReplaceResponse(status, cas, bucket, content, request); } else if (request instanceof RemoveRequest) { response = new RemoveResponse(status, bucket, content, request); } else if (request instanceof CounterRequest) { response = new CounterResponse(status, bucket, msg.content().readLong(), cas, request); } else if (request instanceof UnlockRequest) { response = new UnlockResponse(status, bucket, content, request); } else if (request instanceof TouchRequest) { response = new TouchResponse(status, bucket, content, request); } else if (request instanceof ObserveRequest) { byte observed = content.getByte(content.getShort(2) + 4); response = new ObserveResponse(status, observed, ((ObserveRequest) request).master(), bucket, content, request); } else if (request instanceof AppendRequest) { response = new AppendResponse(status, cas, bucket, content, request); } else if (request instanceof PrependRequest) { response = new PrependResponse(status, cas, bucket, content, request); } else { throw new IllegalStateException( "Unhandled request/response pair: " + request.getClass() + "/" + msg.getClass()); } return response; }
From source file:com.dinstone.rpc.netty.RpcProtocolDecoder.java
License:Apache License
private byte[] readFrame(ByteBuf in) { int remaining = in.readableBytes(); if (remaining < 4) { return null; }/*from w w w. j a v a 2 s . com*/ int objectSize = in.getInt(0); if (objectSize > maxObjectSize) { throw new IllegalArgumentException( "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')'); } if (remaining - 4 >= objectSize) { objectSize = in.readInt(); // RPC object size byte[] rpcBytes = new byte[objectSize]; in.readBytes(rpcBytes); return rpcBytes; } return null; }
From source file:com.facebook.nifty.core.ThriftFrameDecoder.java
License:Apache License
private TTransport tryDecodeFramedMessage(ChannelHandlerContext ctx, ByteBuf buffer) { // Framed messages are prefixed by the size of the frame (which doesn't include the // framing itself). int messageStartReaderIndex = buffer.readerIndex(); // Read the i32 frame contents size int messageContentsLength = buffer.getInt(messageStartReaderIndex); // The full message is larger by the size of the frame size prefix int messageLength = messageContentsLength + MESSAGE_FRAME_SIZE; if (messageContentsLength > maxFrameSize) { ctx.fireExceptionCaught(/* w w w .j ava 2s. c o m*/ new TooLongFrameException("Maximum frame size of " + maxFrameSize + " exceeded")); } int messageContentsOffset = messageStartReaderIndex + MESSAGE_FRAME_SIZE; if (messageLength == 0) { // Zero-sized frame: just ignore it and return nothing buffer.readerIndex(messageContentsOffset); return null; } else if (buffer.readableBytes() < messageLength) { // Full message isn't available yet, return nothing for now return null; } else { // Full message is available, return it ByteBuf messageBuffer = extractFrame(buffer, messageContentsOffset, messageContentsLength); ThriftMessage message = new ThriftMessage(messageBuffer, ThriftTransportType.FRAMED); buffer.readerIndex(messageStartReaderIndex + messageLength); return new TNiftyTransport(ctx.channel(), message); } }
From source file:com.github.mrstampy.pprspray.core.streamer.footer.MediaFooterChunk.java
License:Open Source License
private int extractInt(byte[] message, int start, int end) { byte[] hash = Arrays.copyOfRange(message, start, end); ByteBuf buf = Unpooled.copiedBuffer(hash); return buf.getInt(0); }
From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java
License:Open Source License
/** * Checks if is media footer./*from w w w . j a v a 2 s .c o m*/ * * @param message * the message * @param type * the type * @param mediaHash * the media hash * @return true, if checks if is media footer */ public static boolean isMediaFooter(byte[] message, MediaStreamType type, int mediaHash) { if (message == null || message.length != FOOTER_LENGTH) return false; ByteBuf buf = Unpooled.buffer(8); buf.writeBytes(message); return Integer.MAX_VALUE - type.ordinal() == buf.getInt(0) && mediaHash == buf.getInt(4); }
From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java
License:Open Source License
/** * Gets the integer chunk.//from w ww. ja v a2s. c om * * @param chunk * the chunk * @return the integer chunk */ protected static int getIntegerChunk(byte[] chunk) { ByteBuf buf = Unpooled.copiedBuffer(chunk); return buf.getInt(0); }
From source file:com.hazelcast.simulator.protocol.core.ResponseCodec.java
License:Open Source License
public static boolean isResponse(ByteBuf in) { return (in.getInt(OFFSET_MAGIC_BYTES) == MAGIC_BYTES); }