Exemple #1
0
        void OnReceive(IAsyncResult result)          //当接收完成,调用OnReceive时,其会通过ServerMsgReader的process对二进制报文进行解析,解析完成后会调用handleMsg进行进一步处理
        {
            int bytes = 0;

            if (mSocket == null)
            {
                LogHelper.Log("Error", "SocketClosed");
                Close();
                return;
            }
            try
            {
                bytes = mSocket.EndReceive(result);
            }
            catch (Exception exception)
            {
                Debug.LogError(exception.Message);
                Close();
            }
            if (bytes <= 0)
            {
                Debug.LogError("bytes " + bytes);
                Close();
            }
            else
            {
                //MessageReader
                //BeginReceive
                uint num = (uint)bytes;
                msgReader.process(mTemp, num);
                if (mSocket != null)
                {
                    try
                    {
                        mSocket.BeginReceive(mTemp, 0, mTemp.Length, SocketFlags.None, OnReceive, mSocket);
                    }
                    catch (Exception exception2)
                    {
                        Util.Log(exception2.Message);
                        Close();
                    }
                }
            }
        }
 public void ReceiveData(byte[] bytes)
 {
     lastReceiveTime = Util.GetTimeNow();
     //阻塞处理 线程安全 一个一个接受
     msgReader.process(bytes, (uint)bytes.Length);
 }