• C#实现网络通信共享库NetShare


    前言 

            网络通信共享库NetShare用于保证客户端与服务器通信数据包的规范和统一,客户端与服务器共同使用本库,提升数据包序列化和反序列化的准确性和安全性,并且用于满足不同的通信数据需求。

    NetShare:DataPacket(通用数据包)
    1. using System;
    2. using System.IO;
    3. using System.Runtime.Serialization;
    4. namespace NetShare
    5. {
    6. ///
    7. /// 通用数据包
    8. ///
    9. [KnownType(typeof(PCDataPacket)), KnownType(typeof(PSDataPacket)), KnownType(typeof(ServerDataPacket))]
    10. [DataContract]
    11. public class DataPacket
    12. {
    13. ///
    14. /// 当前数据发送端的IPEndPoint字符串内容(ip地址:端口号)
    15. ///
    16. [DataMember]
    17. public string mLocalEndPointStr;
    18. ///
    19. /// 当前数据发送端发送的其它内容
    20. ///
    21. [DataMember]
    22. public string mContent;
    23. ///
    24. /// 将当前数据包实例转换为字节数组
    25. ///
    26. /// 字节数组
    27. public virtual byte[] ToBytes()
    28. {
    29. return Array.Empty<byte>();
    30. }
    31. ///
    32. /// 将指定的字节数组转换为DataPacket(数据包)对象
    33. ///
    34. /// 指定的字节数组
    35. /// DataPacket派生类类型
    36. /// DataPacket(数据包)对象或null
    37. public static DataPacket ToObject(byte[] bytes, Type type)
    38. {
    39. if (bytes?.Length > 0 && type != null && type.IsAssignableFrom(typeof(DataPacket)))
    40. {
    41. using (MemoryStream ms = new MemoryStream(bytes))
    42. {
    43. DataContractSerializer serializer = new DataContractSerializer(type);
    44. object obj = serializer.ReadObject(ms);
    45. if (obj != null && obj.GetType().Equals(type)) return (DataPacket)obj;
    46. };
    47. }
    48. return null;
    49. }
    50. ///
    51. /// 将指定的字节数组转换为DataPacket派生类(数据包)对象
    52. ///
    53. /// DataPacket派生类类型
    54. /// 指定的字节数组
    55. /// DataPacket派生类(数据包)对象或null
    56. public static T ToObject<T>(byte[] bytes) where T : DataPacket
    57. {
    58. if (bytes?.Length > 0)
    59. {
    60. using (MemoryStream ms = new MemoryStream(bytes))
    61. {
    62. DataContractSerializer serializer = new DataContractSerializer(typeof(T));
    63. object obj = serializer.ReadObject(ms);
    64. if (obj != null && obj is T t) return t;
    65. };
    66. }
    67. return null;
    68. }
    69. }
    70. }
    NetShare:ClientDataPacket(客户端数据包)
    1. using System.Runtime.Serialization;
    2. using System.IO;
    3. namespace NetShare
    4. {
    5. ///
    6. /// 客户端数据包
    7. ///
    8. [DataContract]
    9. public class ClientDataPacket : DataPacket
    10. {
    11. ///
    12. /// 当前客户端的名称
    13. ///
    14. [DataMember]
    15. public string mClientName;
    16. ///
    17. /// 构造函数
    18. ///
    19. public ClientDataPacket()
    20. {
    21. mLocalEndPointStr = string.Empty;
    22. mContent = string.Empty;
    23. mClientName = string.Empty;
    24. }
    25. ///
    26. /// 将客户端数据包实例转换为字节数组
    27. ///
    28. /// 字节数组
    29. public override byte[] ToBytes()
    30. {
    31. using (MemoryStream ms = new MemoryStream())
    32. {
    33. DataContractSerializer serializer = new DataContractSerializer(typeof(ClientDataPacket));
    34. serializer.WriteObject(ms, this);
    35. return ms.ToArray();
    36. }
    37. }
    38. ///
    39. /// 客户端数据包的字符串内容
    40. ///
    41. /// 字符串内容
    42. public override string ToString()
    43. {
    44. return $"[LocalEndPointStr:{mLocalEndPointStr},ClientName:{mClientName},Content:{mContent}]";
    45. }
    46. }
    47. }
    NetShare:ServerDataPacket(服务器数据包)
    1. using System.IO;
    2. using System.Runtime.Serialization;
    3. namespace NetShare
    4. {
    5. ///
    6. /// 服务端数据包
    7. ///
    8. [DataContract]
    9. public class ServerDataPacket : DataPacket
    10. {
    11. ///
    12. /// 构造函数
    13. ///
    14. public ServerDataPacket()
    15. {
    16. mLocalEndPointStr = string.Empty;
    17. mContent = string.Empty;
    18. }
    19. ///
    20. /// 将服务端数据包实例转换为字节数组
    21. ///
    22. /// 字节数组
    23. public override byte[] ToBytes()
    24. {
    25. using (MemoryStream ms = new MemoryStream())
    26. {
    27. DataContractSerializer serializer = new DataContractSerializer(typeof(ServerDataPacket));
    28. serializer.WriteObject(ms, this);
    29. return ms.ToArray();
    30. }
    31. }
    32. ///
    33. /// 服务端数据包的字符串内容
    34. ///
    35. /// 字符串内容
    36. public override string ToString()
    37. {
    38. return $"[LocalEndPointStr:{mLocalEndPointStr},Content:{mContent}]";
    39. }
    40. }
    41. }
    NetShare:PCDataPacket(私聊频道客户端数据包)
    1. using System.IO;
    2. using System.Runtime.Serialization;
    3. namespace NetShare
    4. {
    5. ///
    6. /// 私聊频道客户端数据包
    7. ///
    8. [DataContract]
    9. public class PCDataPacket : ClientDataPacket
    10. {
    11. ///
    12. /// 目标客户端的名称
    13. ///
    14. [DataMember]
    15. public string mDestinationClientName;
    16. ///
    17. /// 构造函数
    18. ///
    19. public PCDataPacket()
    20. {
    21. mDestinationClientName = string.Empty;
    22. }
    23. ///
    24. /// 将私聊频道客户端数据包实例转换为字节数组
    25. ///
    26. /// 字节数组
    27. public override byte[] ToBytes()
    28. {
    29. using (MemoryStream ms = new MemoryStream())
    30. {
    31. DataContractSerializer serializer = new DataContractSerializer(typeof(PCDataPacket));
    32. serializer.WriteObject(ms, this);
    33. return ms.ToArray();
    34. }
    35. }
    36. }
    37. }
    NetShare:PSDataPacket(私聊频道服务器数据包)
    1. using System;
    2. using System.IO;
    3. using System.Runtime.Serialization;
    4. namespace NetShare
    5. {
    6. ///
    7. /// 私聊频道服务端数据包
    8. ///
    9. [DataContract]
    10. public class PSDataPacket : ServerDataPacket
    11. {
    12. ///
    13. /// 已与服务器进行连接的客户端名称合集
    14. ///
    15. [DataMember]
    16. public string[] mClientNames;
    17. ///
    18. /// 构造函数
    19. ///
    20. public PSDataPacket()
    21. {
    22. mClientNames = Array.Empty<string>();
    23. }
    24. ///
    25. /// 将私聊频道服务端数据包实例转换为字节数组
    26. ///
    27. /// 字节数组
    28. public override byte[] ToBytes()
    29. {
    30. using (MemoryStream ms = new MemoryStream())
    31. {
    32. DataContractSerializer serializer = new DataContractSerializer(typeof(PSDataPacket));
    33. serializer.WriteObject(ms, this);
    34. return ms.ToArray();
    35. }
    36. }
    37. }
    38. }

    如果这篇文章对你有帮助,请给作者点个赞吧!

  • 相关阅读:
    SQL sever中表管理
    2022最新IntellJ IDEA的mall开发部署文档
    MySQL-InnoDB索引详解
    Linux线程同步(下)
    论文笔记 《FAST-LIO2: Fast Direct LiDAR-inertial Odometry》及 激光SLAM综述
    私人服务器可以干嘛
    【python】(十五)python内置库——正则表达式re
    教程|fNIRS数据处理工具包Homer2下载与安装
    基于关系抽取的相似度计算
    Midway.js打通WebSocket前后端监听通道
  • 原文地址:https://blog.csdn.net/hgf1037882434/article/details/134541554