Ejemplo n.º 1
0
 /// <summary>
 /// 异步接收数据线程
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void ProcessReceiveThreadCallback(object sender, UploadDataCompletedEventArgs e)
 {
     try {
         // 消息异常处理
         if (e.Error != null)
         {
             string errorState = e.Error.GetType().Name;
             if (errorState == "WebException")
             {
                 WebException    webExp   = (WebException)e.Error;
                 HttpWebResponse response = (HttpWebResponse)webExp.Response;
                 mStatueCodeQueue.Enqueue(response.StatusCode);
             }
             return;
         }
         // httpClient
         // WebClient mClient = (WebClient)sender;
         // 返回结果
         if (e.Result != null && e.Result.Length > 0)
         {
             byte[]   buffer = e.Result;
             PBPacket packet = new PBPacket();
             packet.Decoder(buffer);
             EnqueuePacket(packet);
         }
     } catch (Exception ex) {
         new Exception("not achieve " + ex.ToString());
     } finally {
         mCanSend = true;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 发送消息函数
        /// </summary>
        public override void ProcessSendPacket()
        {
            if (mSendQueue == null)
            {
                return;
            }
            while (mSendQueue.Count > 0)
            {
                if (!IsConnect())
                {
                    continue;
                }

                // 当前待发送消息包
                try {
                    PBPacket packet = mSendQueue.Dequeue();

                    byte[] buffer = packet.Encoder();
                    mSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, (IAsyncResult async) => {
                        try {
                            int sendLen = mSocket.EndSend(async);
                        } catch (Exception ex) {
                            new Exception("not achieve " + ex.ToString());
                        }
                    }, null);
                } catch (Exception ex) {
                    new Exception("not achieve " + ex.ToString());
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理发送消息包
        /// </summary>
        public override void ProcessSendPacket()
        {
            if (mSendQueue == null)
            {
                return;
            }
            try {
                if (!IsConnect())
                {
                    return;
                }
                mCanSend = false;
                PBPacket packet = mSendQueue.Dequeue();
                byte[]   bytes  = packet.Encoder();

                // 发送消息
                WebClient webClient = new WebClient();
                webClient.UploadDataCompleted += ProcessReceiveThreadCallback;
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                webClient.UploadDataAsync(new Uri(mReqUrl), mHttpMethod, bytes);

                // LogUtils.Log("Http Send: " + JsonConvert.SerializeObject(msg), LType.Normal);
            } catch (Exception ex) {
                new Exception("not achieve " + ex.ToString());
            }
        }
Ejemplo n.º 4
0
 public void ExecResult(PBPacket packet)
 {
     try {
         int id = packet.mHead.mMessageId;
         //mFacade.SendNotification(((MessageMapping)id).ToString(), packet.mBody);
     } catch (Exception ex) {
         Log.Error("Error: " + ex.Message);
     }
 }
Ejemplo n.º 5
0
 public void ExecResult(PBPacket packet)
 {
     try {
         int id = packet.mHead.mMessageId;
         //mFacade.SendNotification(((MessageMapping)id).ToString(), packet.mBody);
     } catch (Exception ex) {
         throw new Exception(ex.StackTrace);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg"></param>
        public override void SendMessage(PBBody pbbody)
        {
            PBPacket packet = PBUtils.NewPacket(pbbody);

            try {
                mSendQueue.Enqueue(packet);
            } catch (Exception ex) {
                new Exception(ex.ToString());
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 将消息包转为流
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static byte[] Packet2Buffer(PBPacket packet)
        {
            byte[] buf = new byte[PBConst.PB_HEAD_SIZE + packet.mHead.mBodyLen];

            byte[] headBuf = Head2Buffer(packet.mHead);
            byte[] bodyBuf = PBBody2Buffer(packet.mBody);

            Array.Copy(headBuf, 0, buf, 0, PBConst.PB_HEAD_SIZE);
            Array.Copy(bodyBuf, 0, buf, PBConst.PB_HEAD_SIZE, packet.mHead.mBodyLen);

            return(buf);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 生成消息包
        /// </summary>
        /// <param name="pbbody"></param>
        /// <returns></returns>
        public static PBPacket NewPacket(PBBody pbbody)
        {
            PBPacket packet = new PBPacket();
            PBHead   header = new PBHead();

            header.mMessageId = GetPacketId(pbbody.GetType());
            header.mBodyLen   = GetPBBodyLength(pbbody);
            header.mTimeStamp = DateTime.Now.Ticks;

            packet.mHead = header;
            packet.mBody = pbbody;

            return(packet);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 发送Http消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="key"></param>
        public override void SendMessage(PBBody msg, string key = null)
        {
            PBPacket packet = PBUtils.NewPacket(msg);

            if (string.IsNullOrEmpty(key))
            {
                return;
            }
            mReqUrl = mURL + key;
            try {
                mSendQueue.Enqueue(packet);
            } catch (Exception ex) {
                throw new Exception(ex.ToString());
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 处理返回消息包
 /// </summary>
 public override void ProcessBackPacket()
 {
     while (mRcevQueue.Count > 0)
     {
         PBPacket packet = null;
         lock (mLockObj) {
             packet = mRcevQueue.Dequeue();
         }
         if (packet != null)
         {
             NetworkManager.Instance.ExecResult(packet);
         }
         else
         {
         }
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// 将字节流转为消息包
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static PBPacket Buffer2Packet(PBPacket packet, byte[] bytes)
        {
            packet.mHead = Buffer2Head(bytes);

            // 获取包体字节流
            byte[] bodyBuf = new byte[packet.mHead.mBodyLen];
            Array.Copy(bytes, PBConst.PB_HEAD_SIZE, bodyBuf, 0, packet.mHead.mBodyLen);
            // Type pbType = PBMessageType.GetMessageType((MessageMapping)packet.mHead.mMessageId);
            Type pbType = null;//= PBMessageType.GetMessageType((MessageMapping)packet.mHead.mMessageId);

            if (pbType == null)
            {
                // LogUtils.LogError("未知PBMessageType,请检查proto是否已更新!");
            }
            packet.mBody = Buffer2PBBody(bodyBuf, pbType);
            return(packet);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 处理接收数据
        /// </summary>
        public virtual void ProcessBackPacket()
        {
            int count = 0;

            while (mRcevQueue.Count > 0)
            {
                PBPacket packet = null;
                lock (mLockObj) {
                    packet = mRcevQueue.Dequeue();
                }
                if (packet != null)
                {
                    NetworkManager.Instance.ExecResult(packet);
                }
                count++;
                if (count > NetworkConst.FRAME_MAX_PROCESS)
                {
                    break;
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 处理缓冲区中的数据包
        /// </summary>
        /// <param name="recvLen"></param>
        void TryParsePacket(int recvLen)
        {
            // 更新标识
            mReceivePos += recvLen;
            // 包头长度
            int headerSize = PBConst.PB_HEAD_SIZE;

            while (mReceivePos >= headerSize)
            {
                try {
                    // 获取到消息包体长度
                    int bodySize   = BitConverter.ToInt32(mRecvBuff, 0);
                    int packetSize = headerSize + bodySize;
                    // 检查是否满足一个包长度
                    if (mReceivePos >= packetSize)
                    {
                        PBPacket packet = new PBPacket();
                        packet.Decoder(mRecvBuff);
                        if (packet == null)
                        {
                            continue;
                        }
                        EnqueuePacket(packet);
                        // 接收缓冲区偏移处理
                        for (int i = 0; i < (mReceivePos - packetSize); i++)
                        {
                            mRecvBuff[i] = mRecvBuff[packetSize + i];
                        }
                        mReceivePos -= packetSize;
                    }
                    else
                    {
                        break;
                    }
                } catch (Exception ex) {
                    new Exception("not achieve " + ex.ToString());
                    break;
                }
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// 将包压入接收队列
 /// </summary>
 /// <param name="msg"></param>
 public virtual void EnqueuePacket(PBPacket msg)
 {
     lock (mLockObj) {
         mRcevQueue.Enqueue(msg);
     }
 }