Beispiel #1
0
 // Token: 0x06006AF9 RID: 27385 RVA: 0x001E0208 File Offset: 0x001DE408
 private void OnCompletedForReceive(object sender, SocketAsyncEventArgs eventArgs)
 {
     try
     {
         if (eventArgs.SocketError != SocketError.Success)
         {
             throw new Exception(string.Format("The result of ReceiveAsync is not correct, SocketError={0}", eventArgs.SocketError));
         }
         if (eventArgs.BytesTransferred != 0)
         {
             object recvCache = this.m_recvCache;
             lock (recvCache)
             {
                 this.m_recvCache.Write(eventArgs.Buffer, eventArgs.BytesTransferred);
             }
             if (this.m_receiveEventArg != null)
             {
                 this.m_receiveEventArg.SetBuffer(0, 65536);
                 if (!this.m_socket.ReceiveAsync(this.m_receiveEventArg))
                 {
                     this.OnCompletedForReceive(null, this.m_receiveEventArg);
                 }
             }
             return;
         }
     }
     catch (Exception ex)
     {
         CCMSGConnectionRecvFailure msg = new CCMSGConnectionRecvFailure
         {
             ExceptionInfo = ex.ToString()
         };
         this.WriteMsg2RecvCache(msg);
     }
     if (this.State == ConnectionState.Established || this.State == ConnectionState.Disconnecting)
     {
         this.FireEventOnLogPrint("OnCompletedForReceivestate=" + this.State, string.Empty);
         this.State = ConnectionState.Closed;
         CCMSGConnectionBreak msg2 = new CCMSGConnectionBreak();
         this.WriteMsg2RecvCache(msg2);
     }
 }
        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));
                }
            }
        }