优化服务端异常打印日志

This commit is contained in:
远方 2022-04-16 12:29:14 +08:00
parent 4051660957
commit ac32108750
14 changed files with 96 additions and 32 deletions

View File

@ -1,7 +1,8 @@
package com.farsunset.cim.coder.json; package com.farsunset.cim.coder.json;
import com.farsunset.cim.constant.CIMConstant;
import com.farsunset.cim.constant.ChannelAttr; import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.constant.DataType;
import com.farsunset.cim.exception.ReadInvalidTypeException;
import com.farsunset.cim.model.Pong; import com.farsunset.cim.model.Pong;
import com.farsunset.cim.model.SentBody; import com.farsunset.cim.model.SentBody;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -35,14 +36,17 @@ public class TextMessageDecoder extends MessageToMessageDecoder<TextWebSocketFra
TransmitBody protocol = OBJECT_MAPPER.readValue(text, TransmitBody.class); TransmitBody protocol = OBJECT_MAPPER.readValue(text, TransmitBody.class);
if (protocol.getType() == CIMConstant.DATA_TYPE_PONG){ if (protocol.getType() == DataType.PONG.getValue()){
list.add(Pong.getInstance()); list.add(Pong.getInstance());
return; return;
} }
if (protocol.getType() == CIMConstant.DATA_TYPE_SENT){ if (protocol.getType() == DataType.SENT.getValue()){
SentBody body = OBJECT_MAPPER.readValue(protocol.getContent(), SentBody.class); SentBody body = OBJECT_MAPPER.readValue(protocol.getContent(), SentBody.class);
list.add(body); list.add(body);
return;
} }
throw new ReadInvalidTypeException(protocol.getType());
} }
} }

View File

