From b81d0cf47a08b3b5de01bf753fb71f6d1fda783a Mon Sep 17 00:00:00 2001 From: fctom1215 <fctom1215@outlook.com> Date: 星期日, 23 二月 2020 15:21:02 +0800 Subject: [PATCH] Merge branch 'master' of https://gitee.com/endian11/DriveJudge --- im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java | 64 +++++++++++++++++++++++++++++++- 1 files changed, 62 insertions(+), 2 deletions(-) diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java index ac26ebc..0c6112b 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java +++ b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java @@ -17,6 +17,7 @@ import java.nio.ByteBuffer; import java.util.Vector; +import java.util.concurrent.TimeUnit; import java.util.function.ToDoubleBiFunction; import io.netty.bootstrap.Bootstrap; @@ -330,7 +331,7 @@ // TODO: 2019/12/12 鏍规嵁MSG_ID 鏉ュ姞鍏� 瓒呮椂锛� 鏆備笉鍐� if (channel == null){ - Log.i(TAG, "sendMsg fail,channel涓虹┖"+msg); + MyLog.i("PlatformMessage", "sendMsg fail,channel涓虹┖"+msg); } try { MyLog.i("PlatformMessage", "sendMsg: "+ BytesUtils.bytesToHexString(msg)); @@ -338,7 +339,7 @@ byteBuf.writeBytes(msg); channel.writeAndFlush(byteBuf); } catch (Exception e) { - Log.i(TAG, "鍙戦�佹秷鎭け璐ワ紝reason="+e.getMessage()+"\t"+msg); + Log.i("PlatformMessage", "鍙戦�佹秷鎭け璐ワ紝reason="+e.getMessage()+"\t"+msg); } } @@ -403,6 +404,34 @@ return mOnEventListener.getRegisterMessage(); } return null; + } + + @Override + public byte[] getHeartbeatMsg() { + if (mOnEventListener != null){ + return mOnEventListener.getHearbeatMsg(); + } + return null; + } + + + + /** + * 澧炲姞蹇冭烦锛� + * @param heartBeatInterval 绉� + */ + @Override + public void addHeartbeatHandler(int heartBeatInterval){ + this.heartBeatInterval = heartBeatInterval*1000; + _addHeartbeatHandler(); + } + + public int getHeartbeatInterval() { + return this.heartBeatInterval; + } + + public ExecutorServiceFactory getLoopGroup() { + return loopGroup; } @@ -522,6 +551,37 @@ } + private void _addHeartbeatHandler(){ + MyLog.i("add HeartbeatHandler"); + if (channel == null || !channel.isActive() || channel.pipeline()==null){ + MyLog.i("PlatformMessage","娣诲姞蹇冭烦handler澶辫触"); + return; + } + try { + if(channel.pipeline().get(IdleStateHandler.class.getSimpleName()) != null){ + MyLog.i("PlatformMessage","涔嬪墠瀛樺湪鐨勮鍐欒秴鏃秇andler锛屽厛绉婚櫎鎺夛紝鍐嶉噸鏂版坊鍔�"); + channel.pipeline().remove(IdleStateHandler.class.getSimpleName()); + } + // 3娆″績璺虫病鍝嶅簲锛屼唬琛ㄨ繛鎺ュ凡鏂紑 + //璁惧畾IdleStateHandler蹇冭烦妫�娴嬫瘡鍥涚杩涜涓�娆″啓妫�娴嬶紝濡傛灉鍥涚鍐厀rite()鏂规硶鏈璋冪敤鍒欒Е鍙戜竴娆serEventTrigger()鏂规硶锛屽疄鐜板鎴风姣忓洓绉掑悜鏈嶅姟绔彂閫佷竴娆℃秷鎭紱 + channel.pipeline().addFirst(IdleStateHandler.class.getSimpleName(), new IdleStateHandler( + 0, heartBeatInterval, 0, TimeUnit.MILLISECONDS)); + + // 閲嶆柊娣诲姞HeartbeatHandler + if (channel.pipeline().get(HeartbeatHandler.class.getSimpleName()) != null) { + channel.pipeline().remove(HeartbeatHandler.class.getSimpleName()); + } + if (channel.pipeline().get(TCPReadHandler.class.getSimpleName()) != null) { + MyLog.i("涔嬪墠瀛樺湪鐨勫厛绉婚櫎鎺夛紝鍐嶉噸鏂版坊鍔燞eartbeatHandler"); + channel.pipeline().addBefore(TCPReadHandler.class.getSimpleName(), HeartbeatHandler.class.getSimpleName(), + new HeartbeatHandler(this)); + } + } catch (Exception e) { + e.printStackTrace(); + MyLog.i("娣诲姞蹇冭烦娑堟伅绠$悊handler澶辫触锛宺eason锛�" + e.getMessage()); + } + } + } -- Gitblit v1.8.0