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

前言

网络通信共享库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();
            }
        }
    }
}

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

相关推荐
_微风轻起11 分钟前
linux下网络编程socket&select&epoll的底层实现原理
linux·网络
坐井观老天1 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
vortex52 小时前
Burp与其他安全工具联动及代理设置教程
网络·安全
xserver23 小时前
ensp 基于端口安全的财务部网络组建
网络·安全
pchmi3 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉
从后端到QT4 小时前
boost asio 异步服务器
服务器·网络·tcp/ip
Blankspace学4 小时前
Wireshark软件下载安装及基础
网络·学习·测试工具·网络安全·wireshark
墨水\\4 小时前
Ansible部署及基础模块
服务器·网络·ansible
手心里的白日梦4 小时前
网络计算器的实现:TCP、守护进程、Json、序列化与反序列化
网络·tcp/ip·json
不吃鱼的羊4 小时前
Excel生成DBC脚本源文件
服务器·网络·excel