@ -45,7 +45,7 @@ public class TextMessageEncoder extends MessageToMessageEncoder<Transportable> {
protected void encode(ChannelHandlerContext ctx, Transportable data, List<Object> out) throws JsonProcessingException { protected void encode(ChannelHandlerContext ctx, Transportable data, List<Object> out) throws JsonProcessingException {
TransmitBody protocol = new TransmitBody(); TransmitBody protocol = new TransmitBody();
protocol.setType(data.getType()); protocol.setType(data.getType().getValue());
protocol.setContent(getBody(data)); protocol.setContent(getBody(data));
TextWebSocketFrame frame = new TextWebSocketFrame(OBJECT_MAPPER.writeValueAsString(protocol)); TextWebSocketFrame frame = new TextWebSocketFrame(OBJECT_MAPPER.writeValueAsString(protocol));

View File

@ -26,15 +26,15 @@ package com.farsunset.cim.coder.json;
*/ */
class TransmitBody { class TransmitBody {
private int type; private byte type;
private String content; private String content;
public int getType() { public byte getType() {
return type; return type;
} }
public void setType(int type) { public void setType(byte type) {
this.type = type; this.type = type;
} }

View File

@ -23,6 +23,7 @@ package com.farsunset.cim.coder.protobuf;
import com.farsunset.cim.constant.CIMConstant; import com.farsunset.cim.constant.CIMConstant;
import com.farsunset.cim.constant.ChannelAttr; import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.constant.DataType;
import com.farsunset.cim.exception.ReadInvalidTypeException; import com.farsunset.cim.exception.ReadInvalidTypeException;
import com.farsunset.cim.model.Pong; import com.farsunset.cim.model.Pong;
import com.farsunset.cim.model.SentBody; import com.farsunset.cim.model.SentBody;
@ -77,11 +78,11 @@ public class AppMessageDecoder extends ByteToMessageDecoder {
private Object mappingMessageObject(byte[] data, byte type) throws com.google.protobuf.InvalidProtocolBufferException { private Object mappingMessageObject(byte[] data, byte type) throws com.google.protobuf.InvalidProtocolBufferException {
if (CIMConstant.DATA_TYPE_PONG == type) { if (DataType.PONG.getValue() == type) {
return Pong.getInstance(); return Pong.getInstance();
} }
if (CIMConstant.DATA_TYPE_SENT == type) { if (DataType.SENT.getValue() == type) {
SentBodyProto.Model bodyProto = SentBodyProto.Model.parseFrom(data); SentBodyProto.Model bodyProto = SentBodyProto.Model.parseFrom(data);
SentBody body = new SentBody(); SentBody body = new SentBody();

View File

@ -35,7 +35,7 @@ public class AppMessageEncoder extends MessageToByteEncoder<Transportable> {
@Override @Override
protected void encode(final ChannelHandlerContext ctx, final Transportable data, ByteBuf out){ protected void encode(final ChannelHandlerContext ctx, final Transportable data, ByteBuf out){
byte[] body = data.getBody(); byte[] body = data.getBody();
byte[] header = createHeader(data.getType(), body.length); byte[] header = createHeader(data.getType().getValue(), body.length);
out.writeBytes(header); out.writeBytes(header);
out.writeBytes(body); out.writeBytes(body);
} }

View File

@ -21,8 +21,8 @@
*/ */
package com.farsunset.cim.coder.protobuf; package com.farsunset.cim.coder.protobuf;
import com.farsunset.cim.constant.CIMConstant;
import com.farsunset.cim.constant.ChannelAttr; import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.constant.DataType;
import com.farsunset.cim.exception.ReadInvalidTypeException; import com.farsunset.cim.exception.ReadInvalidTypeException;
import com.farsunset.cim.model.Pong; import com.farsunset.cim.model.Pong;
import com.farsunset.cim.model.SentBody; import com.farsunset.cim.model.SentBody;
@ -48,12 +48,12 @@ public class WebMessageDecoder extends MessageToMessageDecoder<BinaryWebSocketFr
byte type = buffer.readByte(); byte type = buffer.readByte();
if (CIMConstant.DATA_TYPE_PONG == type) { if (DataType.PONG.getValue() == type) {
list.add(Pong.getInstance()); list.add(Pong.getInstance());
return; return;
} }
if (CIMConstant.DATA_TYPE_SENT == type) { if (DataType.SENT.getValue() == type) {
list.add(getBody(buffer)); list.add(getBody(buffer));
return; return;
} }

View File

@ -40,7 +40,7 @@ public class WebMessageEncoder extends MessageToMessageEncoder<Transportable> {
byte[] body = data.getBody(); byte[] body = data.getBody();
ByteBufAllocator allocator = ctx.channel().config().getAllocator(); ByteBufAllocator allocator = ctx.channel().config().getAllocator();
ByteBuf buffer = allocator.buffer(body.length + 1); ByteBuf buffer = allocator.buffer(body.length + 1);
buffer.writeByte(data.getType()); buffer.writeByte(data.getType().getValue());
buffer.writeBytes(body); buffer.writeBytes(body);
out.add(new BinaryWebSocketFrame(buffer)); out.add(new BinaryWebSocketFrame(buffer));
} }

View File

@ -31,12 +31,6 @@ public interface CIMConstant {
*/ */
byte DATA_HEADER_LENGTH = 3; byte DATA_HEADER_LENGTH = 3;
byte DATA_TYPE_PONG = 0;
byte DATA_TYPE_PING = 1;
byte DATA_TYPE_MESSAGE = 2;
byte DATA_TYPE_SENT = 3;
byte DATA_TYPE_REPLY = 4;
String CLIENT_CONNECT_CLOSED = "client_closed"; String CLIENT_CONNECT_CLOSED = "client_closed";
} }

View File

@ -0,0 +1,63 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.constant;
/**
* 数据类型
*/
public enum DataType {
/**
* 客户端发送的的心跳响应
*/
PONG (0),
/**
* 服务端端发送的心跳响应
*/
PING (1),
/**
* 服务端端发送的消息体
*/
MESSAGE (2),
/**
* 客户端发送的请求体
*/
SENT (3),
/**
* 服务端端发送的响应体
*/
REPLY (4);
private final byte value;
DataType(int value) {
this.value = (byte) value;
}
public byte getValue() {
return value;
}
}

View File

@ -21,7 +21,7 @@
*/ */
package com.farsunset.cim.model; package com.farsunset.cim.model;
import com.farsunset.cim.constant.CIMConstant; import com.farsunset.cim.constant.DataType;
import com.farsunset.cim.model.proto.MessageProto; import com.farsunset.cim.model.proto.MessageProto;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
@ -221,7 +221,7 @@ public class Message implements Serializable, Transportable,Cloneable {
@JsonIgnore @JsonIgnore
@Override @Override
public byte getType() { public DataType getType() {
return CIMConstant.DATA_TYPE_MESSAGE; return DataType.MESSAGE;
} }
} }

View File

@ -21,7 +21,8 @@
*/ */
package com.farsunset.cim.model; package com.farsunset.cim.model;
import com.farsunset.cim.constant.CIMConstant; import com.farsunset.cim.constant.DataType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable; import java.io.Serializable;
@ -51,9 +52,9 @@ public class Ping implements Serializable, Transportable {
return DATA.getBytes(); return DATA.getBytes();
} }
@JsonIgnore
@Override @Override
public byte getType() { public DataType getType() {
return CIMConstant.DATA_TYPE_PING; return DataType.PING;
} }
} }

View File

@ -21,7 +21,7 @@
*/ */
package com.farsunset.cim.model; package com.farsunset.cim.model;
import com.farsunset.cim.constant.CIMConstant; import com.farsunset.cim.constant.DataType;
import com.farsunset.cim.model.proto.ReplyBodyProto; import com.farsunset.cim.model.proto.ReplyBodyProto;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
@ -145,8 +145,7 @@ public class ReplyBody implements Serializable, Transportable {
@JsonIgnore @JsonIgnore
@Override @Override
public byte getType() { public DataType getType() {
return CIMConstant.DATA_TYPE_REPLY; return DataType.REPLY;
} }
} }

View File

@ -21,6 +21,8 @@
*/ */
package com.farsunset.cim.model; package com.farsunset.cim.model;
import com.farsunset.cim.constant.DataType;
/** /**
* 需要向另一端发送的结构体 * 需要向另一端发送的结构体
*/ */
@ -35,5 +37,5 @@ public interface Transportable {
* 消息类型 * 消息类型
* @return * @return
*/ */
byte getType(); DataType getType();
} }