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;
                }
            }
        }
        /// <summary>
        /// Use underline socket to send protocol message async.
        /// </summary>
        /// <param name="vMessage">The protocol message to be sended.</param>
        public void SendMessage(System.Object vMsg)
        {
            if (State != ConnectionState.Established)
            {
                Debug.WriteLine(String.Format("SendMessage Error:in State {0}", State));
                return;
            }
            if (vMsg == null)
            {
                Debug.WriteLine(String.Format("SendMessage Error:vMsg is null"));
                return;
            }
            ArraySegment <Byte>  sndBuf        = ProtoHelper.EncodeMessage(vMsg, Provider);
            SocketAsyncEventArgs sendEventArgs = new SocketAsyncEventArgs();

            sendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnCompletedForSend);
            sendEventArgs.SetBuffer(sndBuf.Array, sndBuf.Offset, sndBuf.Count);

            Debug.WriteLine(string.Format("SendMessage Send {0}", vMsg.GetType().Name));

            if (!ConnSocket.SendAsync(sendEventArgs))
            {
                OnCompletedForSendImpl(sendEventArgs);
                sendEventArgs.Dispose();
            }
        }
Example #3
0
        // Token: 0x06006AFA RID: 27386 RVA: 0x001E034C File Offset: 0x001DE54C
        private void WriteMsg2RecvCache(object msg)
        {
            object recvCache = this.m_recvCache;

            lock (recvCache)
            {
                ArraySegment <byte> arraySegment = ProtoHelper.EncodeMessage(msg, this.m_provider);
                this.m_recvCache.Write(arraySegment.Array, arraySegment.Count);
            }
        }
Example #4
0
        // Token: 0x06006AF5 RID: 27381 RVA: 0x001DFFEC File Offset: 0x001DE1EC
        public void SendMessage(object msg)
        {
            if (this.State != ConnectionState.Established)
            {
                global::Debug.WriteLine(string.Format("SendMessage Error:in State {0}", this.State));
                return;
            }
            if (msg == null)
            {
                global::Debug.WriteLine("SendMessage Error:msg is null");
                return;
            }
            ArraySegment <byte>  arraySegment         = ProtoHelper.EncodeMessage(msg, this.m_provider);
            SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();

            socketAsyncEventArgs.Completed += this.OnCompletedForSend;
            socketAsyncEventArgs.SetBuffer(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
            global::Debug.WriteLine(string.Format("SendMessage Send {0}", msg.GetType().Name));
            if (!this.m_socket.SendAsync(socketAsyncEventArgs))
            {
                this.OnCompletedForSendImpl(socketAsyncEventArgs);
                socketAsyncEventArgs.Dispose();
            }
        }
        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));
                }
            }
        }