作者 crossoverJie

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

package com.crossoverjie.cim.client.handle;
import com.crossoverjie.cim.client.util.SpringBeanFactory;
import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.protocol.CIMRequestProto;
import com.crossoverjie.cim.common.protocol.CIMResponseProto;
import io.netty.channel.ChannelFutureListener;
... ... @@ -40,7 +41,7 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
if (idleStateEvent.state() == IdleState.WRITER_IDLE){
CIMRequestProto.CIMReqProtocol heartBeat = SpringBeanFactory.getBean("heartBeat",
CIMRequestProto.CIMReqProtocol.class);
ctx.writeAndFlush(heartBeat).addListeners(ChannelFutureListener.CLOSE_ON_FAILURE) ;
ctx.writeAndFlush(heartBeat).sync().addListeners(ChannelFutureListener.CLOSE_ON_FAILURE) ;
}
... ... @@ -57,15 +58,18 @@ public class CIMClientHandle extends SimpleChannelInboundHandler<CIMResponseProt
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, CIMResponseProto.CIMResProtocol responseProtocol) throws Exception {
protected void channelRead0(ChannelHandlerContext channelHandlerContext, CIMResponseProto.CIMResProtocol msg) throws Exception {
//从服务端收到消息时被调用
//LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ;
//回调消息
callBackMsg(responseProtocol.getResMsg());
if (msg.getType() != Constants.CommandType.PING) {
//回调消息
callBackMsg(msg.getResMsg());
LOGGER.info(msg.getResMsg());
}
LOGGER.info(responseProtocol.getResMsg());
}
/**
... ...
... ... @@ -40,6 +40,15 @@ public final class CIMResponseProto {
*/
com.google.protobuf.ByteString
getResMsgBytes();
/**
* <code>required int32 type = 3;</code>
*/
boolean hasType();
/**
* <code>required int32 type = 3;</code>
*/
int getType();
}
/**
* Protobuf type {@code protocol.CIMResProtocol}
... ... @@ -56,6 +65,7 @@ public final class CIMResponseProto {
private CIMResProtocol() {
responseId_ = 0L;
resMsg_ = "";
type_ = 0;
}
@Override
... ... @@ -100,6 +110,11 @@ public final class CIMResponseProto {
responseId_ = input.readInt64();
break;
}
case 24: {
bitField0_ |= 0x00000004;
type_ = input.readInt32();
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
... ... @@ -182,6 +197,21 @@ public final class CIMResponseProto {
}
}
public static final int TYPE_FIELD_NUMBER = 3;
private int type_;
/**
* <code>required int32 type = 3;</code>
*/
public boolean hasType() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>required int32 type = 3;</code>
*/
public int getType() {
return type_;
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
... ... @@ -196,6 +226,10 @@ public final class CIMResponseProto {
memoizedIsInitialized = 0;
return false;
}
if (!hasType()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
}
... ... @@ -208,6 +242,9 @@ public final class CIMResponseProto {
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeInt64(2, responseId_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
output.writeInt32(3, type_);
}
unknownFields.writeTo(output);
}
... ... @@ -223,6 +260,10 @@ public final class CIMResponseProto {
size += com.google.protobuf.CodedOutputStream
.computeInt64Size(2, responseId_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(3, type_);
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
... ... @@ -249,6 +290,11 @@ public final class CIMResponseProto {
result = result && getResMsg()
.equals(other.getResMsg());
}
result = result && (hasType() == other.hasType());
if (hasType()) {
result = result && (getType()
== other.getType());
}
result = result && unknownFields.equals(other.unknownFields);
return result;
}
... ... @@ -269,6 +315,10 @@ public final class CIMResponseProto {
hash = (37 * hash) + RESMSG_FIELD_NUMBER;
hash = (53 * hash) + getResMsg().hashCode();
}
if (hasType()) {
hash = (37 * hash) + TYPE_FIELD_NUMBER;
hash = (53 * hash) + getType();
}
hash = (29 * hash) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
... ... @@ -402,6 +452,8 @@ public final class CIMResponseProto {
bitField0_ = (bitField0_ & ~0x00000001);
resMsg_ = "";
bitField0_ = (bitField0_ & ~0x00000002);
type_ = 0;
bitField0_ = (bitField0_ & ~0x00000004);
return this;
}
... ... @@ -434,6 +486,10 @@ public final class CIMResponseProto {
to_bitField0_ |= 0x00000002;
}
result.resMsg_ = resMsg_;
if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
to_bitField0_ |= 0x00000004;
}
result.type_ = type_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
... ... @@ -484,6 +540,9 @@ public final class CIMResponseProto {
resMsg_ = other.resMsg_;
onChanged();
}
if (other.hasType()) {
setType(other.getType());
}
this.mergeUnknownFields(other.unknownFields);
onChanged();
return this;
... ... @@ -496,6 +555,9 @@ public final class CIMResponseProto {
if (!hasResMsg()) {
return false;
}
if (!hasType()) {
return false;
}
return true;
}
... ... @@ -625,6 +687,38 @@ public final class CIMResponseProto {
onChanged();
return this;
}
private int type_ ;
/**
* <code>required int32 type = 3;</code>
*/
public boolean hasType() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>required int32 type = 3;</code>
*/
public int getType() {
return type_;
}
/**
* <code>required int32 type = 3;</code>
*/
public Builder setType(int value) {
bitField0_ |= 0x00000004;
type_ = value;
onChanged();
return this;
}
/**
* <code>required int32 type = 3;</code>
*/
public Builder clearType() {
bitField0_ = (bitField0_ & ~0x00000004);
type_ = 0;
onChanged();
return this;
}
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFields(unknownFields);
... ... @@ -688,10 +782,11 @@ public final class CIMResponseProto {
descriptor;
static {
String[] descriptorData = {
"\n\027BaseResponseProto.proto\022\010protocol\"4\n\016C" +
"\n\027BaseResponseProto.proto\022\010protocol\"B\n\016C" +
"IMResProtocol\022\022\n\nresponseId\030\002 \002(\003\022\016\n\006res" +
"Msg\030\001 \002(\tB8\n$com.crossoverjie.cim.common" +
".protocolB\020CIMResponseProto"
"Msg\030\001 \002(\t\022\014\n\004type\030\003 \002(\005B8\n$com.crossover" +
"jie.cim.common.protocolB\020CIMResponseProt" +
"o"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
... ... @@ -710,7 +805,7 @@ public final class CIMResponseProto {
internal_static_protocol_CIMResProtocol_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_protocol_CIMResProtocol_descriptor,
new String[] { "ResponseId", "ResMsg", });
new String[] { "ResponseId", "ResMsg", "Type", });
}
// @@protoc_insertion_point(outer_class_scope)
... ...
... ... @@ -49,7 +49,6 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
if (idleStateEvent.state() == IdleState.READER_IDLE) {
LOGGER.info("向客户端发送心跳");
//向客户端发送消息
CIMRequestProto.CIMReqProtocol heartBeat = SpringBeanFactory.getBean("heartBeat",
... ... @@ -58,13 +57,10 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
LOGGER.info("向客户端发送心跳失败");
//下线客户端
CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) future.channel());
userOffLine(userInfo, (NioSocketChannel) future.channel());
future.channel().close();
}else {
LOGGER.info("向客户端发送心跳成功");
}
}
});
... ...
... ... @@ -8,6 +8,6 @@ option java_outer_classname = "CIMResponseProto";
message CIMResProtocol {
required int64 responseId = 2;
required string resMsg = 1;
required int32 type = 3;
}
... ...