Search in sources :

Example 66 with Channel

use of org.apache.flink.shaded.netty4.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 67 with Channel

use of org.apache.flink.shaded.netty4.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 68 with Channel

use of org.apache.flink.shaded.netty4.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 69 with Channel

use of org.apache.flink.shaded.netty4.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)

Example 70 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project proxyee-down by monkeyWie.

the class HttpDownHandleInterceptFactory method create.

@Override
public HttpProxyIntercept create() {
    return new HttpProxyIntercept() {

        @Override
        public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
            HttpRequest httpRequest = pipeline.getHttpRequest();
            ProxyConfig proxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
            TaskInfo taskInfo = HttpDownUtil.getTaskInfo(httpRequest, httpResponse.headers(), proxyConfig, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup);
            HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest, proxyConfig);
            ContentManager.DOWN.putBoot(httpDownInfo);
            httpResponse.setStatus(HttpResponseStatus.OK);
            httpResponse.headers().clear();
            httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
            byte[] content;
            if (HttpUtil.checkHead(httpRequest, HttpHeaderNames.HOST, "^.*\\.baidupcs\\.com.*$")) {
                content = "<script>window.close();</script>".getBytes();
            } else {
                content = "<script>window.history.go(-1);</script>".getBytes();
            }
            httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
            clientChannel.writeAndFlush(httpResponse);
            HttpContent httpContent = new DefaultLastHttpContent();
            httpContent.content().writeBytes(content);
            clientChannel.writeAndFlush(httpContent);
            clientChannel.close();
            httpDownDispatch.dispatch(httpDownInfo);
        }
    };
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) TaskInfo(lee.study.down.model.TaskInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Channel(io.netty.channel.Channel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpProxyIntercept(lee.study.proxyee.intercept.HttpProxyIntercept) ProxyConfig(lee.study.proxyee.proxy.ProxyConfig) HttpDownInfo(lee.study.down.model.HttpDownInfo) HttpProxyInterceptPipeline(lee.study.proxyee.intercept.HttpProxyInterceptPipeline) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Aggregations

Channel (io.netty.channel.Channel)889 Test (org.junit.Test)242 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)227 ChannelFuture (io.netty.channel.ChannelFuture)204 Bootstrap (io.netty.bootstrap.Bootstrap)201 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)193 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)176 InetSocketAddress (java.net.InetSocketAddress)169 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)152 IOException (java.io.IOException)143 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)131 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)105 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)97 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)88