Example usage for io.netty.channel ChannelHandlerContext alloc

List of usage examples for io.netty.channel ChannelHandlerContext alloc

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext alloc.

Prototype

ByteBufAllocator alloc();

Source Link

Document

Return the assigned ByteBufAllocator which will be used to allocate ByteBuf s.

Usage

From source file:com.lambdaworks.redis.server.RandomServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    byte initial[] = new byte[1];
    random.nextBytes(initial);/*  w ww  .ja  v  a  2  s  . c om*/

    byte[] response = new byte[Math.abs((int) initial[0])];

    Arrays.fill(response, "A".getBytes()[0]);

    ByteBuf buf = ctx.alloc().heapBuffer(response.length);

    ByteBuf encoded = buf.writeBytes(response);
    ctx.write(encoded);
}

From source file:com.liferay.sync.engine.lan.server.file.SyncChunkedFile.java

License:Open Source License

/**
 * @deprecated As of 3.3.0, As of Netty 4.1.0, replaced by {@link
 *             #readChunk(ByteBufAllocator)}
 *//*w  ww.  j  a v  a 2s .  c o m*/
@Deprecated
@Override
public ByteBuf readChunk(ChannelHandlerContext channelHandlerContext) throws Exception {

    return readChunk(channelHandlerContext.alloc());
}

From source file:com.linecorp.armeria.internal.http.HttpObjectEncoder.java

License:Apache License

protected static ByteBuf toByteBuf(ChannelHandlerContext ctx, HttpData data) {
    final ByteBuf buf = ctx.alloc().directBuffer(data.length(), data.length());
    buf.writeBytes(data.array(), data.offset(), data.length());
    return buf;//from  ww w  . ja va2  s.co m
}

From source file:com.linecorp.armeria.internal.HttpObjectEncoder.java

License:Apache License

protected static ByteBuf toByteBuf(ChannelHandlerContext ctx, HttpData data) {
    if (data instanceof ByteBufHolder) {
        return ((ByteBufHolder) data).content();
    }/*w w w.j a va  2  s . c om*/
    final ByteBuf buf = ctx.alloc().directBuffer(data.length(), data.length());
    buf.writeBytes(data.array(), data.offset(), data.length());
    return buf;
}

From source file:com.linkedin.r2.transport.http.client.Http2InitializerHandler.java

License:Apache License

/**
 * Sets up HTTP/2 over TLS through ALPN (h2) pipeline
 *//*w  w w. j  a  v  a2 s.c o  m*/
private void configureHttpsPipeline(ChannelHandlerContext ctx) throws Exception {
    JdkSslContext context = new JdkSslContext(_sslContext, IS_CLIENT,
            Arrays.asList(_sslParameters.getCipherSuites()), IdentityCipherSuiteFilter.INSTANCE,
            new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN,
                    ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE,
                    ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT,
                    ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1),
            _sslParameters.getNeedClientAuth() ? ClientAuth.REQUIRE : ClientAuth.OPTIONAL);
    SslHandler sslHandler = context.newHandler(ctx.alloc());

    Http2StreamCodec http2Codec = new Http2StreamCodecBuilder().connection(_connection)
            .maxContentLength(_maxResponseSize).maxHeaderSize(_maxHeaderSize)
            .gracefulShutdownTimeoutMillis(_gracefulShutdownTimeout).streamingTimeout(_streamingTimeout)
            .scheduler(_scheduler).build();

    Http2AlpnHandler alpnHandler = new Http2AlpnHandler(sslHandler, http2Codec);
    Http2SchemeHandler schemeHandler = new Http2SchemeHandler(HttpScheme.HTTPS.toString());
    Http2StreamResponseHandler responseHandler = new Http2StreamResponseHandler();
    Http2ChannelPoolHandler channelPoolHandler = new Http2ChannelPoolHandler();

    ctx.pipeline().addBefore(ctx.name(), "alpnHandler", alpnHandler);
    ctx.pipeline().addBefore(ctx.name(), "schemeHandler", schemeHandler);
    ctx.pipeline().addBefore(ctx.name(), "responseHandler", responseHandler);
    ctx.pipeline().addBefore(ctx.name(), "channelHandler", channelPoolHandler);

    _setupComplete = true;
}

