예제 #1
0
        /// <summary>
        /// 构造消息包
        /// </summary>
        /// <param name="packet">消息包</param>
        /// <param name="packet_size">消息包长度</param>
        public Packet(byte[] packet, uint packet_size)
        {
            if (packet == null || packet.Length < 16 || packet.Length != packet_size)
            {
                return;
            }
            this.m_PacketBuffer = packet;
            this.m_uiPacketLen  = packet_size;
            uint headlen = Packet_Head.HEAD_LENGTH, bodylen = this.m_uiPacketLen - Packet_Head.HEAD_LENGTH;

            byte[] head = new byte[headlen];
            System.Array.Copy(this.m_PacketBuffer, 0, head, 0, headlen);
            this.m_Head = new Packet_Head(head, headlen);

            byte[] body = new byte[bodylen];
            if (this.m_uiPacketLen > headlen)
            {
                System.Array.Copy(this.m_PacketBuffer, headlen, body, 0, bodylen);
            }
            this.m_Body = new Packet_Body(body, bodylen);

            if (this.m_Head.IsValidHead && this.m_Body.m_Status == CEnum.Body_Status.MSG_STRUCT_OK)
            {
                this.IsValidPacket = true;
            }
        }
예제 #2
0
 /// <summary>
 /// 构造消息包
 /// </summary>
 /// <param name="head">消息头</param>
 /// <param name="body">消息体</param>
 public Packet(Packet_Head head, Packet_Body body)
 {
     this.m_Head = head;
     this.m_Body = body;
     if (this.m_Body.m_Status != CEnum.Body_Status.MSG_STRUCT_OK || !this.m_Head.IsValidHead)
     {
         return;
     }
     byte[] headbuffer = this.m_Head.ToByteArray();
     byte[] bodybuffer = this.m_Body.ToByteArray();
     this.m_PacketBuffer = new byte[headbuffer.Length + bodybuffer.Length];
     headbuffer.CopyTo(this.m_PacketBuffer, 0);
     bodybuffer.CopyTo(this.m_PacketBuffer, headbuffer.Length);
     this.IsValidPacket = true;
 }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="eKey">事件类型</param>
        /// <param name="Msg_Category">消息类型</param>
        /// <param name="tBody">消息内容</param>
        /// <returns>客户端消息</returns>
        public CSocketData SocketSend(CEnum.ServiceKey eKey, CEnum.Msg_Category eCategory, CEnum.Message_Body[] tBody)
        {
            try
            {
                int             iMsgLength = 0;                         //消息长度
                byte[]          bMsg       = null;
                TLV_Structure[] tMsg       = null;

                if (tBody != null)
                {
                    iMsgLength = tBody.GetLength(0);
                    tMsg       = new TLV_Structure[iMsgLength];
                    for (int i = 0; i < iMsgLength; i++)
                    {
                        bMsg    = TLV_Structure.ValueToByteArray(tBody[i].eTag, tBody[i].oContent);
                        tMsg[i] = new TLV_Structure(tBody[i].eName, (uint)bMsg.GetLength(0), bMsg);
                    }
                }
                else
                {
                    string strSendMsg = "Get List";
                    iMsgLength = 1;
                    tMsg       = new TLV_Structure[1];
                    bMsg       = System.Text.Encoding.Default.GetBytes(strSendMsg);
                    tMsg[0]    = new TLV_Structure(CEnum.TagName.Connect_Msg, (uint)bMsg.GetLength(0), bMsg);
                }

                Packet_Body body = new Packet_Body(tMsg, (uint)iMsgLength);
                Packet_Head head = new Packet_Head(SeqId_Generator.Instance().GetNewSeqID(), eCategory, eKey, body.m_uiBodyLen);

                CSocketData m_Return = new CSocketData(new Packet(head, body));
                return(m_Return);
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "构造Socket数据包失败!";
                tLogData.strException = e.Message;

                throw new Exception("发送数据异常!");
            }
        }