List of usage examples for io.netty.channel ChannelHandlerContext alloc
ByteBufAllocator alloc();
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameDecoder.java
License:Apache License
private ByteBuf decompress(ChannelHandlerContext ctx, ByteBuf frame) throws Exception { int readableBytes = frame.readableBytes(); if (frame.hasArray()) { inflater.setInput(frame.array(), 0, readableBytes); } else {//from w w w .ja va2 s. c o m byte[] array = new byte[frame.readableBytes()]; frame.getBytes(0, array); inflater.setInput(array); } int totalLength = 0; List<ByteBuf> all = new LinkedList<ByteBuf>(); int multiplier = 2; alldone: while (true) { int maxOutputLength = inflater.getRemaining() * multiplier; // multiplier keeps increasing, so we will keep picking // larger and larger buffers the more times we have to loop // around, i.e., the more we realize that the data was very // heavily compressed, the larger our buffers are going to be. multiplier += 1; ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength); while (!inflater.needsInput()) { byte[] outArray = decompressed.array(); int outIndex = decompressed.arrayOffset() + decompressed.writerIndex(); int length = outArray.length - outIndex; if (length == 0) break; try { //LOG.debug("here1"); int outputLength = inflater.inflate(outArray, outIndex, length); totalLength += outputLength; //LOG.debug("here2"); if (outputLength > 0) decompressed.writerIndex(decompressed.writerIndex() + outputLength); } catch (DataFormatException e) { throw new Exception("Could not inflate" + e.getMessage()); } if (inflater.finished()) { all.add(decompressed); break alldone; } } all.add(decompressed); } inflater.reset(); if (all.size() == 1) return all.get(0); else { ByteBuf allData = ctx.alloc().heapBuffer(totalLength); for (ByteBuf b : all) { //LOG.debug("capacity=" + allData.capacity()); allData.writeBytes(b); b.release(); } return allData; } }
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;//from w w w. ja v a 2 s. c om 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); 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.GrpcProxyBackendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();//from ww w . j a v a 2 s . co m if (!flags.ack()) { //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleWindowsUpdateFrame(final ChannelHandlerContext ctx) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();//from w w w. ja v a2 s . c o m // 00 00 04 08 00 00 00 00 00 00 0f 00 01 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x08); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0f); byteBuf.writeByte(0x00); byteBuf.writeByte(0x01); ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { future.channel().close(); } } }); }
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 w w w .j ava 2 s. 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.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();/*from w w w .j a v a2 s . c o m*/ if (!flags.ack()) { // System.out.println("********************* setting received ..."); //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.ogarproject.ogar.server.net.PacketEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, Packet packet, List out) throws Exception { ByteBuf buf = ctx.alloc().buffer().order(ByteOrder.LITTLE_ENDIAN); int packetId = PacketRegistry.CLIENTBOUND.getPacketId(packet.getClass()); if (packetId == -1) { throw new IllegalArgumentException("Provided packet is not registered as a clientbound packet!"); }/* w w w . jav a 2s .c o m*/ buf.writeByte(packetId); packet.writeData(buf); out.add(new BinaryWebSocketFrame(buf)); OgarServer.log.finest("Sent packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") to " + ctx.channel().remoteAddress()); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf out = ctx.alloc().buffer(); Handshake handshake = new Handshake(); handshake.sequenceId = 0;//from ww w. j av a 2 s. c o m handshake.protocolVersion = MySQLServer.PROTOCOL_VERSION; handshake.serverVersion = MySQLServer.SERVER_VERSION; handshake.connectionId = session.getThreadId(); handshake.challenge1 = StringUtil.getRandomString(8); handshake.characterSet = CharsetUtil.getIndex(MySQLServer.DEFAULT_CHARSET); handshake.statusFlags = Flags.SERVER_STATUS_AUTOCOMMIT; handshake.challenge2 = StringUtil.getRandomString(12); handshake.authPluginDataLength = 21; handshake.authPluginName = Flags.MYSQL_NATIVE_PASSWORD; handshake.capabilityFlags = Flags.CLIENT_BASIC_FLAGS; handshake.removeCapabilityFlag(Flags.CLIENT_COMPRESS); handshake.removeCapabilityFlag(Flags.CLIENT_SSL); handshake.removeCapabilityFlag(Flags.CLIENT_LOCAL_FILES); session.setCharsetIndex((int) handshake.characterSet); session.setAttachment("seed", handshake.challenge1 + handshake.challenge2); out.writeBytes(handshake.toPacket()); ctx.writeAndFlush(out); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void success(ChannelHandlerContext ctx) { ByteBuf out = ctx.alloc().buffer(); OK ok = new OK(); ok.sequenceId = nextSequenceId();//from ww w.j a va 2 s .co m ok.setStatusFlag(Flags.SERVER_STATUS_AUTOCOMMIT); out.writeBytes(ok.toPacket()); ctx.writeAndFlush(out); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void sendError(ChannelHandlerContext ctx, int errno, String msg) { ByteBuf out = ctx.alloc().buffer(); ERR err = new ERR(); err.sequenceId = nextSequenceId();//from ww w . j ava 2s.c om err.errorCode = errno; err.errorMessage = msg; out.writeBytes(err.toPacket()); ctx.writeAndFlush(out); ACCESSLOGGER.markError(errno, msg); }