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();
            }
        }
    }
}

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

相关推荐
△曉風殘月〆1 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
热爱跑步的恒川2 小时前
【论文复现】基于图卷积网络的轻量化推荐模型
网络·人工智能·开源·aigc·ai编程
云飞云共享云桌面3 小时前
8位机械工程师如何共享一台图形工作站算力?
linux·服务器·网络
逐·風3 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
音徽编程5 小时前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
m0_656974746 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo6 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
幺零九零零6 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
23zhgjx-NanKon7 小时前
华为eNSP:QinQ
网络·安全·华为
23zhgjx-NanKon7 小时前
华为eNSP:mux-vlan
网络·安全·华为