Search in sources :

Example 1 with NetSocketImpl

use of io.vertx.core.net.impl.NetSocketImpl in project vert.x by eclipse.

the class ClientConnection method createNetSocket.

public NetSocket createNetSocket() {
    // connection was upgraded to raw TCP socket
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, client.getSslHelper(), metrics);
    socket.metric(metric());
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(channel, socket);
    // Flush out all pending data
    endReadAndFlush();
    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class);
    if (inflater != null) {
        pipeline.remove(inflater);
    }
    pipeline.remove("codec");
    pipeline.replace("handler", "handler", new VertxNetHandler<NetSocketImpl>(channel, socket, connectionMap) {

        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            pool.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            pool.removeChannel(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                if (msg instanceof LastHttpContent) {
                    handleResponseEnd((LastHttpContent) msg);
                }
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }

        @Override
        protected void handleMsgReceived(Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            conn.handleDataReceived(Buffer.buffer(buf));
        }
    });
    return socket;
}
Also used : HashMap(java.util.HashMap) ByteBuf(io.netty.buffer.ByteBuf) VertxException(io.vertx.core.VertxException) NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl)

Example 2 with NetSocketImpl

use of io.vertx.core.net.impl.NetSocketImpl in project vert.x by eclipse.

the class ServerConnection method createNetSocket.

NetSocket createNetSocket() {
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, server.getSslHelper(), metrics);
    socket.metric(metric());
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(channel, socket);
    // Flush out all pending data
    endReadAndFlush();
    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
        pipeline.remove(compressor);
    }
    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
        pipeline.remove("chunkedWriter");
    }
    channel.pipeline().replace("handler", "handler", new VertxNetHandler<NetSocketImpl>(channel, socket, connectionMap) {

        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            server.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            server.removeChannel(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }

        @Override
        protected void handleMsgReceived(Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            conn.handleDataReceived(Buffer.buffer(buf));
        }
    });
    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
        channel.pipeline().remove("httpEncoder");
    } else {
        lastWriteFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                channel.pipeline().remove("httpEncoder");
            }
        });
    }
    return socket;
}
Also used : HashMap(java.util.HashMap) ByteBuf(io.netty.buffer.ByteBuf) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) IOException(java.io.IOException) NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 NetSocketImpl (io.vertx.core.net.impl.NetSocketImpl)2 HashMap (java.util.HashMap)2 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 VertxException (io.vertx.core.VertxException)1 IOException (java.io.IOException)1