作者 crossoverJie

:sparkles: Introducing new features.服务端心跳检测2

1 package com.crossoverjie.cim.client.handle; 1 package com.crossoverjie.cim.client.handle;
2 2
3 import com.crossoverjie.cim.client.util.SpringBeanFactory; 3 import com.crossoverjie.cim.client.util.SpringBeanFactory;
  4 +import com.crossoverjie.cim.common.constant.Constants;
4 import com.crossoverjie.cim.common.protocol.CIMRequestProto; 5 import com.crossoverjie.cim.common.protocol.CIMRequestProto;
5 import com.crossoverjie.cim.common.protocol.CIMResponseProto; 6 import com.crossoverjie.cim.common.protocol.CIMResponseProto;
6 import io.netty.channel.ChannelFutureListener; 7 import io.netty.channel.ChannelFutureListener;
@@ -40,7 +41,7 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt @@ -40,7 +41,7 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
40 if (idleStateEvent.state() == IdleState.WRITER_IDLE){ 41 if (idleStateEvent.state() == IdleState.WRITER_IDLE){
41 CIMRequestProto.CIMReqProtocol heartBeat = SpringBeanFactory.getBean("heartBeat", 42 CIMRequestProto.CIMReqProtocol heartBeat = SpringBeanFactory.getBean("heartBeat",
42 CIMRequestProto.CIMReqProtocol.class); 43 CIMRequestProto.CIMReqProtocol.class);
43 - ctx.writeAndFlush(heartBeat).addListeners(ChannelFutureListener.CLOSE_ON_FAILURE) ; 44 + ctx.writeAndFlush(heartBeat).sync().addListeners(ChannelFutureListener.CLOSE_ON_FAILURE) ;
44 } 45 }
45 46
46 47
@@ -57,15 +58,18 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt @@ -57,15 +58,18 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
57 } 58 }
58 59
59 @Override 60 @Override
60 - protected void channelRead0(ChannelHandlerContext channelHandlerContext, CIMResponseProto.CIMResProtocol responseProtocol) throws Exception { 61 + protected void channelRead0(ChannelHandlerContext channelHandlerContext, CIMResponseProto.CIMResProtocol msg) throws Exception {
61 62
62 //从服务端收到消息时被调用 63 //从服务端收到消息时被调用
63 //LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ; 64 //LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ;
64 65
65 - //回调消息  
66 - callBackMsg(responseProtocol.getResMsg()); 66 + if (msg.getType() != Constants.CommandType.PING) {
  67 + //回调消息
  68 + callBackMsg(msg.getResMsg());
  69 +
  70 + LOGGER.info(msg.getResMsg());
  71 + }
67 72
68 - LOGGER.info(responseProtocol.getResMsg());  
69 } 73 }
70 74
71 /** 75 /**
@@ -40,6 +40,15 @@ public final class CIMResponseProto { @@ -40,6 +40,15 @@ public final class CIMResponseProto {
40 */ 40 */
41 com.google.protobuf.ByteString 41 com.google.protobuf.ByteString
42 getResMsgBytes(); 42 getResMsgBytes();
  43 +
  44 + /**
  45 + * <code>required int32 type = 3;</code>
  46 + */
  47 + boolean hasType();
  48 + /**
  49 + * <code>required int32 type = 3;</code>
  50 + */
  51 + int getType();
