当前位置: 首页 > web >正文

C# 实现TCP/IP通信协议——Message结构设计

(1)通信Message包括报头+消息体+结束符;

(2)报头包含报文Title、发送时间、报文类型,SequenceNo等数据。

(3)内容采用明文,使用空格补足空缺位置。 

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LearnAuto
{public enum MessageType{Heartbeat = '0',       // 心跳报文Application = 'A',     // 应用报文Response = 'C'         // 应答报文}    public class MessageHeader{public byte[] MessageID { get; set; } = new byte[5]; public DateTime DateTime { get; set; }public int Length { get; set; }      // 报文长度 = 报文头长度+报文体长度+结束符长度public MessageType Type { get; set; } // 类型public int SequenceNo { get; set; } // 报文序号public byte[] Reserved { get; set; } = new byte[10]; // 备用public static readonly int HEADER_LENGTH = 43;public static readonly int DATETIME_BYTE_WIDTH = 14;public static readonly int LENGTH_BYTE_WIDTH = 4;public static readonly int SEQUENCE_NO_BYTE_WIDTH = 8;public readonly static string MESSAGE_ID = "HELLO"; // 报文IDpublic void UpdateDateTime(){DateTime = DateTime.Now;}public int BodyLength(){return Length - HEADER_LENGTH - 1;}public static DateTime BytesToDateTime(byte[] bytes){if (bytes == null || bytes.Length != 14)throw new ArgumentException("Byte array must be exactly 8 bytes long.");// 1. 将 byte[8] 转为字符串(假设是 ASCII 编码)string dateTimeStr = Encoding.ASCII.GetString(bytes);// 2. 使用 DateTime.ParseExact 解析格式 "yyyyMMddHHmmss"return DateTime.ParseExact(dateTimeStr, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);}public static int BytesToInt(byte[] bytes){// 2. 转为字符串(假设是 ASCII 编码)string str = Encoding.ASCII.GetString(bytes);// 3. 去除左侧空格str = str.TrimStart();// 4. 转为 int(如果字符串为空或无效,会抛出异常)return int.Parse(str);}public static MessageHeader FromBytes(byte[] data){// 实现从字节数组解析报文using (var ms = new MemoryStream(data))using (var reader = new BinaryReader(ms)){varHeader = new MessageHeader{MessageID = reader.ReadBytes(5),DateTime = BytesToDateTime(reader.ReadBytes(DATETIME_BYTE_WIDTH)),Length = BytesToInt(reader.ReadBytes(LENGTH_BYTE_WIDTH)),Type = (MessageType)reader.ReadByte(),SequenceNo = BytesToInt(reader.ReadBytes(SEQUENCE_NO_BYTE_WIDTH))};return Header;}}// 实现ToBytes方法public byte[] ToBytes(){// 实现报文头序列化为字节数组using (var ms = new MemoryStream())using (var writer = new BinaryWriter(ms)){writer.Write(MessageID);writer.Write(Encoding.ASCII.GetBytes(DateTime.ToString("yyyyMMddHHmmss")));writer.Write(Encoding.ASCII.GetBytes(Length.ToString().PadLeft(4, ' ')));writer.Write((byte)Type);writer.Write(Encoding.ASCII.GetBytes(SequenceNo.ToString().PadLeft(8, ' ')));writer.Write(Reserved);return ms.ToArray();}}}public class MessageBody{public static readonly int SN_LENGTH = 9;public static readonly int MAX_FEATURE_COUNT = 45;public static readonly int FEATURE_BYTE_WIDTH = 8;public static readonly int RESERVED_LENGTH = 16;public static readonly int MSG_BODY_LENGTH = SN_LENGTH + 1 + MAX_FEATURE_COUNT * (FEATURE_BYTE_WIDTH + 1) + RESERVED_LENGTH;public byte[] SN { get; set; } = new byte[SN_LENGTH]; public List<byte[]> FeatureValues = new List<byte[]>(MAX_FEATURE_COUNT); // 特征尺寸,每个尺寸8个字节public byte[] FeatureOK { get; set; } = new byte[MAX_FEATURE_COUNT]; public byte[] Reserved { get; set; } = new byte[RESERVED_LENGTH]; // 备用private static Dictionary<string, int> _dicFeatures = new Dictionary<string, int>
{{"x", 0 },{"xx", 1 },{"xxx", 2 },{"xxxx", 3 },{"xxxxx", 4 }
};public MessageBody(int SN, ref List<(string, double, bool)> result)
{SN = Encoding.ASCII.GetBytes(SN.ToString().PadLeft(SN_LENGTH, ' '));for (int i = 0; i < MAX_FEATURE_COUNT; i++){FeatureValues.Add(new byte[FEATURE_BYTE_WIDTH]);for (int j = 0; j < FEATURE_BYTE_WIDTH; j++){FeatureValues[i][j] = 32; // 空格的ASCII码}FeatureOK[i] = (byte)'0'; // 初始化为0}for (int i = 0; i < RESERVED_LENGTH; i++){Reserved[i] = 32; // 空格的ASCII码}for (int i = 0; i < result.Count; i++){if (SetFeatureValue(result[i].Item1, result[i].Item2, result[i].Item3)){if (result[i].Item3 == false) {IsOK = (byte)'2'; break;}}}}private bool SetFeatureValue( string item, double value, bool isOk){int index = 0;if (_dicFeatures.TryGetValue(item, out int featureIndex)){index = featureIndex;FeatureValues[index] = Encoding.ASCII.GetBytes(value.ToString("F3").PadLeft(8, ' '));FeatureOK[index] = (byte)(isOk ? '1' : '2'); return true;}else{Log.Error("Invalid feature name: " + item);return false;}            }// 实现ToBytes方法public byte[] ToBytes(){// 实现报文头序列化为字节数组using (var ms = new MemoryStream())using (var writer = new BinaryWriter(ms)){writer.Write(SN);foreach (byte[] feature in FeatureValues){writer.Write(feature);}writer.Write(FeatureOK);writer.Write(Reserved);return ms.ToArray();}}}// 报文类public class NetworkMessage{public MessageHeader Header { get; set; }public byte[] Body { get; set; }public static readonly int EXT_LENGTH = 1;public static readonly byte EXT_CHAR = 0x03;public static readonly int MIN_LENGTH = MessageHeader.HEADER_LENGTH + EXT_LENGTH;public void UpdateDateTime(){Header.UpdateDateTime();}public byte[] ToBytes(){// 实现报文序列化为字节数组using (var ms = new MemoryStream())using (var writer = new BinaryWriter(ms)){writer.Write(Header.ToBytes());writer.Write(Body);writer.Write(NetworkMessage.EXT_CHAR); // 结束符return ms.ToArray();}}public static NetworkMessage FromBytes(byte[] data){// 实现从字节数组解析报文using (var ms = new MemoryStream(data))using (var reader = new BinaryReader(ms)){var msg = new NetworkMessage();msg.Header = MessageHeader.FromBytes(reader.ReadBytes(MessageHeader.HEADER_LENGTH));msg.Body = reader.ReadBytes(msg.Header.Length);return msg;}}}
}

http://www.xdnf.cn/news/1085.html

相关文章:

  • C# AppContext.BaseDirectory 应用程序的启动目录
  • Smart AI:在AI浪潮中崛起的智能NFT生态革命者
  • 【高并发内存池】从零到一的项目之高并发内存池整体框架设计及thread cache设计
  • 晶振详解:原理、作用、种类、应用与选型要点
  • Scribe: 一个非常方便的操作文档编写工具
  • 爬虫(requests库,logging库)
  • Linux ssh免密登陆设置
  • 【ECharts】ECharts曲线图节点点击事件实现
  • React18+ 项目搭建-从初始化、技术选型到开发部署的全流程规划
  • ProxySQL 性能调优实战案例
  • npm的基本使用安装所有包,安装删除指定版本的包,配置命名别名
  • 遨游通讯发布国产化旗舰三防手机AORO AU1:以自主可控重塑工业安全
  • 基于 Vue 的Tiptap 富文本编辑器使用指南
  • 【MCP Node.js SDK 全栈进阶指南】中级篇(4):MCP错误处理与日志系统
  • 【MCP Node.js SDK 全栈进阶指南】初级篇(3):MCP资源开发基础
  • TextCNN 模型文本分类实战:深度学习在自然语言处理中的应用
  • 宏碁笔记本电脑怎样开启/关闭触摸板
  • 高并发抢券系统设计与落地实现详解
  • 【git】subtree拆分大的git库到多个独立git库
  • @Configuration注解对应实现implements WebMvcConfigurer的配置不生效问题。
  • Java实例化对象都有几种方式
  • React 单一职责原则:优化组件设计与提高可维护性
  • 马浩棋:产通链CT-Chain 破局不动产 RWA,引领数智金融新变革
  • 程序生成随机数
  • 什么是API
  • 数智读书笔记系列030《曲折的职业道路:在终身工作时代找准定位》与《做自己的教练:战胜工作挑战掌控职业生涯》
  • 离线-DataX
  • 【AI微信小程序开发】大转盘小程序项目代码:自设转盘选项和概率(含完整前端+后端代码)
  • 刷题之路:C++ 解题分享与技术总结
  • Yocto项目实战教程-第7章定制镜像菜谱与内核菜谱-7.2小节-定制应用程序