List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:com.turn.ttorrent.client.io.PeerHandshakeMessage.java
License:Apache License
@Override public void fromWire(ByteBuf in) { int pstrlen = in.readUnsignedByte(); if (pstrlen < 0 || in.readableBytes() < BASE_HANDSHAKE_LENGTH + pstrlen - 1) throw new IllegalArgumentException("Incorrect handshake message length (pstrlen=" + pstrlen + ") !"); // Check the protocol identification string protocolName = new byte[pstrlen]; in.readBytes(protocolName);/*from ww w .ja v a 2 s. c o m*/ if (!Arrays.equals(protocolName, BITTORRENT_PROTOCOL_IDENTIFIER)) throw new IllegalArgumentException("Unknown protocol " + new String(protocolName, Charsets.ISO_8859_1)); // Ignore reserved bytes in.readBytes(reserved); infoHash = new byte[20]; in.readBytes(infoHash); peerId = new byte[20]; in.readBytes(peerId); }
From source file:com.twitter.http2.HttpFrameDecoder.java
License:Apache License
/** * Decode the byte buffer.//ww w . j a v a2 s . c om */ public void decode(ByteBuf buffer) { boolean endStream; boolean endSegment; int minLength; int dependency; int weight; boolean exclusive; int errorCode; while (true) { switch (state) { case READ_CONNECTION_HEADER: while (buffer.isReadable()) { byte b = buffer.readByte(); if (b != CLIENT_CONNECTION_PREFACE[length++]) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Connection Header"); return; } if (length == CLIENT_CONNECTION_PREFACE.length) { state = State.READ_FRAME_HEADER; break; } } if (buffer.isReadable()) { break; } else { return; } case READ_FRAME_HEADER: // Wait until entire header is readable if (buffer.readableBytes() < HTTP_FRAME_HEADER_SIZE) { return; } // Read frame header fields readFrameHeader(buffer); // TODO(jpinner) Sec 4.2 FRAME_SIZE_ERROR if (!isValidFrameHeader(length, type, flags, streamId)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Header"); } else if (frameHasPadding(type, flags)) { state = State.READ_PADDING_LENGTH; } else { paddingLength = 0; state = getNextState(length, type); } break; case READ_PADDING_LENGTH: if (buffer.readableBytes() < 1) { return; } paddingLength = buffer.readUnsignedByte(); --length; if (!isValidPaddingLength(length, type, flags, paddingLength)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Padding Length"); } else { state = getNextState(length, type); } break; case READ_DATA_FRAME: endStream = hasFlag(flags, HTTP_FLAG_END_STREAM); state = State.READ_DATA; if (hasFlag(flags, HTTP_FLAG_PADDED)) { delegate.readDataFramePadding(streamId, endStream, paddingLength + 1); } break; case READ_DATA: // Generate data frames that do not exceed maxChunkSize // maxChunkSize must be > 0 so we cannot infinitely loop int dataLength = Math.min(maxChunkSize, length - paddingLength); // Wait until entire frame is readable if (buffer.readableBytes() < dataLength) { return; } ByteBuf data = buffer.readBytes(dataLength); length -= dataLength; if (length == paddingLength) { if (paddingLength == 0) { state = State.READ_FRAME_HEADER; } else { state = State.SKIP_FRAME_PADDING; } } endStream = length == paddingLength && hasFlag(flags, HTTP_FLAG_END_STREAM); endSegment = length == paddingLength && hasFlag(flags, HTTP_FLAG_END_SEGMENT); delegate.readDataFrame(streamId, endStream, endSegment, data); break; case READ_HEADERS_FRAME: minLength = 0; if (hasFlag(flags, HTTP_FLAG_PRIORITY)) { minLength = 5; } if (buffer.readableBytes() < minLength) { return; } endStream = hasFlag(flags, HTTP_FLAG_END_STREAM); endSegment = hasFlag(flags, HTTP_FLAG_END_SEGMENT); exclusive = false; dependency = 0; weight = 16; if (hasFlag(flags, HTTP_FLAG_PRIORITY)) { dependency = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); weight = buffer.readUnsignedByte() + 1; if (dependency < 0) { dependency = dependency & 0x7FFFFFFF; exclusive = true; } length -= 5; } state = State.READ_HEADER_BLOCK; delegate.readHeadersFrame(streamId, endStream, endSegment, exclusive, dependency, weight); break; case READ_PRIORITY_FRAME: if (buffer.readableBytes() < length) { return; } exclusive = false; dependency = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); weight = buffer.readUnsignedByte() + 1; if (dependency < 0) { dependency = dependency & 0x7FFFFFFF; exclusive = true; } state = State.READ_FRAME_HEADER; delegate.readPriorityFrame(streamId, exclusive, dependency, weight); break; case READ_RST_STREAM_FRAME: if (buffer.readableBytes() < length) { return; } errorCode = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(length); state = State.READ_FRAME_HEADER; delegate.readRstStreamFrame(streamId, errorCode); break; case READ_SETTINGS_FRAME: boolean ack = hasFlag(flags, HTTP_FLAG_ACK); state = State.READ_SETTING; delegate.readSettingsFrame(ack); break; case READ_SETTING: if (length == 0) { state = State.READ_FRAME_HEADER; delegate.readSettingsEnd(); break; } if (buffer.readableBytes() < 6) { return; } int id = getUnsignedShort(buffer, buffer.readerIndex()); int value = getSignedInt(buffer, buffer.readerIndex() + 2); buffer.skipBytes(6); length -= 6; delegate.readSetting(id, value); break; case READ_PUSH_PROMISE_FRAME: if (buffer.readableBytes() < 4) { return; } int promisedStreamId = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); length -= 4; if (promisedStreamId == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Promised-Stream-ID"); } else { state = State.READ_HEADER_BLOCK; delegate.readPushPromiseFrame(streamId, promisedStreamId); } break; case READ_PING_FRAME: if (buffer.readableBytes() < length) { return; } long ping = getSignedLong(buffer, buffer.readerIndex()); buffer.skipBytes(length); boolean pong = hasFlag(flags, HTTP_FLAG_ACK); state = State.READ_FRAME_HEADER; delegate.readPingFrame(ping, pong); break; case READ_GOAWAY_FRAME: if (buffer.readableBytes() < 8) { return; } int lastStreamId = getUnsignedInt(buffer, buffer.readerIndex()); errorCode = getSignedInt(buffer, buffer.readerIndex() + 4); buffer.skipBytes(8); length -= 8; if (length == 0) { state = State.READ_FRAME_HEADER; } else { paddingLength = length; state = State.SKIP_FRAME_PADDING; } delegate.readGoAwayFrame(lastStreamId, errorCode); break; case READ_WINDOW_UPDATE_FRAME: // Wait until entire frame is readable if (buffer.readableBytes() < length) { return; } int windowSizeIncrement = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(length); if (windowSizeIncrement == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Window Size Increment"); } else { state = State.READ_FRAME_HEADER; delegate.readWindowUpdateFrame(streamId, windowSizeIncrement); } break; case READ_CONTINUATION_FRAME_HEADER: // Wait until entire frame header is readable if (buffer.readableBytes() < HTTP_FRAME_HEADER_SIZE) { return; } // Read and validate continuation frame header fields int prevStreamId = streamId; readFrameHeader(buffer); // TODO(jpinner) Sec 4.2 FRAME_SIZE_ERROR // TODO(jpinner) invalid flags if (type != HTTP_CONTINUATION_FRAME || streamId != prevStreamId) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Continuation Frame"); } else { paddingLength = 0; state = State.READ_HEADER_BLOCK; } break; case READ_HEADER_BLOCK: if (length == paddingLength) { boolean endHeaders = hasFlag(flags, HTTP_FLAG_END_HEADERS); if (endHeaders) { state = State.SKIP_FRAME_PADDING; delegate.readHeaderBlockEnd(); } else { state = State.SKIP_FRAME_PADDING_CONTINUATION; } break; } if (!buffer.isReadable()) { return; } int readableBytes = Math.min(buffer.readableBytes(), length - paddingLength); ByteBuf headerBlockFragment = buffer.readBytes(readableBytes); length -= readableBytes; delegate.readHeaderBlock(headerBlockFragment); break; case SKIP_FRAME_PADDING: int numBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numBytes); length -= numBytes; if (length == 0) { state = State.READ_FRAME_HEADER; break; } return; case SKIP_FRAME_PADDING_CONTINUATION: int numPaddingBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numPaddingBytes); length -= numPaddingBytes; if (length == 0) { state = State.READ_CONTINUATION_FRAME_HEADER; break; } return; case FRAME_ERROR: buffer.skipBytes(buffer.readableBytes()); return; default: throw new Error("Shouldn't reach here."); } } }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertHeadersFrame(ByteBuf frame, int streamId, boolean exclusive, int dependency, int weight, boolean last, ByteBuf headerBlock) { boolean hasPriority = exclusive || dependency != HTTP_DEFAULT_DEPENDENCY || weight != HTTP_DEFAULT_WEIGHT; int maxLength = hasPriority ? HTTP_MAX_LENGTH - 5 : HTTP_MAX_LENGTH; byte type = 0x01; byte flags = 0x00; if (last) {/* ww w .j av a2 s. c om*/ flags |= 0x01; } if (headerBlock.readableBytes() <= maxLength) { flags |= 0x04; } if (hasPriority) { flags |= 0x20; } int length = assertFrameHeader(frame, type, flags, streamId); if (hasPriority) { assertTrue(length >= 5); if (exclusive) { assertEquals(dependency | 0x80000000, frame.readInt()); } else { assertEquals(dependency, frame.readInt()); } assertEquals(weight - 1, frame.readUnsignedByte()); length -= 5; } assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } while (headerBlock.isReadable()) { type = 0x09; flags = 0x00; if (headerBlock.readableBytes() <= HTTP_MAX_LENGTH) { flags |= 0x04; } length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertPriorityFrame(ByteBuf frame, int streamId, boolean exclusive, int dependency, int weight) { byte type = 0x02; byte flags = 0x00; assertEquals(5, assertFrameHeader(frame, type, flags, streamId)); if (exclusive) { assertEquals(dependency | 0x80000000, frame.readInt()); } else {//w ww . ja va2s . c o m assertEquals(dependency, frame.readInt()); } assertEquals(weight - 1, frame.readUnsignedByte()); assertFalse(frame.isReadable()); }
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static String decodeSmallString(ByteBuf buffer) { int valueLength = buffer.readUnsignedByte(); byte[] valueBytes = new byte[valueLength]; buffer.readBytes(valueBytes);/*w w w . jav a 2 s .co m*/ return new String(valueBytes); }
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static Map<String, String> decodeSmallHeaders(ByteBuf buffer) { short numHeaders = buffer.readUnsignedByte(); Map<String, String> headers = new HashMap<String, String>(numHeaders); for (int i = 0; i < numHeaders; i++) { String key = CodecUtils.decodeSmallString(buffer); String value = CodecUtils.decodeSmallString(buffer); headers.put(key, value);/*from w w w . j a va2s . c o m*/ } return headers; }
From source file:com.vethrfolnir.game.network.login.LoginServerClientHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buff = (ByteBuf) msg; int opcode = buff.readUnsignedByte(); switch (opcode) { case 0xBB:/*from w ww . j a v a2 s.c o m*/ ServerKill.read(serverClient, buff); break; case 0xA1: int newId = buff.readInt(); RecievedAlternativeId.read(serverClient, buff, newId); break; default: log.warn("Unknown packet 0x" + PrintData.fillHex(opcode, 2) + ". Dump: " + PrintData.printData(buff.nioBuffer(0, buff.writerIndex()))); break; } buff.release(); }
From source file:com.vethrfolnir.game.network.mu.crypt.MuCryptUtils.java
License:Open Source License
public static final void readAsUByteArray(ByteBuf buff, short[] uByteArray) { for (int i = 0; i < uByteArray.length; i++) { uByteArray[i] = buff.readUnsignedByte(); }// www . j a va2 s .com }
From source file:com.vethrfolnir.game.network.mu.crypt.MuCryptUtils.java
License:Open Source License
public static final void readAsUByteArray(ByteBuf buff, short[] uByteArray, int len) { for (int i = 0; i < len; i++) { uByteArray[i] = buff.readUnsignedByte(); }//from ww w . j ava 2 s . co m }
From source file:com.vethrfolnir.game.network.mu.MuChannelHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buff = (msg instanceof ByteBuffer) ? ctx.alloc().buffer().writeBytes((ByteBuffer) msg) : (ByteBuf) msg;/*from www . j a v a 2 s. c om*/ buff.readerIndex(2); int opcode = buff.readUnsignedByte(); switch (opcode) { // double opcode case 0xf1: case 0xf3: case 0x0e: case 0x03: buff.readerIndex(buff.readerIndex() - 1); opcode = buff.readUnsignedShort(); // ex 0xF1_03 break; default: break; } if (opcode == 0xe00) { // Time packet? buff.clear(); buff.release(); return; } ReadPacket packet = clientpackets.get(opcode); if (packet != null) { MuClient client = ctx.channel().attr(MuClient.ClientKey).get(); //System.out.println("Got opcode: 0x"+PrintData.fillHex(opcode, 2)+ " packet = \n"+packet.getClass().getSimpleName()); packet.read(client, buff); } else { log.warn("Unknown packet[opcode = 0x" + PrintData.fillHex(opcode, 2) + "]. Dump: "); log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex()))); } //log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex()))); if (buff.refCnt() > 0) { //System.out.println("Handler Release when packet[opcode = 0x"+PrintData.fillHex(opcode, 2)+"]"); buff.release(); } }