Search in sources :

Example 71 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project tutorials-java by Artister.

the class ServerChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 字符长度
    pipeline.addLast(new LineBasedFrameDecoder(2048));
    // 自己的逻辑Handler
    pipeline.addLast("handler", new HelloWordServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HelloWordServerHandler(org.ko.netty.t3.handler.HelloWordServerHandler) StringDecoder(io.netty.handler.codec.string.StringDecoder) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 72 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project cosmic by MissionCriticalCloud.

the class NfsSecondaryStorageResource method startPostUploadServer.

private void startPostUploadServer() {
    final int PORT = 8210;
    final int NO_OF_WORKERS = 15;
    final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    final EventLoopGroup workerGroup = new NioEventLoopGroup(NO_OF_WORKERS);
    final ServerBootstrap b = new ServerBootstrap();
    final NfsSecondaryStorageResource storageResource = this;
    b.group(bossGroup, workerGroup);
    b.channel(NioServerSocketChannel.class);
    b.handler(new LoggingHandler(LogLevel.INFO));
    b.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(final SocketChannel ch) throws Exception {
            final ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new HttpRequestDecoder());
            pipeline.addLast(new HttpResponseEncoder());
            pipeline.addLast(new HttpContentCompressor());
            pipeline.addLast(new HttpUploadServerHandler(storageResource));
        }
    });
    new Thread() {

        @Override
        public void run() {
            try {
                final Channel ch = b.bind(PORT).sync().channel();
                s_logger.info(String.format("Started post upload server on port %d with %d workers", PORT, NO_OF_WORKERS));
                ch.closeFuture().sync();
            } catch (final InterruptedException e) {
                s_logger.info("Failed to start post upload server");
                s_logger.debug("Exception while starting post upload server", e);
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
                s_logger.info("shutting down post upload server");
            }
        }
    }.start();
    s_logger.info("created a thread to start post upload server");
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 73 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project autobahn-java by crossbario.

the class NettyWebSocket method connect.

@Override
public void connect(ITransportHandler transportHandler, TransportOptions options) throws Exception {
    if (options == null) {
        if (mOptions == null) {
            options = new TransportOptions();
        } else {
            options = new TransportOptions();
            options.setAutoPingInterval(mOptions.getAutoPingInterval());
            options.setAutoPingTimeout(mOptions.getAutoPingTimeout());
            options.setMaxFramePayloadSize(mOptions.getMaxFramePayloadSize());
        }
    }
    URI uri;
    uri = new URI(mUri);
    int port = validateURIAndGetPort(uri);
    String scheme = uri.getScheme();
    String host = uri.getHost();
    final SslContext sslContext = getSSLContext(scheme);
    WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, mSerializers, true, new DefaultHttpHeaders(), options.getMaxFramePayloadSize());
    mHandler = new NettyWebSocketClientHandler(handshaker, this, transportHandler);
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group);
    bootstrap.channel(NioSocketChannel.class);
    TransportOptions opt = options;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline channelPipeline = ch.pipeline();
            if (sslContext != null) {
                channelPipeline.addLast(sslContext.newHandler(ch.alloc(), host, port));
            }
            channelPipeline.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, new IdleStateHandler(opt.getAutoPingInterval() + opt.getAutoPingTimeout(), opt.getAutoPingInterval(), 0, TimeUnit.SECONDS), mHandler);
        }
    });
    mChannel = bootstrap.connect(uri.getHost(), port).sync().channel();
    mHandler.getHandshakeFuture().sync();
}
Also used : WebSocketClientHandshaker(io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) TransportOptions(io.crossbar.autobahn.wamp.types.TransportOptions) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) URI(java.net.URI) SSLException(javax.net.ssl.SSLException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 74 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project jim-framework by jiangmin168168.

the class RpcServerInitializer method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    // logger.info("RpcServerInitializer.initChannel");
    ChannelPipeline pipeline = socketChannel.pipeline();
    ;
    Executor executor = this.rpcThreadPoolFactory.getThreadPool(ConstantConfig.DEFAULT_THREAD_POOL_NAME).getExecutor(1, 1);
    pipeline.addLast(new RpcEncoder(RpcResponse.class)).addLast(new RpcDecoder(RpcRequest.class)).addLast(new IdleStateHandler(Constants.READER_TIME_SECONDS, 0, 0)).addLast(new ServerHeartbeatHandler()).addLast(new RpcServerInvoker(this.handlerMap, this.filterMap, executor));
}
Also used : RpcEncoder(com.jim.framework.rpc.codec.RpcEncoder) Executor(java.util.concurrent.Executor) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ServerHeartbeatHandler(com.jim.framework.rpc.keepalive.ServerHeartbeatHandler) RpcDecoder(com.jim.framework.rpc.codec.RpcDecoder) RpcResponse(com.jim.framework.rpc.common.RpcResponse) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 75 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project runelite by runelite.

the class CacheClient method connect.

public void connect() {
    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            // p.addFirst(new HttpProxyHandler(new InetSocketAddress("runelite.net", 3128)));
            p.addLast("decoder", new HandshakeResponseDecoder());
            p.addLast(new CacheClientHandler(), new HandshakeResponseHandler(CacheClient.this), new ArchiveResponseHandler(CacheClient.this));
            p.addLast(new UpdateHandshakeEncoder(), new EncryptionEncoder(), new ArchiveRequestEncoder());
        }
    });
    // Start the client.
    ChannelFuture f = b.connect(host, PORT).syncUninterruptibly();
    channel = f.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EncryptionEncoder(net.runelite.protocol.update.encoders.EncryptionEncoder) UpdateHandshakeEncoder(net.runelite.protocol.handshake.UpdateHandshakeEncoder) HandshakeResponseDecoder(net.runelite.protocol.update.decoders.HandshakeResponseDecoder) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) ArchiveRequestEncoder(net.runelite.protocol.update.encoders.ArchiveRequestEncoder) Bootstrap(io.netty.bootstrap.Bootstrap)

Aggregations

ChannelPipeline (io.netty.channel.ChannelPipeline)331 SocketChannel (io.netty.channel.socket.SocketChannel)95 Channel (io.netty.channel.Channel)86 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)84 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)77 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)74 Bootstrap (io.netty.bootstrap.Bootstrap)72 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)62 ChannelFuture (io.netty.channel.ChannelFuture)61 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)57 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)57 EventLoopGroup (io.netty.channel.EventLoopGroup)49 InetSocketAddress (java.net.InetSocketAddress)43 SslHandler (io.netty.handler.ssl.SslHandler)42 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)37 IOException (java.io.IOException)35 LoggingHandler (io.netty.handler.logging.LoggingHandler)33 StringDecoder (io.netty.handler.codec.string.StringDecoder)32 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)30 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)29