前言
网络通信共享库NetShare用于保证客户端与服务器通信数据包的规范和统一,客户端与服务器共同使用本库,提升数据包序列化和反序列化的准确性和安全性,并且用于满足不同的通信数据需求。
NetShare:DataPacket(通用数据包)
cs
using System;
using System.IO;
using System.Runtime.Serialization;
namespace NetShare
{
/// <summary>
/// 通用数据包
/// </summary>
[KnownType(typeof(PCDataPacket)), KnownType(typeof(PSDataPacket)), KnownType(typeof(ServerDataPacket))]
[DataContract]
public class DataPacket
{
/// <summary>
/// 当前数据发送端的IPEndPoint字符串内容(ip地址:端口号)
/// </summary>
[DataMember]
public string mLocalEndPointStr;
/// <summary>
/// 当前数据发送端发送的其它内容
/// </summary>
[DataMember]
public string mContent;
/// <summary>
/// 将当前数据包实例转换为字节数组
/// </summary>
/// <returns>字节数组</returns>
public virtual byte[] ToBytes()
{
return Array.Empty<byte>();
}
/// <summary>
/// 将指定的字节数组转换为DataPacket(数据包)对象
/// </summary>
/// <param name="bytes">指定的字节数组</param>
/// <param name="type">DataPacket派生类类型</param>
/// <returns>DataPacket(数据包)对象或null</returns>
public static DataPacket ToObject(byte[] bytes, Type type)
{
if (bytes?.Length > 0 && type != null && type.IsAssignableFrom(typeof(DataPacket)))
{
using (MemoryStream ms = new MemoryStream(bytes))
{
DataContractSerializer serializer = new DataContractSerializer(type);
object obj = serializer.ReadObject(ms);
if (obj != null && obj.GetType().Equals(type)) return (DataPacket)obj;
};
}
return null;
}
/// <summary>
/// 将指定的字节数组转换为DataPacket派生类(数据包)对象
/// </summary>
/// <typeparam name="T">DataPacket派生类类型</typeparam>
/// <param name="bytes">指定的字节数组</param>
/// <returns>DataPacket派生类(数据包)对象或null</returns>
public static T ToObject<T>(byte[] bytes) where T : DataPacket
{
if (bytes?.Length > 0)
{
using (MemoryStream ms = new MemoryStream(bytes))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
object obj = serializer.ReadObject(ms);
if (obj != null && obj is T t) return t;
};
}
return null;
}
}
}
NetShare:ClientDataPacket(客户端数据包)
cs
using System.Runtime.Serialization;
using System.IO;
namespace NetShare
{
/// <summary>
/// 客户端数据包
/// </summary>
[DataContract]
public class ClientDataPacket : DataPacket
{
/// <summary>
/// 当前客户端的名称
/// </summary>
[DataMember]
public string mClientName;
/// <summary>
/// 构造函数
/// </summary>
public ClientDataPacket()
{
mLocalEndPointStr = string.Empty;
mContent = string.Empty;
mClientName = string.Empty;
}
/// <summary>
/// 将客户端数据包实例转换为字节数组
/// </summary>
/// <returns>字节数组</returns>
public override byte[] ToBytes()
{
using (MemoryStream ms = new MemoryStream())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(ClientDataPacket));
serializer.WriteObject(ms, this);
return ms.ToArray();
}
}
/// <summary>
/// 客户端数据包的字符串内容
/// </summary>
/// <returns>字符串内容</returns>
public override string ToString()
{
return $"[LocalEndPointStr:{mLocalEndPointStr},ClientName:{mClientName},Content:{mContent}]";
}
}
}
NetShare:ServerDataPacket(服务器数据包)
cs
using System.IO;
using System.Runtime.Serialization;
namespace NetShare
{
/// <summary>
/// 服务端数据包
/// </summary>
[DataContract]
public class ServerDataPacket : DataPacket
{
/// <summary>
/// 构造函数
/// </summary>
public ServerDataPacket()
{
mLocalEndPointStr = string.Empty;
mContent = string.Empty;
}
/// <summary>
/// 将服务端数据包实例转换为字节数组
/// </summary>
/// <returns>字节数组</returns>
public override byte[] ToBytes()
{
using (MemoryStream ms = new MemoryStream())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(ServerDataPacket));
serializer.WriteObject(ms, this);
return ms.ToArray();
}
}
/// <summary>
/// 服务端数据包的字符串内容
/// </summary>
/// <returns>字符串内容</returns>
public override string ToString()
{
return $"[LocalEndPointStr:{mLocalEndPointStr},Content:{mContent}]";
}
}
}
NetShare:PCDataPacket(私聊频道客户端数据包)
cs
using System.IO;
using System.Runtime.Serialization;
namespace NetShare
{
/// <summary>
/// 私聊频道客户端数据包
/// </summary>
[DataContract]
public class PCDataPacket : ClientDataPacket
{
/// <summary>
/// 目标客户端的名称
/// </summary>
[DataMember]
public string mDestinationClientName;
/// <summary>
/// 构造函数
/// </summary>
public PCDataPacket()
{
mDestinationClientName = string.Empty;
}
/// <summary>
/// 将私聊频道客户端数据包实例转换为字节数组
/// </summary>
/// <returns>字节数组</returns>
public override byte[] ToBytes()
{
using (MemoryStream ms = new MemoryStream())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(PCDataPacket));
serializer.WriteObject(ms, this);
return ms.ToArray();
}
}
}
}
NetShare:PSDataPacket(私聊频道服务器数据包)
cs
using System;
using System.IO;
using System.Runtime.Serialization;
namespace NetShare
{
/// <summary>
/// 私聊频道服务端数据包
/// </summary>
[DataContract]
public class PSDataPacket : ServerDataPacket
{
/// <summary>
/// 已与服务器进行连接的客户端名称合集
/// </summary>
[DataMember]
public string[] mClientNames;
/// <summary>
/// 构造函数
/// </summary>
public PSDataPacket()
{
mClientNames = Array.Empty<string>();
}
/// <summary>
/// 将私聊频道服务端数据包实例转换为字节数组
/// </summary>
/// <returns>字节数组</returns>
public override byte[] ToBytes()
{
using (MemoryStream ms = new MemoryStream())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(PSDataPacket));
serializer.WriteObject(ms, this);
return ms.ToArray();
}
}
}
}
如果这篇文章对你有帮助,请给作者点个赞吧!