From source file:com.magnet.yak.load.XMPPHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) { // (1)

    String s = "<stream:stream to=\"54.148.43.16\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">\"\r\n";
    final ByteBuf str = ctx.alloc().buffer(s.getBytes().length); // (2)
    str.writeBytes(s.getBytes());/*  w ww .  j  a  v  a 2s  .c o  m*/
    LOGGER.trace("channelActive : {}");
    final ChannelFuture f = ctx.writeAndFlush(str);
    f.addListener(new ChannelFutureListener() {
        public void operationComplete(ChannelFuture arg0) throws Exception {
            LOGGER.trace("operationComplete : {}");
        }

    });
    /*final ChannelFuture f = ctx.writeAndFlush(time); // (3)
    f.addListener(new ChannelFutureListener() {
     public void operationComplete(ChannelFuture arg0) throws Exception {
            
     }
            
    }); */// (4)
}

From source file:com.mastfrog.scamper.compression.CompressingCodec.java

License:Open Source License

private MessageTypeAndBuffer decodeImpl(ByteBuf buf, ChannelHandlerContext ctx, int sctpChannel)
        throws Exception {
    MessageType messageType = reg.forByteBuf(buf);
    if (messageType.isUnknown()) {
        return new MessageTypeAndBuffer(messageType, buf.slice(), sctpChannel);
    }//from www.  jav  a  2  s .  c o  m
    ByteBuf bb = ctx.alloc().buffer();
    uncompress(buf.slice(), bb);
    return new MessageTypeAndBuffer(messageType, bb, sctpChannel);
}

From source file:com.minetats.mw.NamedPipe.java

License:Apache License

@Override
public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
    return readChunk(ctx.alloc());
}

From source file:com.mycompany.device.FFDevice.java

public FFDevice(SocketChannel ch) {
    this.req = null;
    this.soc = ch;
    this.data_rcv = soc.alloc().buffer(512);
    this.reg_str = "";
    this.connect_time = System.currentTimeMillis();

    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new IdleStateHandler(0, 0, idle_time_interval_s));
    pipeline.addLast(new MessageToByteEncoder<byte[]>() {
        @Override// w w w. j av  a2 s  .co  m
        protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception {
            // TODO Auto-generated method stub
            out.writeBytes(msg);
        }
    });
    pipeline.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            // TODO Auto-generated method stub
            ByteBuf bb = (ByteBuf) msg;
            if (reg_str.equals("")) {
                reg_str = bb.toString(Charset.defaultCharset());
                FFServer.logger.info(String.format("device that has regs %s is registed", reg_str));
            } else {
                FFServer.logger.debug(String.format("%s receive: %d bytes", reg_str, bb.readableBytes()));
                data_rcv.writeBytes(bb);
            }
            ReferenceCountUtil.release(msg);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            // TODO Auto-generated method stub
            FFServer.logger.error(cause);
            Close();
        }

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                IdleStateEvent event = (IdleStateEvent) evt;
                if (event.state() == IdleState.ALL_IDLE) {
                    FFServer.logger.info(String.format("%s in idle state", reg_str));

                    ByteBuf hb = ctx.alloc().buffer(1).writeByte('.');
                    Send(hb);
                }
            }
        }
    });

    ChannelFuture f = soc.closeFuture();
    f.addListener((ChannelFutureListener) new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            is_closed = true;
            FFServer.logger.info(String.format("%s disconnected", reg_str));
        }
    });
}

From source file:com.necla.simba.server.gateway.server.backend.BackendFrameDecoder.java

License:Apache License

private ByteBuf extractMessage(ChannelHandlerContext ctx, ByteBuf in, int index, int length) {
    ByteBuf frame = ctx.alloc().buffer(length);
    frame.writeBytes(in, index, length);
    return frame;
}