List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:com.mpush.api.protocol.Packet.java
License:Apache License
public static Packet decodePacket(Packet packet, ByteBuf in, int bodyLength) { packet.cc = in.readShort();//read cc packet.flags = in.readByte();//read flags packet.sessionId = in.readInt();//read sessionId packet.lrc = in.readByte();//read lrc //read body/*from w w w.j a va2s . c om*/ if (bodyLength > 0) { in.readBytes(packet.body = new byte[bodyLength]); } return packet; }
From source file:com.mpush.common.message.ByteBufMessage.java
License:Apache License
public byte decodeByte(ByteBuf body) { return body.readByte(); }
From source file:com.mpush.netty.codec.PacketDecoder.java
License:Apache License
private void decodeHeartbeat(ByteBuf in, List<Object> out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET_BYTE) { out.add(Packet.HB_PACKET);/*from w w w .ja va 2s .c om*/ } else { in.readerIndex(in.readerIndex() - 1); break; } } }
From source file:com.mpush.netty.codec.PacketDecoder.java
License:Apache License
private Packet decodeFrame(ByteBuf in) { int readableBytes = in.readableBytes(); int bodyLength = in.readInt(); if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { return null; }/*from www . ja va 2 s .c o m*/ if (bodyLength > maxPacketSize) { throw new TooLongFrameException("packet body length over limit:" + bodyLength); } return decodePacket(new Packet(in.readByte()), in, bodyLength); }
From source file:com.mpush.netty.codec.PacketDecoder.java
License:Apache License
public static Packet decodeFrame(DatagramPacket frame) { ByteBuf in = frame.content(); int readableBytes = in.readableBytes(); int bodyLength = in.readInt(); if (readableBytes < (bodyLength + Packet.HEADER_LEN)) { return null; }//from www. j a va2 s . co m return decodePacket(new UDPPacket(in.readByte(), frame.sender()), in, bodyLength); }
From source file:com.nanxiaoqiang.test.netty.protocol.demo1.codec.NettyMessageDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null;// ? }/*from w w w . j ava 2 s . c o m*/ NettyMessage message = new NettyMessage(); Header header = new Header(); header.setCrcCode(frame.readInt()); header.setLength(frame.readInt()); header.setSessionID(frame.readLong()); header.setType(frame.readByte()); header.setPriority(frame.readByte()); int size = frame.readInt();// ?Header? if (size > 0) { Map<String, Object> attch = new HashMap<String, Object>(size); int keySize = 0; byte[] keyArray = null; String key = null; for (int i = 0; i < size; i++) { keySize = frame.readInt(); keyArray = new byte[keySize]; frame.readBytes(keyArray); key = new String(keyArray, "UTF-8");// keyUTF-8String attch.put(key, marshallingDecoder.decode(frame)); } keyArray = null; key = null; header.setAttachment(attch); } // Header End if (frame.readableBytes() > 4) { message.setBody(marshallingDecoder.decode(frame));// 4Data } message.setHeader(header); return message; }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) { ByteBuf buf = (ByteBuf) msg; System.out.println("channelRead:" + ByteBufUtil.hexDump((buf))); while (buf.readableBytes() > 0) { int payload = buf.readUnsignedMedium(); int frameType = buf.readByte(); Http2Flags flags = new Http2Flags(buf.readUnsignedByte()); int streamId = readUnsignedInt(buf); ByteBuf payloadBuf = buf.readBytes(payload); ByteBuf copy = ctx.alloc().buffer(); System.out.println("frame_type:" + frameType + "," + ByteBufUtil.hexDump((payloadBuf))); switch (frameType) { case Http2FrameTypes.SETTINGS: handleSettingFrame(ctx, flags); break; case Http2FrameTypes.WINDOW_UPDATE: handleWindowsUpdateFrame(ctx); break; case Http2FrameTypes.HEADERS: copy.writeMedium(payload);/*from w w w . j a va 2s . c o m*/ copy.writeByte(frameType); copy.writeByte(flags.value()); copy.writeInt(streamId); copy.writeBytes(payloadBuf); forward(ctx, copy); break; case Http2FrameTypes.DATA: copy.writeMedium(payload); copy.writeByte(frameType); copy.writeByte(flags.value()); copy.writeInt(streamId); copy.writeBytes(payloadBuf); forward(ctx, copy); break; default: break; } } }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
private void readFrame(final ChannelHandlerContext ctx, ByteBuf buf) { if (first) {/*from ww w .j a v a 2s . c o m*/ try { readClientPrefaceString(buf); } catch (Http2Exception e) { e.printStackTrace(); } first = false; } while (buf.readableBytes() > 0) { int payload = buf.readUnsignedMedium(); int frameType = buf.readByte(); Http2Flags flags = new Http2Flags(buf.readUnsignedByte()); int streamId = readUnsignedInt(buf); ByteBuf payloadBuf = buf.readBytes(payload); ByteBuf copy = ctx.alloc().buffer(); switch (frameType) { case Http2FrameTypes.SETTINGS: handleSettingFrame(ctx, flags); break; case Http2FrameTypes.WINDOW_UPDATE: handleWindowsUpdateFrame(ctx); break; case Http2FrameTypes.HEADERS: copy.writeMedium(payload); copy.writeByte(frameType); copy.writeByte(flags.value()); copy.writeInt(streamId); copy.writeBytes(payloadBuf); handleHeaderFrame(ctx, copy, streamId); break; case Http2FrameTypes.DATA: copy.writeMedium(payload); copy.writeByte(frameType); copy.writeByte(flags.value()); copy.writeInt(streamId); copy.writeBytes(payloadBuf); handleDataFrame(ctx, copy, streamId); break; default: break; } } }
From source file:com.ogarproject.ogar.server.net.packet.Packet.java
License:Open Source License
public static String readUTF8(ByteBuf in) { ByteBuf buffer = in.alloc().buffer(); byte b;// w w w . j a v a2 s . co m while (in.readableBytes() > 0 && (b = in.readByte()) != 0) { buffer.writeByte(b); } return buffer.toString(Charsets.UTF_8); }
From source file:com.oskm.netty.DiscardServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // TODO Auto-generated method stub ByteBuf in = (ByteBuf) msg; try {//from ww w . ja va 2 s. c o m while (in.isReadable()) { System.out.print((char) in.readByte()); System.out.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { ReferenceCountUtil.release(msg); } }