mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-27 10:30:34 +08:00
优化服务端异常打印日志
This commit is contained in:
parent
4051660957
commit
ac32108750
Binary file not shown.
@ -1,7 +1,8 @@
|
||||
package com.farsunset.cim.coder.json;
|
||||
|
||||
import com.farsunset.cim.constant.CIMConstant;
|
||||
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.SentBody;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
@ -35,14 +36,17 @@ public class TextMessageDecoder extends MessageToMessageDecoder<TextWebSocketFra
|
||||
|
||||
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());
|
||||
return;
|
||||
}
|
||||
|
||||
if (protocol.getType() == CIMConstant.DATA_TYPE_SENT){
|
||||
if (protocol.getType() == DataType.SENT.getValue()){
|
||||
SentBody body = OBJECT_MAPPER.readValue(protocol.getContent(), SentBody.class);
|
||||
list.add(body);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new ReadInvalidTypeException(protocol.getType());
|
||||
}
|
||||
}
|
@ -45,7 +45,7 @@ public class TextMessageEncoder extends MessageToMessageEncoder<Transportable> {
|
||||
protected void encode(ChannelHandlerContext ctx, Transportable data, List<Object> out) throws JsonProcessingException {
|
||||
|
||||
TransmitBody protocol = new TransmitBody();
|
||||
protocol.setType(data.getType());
|
||||
protocol.setType(data.getType().getValue());
|
||||
protocol.setContent(getBody(data));
|
||||
|
||||
TextWebSocketFrame frame = new TextWebSocketFrame(OBJECT_MAPPER.writeValueAsString(protocol));
|
||||
|
@ -26,15 +26,15 @@ package com.farsunset.cim.coder.json;
|
||||
*/
|
||||
class TransmitBody {
|
||||
|
||||
private int type;
|
||||
private byte type;
|
||||
|
||||
private String content;
|
||||
|
||||
public int getType() {
|
||||
public byte getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(int type) {
|
||||
public void setType(byte type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ package com.farsunset.cim.coder.protobuf;
|
||||
|
||||
import com.farsunset.cim.constant.CIMConstant;
|
||||
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.SentBody;
|
||||
@ -77,11 +78,11 @@ public class AppMessageDecoder extends ByteToMessageDecoder {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (CIMConstant.DATA_TYPE_SENT == type) {
|
||||
if (DataType.SENT.getValue() == type) {
|
||||
|
||||
SentBodyProto.Model bodyProto = SentBodyProto.Model.parseFrom(data);
|
||||
SentBody body = new SentBody();
|
||||
|
@ -35,7 +35,7 @@ public class AppMessageEncoder extends MessageToByteEncoder<Transportable> {
|
||||
@Override
|
||||
protected void encode(final ChannelHandlerContext ctx, final Transportable data, ByteBuf out){
|
||||
byte[] body = data.getBody();
|
||||
byte[] header = createHeader(data.getType(), body.length);
|
||||
byte[] header = createHeader(data.getType().getValue(), body.length);
|
||||
out.writeBytes(header);
|
||||
out.writeBytes(body);
|
||||
}
|
||||
|
@ -21,8 +21,8 @@
|
||||
*/
|
||||
package com.farsunset.cim.coder.protobuf;
|
||||
|
||||
import com.farsunset.cim.constant.CIMConstant;
|
||||
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.SentBody;
|
||||
@ -48,12 +48,12 @@ public class WebMessageDecoder extends MessageToMessageDecoder<BinaryWebSocketFr
|
||||
|
||||
byte type = buffer.readByte();
|
||||
|
||||
if (CIMConstant.DATA_TYPE_PONG == type) {
|
||||
if (DataType.PONG.getValue() == type) {
|
||||
list.add(Pong.getInstance());
|
||||
return;
|
||||
}
|
||||
|
||||
if (CIMConstant.DATA_TYPE_SENT == type) {
|
||||
if (DataType.SENT.getValue() == type) {
|
||||
list.add(getBody(buffer));
|
||||
return;
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public class WebMessageEncoder extends MessageToMessageEncoder<Transportable> {
|
||||
byte[] body = data.getBody();
|
||||
ByteBufAllocator allocator = ctx.channel().config().getAllocator();
|
||||
ByteBuf buffer = allocator.buffer(body.length + 1);
|
||||
buffer.writeByte(data.getType());
|
||||
buffer.writeByte(data.getType().getValue());
|
||||
buffer.writeBytes(body);
|
||||
out.add(new BinaryWebSocketFrame(buffer));
|
||||
}
|
||||
|
@ -31,12 +31,6 @@ public interface CIMConstant {
|
||||
*/
|
||||
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";
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@
|
||||
*/
|
||||
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.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
@ -221,7 +221,7 @@ public class Message implements Serializable, Transportable,Cloneable {
|
||||
|
||||
@JsonIgnore
|
||||
@Override
|
||||
public byte getType() {
|
||||
return CIMConstant.DATA_TYPE_MESSAGE;
|
||||
public DataType getType() {
|
||||
return DataType.MESSAGE;
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,8 @@
|
||||
*/
|
||||
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;
|
||||
|
||||
@ -51,9 +52,9 @@ public class Ping implements Serializable, Transportable {
|
||||
return DATA.getBytes();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
@Override
|
||||
public byte getType() {
|
||||
return CIMConstant.DATA_TYPE_PING;
|
||||
public DataType getType() {
|
||||
return DataType.PING;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
*/
|
||||
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.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
@ -145,8 +145,7 @@ public class ReplyBody implements Serializable, Transportable {
|
||||
|
||||
@JsonIgnore
|
||||
@Override
|
||||
public byte getType() {
|
||||
return CIMConstant.DATA_TYPE_REPLY;
|
||||
public DataType getType() {
|
||||
return DataType.REPLY;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,8 @@
|
||||
*/
|
||||
package com.farsunset.cim.model;
|
||||
|
||||
import com.farsunset.cim.constant.DataType;
|
||||
|
||||
/**
|
||||
* 需要向另一端发送的结构体
|
||||
*/
|
||||
@ -35,5 +37,5 @@ public interface Transportable {
|
||||
* 消息类型
|
||||
* @return
|
||||
*/
|
||||
byte getType();
|
||||
DataType getType();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user