From ef7d5dd6984db6ba0b95822b92f2dad71c742dd3 Mon Sep 17 00:00:00 2001 From: monan <651932351@qq.com> Date: Wed, 19 Apr 2023 15:25:56 +0800 Subject: [PATCH 1/3] Add exception caught --- .../bcos/sdk/network/ChannelHandler.java | 241 ++++++++---------- 1 file changed, 109 insertions(+), 132 deletions(-) diff --git a/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java b/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java index 2577cc679..97bb9d2af 100644 --- a/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java +++ b/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java @@ -31,136 +31,113 @@ /** Channel handler process inbound message. */ @Sharable public class ChannelHandler extends SimpleChannelInboundHandler { - private static Logger logger = LoggerFactory.getLogger(ChannelHandler.class); - private MsgHandler msgHandler; - private ConnectionManager connectionManager; - private ExecutorService msgHandleThreadPool; - - public void setMsgHandleThreadPool(ExecutorService msgHandleThreadPool) { - this.msgHandleThreadPool = msgHandleThreadPool; - } - - public ChannelHandler(ConnectionManager connManager, MsgHandler msgHandler) { - this.msgHandler = msgHandler; - this.connectionManager = connManager; - } - - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); - Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); - if (evt instanceof IdleStateEvent) { - final IdleStateEvent e = (IdleStateEvent) evt; - switch (e.state()) { - case READER_IDLE: - case WRITER_IDLE: - case ALL_IDLE: - logger.error( - " idle state event:{} connect{}:{} long time Inactive, disconnect", - e.state(), - host, - port); - channelInactive(ctx); - ctx.disconnect(); - ctx.close(); - break; - default: - break; - } - } else if (evt instanceof SslHandshakeCompletionEvent) { - SslHandshakeCompletionEvent e = (SslHandshakeCompletionEvent) evt; - if (e.isSuccess()) { - logger.info( - " handshake success, host: {}, port: {}, ctx: {}", - host, - port, - System.identityHashCode(ctx)); - ChannelHandlerContext oldCtx = - connectionManager.addConnectionContext(host, port, ctx); - msgHandler.onConnect(ctx); - - if (Objects.nonNull(oldCtx)) { - oldCtx.close(); - oldCtx.disconnect(); - - logger.warn( - " disconnect old connection, host: {}, port: {}, ctx: {}", - host, - port, - System.identityHashCode(ctx)); - } - } else { - logger.error( - " handshake failed, host: {}, port: {}, reason: {}, error stack: {} ", - host, - port, - e.cause().getLocalizedMessage(), - e.cause()); - ctx.disconnect(); - ctx.close(); - } - - } else if (evt instanceof SslCloseCompletionEvent) { - logger.info( - " ssl close completion event, host: {}, port: {}, ctx: {} ", - host, - port, - System.identityHashCode(ctx)); - } else { - logger.info( - " userEventTriggered event, host: {}, port: {}, evt: {}, ctx: {} ", - host, - port, - evt, - System.identityHashCode(ctx)); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - try { - // lost the connection, get ip info - String host = - ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); - Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); - - logger.debug( - " channelInactive, disconnect " - + host - + ":" - + String.valueOf(port) - + " ," - + String.valueOf(ctx.channel().isActive())); - - connectionManager.removeConnectionContext(host, port, ctx); - msgHandler.onDisconnect(ctx); - - } catch (Exception e) { - logger.error("error ", e); - } - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - final ChannelHandlerContext ctxF = ctx; - final Message in = (Message) msg; - - if (msgHandleThreadPool == null) { - msgHandler.onMessage(ctxF, in); - } else { - msgHandleThreadPool.execute( - new Runnable() { - @Override - public void run() { - msgHandler.onMessage(ctxF, in); - } - }); - } - } - - @Override - protected void channelRead0(ChannelHandlerContext ctx, Message msg) { - final ChannelHandlerContext ctxF = ctx; - msgHandler.onMessage(ctxF, msg); - } + private static Logger logger = LoggerFactory.getLogger(ChannelHandler.class); + private MsgHandler msgHandler; + private ConnectionManager connectionManager; + private ExecutorService msgHandleThreadPool; + + public void setMsgHandleThreadPool(ExecutorService msgHandleThreadPool) { + this.msgHandleThreadPool = msgHandleThreadPool; + } + + public ChannelHandler(ConnectionManager connManager, MsgHandler msgHandler) { + this.msgHandler = msgHandler; + this.connectionManager = connManager; + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); + Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); + if (evt instanceof IdleStateEvent) { + final IdleStateEvent e = (IdleStateEvent) evt; + switch (e.state()) { + case READER_IDLE: + case WRITER_IDLE: + case ALL_IDLE: + logger.error(" idle state event:{} connect{}:{} long time Inactive, disconnect", e.state(), host, port); + channelInactive(ctx); + ctx.disconnect(); + ctx.close(); + break; + default: + break; + } + } else if (evt instanceof SslHandshakeCompletionEvent) { + SslHandshakeCompletionEvent e = (SslHandshakeCompletionEvent) evt; + if (e.isSuccess()) { + logger.info(" handshake success, host: {}, port: {}, ctx: {}", host, port, + System.identityHashCode(ctx)); + ChannelHandlerContext oldCtx = connectionManager.addConnectionContext(host, port, ctx); + msgHandler.onConnect(ctx); + + if (Objects.nonNull(oldCtx)) { + oldCtx.close(); + oldCtx.disconnect(); + + logger.warn(" disconnect old connection, host: {}, port: {}, ctx: {}", host, port, + System.identityHashCode(ctx)); + } + } else { + logger.error(" handshake failed, host: {}, port: {}, reason: {}, error stack: {} ", host, port, + e.cause().getLocalizedMessage(), e.cause()); + ctx.disconnect(); + ctx.close(); + } + + } else if (evt instanceof SslCloseCompletionEvent) { + logger.info(" ssl close completion event, host: {}, port: {}, ctx: {} ", host, port, + System.identityHashCode(ctx)); + } else { + logger.info(" userEventTriggered event, host: {}, port: {}, evt: {}, ctx: {} ", host, port, evt, + System.identityHashCode(ctx)); + } + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + try { + // lost the connection, get ip info + String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); + Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); + + logger.debug(" channelInactive, disconnect " + host + ":" + String.valueOf(port) + " ," + + String.valueOf(ctx.channel().isActive())); + + connectionManager.removeConnectionContext(host, port, ctx); + msgHandler.onDisconnect(ctx); + + } catch (Exception e) { + logger.error("error ", e); + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + final ChannelHandlerContext ctxF = ctx; + final Message in = (Message) msg; + + if (msgHandleThreadPool == null) { + msgHandler.onMessage(ctxF, in); + } else { + msgHandleThreadPool.execute(new Runnable() { + @Override + public void run() { + msgHandler.onMessage(ctxF, in); + } + }); + } + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, Message msg) { + final ChannelHandlerContext ctxF = ctx; + msgHandler.onMessage(ctxF, msg); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + logger.debug("exceptionCaught: ", cause); + this.channelInactive(ctx); + } } From 6a615fe5881b28bdcfd9cb676b860aa7b95ae9bc Mon Sep 17 00:00:00 2001 From: monan <651932351@qq.com> Date: Wed, 19 Apr 2023 15:27:47 +0800 Subject: [PATCH 2/3] Revert format changes --- .../bcos/sdk/network/ChannelHandler.java | 240 ++++++++++-------- 1 file changed, 135 insertions(+), 105 deletions(-) diff --git a/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java b/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java index 97bb9d2af..94c051908 100644 --- a/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java +++ b/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java @@ -31,113 +31,143 @@ /** Channel handler process inbound message. */ @Sharable public class ChannelHandler extends SimpleChannelInboundHandler { - private static Logger logger = LoggerFactory.getLogger(ChannelHandler.class); - private MsgHandler msgHandler; - private ConnectionManager connectionManager; - private ExecutorService msgHandleThreadPool; - - public void setMsgHandleThreadPool(ExecutorService msgHandleThreadPool) { - this.msgHandleThreadPool = msgHandleThreadPool; - } - - public ChannelHandler(ConnectionManager connManager, MsgHandler msgHandler) { - this.msgHandler = msgHandler; - this.connectionManager = connManager; - } - - @Override - public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { - String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); - Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); - if (evt instanceof IdleStateEvent) { - final IdleStateEvent e = (IdleStateEvent) evt; - switch (e.state()) { - case READER_IDLE: - case WRITER_IDLE: - case ALL_IDLE: - logger.error(" idle state event:{} connect{}:{} long time Inactive, disconnect", e.state(), host, port); - channelInactive(ctx); - ctx.disconnect(); - ctx.close(); - break; - default: - break; - } - } else if (evt instanceof SslHandshakeCompletionEvent) { - SslHandshakeCompletionEvent e = (SslHandshakeCompletionEvent) evt; - if (e.isSuccess()) { - logger.info(" handshake success, host: {}, port: {}, ctx: {}", host, port, - System.identityHashCode(ctx)); - ChannelHandlerContext oldCtx = connectionManager.addConnectionContext(host, port, ctx); - msgHandler.onConnect(ctx); - - if (Objects.nonNull(oldCtx)) { - oldCtx.close(); - oldCtx.disconnect(); - - logger.warn(" disconnect old connection, host: {}, port: {}, ctx: {}", host, port, - System.identityHashCode(ctx)); - } - } else { - logger.error(" handshake failed, host: {}, port: {}, reason: {}, error stack: {} ", host, port, - e.cause().getLocalizedMessage(), e.cause()); - ctx.disconnect(); - ctx.close(); - } - - } else if (evt instanceof SslCloseCompletionEvent) { - logger.info(" ssl close completion event, host: {}, port: {}, ctx: {} ", host, port, - System.identityHashCode(ctx)); - } else { - logger.info(" userEventTriggered event, host: {}, port: {}, evt: {}, ctx: {} ", host, port, evt, - System.identityHashCode(ctx)); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - try { - // lost the connection, get ip info - String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); - Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); - - logger.debug(" channelInactive, disconnect " + host + ":" + String.valueOf(port) + " ," - + String.valueOf(ctx.channel().isActive())); - - connectionManager.removeConnectionContext(host, port, ctx); - msgHandler.onDisconnect(ctx); - - } catch (Exception e) { - logger.error("error ", e); - } - } - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - final ChannelHandlerContext ctxF = ctx; - final Message in = (Message) msg; - - if (msgHandleThreadPool == null) { - msgHandler.onMessage(ctxF, in); - } else { - msgHandleThreadPool.execute(new Runnable() { - @Override - public void run() { - msgHandler.onMessage(ctxF, in); - } - }); - } - } - - @Override - protected void channelRead0(ChannelHandlerContext ctx, Message msg) { - final ChannelHandlerContext ctxF = ctx; - msgHandler.onMessage(ctxF, msg); - } - - @Override + private static Logger logger = LoggerFactory.getLogger(ChannelHandler.class); + private MsgHandler msgHandler; + private ConnectionManager connectionManager; + private ExecutorService msgHandleThreadPool; + + public void setMsgHandleThreadPool(ExecutorService msgHandleThreadPool) { + this.msgHandleThreadPool = msgHandleThreadPool; + } + + public ChannelHandler(ConnectionManager connManager, MsgHandler msgHandler) { + this.msgHandler = msgHandler; + this.connectionManager = connManager; + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); + Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); + if (evt instanceof IdleStateEvent) { + final IdleStateEvent e = (IdleStateEvent) evt; + switch (e.state()) { + case READER_IDLE: + case WRITER_IDLE: + case ALL_IDLE: + logger.error( + " idle state event:{} connect{}:{} long time Inactive, disconnect", + e.state(), + host, + port); + channelInactive(ctx); + ctx.disconnect(); + ctx.close(); + break; + default: + break; + } + } else if (evt instanceof SslHandshakeCompletionEvent) { + SslHandshakeCompletionEvent e = (SslHandshakeCompletionEvent) evt; + if (e.isSuccess()) { + logger.info( + " handshake success, host: {}, port: {}, ctx: {}", + host, + port, + System.identityHashCode(ctx)); + ChannelHandlerContext oldCtx = + connectionManager.addConnectionContext(host, port, ctx); + msgHandler.onConnect(ctx); + + if (Objects.nonNull(oldCtx)) { + oldCtx.close(); + oldCtx.disconnect(); + + logger.warn( + " disconnect old connection, host: {}, port: {}, ctx: {}", + host, + port, + System.identityHashCode(ctx)); + } + } else { + logger.error( + " handshake failed, host: {}, port: {}, reason: {}, error stack: {} ", + host, + port, + e.cause().getLocalizedMessage(), + e.cause()); + ctx.disconnect(); + ctx.close(); + } + + } else if (evt instanceof SslCloseCompletionEvent) { + logger.info( + " ssl close completion event, host: {}, port: {}, ctx: {} ", + host, + port, + System.identityHashCode(ctx)); + } else { + logger.info( + " userEventTriggered event, host: {}, port: {}, evt: {}, ctx: {} ", + host, + port, + evt, + System.identityHashCode(ctx)); + } + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + try { + // lost the connection, get ip info + String host = + ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress(); + Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort(); + + logger.debug( + " channelInactive, disconnect " + + host + + ":" + + String.valueOf(port) + + " ," + + String.valueOf(ctx.channel().isActive())); + + connectionManager.removeConnectionContext(host, port, ctx); + msgHandler.onDisconnect(ctx); + + } catch (Exception e) { + logger.error("error ", e); + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + final ChannelHandlerContext ctxF = ctx; + final Message in = (Message) msg; + + if (msgHandleThreadPool == null) { + msgHandler.onMessage(ctxF, in); + } else { + msgHandleThreadPool.execute( + new Runnable() { + @Override + public void run() { + msgHandler.onMessage(ctxF, in); + } + }); + } + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, Message msg) { + final ChannelHandlerContext ctxF = ctx; + msgHandler.onMessage(ctxF, msg); + } + + @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.debug("exceptionCaught: ", cause); this.channelInactive(ctx); } + } From 44262b29677855475a05fab6570adbef9d5392d5 Mon Sep 17 00:00:00 2001 From: monan <651932351@qq.com> Date: Wed, 19 Apr 2023 15:37:48 +0800 Subject: [PATCH 3/3] using gojf --- .../org/fisco/bcos/sdk/network/ChannelHandler.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java b/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java index 94c051908..607051adf 100644 --- a/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java +++ b/sdk-core/src/main/java/org/fisco/bcos/sdk/network/ChannelHandler.java @@ -163,11 +163,10 @@ protected void channelRead0(ChannelHandlerContext ctx, Message msg) { final ChannelHandlerContext ctxF = ctx; msgHandler.onMessage(ctxF, msg); } - + @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - logger.debug("exceptionCaught: ", cause); - this.channelInactive(ctx); - } - + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + logger.debug("exceptionCaught: ", cause); + this.channelInactive(ctx); + } }