首页 > 代码库 > PacketHeader类(NetworkComms 2.3.1源码了解和学习)

PacketHeader类(NetworkComms 2.3.1源码了解和学习)

networkComms.net2.3.1开源版本,基于gpl V3协议。因为不能公开3.x版本的源码,所以基于此版本进行学习。3.X版本进行了诸多改进和Bug修复,使用方法上两者相差不大。/*请注意使用以下代码,需遵循GplV3协议*//// <summary>    /// PacketHeader 包含发送,接收,重建数据包的相关信息    /// </summary>    [ProtoContract]    public sealed class PacketHeader    {        [ProtoMember(1)]        Dictionary<PacketHeaderLongItems, long> longItems;        [ProtoMember(2)]        Dictionary<PacketHeaderStringItems, string> stringItems;                /// <summary>        /// Blank constructor required for deserialisation        /// </summary>#if iOS || ANDROID        public PacketHeader() { }#else        private PacketHeader() { }#endif                /// <summary>        /// 创建一个数据包头        /// </summary>        /// <param name="packetTypeStr">数据包类型.</param>        /// <param name="payloadPacketSize">数据包的大小,注意不是包包头的大小</param>        /// <param name="requestedReturnPacketTypeStr">返回的类型</param>        /// <param name="receiveConfirmationRequired">是否需要确认收到</param>        /// <param name="checkSumHash">检验和</param>        /// <param name="includeConstructionTime">是否包含创建时间</param>         // 这里说一下 数据包类型和 返回的类型   这个类型的作用在于定位,比如登陆时,我发送一个 User类,数据包类型我可以设定为"a01",然后在服务器的         // 处理程序中,我针对"a01"进行处理。a01可以换成其他的字符,只是个定位作用。         // 返回的类型,比如我发送一个 User类,数据包类型"a1",设定返回的类型"b1",然后在服务器的处理程序中,根据"a1"进行相应的处理,处理完成后发送一个         // 处理结果的消息给客户端,这个消息就需要设定类型为前面我们预设的"b1"类型,客户端一直在等待"b1"类型的消息,收到后,就可以进行相关的处理了                                 public PacketHeader(string packetTypeStr, long payloadPacketSize, string requestedReturnPacketTypeStr = null, bool receiveConfirmationRequired = false, string checkSumHash = null, bool includeConstructionTime = false)        {            longItems = new Dictionary<PacketHeaderLongItems, long>();            stringItems = new Dictionary<PacketHeaderStringItems, string>();            stringItems.Add(PacketHeaderStringItems.PacketType, packetTypeStr);            longItems.Add(PacketHeaderLongItems.PayloadPacketSize, payloadPacketSize);            if (payloadPacketSize < 0)                throw new Exception("payloadPacketSize can not be less than 0.");            if (requestedReturnPacketTypeStr != null)                stringItems.Add(PacketHeaderStringItems.RequestedReturnPacketType, requestedReturnPacketTypeStr);            if (receiveConfirmationRequired)                stringItems.Add(PacketHeaderStringItems.ReceiveConfirmationRequired, "");            if (checkSumHash != null)                stringItems.Add(PacketHeaderStringItems.CheckSumHash, checkSumHash);            if (includeConstructionTime)                longItems.Add(PacketHeaderLongItems.PacketCreationTime, DateTime.Now.Ticks);        }        internal PacketHeader(MemoryStream packetData, SendReceiveOptions sendReceiveOptions)        {            try            {                if (packetData =http://www.mamicode.com/= null) throw new ArgumentNullException("packetData", "Provided MemoryStream parameter cannot be null.");                if (sendReceiveOptions == null) throw new ArgumentNullException("sendReceiveOptions", "Provided SendReceiveOptions parameter cannot be null.");                if (packetData.Length == 0)                    throw new SerialisationException("Attempted to create packetHeader using 0 packetData bytes.");                //创建数据包包头                PacketHeader tempObject = sendReceiveOptions.DataSerializer.DeserialiseDataObject<PacketHeader>(packetData, sendReceiveOptions.DataProcessors, sendReceiveOptions.Options);                if (tempObject == null || !tempObject.longItems.ContainsKey(PacketHeaderLongItems.PayloadPacketSize) || !tempObject.stringItems.ContainsKey(PacketHeaderStringItems.PacketType))                    throw new SerialisationException("Something went wrong when trying to deserialise the packet header object");                else                {                    stringItems = new Dictionary<PacketHeaderStringItems, string>();                    foreach (var pair in tempObject.stringItems)                        stringItems.Add(pair.Key, String.Copy(pair.Value));                    longItems = new Dictionary<PacketHeaderLongItems, long>();                    foreach (var pair in tempObject.longItems)                        longItems.Add(pair.Key, pair.Value);                }            }            catch (Exception ex)            {                throw new SerialisationException("Error deserialising packetHeader. " + ex.ToString());            }        }        #region Get & Set        /// <summary>        /// 数据包的大小        /// </summary>        public int PayloadPacketSize        {            get { return (int)longItems[PacketHeaderLongItems.PayloadPacketSize]; }            //private set { longItems[PacketHeaderLongItems.PayloadPacketSize] = value; }        }        /// <summary>        /// 数据包的类型        /// </summary>        public string PacketType        {            get { return stringItems[PacketHeaderStringItems.PacketType]; }            //private set { stringItems[PacketHeaderStringItems.PacketType] = value; }        }       //检查某个选项是否已被设定        public bool ContainsOption(PacketHeaderStringItems option)        {            return stringItems.ContainsKey(option);        }      //检查某个选项是否已被设定        public bool ContainsOption(PacketHeaderLongItems option)        {            return longItems.ContainsKey(option);        }          //在英文网站上购买 九折折扣代码: NCDN_PRCLW

//淘宝正版销售 http://shop115882994.taobao.com/ 八折
public long GetOption(PacketHeaderLongItems option) { return longItems[option]; } public string GetOption(PacketHeaderStringItems options) { return stringItems[options]; } public void SetOption(PacketHeaderLongItems option, long Value) { longItems[option] = Value; } /// <summary> /// Set a string option with the provided value. /// </summary> /// <param name="option">The option to set</param> /// <param name="Value">The option value</param> public void SetOption(PacketHeaderStringItems option, string Value) { stringItems[option] = Value; } #endregion } http://www.cnblogs.com/networkcommshttp://www.networkcoms.cn 编辑

 

PacketHeader类(NetworkComms 2.3.1源码了解和学习)