示例#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
        public void HandleMsg(uint uSession, uint code, object msg)
        {
            switch ((PBCodeEnum)code)
            {
            case PBCodeEnum.CSHeartBeat:
            {
                print((CSHeartBeat)msg);
                SCHeartBeat heart = new SCHeartBeat();
                heart.clientTime = CommonHelper._UtcNowMs;
                heart.serverTime = CommonHelper._UtcNowMs + 1;
                TcpPacket p     = new TcpPacket(uSession, (uint)PBCodeEnum.SCHeartBeat, heart);
                byte[]    bytes = NetEnCoder.Encode(p);
                WriteSendData(bytes);
            }
            break;

            case PBCodeEnum.CSLogin:
            {
                print((CSLogin)msg);
                SCLogin login = new SCLogin();
                login.errorCode = 0;
                login.loginRet  = 1;
                TcpPacket p     = new TcpPacket(uSession, (uint)PBCodeEnum.SCLogin, login);
                byte[]    bytes = NetEnCoder.Encode(p);
                WriteSendData(bytes);
            }
            break;
            }
        }
示例#3
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();
            }
        }
示例#4
0
        /// <summary>
        /// 处理发送
        /// </summary>
        private void DoSend()
        {
            lock (m_tcpClientLocker)
            {
                if ((m_socket == null) || (m_socket.Connected == false))
                {
                    return;
                }
            }

            int nTotalLength = 0;

            //并包
            lock (m_sendQueueLocker)
            {
                while (nTotalLength < c_MaxBufferSize && m_sendQueue.Count > 0)
                {
                    TcpPacket packet = m_sendQueue.Peek();
                    byte[]    bytes  = NetEnCoder.Encode(packet);


                    if (nTotalLength + bytes.Length < c_MaxBufferSize)
                    {
                        Buffer.BlockCopy(bytes, 0, m_sendBuffer, nTotalLength, bytes.Length);
                        nTotalLength += bytes.Length;
                        m_sendQueue.Dequeue();
                    }
                    else
                    {
                        break;
                    }
                }
            }

            try
            {
                if (nTotalLength > 0)
                {
                    m_socket.Send(m_sendBuffer, 0, nTotalLength, SocketFlags.None);
                    Array.Clear(m_sendBuffer, 0, c_MaxBufferSize);
                    nTotalLength = 0;
                }
            }
            catch (Exception e)
            {
                LoggerHelper.Except(e);
            }
        }
示例#5
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;
            }
        }