Esempio n. 1
0
        // Token: 0x06006AF2 RID: 27378 RVA: 0x001DFE2C File Offset: 0x001DE02C
        public void Disconnect()
        {
            if (this.State != ConnectionState.Established)
            {
                return;
            }
            Socket oldSocket = this.m_socket;

            if (this.m_disconnectTimer == null)
            {
                this.m_disconnectTimer = new Timer(delegate(object o)
                {
                    if (oldSocket == this.m_socket && !this.m_socket.Connected && this.State == ConnectionState.Disconnecting)
                    {
                        this.FireEventOnLogPrint("Disconnect.Timerstate=" + this.State, string.Empty);
                        this.State = ConnectionState.Closed;
                        CCMSGConnectionBreak msg = new CCMSGConnectionBreak();
                        this.WriteMsg2RecvCache(msg);
                    }
                }, null, 500, 1000);
                try
                {
                    this.m_socket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception arg)
                {
                    string str = string.Format("{0}", arg);
                    global::Debug.WriteLine(str);
                }
            }
            this.State = ConnectionState.Disconnecting;
        }
Esempio n. 2
0
 // Token: 0x06006AF7 RID: 27383 RVA: 0x001E00BC File Offset: 0x001DE2BC
 private void OnCompletedForSendImpl(SocketAsyncEventArgs e)
 {
     try
     {
         if (e.SocketError == SocketError.Success)
         {
             return;
         }
         throw new Exception("The result of SendAsync is not correct");
     }
     catch (Exception ex)
     {
         CCMSGConnectionSendFailure msg = new CCMSGConnectionSendFailure
         {
             ExceptionInfo = ex.ToString()
         };
         this.WriteMsg2RecvCache(msg);
     }
     if (this.State == ConnectionState.Established)
     {
         this.State = ConnectionState.Closed;
         CCMSGConnectionBreak msg2 = new CCMSGConnectionBreak();
         this.WriteMsg2RecvCache(msg2);
         this.FireEventOnLogPrint("OnCompletedForSendImpl", string.Empty);
     }
 }
Esempio n. 3
0
        //The callback of BeginSend on fuction "SendMessage"
        private void OnCompletedForSendImpl(SocketAsyncEventArgs e)
        {
            try
            {
                if (e.SocketError == SocketError.Success)
                {
                    return;
                }
                else
                {
                    throw new Exception("The result of SendAsync is not correct");
                }
            }
            catch (Exception ex)
            {
                // Connection exception means connection is disconnected
                CCMSGConnectionSendFailure failureMsg = new CCMSGConnectionSendFailure();
                failureMsg.ExceptionInfo = ex.ToString();
                lock (RecvQueue)
                {
                    RecvQueue.Enqueue(new KeyValuePair <int, object>(failureMsg.MessageId, failureMsg));
                }
                goto BREAK_CONNECT;
            }
BREAK_CONNECT:
            lock (RecvQueue)
            {
                if (State == ConnectionState.Established)
                {
                    State = ConnectionState.Closed;
                    var vMsg = new CCMSGConnectionBreak();
                    RecvQueue.Enqueue(new KeyValuePair <int, object>(vMsg.MessageId, vMsg));
                }
            }
        }
Esempio n. 4
0
        ///// <summary>
        ///// shutdown the socket
        ///// </summary>
        //public void Shutdown()
        //{
        //    try
        //    {
        //        lock (_lockSocket)
        //        {
        //            ////Console.WriteLine("Shutdown::lock (_lockPeer)");
        //            ConnSocket.Shutdown(SocketShutdown.Both);
        //        }

        //    }
        //    catch (Exception)
        //    {
        //        //String msg = String.Format("{0}:{1}", ex.GetType().ToString(), ex.Message);
        //        //Debug.WriteLine(msg);
        //        //Console.WriteLine(msg);
        //        //throw ex;
        //    }
        //    //Console.WriteLine("Connection SHUTDOWN!");
        //}

        /// <summary>
        /// �Ͽ�soket
        /// </summary>
        private void SocketDisconnect()
        {
            try
            {
                if (ConnSocket != null)
                {
                    lock (_lockSocket)
                    {
                        // ����Ͽ����ӵ�timer
                        if (m_disconnectTimer == null)
                        {
                            m_disconnectTimer = new Timer((c) =>
                            {
                                if (ConnSocket.Connected)
                                {
                                    return;
                                }
                                if (State != ConnectionState.Disconnecting)
                                {
                                    return;
                                }

                                // ��¼��־
                                Debug.WriteLine(string.Format("SocketDisconnect disconnected timer start..."));
                                FireEventOnLogPrint("Disconnect.Timer" + "state=" + State);

                                // ����״̬
                                State = ConnectionState.Closed;

                                // �������ӶϿ�����Ϣ
                                var vMsg = new CCMSGConnectionBreak();
                                RecvQueue.Enqueue(new KeyValuePair <int, object>(vMsg.MessageId, vMsg));

                                // ����timer
                                if (m_disconnectTimer != null)
                                {
                                    m_disconnectTimer.Dispose();
                                    m_disconnectTimer = null;
                                }
                            }, null, 500, 1000); // 500���������� 1000������һ��
                        }

                        ConnSocket.Shutdown(SocketShutdown.Both);
                        //ConnSocket.Disconnect(false);
                    }
                }
            }
            catch (Exception ex)
            {
                String msg = String.Format("{0}:{1}", ex.GetType().ToString(), ex.Message);
                Debug.WriteLine(msg);
                //throw ex;
            }
        }
Esempio n. 5
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);
     }
 }
Esempio n. 6
0
        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));
                }
            }
        }