| | |
| | | |
| | | import java.nio.ByteBuffer; |
| | | import java.util.Vector; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.ToDoubleBiFunction; |
| | | |
| | | import io.netty.bootstrap.Bootstrap; |
| | |
| | | // 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)); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | 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","之前存在的读写超时handler,先移除掉,再重新添加"); |
| | | channel.pipeline().remove(IdleStateHandler.class.getSimpleName()); |
| | | } |
| | | // 3次心跳没响应,代表连接已断开 |
| | | //设定IdleStateHandler心跳检测每四秒进行一次写检测,如果四秒内write()方法未被调用则触发一次userEventTrigger()方法,实现客户端每四秒向服务端发送一次消息; |
| | | 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("之前存在的先移除掉,再重新添加HeartbeatHandler"); |
| | | channel.pipeline().addBefore(TCPReadHandler.class.getSimpleName(), HeartbeatHandler.class.getSimpleName(), |
| | | new HeartbeatHandler(this)); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | MyLog.i("添加心跳消息管理handler失败,reason:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |