Search in sources :

Example 66 with Channel

use of io.netty.channel.Channel in project ratpack by ratpack.

the class WebSocketEngine method connect.

@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
    PublicAddress publicAddress = context.get(PublicAddress.class);
    URI address = publicAddress.get(context);
    URI httpPath = address.resolve(path);
    URI wsPath;
    try {
        wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
    } catch (URISyntaxException e) {
        throw uncheck(e);
    }
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
    Request request = context.getRequest();
    HttpMethod method = valueOf(request.getMethod().getName());
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
    nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
    final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
    final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
    final Channel channel = directChannelAccess.getChannel();
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
    }
    handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) Channel(io.netty.channel.Channel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Request(ratpack.http.Request) PublicAddress(ratpack.server.PublicAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 67 with Channel

use of io.netty.channel.Channel in project redisson by redisson.

the class CommandAsyncService method handleBlockingOperations.

private <R, V> void handleBlockingOperations(final AsyncDetails<V, R> details, final RedisConnection connection, Long popTimeout) {
    final FutureListener<Boolean> listener = new FutureListener<Boolean>() {

        @Override
        public void operationComplete(Future<Boolean> future) throws Exception {
            details.getMainPromise().tryFailure(new RedissonShutdownException("Redisson is shutdown"));
        }
    };
    final AtomicBoolean canceledByScheduler = new AtomicBoolean();
    final Timeout scheduledFuture;
    if (popTimeout != 0) {
        // to handle cases when connection has been lost
        final Channel orignalChannel = connection.getChannel();
        scheduledFuture = connectionManager.newTimeout(new TimerTask() {

            @Override
            public void run(Timeout timeout) throws Exception {
                // and connection is still active
                if (orignalChannel == connection.getChannel() && connection.isActive()) {
                    return;
                }
                canceledByScheduler.set(true);
                details.getAttemptPromise().trySuccess(null);
            }
        }, popTimeout, TimeUnit.SECONDS);
    } else {
        scheduledFuture = null;
    }
    details.getMainPromise().addListener(new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            if (scheduledFuture != null) {
                scheduledFuture.cancel();
            }
            synchronized (listener) {
                connectionManager.getShutdownPromise().removeListener(listener);
            }
            // handling cancel operation for commands from skipTimeout collection
            if ((future.isCancelled() && details.getAttemptPromise().cancel(true)) || canceledByScheduler.get()) {
                connection.forceFastReconnectAsync();
                return;
            }
            if (future.cause() instanceof RedissonShutdownException) {
                details.getAttemptPromise().tryFailure(future.cause());
            }
        }
    });
    synchronized (listener) {
        if (!details.getMainPromise().isDone()) {
            connectionManager.getShutdownPromise().addListener(listener);
        }
    }
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Timeout(io.netty.util.Timeout) Channel(io.netty.channel.Channel) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimerTask(io.netty.util.TimerTask) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) RedissonShutdownException(org.redisson.RedissonShutdownException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 68 with Channel

use of io.netty.channel.Channel in project proxyee-down by monkeyWie.

the class AbstractHttpDownBootstrap method startChunkDown.

protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
    HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
    RequestProto requestProto = requestInfo.requestProto();
    LOGGER.debug("开始下载:" + chunkInfo);
    Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
    if (httpDownInfo.getProxyConfig() != null) {
        // 代理服务器解析DNS和连接
        bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
    }
    if (callback != null) {
        callback.onChunkConnecting(httpDownInfo, chunkInfo);
    }
    ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
    chunkInfo.setStatus(updateStatus);
    // 重置最后下载时间
    chunkInfo.setLastDownTime(System.currentTimeMillis());
    cf.addListener((ChannelFutureListener) future -> {
        if (future.isSuccess()) {
            synchronized (chunkInfo) {
                setChannel(chunkInfo, future.channel());
            }
            synchronized (requestInfo) {
                LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
                if (httpDownInfo.getTaskInfo().isSupportRange()) {
                    requestInfo.headers().set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition());
                } else {
                    requestInfo.headers().remove(HttpHeaderNames.RANGE);
                }
                future.channel().writeAndFlush(httpDownInfo.getRequest());
            }
            if (requestInfo.content() != null) {
                HttpContent content = new DefaultLastHttpContent();
                content.content().writeBytes(requestInfo.content());
                future.channel().writeAndFlush(content);
            }
        } else {
            LOGGER.debug("下载连接失败:" + chunkInfo);
            chunkInfo.setStatus(HttpDownStatus.FAIL);
            future.channel().close();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) RandomAccessFile(java.io.RandomAccessFile) HttpDownStatus(lee.study.down.constant.HttpDownStatus) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) ByteBuffer(java.nio.ByteBuffer) FileUtil(lee.study.down.util.FileUtil) NoopAddressResolverGroup(io.netty.resolver.NoopAddressResolverGroup) TaskInfo(lee.study.down.model.TaskInfo) HttpDownInitializer(lee.study.down.handle.HttpDownInitializer) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) ChunkInfo(lee.study.down.model.ChunkInfo) HttpContent(io.netty.handler.codec.http.HttpContent) Logger(org.slf4j.Logger) SslContext(io.netty.handler.ssl.SslContext) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) IOException(java.io.IOException) HttpDownCallback(lee.study.down.dispatch.HttpDownCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) HttpDownInfo(lee.study.down.model.HttpDownInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Closeable(java.io.Closeable) Data(lombok.Data) BootstrapException(lee.study.down.exception.BootstrapException) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AllArgsConstructor(lombok.AllArgsConstructor) HttpDownUtil(lee.study.down.util.HttpDownUtil) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Bootstrap(io.netty.bootstrap.Bootstrap) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) HttpDownInitializer(lee.study.down.handle.HttpDownInitializer) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 69 with Channel

