示例#1
0
        void Close(bool isPassive)
        {
            Debug.Log("Close");
            if (TcpClient != null)
            {
                if (TcpClient.Connected)
                {
                    TcpClient.Close();
                }
                if (Receiving)
                {
                    mManualResetEvent = new ManualResetEvent(false);
                    mManualResetEvent.WaitOne();
                    mManualResetEvent = null;
                }
                TcpClient = null;
            }

            if (mSendThread != null)
            {
                mSendThread.Stop();
                mSendThread = null;
            }
            Status = NetworkStatus.Disconnected;
            lock (mMessageQueue)
            {
                mMessageQueue.Enqueue(mObjectPool.Acquire().DisconnectedMessage(isPassive));
            }
            Debug.Log("Close Done");
        }
示例#2
0
        void ConnectCallback(IAsyncResult ar)
        {
            Debug.Log("@@@@@ ConnectCallback");
            try
            {
                TcpClient.EndConnect(ar);
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception exception)
            {
                Status = NetworkStatus.Disconnected;

                SocketException socketException = exception as SocketException;
                if (socketException != null)
                {
                    OnNetworkError(string.Format("Connect socket error code = {0} see detail from https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2"
                                                 , socketException.ErrorCode));
                    OnConnectResult(false, socketException.ErrorCode);
                    return;
                }
                else
                {
                    OnConnectResult(false, -1);
                }
                throw;
            }

            mSendThread = new SendThread(mHandler, TcpClient.GetStream(), OnNetworkError);
            mSendThread.Start();

            mReceiveData = new ReceiveData();

            Status = NetworkStatus.Connected;
            OnConnectResult(true, 0);
            Receive();
        }