43 } 52 }
44 /** 53 /**
45 * Protobuf type {@code protocol.CIMResProtocol} 54 * Protobuf type {@code protocol.CIMResProtocol}
@@ -56,6 +65,7 @@ public final class CIMResponseProto { @@ -56,6 +65,7 @@ public final class CIMResponseProto {
56 private CIMResProtocol() { 65 private CIMResProtocol() {
57 responseId_ = 0L; 66 responseId_ = 0L;
58 resMsg_ = ""; 67 resMsg_ = "";
  68 + type_ = 0;
59 } 69 }
60 70
61 @Override 71 @Override
@@ -100,6 +110,11 @@ public final class CIMResponseProto { @@ -100,6 +110,11 @@ public final class CIMResponseProto {
100 responseId_ = input.readInt64(); 110 responseId_ = input.readInt64();
101 break; 111 break;
102 } 112 }
  113 + case 24: {
  114 + bitField0_ |= 0x00000004;
  115 + type_ = input.readInt32();
  116 + break;
  117 + }
103 } 118 }
104 } 119 }
105 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 120 } catch (com.google.protobuf.InvalidProtocolBufferException e) {
@@ -182,6 +197,21 @@ public final class CIMResponseProto { @@ -182,6 +197,21 @@ public final class CIMResponseProto {
182 } 197 }
183 } 198 }
184 199
  200 + public static final int TYPE_FIELD_NUMBER = 3;
  201 + private int type_;
  202 + /**
  203 + * <code>required int32 type = 3;</code>
  204 + */
  205 + public boolean hasType() {
  206 + return ((bitField0_ & 0x00000004) == 0x00000004);
  207 + }
  208 + /**
  209 + * <code>required int32 type = 3;</code>
  210 + */
  211 + public int getType() {
  212 + return type_;
  213 + }
  214 +
185 private byte memoizedIsInitialized = -1; 215 private byte memoizedIsInitialized = -1;
186 public final boolean isInitialized() { 216 public final boolean isInitialized() {
187 byte isInitialized = memoizedIsInitialized; 217 byte isInitialized = memoizedIsInitialized;
@@ -196,6 +226,10 @@ public final class CIMResponseProto { @@ -196,6 +226,10 @@ public final class CIMResponseProto {
196 memoizedIsInitialized = 0; 226 memoizedIsInitialized = 0;
197 return false; 227 return false;
198 } 228 }
  229 + if (!hasType()) {
  230 + memoizedIsInitialized = 0;
  231 + return false;
  232 + }
199 memoizedIsInitialized = 1; 233 memoizedIsInitialized = 1;
200 return true; 234 return true;
201 } 235 }
@@ -208,6 +242,9 @@ public final class CIMResponseProto { @@ -208,6 +242,9 @@ public final class CIMResponseProto {
208 if (((bitField0_ & 0x00000001) == 0x00000001)) { 242 if (((bitField0_ & 0x00000001) == 0x00000001)) {
209 output.writeInt64(2, responseId_); 243 output.writeInt64(2, responseId_);
210 } 244 }
  245 + if (((bitField0_ & 0x00000004) == 0x00000004)) {
  246 + output.writeInt32(3, type_);
  247 + }
211 unknownFields.writeTo(output); 248 unknownFields.writeTo(output);
212 } 249 }
213 250
@@ -223,6 +260,10 @@ public final class CIMResponseProto { @@ -223,6 +260,10 @@ public final class CIMResponseProto {
223 size += com.google.protobuf.CodedOutputStream 260 size += com.google.protobuf.CodedOutputStream
224 .computeInt64Size(2, responseId_); 261 .computeInt64Size(2, responseId_);
225 } 262 }
  263 + if (((bitField0_ & 0x00000004) == 0x00000004)) {
  264 + size += com.google.protobuf.CodedOutputStream
  265 + .computeInt32Size(3, type_);
  266 + }
226 size += unknownFields.getSerializedSize(); 267 size += unknownFields.getSerializedSize();
227 memoizedSize = size; 268 memoizedSize = size;
228 return size; 269 return size;
@@ -249,6 +290,11 @@ public final class CIMResponseProto { @@ -249,6 +290,11 @@ public final class CIMResponseProto {
249 result = result && getResMsg() 290 result = result && getResMsg()
250 .equals(other.getResMsg()); 291 .equals(other.getResMsg());
251 } 292 }
  293 + result = result && (hasType() == other.hasType());
  294 + if (hasType()) {
  295 + result = result && (getType()
  296 + == other.getType());
  297 + }
252 result = result && unknownFields.equals(other.unknownFields); 298 result = result && unknownFields.equals(other.unknownFields);
253 return result; 299 return result;
254 } 300 }
@@ -269,6 +315,10 @@ public final class CIMResponseProto { @@ -269,6 +315,10 @@ public final class CIMResponseProto {
269 hash = (37 * hash) + RESMSG_FIELD_NUMBER; 315 hash = (37 * hash) + RESMSG_FIELD_NUMBER;
270 hash = (53 * hash) + getResMsg().hashCode(); 316 hash = (53 * hash) + getResMsg().hashCode();
271 } 317 }
  318 + if (hasType()) {
  319 + hash = (37 * hash) + TYPE_FIELD_NUMBER;
  320 + hash = (53 * hash) + getType();
  321 + }
