io.netty.channel.ChannelHandlerContext#close ( )源码实例Demo

下面列出了io.netty.channel.ChannelHandlerContext#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: netty-4.1.22   文件: Http2ConnectionHandler.java
private static void processGoAwayWriteResult(final ChannelHandlerContext ctx, final int lastStreamId,
                                             final long errorCode, final ByteBuf debugData, ChannelFuture future) {
    try {
        if (future.isSuccess()) {
            if (errorCode != NO_ERROR.code()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("{} Sent GOAWAY: lastStreamId '{}', errorCode '{}', " +
                                 "debugData '{}'. Forcing shutdown of the connection.",
                                 ctx.channel(), lastStreamId, errorCode, debugData.toString(UTF_8), future.cause());
                }
                ctx.close();
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("{} Sending GOAWAY failed: lastStreamId '{}', errorCode '{}', " +
                             "debugData '{}'. Forcing shutdown of the connection.",
                             ctx.channel(), lastStreamId, errorCode, debugData.toString(UTF_8), future.cause());
            }
            ctx.close();
        }
    } finally {
        // We're done with the debug data now.
        debugData.release();
    }
}
 
源代码2 项目: redant   文件: BadClientSilencer.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
    ctx.close();

    // To clarify where exceptions are from, imports are not used
    if (e instanceof java.io.IOException                            ||  // Connection reset by peer, Broken pipe
        e instanceof java.nio.channels.ClosedChannelException       ||
        e instanceof io.netty.handler.codec.DecoderException        ||
        e instanceof io.netty.handler.codec.CorruptedFrameException ||  // Bad WebSocket frame
        e instanceof IllegalArgumentException             ||  // Use https://... to connect to HTTP server
        e instanceof javax.net.ssl.SSLException                     ||  // Use http://... to connect to HTTPS server
        e instanceof io.netty.handler.ssl.NotSslRecordException) {
        onBadClient(e);  // Maybe client is bad
    } else {
        onBadServer(e);  // Maybe server is bad
    }
}
 
源代码3 项目: grpc-nebula-java   文件: ProtocolNegotiators.java
/**
 * Propagate failures to all buffered writes.
 */
@SuppressWarnings("FutureReturnValueIgnored")
protected final void fail(ChannelHandlerContext ctx, Throwable cause) {
  if (failCause == null) {
    failCause = cause;
  }
  if (bufferedWrites != null) {
    while (!bufferedWrites.isEmpty()) {
      ChannelWrite write = bufferedWrites.poll();
      write.promise.setFailure(cause);
      ReferenceCountUtil.release(write.msg);
    }
    bufferedWrites = null;
  }

  // In case something goes wrong ensure that the channel gets closed as the
  // NettyClientTransport relies on the channel's close future to get completed.
  ctx.close();
}
 
源代码4 项目: netty4.0.27Learn   文件: SocketStartTlsTest.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx,
                            Throwable cause) throws Exception {
    if (logger.isWarnEnabled()) {
        logger.warn("Unexpected exception from the server side", cause);
    }

    exception.compareAndSet(null, cause);
    ctx.close();
}
 
源代码5 项目: pulsar   文件: ProxyConnection.java
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    super.channelRegistered(ctx);
    ProxyService.activeConnections.inc();
    if (ProxyService.activeConnections.get() > service.getConfiguration().getMaxConcurrentInboundConnections()) {
        ctx.close();
        ProxyService.rejectedConnections.inc();
        return;
    }
}
 
源代码6 项目: fastjgame   文件: NetUtils.java
/**
 * 安静的关闭ctx,不产生任何影响
 */
public static void closeQuietly(@Nullable ChannelHandlerContext ctx) {
    if (null != ctx) {
        try {
            ctx.close();
        } catch (Throwable ignore) {

        }
    }
}
 
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    boolean channelInUse = getAttribute(ctx.channel(), ChannelAttributeKey.IN_USE).orElse(false);

    if (channelInUse) {
        ctx.fireExceptionCaught(cause);
    } else {
        ctx.close();

        Optional<CompletableFuture<Void>> executeFuture = getAttribute(ctx.channel(), ChannelAttributeKey.EXECUTE_FUTURE_KEY);

        if (executeFuture.isPresent() && !executeFuture.get().isDone()) {
            log.error(() -> "An exception occurred on an channel (" + ctx.channel().id() + ") that was not in use, " +
                            "but was associated with a future that wasn't completed. This indicates a bug in the " +
                            "Java SDK, where a future was not completed while the channel was in use. The channel has " +
                            "been closed, and the future will be completed to prevent any ongoing issues.", cause);
            executeFuture.get().completeExceptionally(cause);
        } else if (isNettyIoException(cause) || hasNettyIoExceptionCause(cause)) {
            log.debug(() -> "An I/O exception (" + cause.getMessage() + ") occurred on a channel (" + ctx.channel().id() +
                            ") that was not in use. The channel has been closed. This is usually normal.");

        } else {
            log.warn(() -> "A non-I/O exception occurred on a channel (" + ctx.channel().id() + ") that was not in use. " +
                           "The channel has been closed to prevent any ongoing issues.", cause);
        }
    }
}
 
