|
@@ -2,65 +2,107 @@ package com.phm.netty.domain;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
-import lombok.Getter;
|
|
|
-import lombok.Setter;
|
|
|
+import com.phm.netty.utils.ByteUtils;
|
|
|
+import io.netty.util.CharsetUtil;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.experimental.Accessors;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
|
* @Description Message
|
|
|
* @Author WGK
|
|
|
* @Date 2023/9/14 11:41
|
|
|
*/
|
|
|
-@Setter
|
|
|
-@Getter
|
|
|
-public class Message {
|
|
|
- /**
|
|
|
- * 客户端ID
|
|
|
- */
|
|
|
- private String id;
|
|
|
+@Data
|
|
|
+@Accessors(chain = true)
|
|
|
+public class Message implements Serializable {
|
|
|
/**
|
|
|
* 数据长度
|
|
|
*/
|
|
|
- private Integer len;
|
|
|
+ private int len;
|
|
|
|
|
|
/**
|
|
|
* 接收的通讯数据body
|
|
|
*/
|
|
|
- private String content;
|
|
|
+ private String data;
|
|
|
|
|
|
/**
|
|
|
* 消息类型
|
|
|
*/
|
|
|
- private Integer type;
|
|
|
+ private short type;
|
|
|
|
|
|
/**
|
|
|
* 毫秒时间戳,从2020.1.1开始
|
|
|
*/
|
|
|
- private Long timestamp = DateUtil.current(false) - DateUtil.parse("2020-01-01").getTime();
|
|
|
+ private int timestamp = (int) (DateUtil.current(false) - DateUtil.parse("2020-01-01").getTime());
|
|
|
|
|
|
/**
|
|
|
* 数据源
|
|
|
*/
|
|
|
- private String source = "GPHM";
|
|
|
+ private String source = StringUtils.rightPad("GPHM", 8);
|
|
|
|
|
|
/**
|
|
|
* 目标
|
|
|
*/
|
|
|
- private String target = "SPHNM";
|
|
|
+ private String target = StringUtils.rightPad("SPHM", 8);
|
|
|
|
|
|
/**
|
|
|
* 目标
|
|
|
*/
|
|
|
- private Integer reserve = 0;
|
|
|
+ private int reserver = 0;
|
|
|
|
|
|
public Message() {
|
|
|
}
|
|
|
|
|
|
+ public static void bytesToMsg(Message msg, byte[] buf, int len) {
|
|
|
+ int index = 0;
|
|
|
+ msg.len = len;
|
|
|
+ msg.type = ByteUtils.toShort(buf, index);
|
|
|
+ index += 2;
|
|
|
+ msg.timestamp = ByteUtils.toInt(buf, index);
|
|
|
+ index += 4;
|
|
|
+ msg.reserver = ByteUtils.toInt(buf, index);
|
|
|
+ index += 4;
|
|
|
+ byte[] bytes = new byte[len - index];
|
|
|
+ System.arraycopy(buf, index, bytes, 0, len - index);
|
|
|
+ msg.data = new String(bytes, CharsetUtil.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Message message = new Message();
|
|
|
+ message.setLen(123).setTarget("GPHM").setSource("SPHM").setData("HHHHHHGGGFFFJHGSJDGHJSGDH");
|
|
|
+ byte[] byteArray = Message.msgToBytes(message,200);
|
|
|
+ System.out.println(Arrays.toString(byteArray));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] msgToBytes(Message msg, int len) {
|
|
|
+ byte[] bytes = new byte[len + 4];
|
|
|
+ byte[] msgLenBytes = new byte[4];
|
|
|
+ msgLenBytes = ByteUtils.getBytes(msg.len, 4);
|
|
|
+ System.arraycopy(msgLenBytes, 0, bytes, 0, 4);
|
|
|
+ byte[] msgTypeBytes = new byte[2];
|
|
|
+ msgTypeBytes = ByteUtils.getBytes(msg.type, 2);
|
|
|
+ System.arraycopy(msgTypeBytes, 0, bytes, 4, 2);
|
|
|
+ byte[] msgTimeStampBytes = new byte[4];
|
|
|
+ msgTimeStampBytes = ByteUtils.getBytes(msg.timestamp, 4);
|
|
|
+ System.arraycopy(msgTimeStampBytes, 0, bytes, 6, 4);
|
|
|
+ byte[] msgReserverBytes = new byte[4];
|
|
|
+ msgReserverBytes = ByteUtils.getBytes(msg.reserver, 4);
|
|
|
+ System.arraycopy(msgReserverBytes, 0, bytes, 10, 4);
|
|
|
+ byte[] msgDataBytes = new byte[len];
|
|
|
+ msgDataBytes = msg.getData().getBytes();
|
|
|
+ System.arraycopy(msgDataBytes, 0, bytes, 14, msgDataBytes.length);
|
|
|
+ return bytes;
|
|
|
+ }
|
|
|
+
|
|
|
public Message(Object object) {
|
|
|
String str = object.toString();
|
|
|
JSONObject jsonObject = JSONObject.parseObject(str);
|
|
|
- type = Integer.valueOf(jsonObject.getString("type"));
|
|
|
- content = jsonObject.getString("content");
|
|
|
- id = jsonObject.getString("id");
|
|
|
+ type = Short.valueOf(jsonObject.getString("type"));
|
|
|
+ data = jsonObject.getString("content");
|
|
|
len = str.length();
|
|
|
}
|
|
|
|
|
@@ -71,7 +113,7 @@ public class Message {
|
|
|
" \"source\": " + source + ",\n" +
|
|
|
" \"target\": " + target + ",\n" +
|
|
|
" \"timestamp\": " + timestamp + ",\n" +
|
|
|
- " \"content\": " + content + "\n" +
|
|
|
+ " \"data\": " + data + "\n" +
|
|
|
"}";
|
|
|
}
|
|
|
}
|