272 hash = (29 * hash) + unknownFields.hashCode(); 322 hash = (29 * hash) + unknownFields.hashCode();
273 memoizedHashCode = hash; 323 memoizedHashCode = hash;
274 return hash; 324 return hash;
@@ -402,6 +452,8 @@ public final class CIMResponseProto { @@ -402,6 +452,8 @@ public final class CIMResponseProto {
402 bitField0_ = (bitField0_ & ~0x00000001); 452 bitField0_ = (bitField0_ & ~0x00000001);
403 resMsg_ = ""; 453 resMsg_ = "";
404 bitField0_ = (bitField0_ & ~0x00000002); 454 bitField0_ = (bitField0_ & ~0x00000002);
  455 + type_ = 0;
  456 + bitField0_ = (bitField0_ & ~0x00000004);
405 return this; 457 return this;
406 } 458 }
407 459
@@ -434,6 +486,10 @@ public final class CIMResponseProto { @@ -434,6 +486,10 @@ public final class CIMResponseProto {
434 to_bitField0_ |= 0x00000002; 486 to_bitField0_ |= 0x00000002;
435 } 487 }
436 result.resMsg_ = resMsg_; 488 result.resMsg_ = resMsg_;
  489 + if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
  490 + to_bitField0_ |= 0x00000004;
  491 + }
  492 + result.type_ = type_;
437 result.bitField0_ = to_bitField0_; 493 result.bitField0_ = to_bitField0_;
438 onBuilt(); 494 onBuilt();
439 return result; 495 return result;
@@ -484,6 +540,9 @@ public final class CIMResponseProto { @@ -484,6 +540,9 @@ public final class CIMResponseProto {
484 resMsg_ = other.resMsg_; 540 resMsg_ = other.resMsg_;
485 onChanged(); 541 onChanged();
486 } 542 }
  543 + if (other.hasType()) {
  544 + setType(other.getType());
  545 + }
487 this.mergeUnknownFields(other.unknownFields); 546 this.mergeUnknownFields(other.unknownFields);
488 onChanged(); 547 onChanged();
489 return this; 548 return this;
@@ -496,6 +555,9 @@ public final class CIMResponseProto { @@ -496,6 +555,9 @@ public final class CIMResponseProto {
496 if (!hasResMsg()) { 555 if (!hasResMsg()) {
497 return false; 556 return false;
498 } 557 }
  558 + if (!hasType()) {
  559 + return false;
  560 + }
499 return true; 561 return true;
500 } 562 }
501 563
@@ -625,6 +687,38 @@ public final class CIMResponseProto { @@ -625,6 +687,38 @@ public final class CIMResponseProto {
625 onChanged(); 687 onChanged();
626 return this; 688 return this;
627 } 689 }
  690 +
  691 + private int type_ ;
  692 + /**
  693 + * <code>required int32 type = 3;</code>
  694 + */
  695 + public boolean hasType() {
  696 + return ((bitField0_ & 0x00000004) == 0x00000004);
  697 + }
  698 + /**
  699 + * <code>required int32 type = 3;</code>
  700 + */
  701 + public int getType() {
  702 + return type_;
  703 + }
  704 + /**
  705 + * <code>required int32 type = 3;</code>
  706 + */
  707 + public Builder setType(int value) {
  708 + bitField0_ |= 0x00000004;
  709 + type_ = value;
  710 + onChanged();
  711 + return this;
  712 + }
  713 + /**
  714 + * <code>required int32 type = 3;</code>
  715 + */
  716 + public Builder clearType() {
  717 + bitField0_ = (bitField0_ & ~0x00000004);
  718 + type_ = 0;
  719 + onChanged();
  720 + return this;
  721 + }