源代码8 项目: netty-cookbook   文件: TcpClientLambdaHandler.java
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
	 ctx.flush();
     
     //close the connection after flushing data to client
	 if(close){
		 ctx.close();	 
	 }         
}
 
源代码9 项目: netty-custom-protocol   文件: ServerHandler.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
        throws Exception {
	System.err.println("--------服务器数据读异常----------: ");
	cause.printStackTrace();
    ctx.close();
}
 
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    try {
        randomAccessFile.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ctx.close();
}
 
源代码11 项目: dfactor   文件: DFHttpCliHandler.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	cause.printStackTrace();
	ctx.close();
}
 
源代码12 项目: couchbase-jvm-core   文件: KeyValueAuthHandler.java
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    ctx.close(promise);
}
 
源代码13 项目: netty.book.kor   文件: EchoClientHandler.java
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.close();
}
 
源代码14 项目: netty4.0.27Learn   文件: HttpSnoopServerHandler.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    ctx.close();
}
 
源代码15 项目: garmadon   文件: GreetingHandler.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    PrometheusHttpMetrics.GREETINGS_IN_ERROR.inc();
    LOGGER.error("", cause);
    ctx.close();
}
 
源代码16 项目: ethereumj   文件: EthHandler.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    logger.error(cause.getCause().toString());
    super.exceptionCaught(ctx, cause);
    ctx.close();
}
 
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    log.error("发生错误.......",cause);
    ctx.close();
}
 
源代码18 项目: netty-http2   文件: HttpHeaderCompressionTest.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (exception.compareAndSet(null, cause)) {
        ctx.close();
    }
}
 
源代码19 项目: netty-4.1.22   文件: AutobahnServerHandler.java
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    ctx.close();
}
 
源代码20 项目: jumbune   文件: JumbuneAgentDecoder.java
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
		List<Object> out) throws Exception {
	if (in.readableBytes() < RemotingConstants.THREE) {
		return;
	}
	final int magic1 = in.getUnsignedByte(in.readerIndex());
	final int magic2 = in.getUnsignedByte(in.readerIndex()+1);
	final int magic3 = in.getUnsignedByte(in.readerIndex()+2);
	if (isJar(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeJarReceiveHandler(ctx);
	} else if (isJas(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeJarSendHandler(ctx);
	} else if (isTxr(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeLogFilesReceiveHandler(ctx);
	} else if (isTxs(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeLogFilesSendHandler(ctx);
	} else if (isCmd(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeFireAndForgetCommandHandler(ctx);
	} else if(isCma(magic1, magic2, magic3)){
		throwAwayReadUnsignedBytes(in);
		invokeAsyncFireAndForgetCommandHandler(ctx);
	}/*else if (isCmg(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeFireAndGetCommandHandler(ctx);
	}*/ else if (isCmo(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeFireAndGetObjectResponseCommandHandler(ctx);
	} else if (isCmoHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeFireAndGetObjectResponseCommandHandlerForHA(ctx);
	} else if (isCmdHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeFireAndForgetCommandHandlerForHA(ctx);
	} else if (isTxrHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeLogFilesReceiveHandlerForHA(ctx);
	} else if (isTxsHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeLogFilesSendHandlerForHA(ctx);
	} else if (isJarHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeJarReceiveHandlerForHA(ctx);
	} else if (isJasHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeJarSendHandlerForHA(ctx);
	}else if (isSdaHA(magic1, magic2, magic3)) {
		throwAwayReadUnsignedBytes(in);
		invokeAgentShutdownHandler(ctx);
	} else {
        // Unknown protocol; discard everything and close the connection.
        in.clear();
        ctx.close();
	}

	if (haEnabled) {
		syncExecutor.sync();
	}
}