use of io.netty.channel.Channel in project proxyee-down by monkeyWie.

the class AbstractHttpDownBootstrap method close.

public void close(ChunkInfo chunkInfo, boolean isDone) {
    try {
        if (!isDone) {
            chunkInfo.setStatus(HttpDownStatus.WAIT);
        }
        if (!attr.containsKey(chunkInfo.getIndex())) {
            return;
        }
        Channel channel = getChannel(chunkInfo);
        LOGGER.debug("下载连接关闭:channelId[" + (channel != null ? channel.id() : "null") + "]\t" + chunkInfo);
        HttpDownUtil.safeClose(channel);
        Closeable closeable = (Closeable) getAttr(chunkInfo, ATTR_FILE_CLOSEABLE);
        if (closeable != null) {
            closeable.close();
        }
        attr.remove(chunkInfo.getIndex());
    } catch (Exception e) {
        LOGGER.error("closeChunk error", e);
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) Closeable(java.io.Closeable) IOException(java.io.IOException) BootstrapException(lee.study.down.exception.BootstrapException)

Example 70 with Channel

use of io.netty.channel.Channel in project proxyee-down by monkeyWie.

the class HttpDownProxyServer method start.

public void start(int port) {
    LOGGER.debug("HttpDownProxyServer listen " + port + "\tproxyConfig:" + proxyConfig);
    // 监听http下载请求
    proxyServer.proxyConfig(proxyConfig);
    proxyServer.proxyInterceptInitializer(new HttpProxyInterceptInitializer() {

        @Override
        public void init(HttpProxyInterceptPipeline pipeline) {
            pipeline.addLast(new BdyIntercept());
            pipeline.addLast(new HttpDownSniffIntercept());
            HttpProxyIntercept downIntercept = interceptFactory.create();
            if (downIntercept != null) {
                pipeline.addLast(downIntercept);
            }
        }
    }).httpProxyExceptionHandle(new HttpProxyExceptionHandle() {

        @Override
        public void beforeCatch(Channel clientChannel, Throwable cause) throws Exception {
            LOGGER.warn("beforeCatch:", cause);
        }

        @Override
        public void afterCatch(Channel clientChannel, Channel proxyChannel, Throwable cause) throws Exception {
            LOGGER.warn("afterCatch:", cause);
        }
    }).start(port);
}
Also used : BdyIntercept(lee.study.down.intercept.BdyIntercept) Channel(io.netty.channel.Channel) HttpProxyIntercept(lee.study.proxyee.intercept.HttpProxyIntercept) HttpProxyExceptionHandle(lee.study.proxyee.exception.HttpProxyExceptionHandle) HttpDownSniffIntercept(lee.study.down.intercept.HttpDownSniffIntercept) HttpProxyInterceptPipeline(lee.study.proxyee.intercept.HttpProxyInterceptPipeline) HttpProxyInterceptInitializer(lee.study.proxyee.intercept.HttpProxyInterceptInitializer)

Aggregations

Channel (io.netty.channel.Channel)884 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)226 ChannelFuture (io.netty.channel.ChannelFuture)204 Test (org.junit.Test)203 Bootstrap (io.netty.bootstrap.Bootstrap)199 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)191 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 InetSocketAddress (java.net.InetSocketAddress)165 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)132 IOException (java.io.IOException)126 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)106 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)96 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)89