Example #1
0
        // Token: 0x06006AFB RID: 27387 RVA: 0x001E03AC File Offset: 0x001DE5AC
        private void TryDecodeMsg()
        {
            object recvCache = this.m_recvCache;

            lock (recvCache)
            {
                if (this.m_lastRecvCacheLength != this.m_recvCache.Length)
                {
                    do
                    {
                        int    key;
                        object obj = ProtoHelper.DecodeMessage(this.m_recvCache, this.m_provider, out key, this.m_messageDeserializeAction);
                        if (obj == null)
                        {
                            break;
                        }
                        KeyValuePair <int, object> item = new KeyValuePair <int, object>(key, obj);
                        object recvQueue = this.m_recvQueue;
                        lock (recvQueue)
                        {
                            this.m_recvQueue.Enqueue(item);
                        }
                    }while (this.m_recvCache.Length != 0);
                    this.m_recvCache.Crunch();
                    this.m_lastRecvCacheLength = this.m_recvCache.Length;
                }
            }
        }
        private void OnCompletedForReceiveImpl(SocketAsyncEventArgs e)
        {
            try
            {
                if (e.SocketError == SocketError.Success && e.BytesTransferred != 0)
                {
                    // at first, write state.buffer to recvcache
                    RecvCache.Write(e.Buffer, e.BytesTransferred);

                    //Debug.WriteLine(string.Format("OnCompletedForReceiveImpl Receive {0} RecvCache.Length={1}", e.BytesTransferred, RecvCache.Length));

                    // second, push the msg to recvqueue and decode message
                    while (true)
                    {
                        var    msgId = 0;
                        object msg   = ProtoHelper.DecodeMessage(RecvCache, Provider, out msgId);
                        if (msg == null)
                        {
                            break;
                        }
                        lock (RecvQueue)
                        {
                            RecvQueue.Enqueue(new KeyValuePair <int, object>(msgId, msg));
                        }

                        // all cache is handled
                        if (RecvCache.Length == 0)
                        {
                            break;
                        }
                    }
                    //Debug.WriteLine(string.Format("OnCompletedForReceiveImpl Receive End, RecvCache.Length={0}", RecvCache.Length));
                    RecvCache.Crunch();

                    // third, restart the async receive process
                    m_receiveEventArg.SetBuffer(0, ProtoConst.MAX_PACKAGE_LENGTH);
                    if (!ConnSocket.ReceiveAsync(m_receiveEventArg))
                    {
                        OnCompletedForReceiveImpl(m_receiveEventArg);
                    }
                    return;
                }
                else
                {
                    throw new Exception(string.Format("The result of ReceiveAsync is not correct, SocketError={0} BytesTransferred={1}", e.SocketError, e.BytesTransferred));
                }
            }
            catch (Exception ex)
            {
                // Connection exception means connection is disconnected
                // Or, there is an bad message format in byte stream
                CCMSGConnectionRecvFailure eMsg = new CCMSGConnectionRecvFailure();
                eMsg.ExceptionInfo = ex.ToString();
                lock (RecvQueue)
                {
                    RecvQueue.Enqueue(new KeyValuePair <int, object>(eMsg.MessageId, eMsg));
                }
                goto BREAK_CONNECT;
            }

BREAK_CONNECT:
            // when receive action is over, which represents that socket can't receive any data
            lock (RecvQueue)
            {
                if (State == ConnectionState.Established ||
                    State == ConnectionState.Disconnecting)  // ���״̬��Ϊ���ÿͻ��������Ͽ���������ʱ��
                {
                    State = ConnectionState.Closed;
                    var vMsg = new CCMSGConnectionBreak();
                    RecvQueue.Enqueue(new KeyValuePair <int, object>(vMsg.MessageId, vMsg));
                }
            }
        }