Search in sources :

Example 91 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project motan by weibocom.

the class NettyClient method open.

@Override
public synchronized boolean open() {
    if (isAvailable()) {
        return true;
    }
    bootstrap = new Bootstrap();
    int timeout = getUrl().getIntParameter(URLParamType.connectTimeout.getName(), URLParamType.connectTimeout.getIntValue());
    if (timeout <= 0) {
        throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    // 最大响应包限制
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyClient.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder());
            pipeline.addLast("handler", new NettyChannelHandler(NettyClient.this, new MessageHandler() {

                @Override
                public Object handle(Channel channel, Object message) {
                    Response response = (Response) message;
                    ResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId());
                    if (responseFuture == null) {
                        LoggerUtil.warn("NettyClient has response from server, but responseFuture not exist, requestId={}", response.getRequestId());
                        return null;
                    }
                    if (response.getException() != null) {
                        responseFuture.onFailure(response);
                    } else {
                        responseFuture.onSuccess(response);
                    }
                    return null;
                }
            }));
        }
    });
    // 初始化连接池
    initPool();
    LoggerUtil.info("NettyClient finish Open: url={}", url);
    // 注册统计回调
    StatsUtil.registryStatisticCallback(this);
    // 设置可用状态
    state = ChannelState.ALIVE;
    return true;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanAbstractException(com.weibo.api.motan.exception.MotanAbstractException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 92 with ChannelPipeline

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

the class TransportSelectionHandler method switchToWebsocket.

private void switchToWebsocket(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();
    memoryTracker.allocateHeap(HTTP_SERVER_CODEC_SHALLOW_SIZE + HTTP_OBJECT_AGGREGATOR_SHALLOW_SIZE + WEB_SOCKET_SERVER_PROTOCOL_HANDLER_SHALLOW_SIZE + WEB_SOCKET_FRAME_AGGREGATOR_SHALLOW_SIZE + WebSocketFrameTranslator.SHALLOW_SIZE + ProtocolHandshaker.SHALLOW_SIZE);
    p.addLast(new HttpServerCodec(), new HttpObjectAggregator(MAX_WEBSOCKET_HANDSHAKE_SIZE), new WebSocketServerProtocolHandler("/", null, false, MAX_WEBSOCKET_FRAME_SIZE), new WebSocketFrameAggregator(MAX_WEBSOCKET_FRAME_SIZE), new WebSocketFrameTranslator(), newHandshaker());
    p.remove(this);
}
Also used : WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) WebSocketFrameTranslator(org.neo4j.bolt.transport.pipeline.WebSocketFrameTranslator) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 93 with ChannelPipeline

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

the class TransportSelectionHandler method enableSsl.

private void enableSsl(ChannelHandlerContext ctx) {
    // allocate sufficient space for another transport selection handlers as this instance will be freed upon pipeline removal
    memoryTracker.allocateHeap(SSL_HANDLER_SHALLOW_SIZE + SHALLOW_SIZE);
    ChannelPipeline p = ctx.pipeline();
    p.addLast(sslCtx.newHandler(ctx.alloc()));
    p.addLast(new TransportSelectionHandler(boltChannel, null, encryptionRequired, true, logging, boltProtocolFactory, channelProtector, memoryTracker));
    p.remove(this);
}
Also used : ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 94 with ChannelPipeline

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

the class TransportWriteThrottleTest method setup.

@BeforeEach
void setup() {
    config = mock(SocketChannelConfig.class);
    lockAttribute = mock(Attribute.class);
    Attribute durationExceedAttribute = mock(Attribute.class);
    when(durationExceedAttribute.get()).thenReturn(null);
    channel = mock(SocketChannel.class, Answers.RETURNS_MOCKS);
    when(channel.config()).thenReturn(config);
    when(channel.isOpen()).thenReturn(true);
    when(channel.remoteAddress()).thenReturn(InetSocketAddress.createUnresolved("localhost", 0));
    when(channel.attr(TransportWriteThrottle.LOCK_KEY)).thenReturn(lockAttribute);
    when(channel.attr(TransportWriteThrottle.MAX_DURATION_EXCEEDED_KEY)).thenReturn(durationExceedAttribute);
    ChannelPipeline pipeline = channel.pipeline();
    when(channel.pipeline()).thenReturn(pipeline);
    context = mock(ChannelHandlerContext.class, Answers.RETURNS_MOCKS);
    when(context.channel()).thenReturn(channel);
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) Attribute(io.netty.util.Attribute) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 95 with ChannelPipeline

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

the class TransportSelectionHandlerTest method channelHandlerContextMockSslAlreadyConfigured.

private static ChannelHandlerContext channelHandlerContextMockSslAlreadyConfigured() {
    Channel channel = mock(Channel.class);
    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    ChannelPipeline pipeline = mock(ChannelPipeline.class);
    SslHandler sslHandler = mock(SslHandler.class);
    when(context.channel()).thenReturn(channel);
    when(context.pipeline()).thenReturn(pipeline);
    when(context.pipeline().get(SslHandler.class)).thenReturn(sslHandler);
    return context;
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

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