628 public final Builder setUnknownFields( 722 public final Builder setUnknownFields(
629 final com.google.protobuf.UnknownFieldSet unknownFields) { 723 final com.google.protobuf.UnknownFieldSet unknownFields) {
630 return super.setUnknownFields(unknownFields); 724 return super.setUnknownFields(unknownFields);
@@ -688,10 +782,11 @@ public final class CIMResponseProto { @@ -688,10 +782,11 @@ public final class CIMResponseProto {
688 descriptor; 782 descriptor;
689 static { 783 static {
690 String[] descriptorData = { 784 String[] descriptorData = {
691 - "\n\027BaseResponseProto.proto\022\010protocol\"4\n\016C" + 785 + "\n\027BaseResponseProto.proto\022\010protocol\"B\n\016C" +
692 "IMResProtocol\022\022\n\nresponseId\030\002 \002(\003\022\016\n\006res" + 786 "IMResProtocol\022\022\n\nresponseId\030\002 \002(\003\022\016\n\006res" +
693 - "Msg\030\001 \002(\tB8\n$com.crossoverjie.cim.common" +  
694 - ".protocolB\020CIMResponseProto" 787 + "Msg\030\001 \002(\t\022\014\n\004type\030\003 \002(\005B8\n$com.crossover" +
  788 + "jie.cim.common.protocolB\020CIMResponseProt" +
  789 + "o"
695 }; 790 };
696 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = 791 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
697 new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { 792 new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
@@ -710,7 +805,7 @@ public final class CIMResponseProto { @@ -710,7 +805,7 @@ public final class CIMResponseProto {
710 internal_static_protocol_CIMResProtocol_fieldAccessorTable = new 805 internal_static_protocol_CIMResProtocol_fieldAccessorTable = new
711 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 806 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
712 internal_static_protocol_CIMResProtocol_descriptor, 807 internal_static_protocol_CIMResProtocol_descriptor,
713 - new String[] { "ResponseId", "ResMsg", }); 808 + new String[] { "ResponseId", "ResMsg", "Type", });
714 } 809 }
715 810
716 // @@protoc_insertion_point(outer_class_scope) 811 // @@protoc_insertion_point(outer_class_scope)
@@ -49,7 +49,6 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto @@ -49,7 +49,6 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto
49 if (evt instanceof IdleStateEvent) { 49 if (evt instanceof IdleStateEvent) {
50 IdleStateEvent idleStateEvent = (IdleStateEvent) evt; 50 IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
51 if (idleStateEvent.state() == IdleState.READER_IDLE) { 51 if (idleStateEvent.state() == IdleState.READER_IDLE) {
52 - LOGGER.info("向客户端发送心跳");  
53 52
54 //向客户端发送消息 53 //向客户端发送消息
55 CIMRequestProto.CIMReqProtocol heartBeat = SpringBeanFactory.getBean("heartBeat", 54 CIMRequestProto.CIMReqProtocol heartBeat = SpringBeanFactory.getBean("heartBeat",
@@ -58,13 +57,10 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto @@ -58,13 +57,10 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto
58 @Override 57 @Override
59 public void operationComplete(ChannelFuture future) throws Exception { 58 public void operationComplete(ChannelFuture future) throws Exception {
60 if (!future.isSuccess()) { 59 if (!future.isSuccess()) {
61 - LOGGER.info("向客户端发送心跳失败");  
62 //下线客户端 60 //下线客户端
63 CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) future.channel()); 61 CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) future.channel());
64 userOffLine(userInfo, (NioSocketChannel) future.channel()); 62 userOffLine(userInfo, (NioSocketChannel) future.channel());
65 future.channel().close(); 63 future.channel().close();
66 - }else {  
67 - LOGGER.info("向客户端发送心跳成功");  
68 } 64 }
69 } 65 }
70 }); 66 });
@@ -8,6 +8,6 @@ option java_outer_classname = "CIMResponseProto"; @@ -8,6 +8,6 @@ option java_outer_classname = "CIMResponseProto";
8 message CIMResProtocol { 8 message CIMResProtocol {
9 required int64 responseId = 2; 9 required int64 responseId = 2;
10 required string resMsg = 1; 10 required string resMsg = 1;
11 - 11 + required int32 type = 3;
12 12
13 } 13 }