C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# 网络共享NetShare

C#实现网络通信共享库NetShare的使用示例

作者:我与岁月的森林

本文主要介绍了C#实现网络通信共享库NetShare,网络通信共享库NetShare用于保证客户端与服务器通信数据包的规范和统一,感兴趣的可以了解一下

前言 

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

NetShare:DataPacket(通用数据包)

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(客户端数据包)

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(服务器数据包)

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(私聊频道客户端数据包)

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(私聊频道服务器数据包)

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

到此这篇关于C#实现网络通信共享库NetShare的使用示例的文章就介绍到这了,更多相关C# 网络共享NetShare内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文