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

前言 

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

NetShare:DataPacket(通用数据包)
using System;
using System.IO;
using System.Runtime.Serialization;

namespace NetShare
{
    /// 
    /// 通用数据包
    /// 
    [KnownType(typeof(PCDataPacket)), KnownType(typeof(PSDataPacket)), KnownType(typeof(ServerDataPacket))]
    [DataContract]
    public class DataPacket
    {
        /// 
        /// 当前数据发送端的IPEndPoint字符串内容(ip地址:端口号)
        /// 
        [DataMember]
        public string mLocalEndPointStr;

        /// 
        /// 当前数据发送端发送的其它内容
        /// 
        [DataMember]
        public string mContent;

        /// 
        /// 将当前数据包实例转换为字节数组
        /// 
        /// 字节数组
        public virtual byte[] ToBytes()
        {
            return Array.Empty();
        }

        /// 
        /// 将指定的字节数组转换为DataPacket(数据包)对象
        /// 
        /// 指定的字节数组
        /// DataPacket派生类类型
        /// DataPacket(数据包)对象或null
        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;
        }

        /// 
        /// 将指定的字节数组转换为DataPacket派生类(数据包)对象
        /// 
        /// DataPacket派生类类型
        /// 指定的字节数组
        /// DataPacket派生类(数据包)对象或null
        public static T ToObject(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(客户端数据包)
using System.Runtime.Serialization;
using System.IO;

namespace NetShare
{
    /// 
    /// 客户端数据包
    /// 
    [DataContract]
    public class ClientDataPacket : DataPacket
    {
        /// 
        /// 当前客户端的名称
        /// 
        [DataMember]
        public string mClientName;

        /// 
        /// 构造函数
        /// 
        public ClientDataPacket()
        {
            mLocalEndPointStr = string.Empty;
            mContent = string.Empty;
            mClientName = string.Empty;
        }

        /// 
        /// 将客户端数据包实例转换为字节数组
        /// 
        /// 字节数组
        public override byte[] ToBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(ClientDataPacket));
                serializer.WriteObject(ms, this);
                return ms.ToArray();
            }
        }

        /// 
        /// 客户端数据包的字符串内容
        /// 
        /// 字符串内容
        public override string ToString()
        {
            return $"[LocalEndPointStr:{mLocalEndPointStr},ClientName:{mClientName},Content:{mContent}]";
        }
    }
}
NetShare:ServerDataPacket(服务器数据包)
using System.IO;
using System.Runtime.Serialization;

namespace NetShare
{
    /// 
    /// 服务端数据包
    /// 
    [DataContract]
    public class ServerDataPacket : DataPacket
    {
        /// 
        /// 构造函数
        /// 
        public ServerDataPacket()
        {
            mLocalEndPointStr = string.Empty;
            mContent = string.Empty;
        }

        /// 
        /// 将服务端数据包实例转换为字节数组
        /// 
        /// 字节数组
        public override byte[] ToBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(ServerDataPacket));
                serializer.WriteObject(ms, this);
                return ms.ToArray();
            }
        }

        /// 
        /// 服务端数据包的字符串内容
        /// 
        /// 字符串内容
        public override string ToString()
        {
            return $"[LocalEndPointStr:{mLocalEndPointStr},Content:{mContent}]";
        }
    }
}
NetShare:PCDataPacket(私聊频道客户端数据包)
using System.IO;
using System.Runtime.Serialization;

namespace NetShare
{
    /// 
    /// 私聊频道客户端数据包
    /// 
    [DataContract]
    public class PCDataPacket : ClientDataPacket
    {
        /// 
        /// 目标客户端的名称
        /// 
        [DataMember]
        public string mDestinationClientName;

        /// 
        /// 构造函数
        /// 
        public PCDataPacket()
        {
            mDestinationClientName = string.Empty;
        }

        /// 
        /// 将私聊频道客户端数据包实例转换为字节数组
        /// 
        /// 字节数组
        public override byte[] ToBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(PCDataPacket));
                serializer.WriteObject(ms, this);
                return ms.ToArray();
            }
        }
    }
}
NetShare:PSDataPacket(私聊频道服务器数据包)
using System;
using System.IO;
using System.Runtime.Serialization;

namespace NetShare
{
    /// 
    /// 私聊频道服务端数据包
    /// 
    [DataContract]
    public class PSDataPacket : ServerDataPacket
    {
        /// 
        /// 已与服务器进行连接的客户端名称合集
        /// 
        [DataMember]
        public string[] mClientNames;

        /// 
        /// 构造函数
        /// 
        public PSDataPacket()
        {
            mClientNames = Array.Empty();
        }

        /// 
        /// 将私聊频道服务端数据包实例转换为字节数组
        /// 
        /// 字节数组
        public override byte[] ToBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(PSDataPacket));
                serializer.WriteObject(ms, this);
                return ms.ToArray();
            }
        }
    }
}

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

你可能感兴趣的:(网络通信,c#,网络,网络通信)