List of usage examples for io.netty.buffer ByteBuf readShort
public abstract short readShort();
From source file:ru.jts.authserver.network.handler.packet.Client2AuthPacketHandler.java
License:Apache License
@Override public ClientPacket<Client> handlePacket(ByteBuf buf) { byte b1 = buf.readByte(); // 1 short unk1 = buf.readShort(); // 0 short size = buf.readShort(); // data size short sessionId = buf.readShort(); // sessionKey byte b2 = buf.readByte(); // 1 int unk2 = buf.readInt(); // 0 byte b3 = buf.readByte(); // 8 byte[] packetData = new byte[buf.readableBytes() - 4]; short unk3 = buf.readShort(); // 00 02 buf.readBytes(packetData);//from www . j a v a2s. c o m short unk4 = buf.readShort(); // 02 00 buf.clear(); ByteBuf packetBuf = Unpooled.copiedBuffer(packetData).order(ByteOrder.LITTLE_ENDIAN); packetBuf = decryptBuffer(packetBuf); int opcode = packetBuf.readUnsignedByte(); ClientPacket<Client> packet = null; switch (opcode) { case 0x01: packet = new CM_AUTH_BY_PASS(sessionId); break; //case 0x02: // packet = new AuthorizeBySession(); // break; default: log.warn("Unknown opcode {}", Integer.toHexString(opcode)); log.warn(ArrayUtils .bytesToHexString(packetBuf.copy(packetBuf.readerIndex(), packetBuf.readableBytes()).array())); log.warn(new String(packetBuf.copy(packetBuf.readerIndex(), packetBuf.readableBytes()).array())); break; } if (packet != null) { packet.setContent(packetBuf); } return packet; }
From source file:sailfish.remoting.codec.DefaultRemotingCodec.java
License:Apache License
@Override public Protocol decode(ByteBuf buffer) throws SailfishException { short magic = buffer.readShort(); if (RemotingConstants.SAILFISH_MAGIC != magic) { throw new SailfishException(ExceptionCode.BAD_PACKAGE, "bad packet, expected magic:" + RemotingConstants.SAILFISH_MAGIC + ", but actual:" + magic + ", current channel will be closed!"); }//from www . j a va2 s .c o m int totalLength = buffer.readInt(); byte compactByte = buffer.getByte(buffer.readerIndex()); boolean request = ((compactByte & RequestProtocol.REQUEST_FLAG) != 0); Protocol protocol; //recycle Protocol if (request) {//request protocol = RequestProtocol.newInstance(); } else {//response protocol = ResponseProtocol.newInstance(); } protocol.deserialize(buffer, totalLength); return protocol; }
From source file:sailfish.remoting.protocol.RequestProtocol.java
License:Apache License
@Override public void deserialize(ByteBuf input, int totalLength) throws SailfishException { try {//w w w. j a v a2 s . c o m byte compactByte = input.readByte(); this.oneway = ((compactByte & ONEWAY_FLAG) != 0); this.heartbeat = ((compactByte & HEARTBEAT_FLAG) != 0); this.serializeType = (byte) (compactByte & 0x1F); this.packetId = input.readInt(); this.opcode = input.readShort(); byte tmp = input.readByte(); this.compressType = (byte) (tmp >> 4 & 0xF); this.langType = (byte) (tmp >> 0 & 0xF); // read body int bodyLength = totalLength - HEADER_LENGTH; if (bodyLength > 0) { this.body = new byte[bodyLength]; input.readBytes(this.body); } } catch (Throwable cause) { throw new SailfishException(cause); } }
From source file:sailfish.remoting.ProtocolTest.java
License:Apache License
@Test public void testRequestProtocol() throws SailfishException { RequestProtocol send = RequestProtocol.newInstance(); send.body(new byte[] { 1, 2, 3, 4 }); send.compressType(CompressType.NON_COMPRESS); send.heartbeat(false);//w w w.j a va 2 s. com send.langType(LangType.JAVA); send.oneway(false); send.opcode((short) 1); send.packetId(1); send.serializeType(SerializeType.NON_SERIALIZE); ByteBuf output = ByteBufAllocator.DEFAULT.buffer(128); send.serialize(output); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); RequestProtocol receive = RequestProtocol.newInstance(); Assert.assertTrue(send == receive); receive.deserialize(output, output.readInt()); Assert.assertArrayEquals(send.body(), receive.body()); Assert.assertTrue(receive.compressType() == CompressType.NON_COMPRESS); Assert.assertFalse(receive.heartbeat()); Assert.assertTrue(receive.langType() == LangType.JAVA); Assert.assertFalse(receive.oneway()); Assert.assertTrue(1 == receive.opcode()); Assert.assertTrue(1 == receive.packetId()); Assert.assertTrue(receive.serializeType() == SerializeType.NON_SERIALIZE); output.clear(); send.body(new byte[] { -1, -1, -1, -1 }); send.heartbeat(true); send.oneway(true); send.langType(LangType.CPP); send.serializeType(SerializeType.PROTOBUF_SERIALIZE); send.compressType(CompressType.LZ4_COMPRESS); send.opcode((short) 100); send.packetId(1000); send.serialize(output); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); receive = RequestProtocol.newInstance(); Assert.assertTrue(send == receive); receive.deserialize(output, output.readInt()); Assert.assertArrayEquals(send.body(), receive.body()); Assert.assertTrue(receive.compressType() == CompressType.LZ4_COMPRESS); Assert.assertTrue(receive.heartbeat()); Assert.assertTrue(receive.langType() == LangType.CPP); Assert.assertTrue(receive.oneway()); Assert.assertTrue(100 == receive.opcode()); Assert.assertTrue(1000 == receive.packetId()); Assert.assertTrue(receive.serializeType() == SerializeType.PROTOBUF_SERIALIZE); }
From source file:sailfish.remoting.ProtocolTest.java
License:Apache License
@Test public void testResponseProtocol() throws SailfishException { ResponseProtocol send = ResponseProtocol.newInstance(); send.body(new byte[] { 1, 2, 3, 4 }); send.compressType(CompressType.GZIP_COMPRESS); send.heartbeat(false);//from w w w . ja va 2s. c o m send.packetId(1); send.result((byte) 0); send.serializeType(SerializeType.JDK_SERIALIZE); ByteBuf output = ByteBufAllocator.DEFAULT.buffer(128); send.serialize(output); ResponseProtocol receive = ResponseProtocol.newInstance(); Assert.assertTrue(send == receive); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); receive.deserialize(output, output.readInt()); Assert.assertArrayEquals(send.body(), receive.body()); Assert.assertTrue(send.compressType() == CompressType.GZIP_COMPRESS); Assert.assertTrue(send.serializeType() == SerializeType.JDK_SERIALIZE); Assert.assertFalse(receive.heartbeat()); Assert.assertTrue(1 == receive.packetId()); Assert.assertTrue(0 == receive.result()); output.clear(); send.heartbeat(true); send.serialize(output); Assert.assertTrue(output.readShort() == RemotingConstants.SAILFISH_MAGIC); receive = ResponseProtocol.newInstance(); Assert.assertTrue(send == receive); receive.deserialize(output, output.readInt()); Assert.assertTrue(receive.heartbeat()); }
From source file:sas.systems.imflux.packet.DataPacket.java
License:Apache License
/** * Decodes a {@code DataPacket}./*www . j a v a2 s.c o m*/ * * @param buffer as a ByteBuf * @return the DataPacket object * @throws IndexOutOfBoundsException */ public static DataPacket decode(ByteBuf buffer) throws IndexOutOfBoundsException { if (buffer.readableBytes() < 12) { throw new IllegalArgumentException("A RTP packet must be at least 12 octets long"); } // Version, Padding, eXtension, CSRC Count DataPacket packet = new DataPacket(); byte b = buffer.readByte(); packet.version = RtpVersion.fromByte(b); boolean padding = (b & 0x20) > 0; // mask 0010 0000 boolean extension = (b & 0x10) > 0; // mask 0001 0000 int contributingSourcesCount = b & 0x0f; // mask 0000 1111 // Marker, Payload Type b = buffer.readByte(); packet.marker = (b & 0x80) > 0; // mask 1000 0000 packet.payloadType = (b & 0x7f); // mask 0111 1111 packet.sequenceNumber = buffer.readUnsignedShort(); packet.timestamp = buffer.readUnsignedInt(); packet.ssrc = buffer.readUnsignedInt(); // Read extension headers & data if (extension) { packet.extensionHeaderData = buffer.readShort(); packet.extensionData = new byte[buffer.readUnsignedShort() * 4]; buffer.readBytes(packet.extensionData); } // Read CCRC's if (contributingSourcesCount > 0) { packet.contributingSourceIds = new ArrayList<Long>(contributingSourcesCount); for (int i = 0; i < contributingSourcesCount; i++) { long contributingSource = buffer.readUnsignedInt(); packet.contributingSourceIds.add(contributingSource); } } if (!padding) { // No padding used, assume remaining data is the packet byte[] remainingBytes = new byte[buffer.readableBytes()]; buffer.readBytes(remainingBytes); packet.setData(remainingBytes); } else { // Padding bit was set, so last byte contains the number of padding octets that should be discarded. short lastByte = buffer.getUnsignedByte(buffer.readerIndex() + buffer.readableBytes() - 1); byte[] dataBytes = new byte[buffer.readableBytes() - lastByte]; buffer.readBytes(dataBytes); packet.setData(dataBytes); // Discard rest of buffer. buffer.skipBytes(buffer.readableBytes()); } return packet; }
From source file:sas.systems.imflux.packet.rtcp.ControlPacket.java
License:Apache License
/** * Decodes a control packet from a {@code ChannelBuffer}. The first fields of the RTCP-header is the same for each * control packet. This is done in this method, after that it is delegated to the specified control packet. * * @param buffer bytes to be decoded/*from w w w .j a v a 2s.c om*/ * @return a new {@code ControlPacket} containing all information from the {@code buffer} * @throws IllegalArgumentException */ public static ControlPacket decode(ByteBuf buffer) throws IllegalArgumentException { // check buffer size if ((buffer.readableBytes() % 4) > 0) { throw new IllegalArgumentException( "Invalid RTCP packet length: expecting multiple of 4 and got " + buffer.readableBytes()); } // extract version, padding, innerBlocks and control packet type byte b = buffer.readByte(); RtpVersion version = RtpVersion.fromByte(b); if (!version.equals(RtpVersion.V2)) { return null; } boolean hasPadding = (b & 0x20) > 0; // mask: 0010 0000 byte innerBlocks = (byte) (b & 0x1f); // mask: 0001 1111 ControlPacket.Type type = ControlPacket.Type.fromByte(buffer.readByte()); // This length is in 32bit (4byte) words. These first 4 bytes already read, don't count. int length = buffer.readShort(); if (length == 0) { return null; } // No need to pass version downwards, only V2 is supported so subclasses can safely assume V2. // I know it's ugly when the superclass knows about the subclasses but since this method is static (and NEEDS // to be) the alternative was having this method in a external class. Pointless. switch (type) { case SENDER_REPORT: return SenderReportPacket.decode(buffer, innerBlocks, length); case RECEIVER_REPORT: return ReceiverReportPacket.decode(buffer, innerBlocks, length); case SOURCE_DESCRIPTION: return SourceDescriptionPacket.decode(buffer, hasPadding, innerBlocks, length); case BYE: return ByePacket.decode(buffer, innerBlocks, length); case APP_DATA: return null; default: throw new IllegalArgumentException("Unknown RTCP packet type: " + type); } }
From source file:tachyon.network.protocol.RPCBlockRequest.java
License:Apache License
/** * Decode the input {@link ByteBuf} into a {@link RPCBlockRequest} object and return it. * * @param in the input {@link ByteBuf}/* w w w . ja v a 2s . co m*/ * @return The decoded RPCBlockRequest object */ public static RPCBlockRequest decode(ByteBuf in) { // TODO: remove this short when client also uses netty. in.readShort(); long blockId = in.readLong(); long offset = in.readLong(); long length = in.readLong(); return new RPCBlockRequest(blockId, offset, length); }
From source file:tachyon.network.protocol.RPCBlockResponse.java
License:Apache License
/** * Decode the input {@link ByteBuf} into a {@link RPCBlockResponse} object and return it. * * @param in the input {@link ByteBuf}/* w w w . j a v a2 s . c o m*/ * @return The decoded RPCBlockResponse object */ public static RPCBlockResponse decode(ByteBuf in) { // TODO: remove this short when client also uses netty. in.readShort(); long blockId = in.readLong(); long offset = in.readLong(); long length = in.readLong(); DataBuffer data = null; if (length > 0) { // TODO: look into accessing Netty ByteBuf directly, to avoid copying the data. ByteBuffer buffer = ByteBuffer.allocate((int) length); in.readBytes(buffer); data = new DataByteBuffer(buffer, (int) length); } return new RPCBlockResponse(blockId, offset, length, data); }
From source file:tachyon.worker.netty.protocol.RPCBlockResponse.java
License:Apache License
/** * Decode the input {@link ByteBuf} into a {@link RPCBlockResponse} object and return it. * * @param in the input {@link ByteBuf}//from w ww. ja va 2s. com * @return The decoded RPCBlockResponse object */ public static RPCBlockResponse decode(ByteBuf in) { // TODO: remove this short when client also uses netty. in.readShort(); long blockId = in.readLong(); long offset = in.readLong(); long length = in.readLong(); DataBuffer data = null; if (length > 0) { ByteBuffer buffer = ByteBuffer.allocate((int) length); in.readBytes(buffer); data = new DataByteBuffer(buffer, (int) length); } return new RPCBlockResponse(blockId, offset, length, data); }