예제 #1
0
        /// <summary>
        /// 处理网络操作线程
        /// </summary>
        private void WebThreadHandle()
        {
            while (m_threadRunFlag)
            {
                if (!m_netRunFlag)
                {
                    continue;
                }

                HttpPacket sendPacket = null;
                lock (m_sendQueue)
                {
                    if (m_sendQueue.Count > 0)
                    {
                        sendPacket = m_sendQueue.Dequeue();
                    }
                }

                if (null == sendPacket)
                {
                    Thread.Sleep(c_ThreadSleepTime);
                    continue;
                }

                try
                {
                    byte[] bytes   = NetEnCoder.Encode(sendPacket);
                    byte[] byteRes = m_webClient.UploadData(NetHttpManager.Instance.GetUrl(), c_httpMethod, bytes);

                    if (null != byteRes)
                    {
                        //解析http返回包
                        //目前解析 太浪费内存、使用缓冲区
                        int resLength = byteRes.Length;
                        int offset    = 0;
                        int nLength   = NetEnCoder.DecodeInt(byteRes, ref offset);
                        resLength -= offset;

                        if (resLength >= nLength)
                        {
                            uint uCode = NetEnCoder.DecodeUInt(byteRes, ref offset);

                            int        nCount     = nLength - NetEnCoder.GetIntLength();
                            object     msg        = PBEnCoder.Decode(uCode, byteRes, offset, nCount);
                            HttpPacket recvPacket = new HttpPacket(uCode, nCount);
                            recvPacket.handler = sendPacket.handler;

                            lock (m_recvQueueLocker)
                            {
                                m_recvQueue.Enqueue(recvPacket);
                            }
                        }
                    }
                }
                catch (WebException e)
                {
                    LoggerHelper.Except(e);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 切包
        /// </summary>
        private void SplitPackets()
        {
            try
            {
                int offset = 0; //recvBuffer的整体读取游标
                while (m_recvUnreadBytes > NetEnCoder.GetIntLength())
                {
                    try
                    {
                        //备注:消息协议=长度(nCode和body所占字节长度) + sessionId +  nCode + body
                        int nLength = NetEnCoder.DecodeInt(m_recvBuffer, ref offset);//nocde + body 所占字节数
                        m_recvUnreadBytes -= offset;

                        if (m_recvUnreadBytes >= nLength)
                        {
                            uint uSession = NetEnCoder.DecodeUInt(m_recvBuffer, ref offset);

                            uint uCode = NetEnCoder.DecodeUInt(m_recvBuffer, ref offset);

                            int    nCount = nLength - 2 * NetEnCoder.GetIntLength();
                            object msg    = PBEnCoder.Decode(uCode, m_recvBuffer, offset, nCount);

                            TcpPacket packet = new TcpPacket(uSession, uCode, msg);
                            lock (m_recvQueueLocker)
                            {
                                LoggerHelper.Log(packet.ToString());
                                m_recvQueue.Enqueue(packet);
                            }

                            offset            += nCount;
                            m_recvUnreadBytes -= nLength;
                        }
                        else
                        {
                            m_recvUnreadBytes += offset;
                            offset            -= NetEnCoder.GetIntLength();
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        LoggerHelper.Except(e);
                        break;
                    }
                }

                // 整理 RecvBuffer, 将buffer 内容前移
                Buffer.BlockCopy(m_recvBuffer, offset, m_recvBuffer, 0, m_recvUnreadBytes);
            }
            catch (Exception e)
            {
                LoggerHelper.Except(e);
                LoggerHelper.Critical("SplitPackets error.");
                Close();
            }
        }
예제 #3
0
        private void ReadData()
        {
            byte[] data = NetEncode.Decode(ref receiveCache);
            if (null != data)
            {
                int    offset   = 0;
                uint   uSession = NetEnCoder.DecodeUInt(data, ref offset);
                uint   uCode    = NetEnCoder.DecodeUInt(data, ref offset);
                int    nCount   = data.Length - offset;
                object msg      = PBEnCoder.Decode(uCode, data, offset, nCount);

                if (0 != uCode)
                {
                    HandleMsg(uSession, uCode, msg);
                }

                ReadData();
            }
            else
            {
                isReceiving = false;
            }
        }