private void ConnectCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                if (errorRecorder != null && !requireClose)
                {
                    errorRecorder.RecordError("Connect to " + endpoint + " failed. Due to SocketError: " + e.SocketError);
                }

                CloseWhenConnectingError();
                return;
            }

            if (requireClose)
            {
                CallConnectionConnectedDelegate(0, false, "Connecting cannel event exception. Remote endpoint: " + endpoint + ".");
                connectionCloseDelegate = null;
                Close();
                return;
            }

            receiveAsyncEventArgs.SetBuffer(receiver.buffer, receiver.offset, receiver.requireLength - receiver.offset);
            connectionId = socket.Handle.ToInt64();

            CallConnectionConnectedDelegate(connectionId, true, "Connected event exception. Remote endpoint: " + endpoint + ".");

            lock (interLocker)
            {
                status = TCPClient.ClientStatus.Connected;
            }

            if (requireClose)
            {
                Close();
                return;
            }

            if (ClientEngine.RegisterConnectedConnection(this) == false)
            {
                Close();
                return;
            }

            CheckSending();

            try
            {
                if (!socket.ReceiveAsync(receiveAsyncEventArgs))
                {
                    ReceiveCompleted(socket, receiveAsyncEventArgs);
                }
            }
            catch (ObjectDisposedException ex)
            {
                CloseByException("Receive data from " + endpoint + " exception. Connection is broken.", ex, true);
            }
            catch (SocketException ex)
            {
                CloseByException("Receive data from " + endpoint + " exception. Access socket is error.", ex, false);
            }
        }
Esempio n. 2
0
        private void ConnectCompleted(object sender, SocketAsyncEventArgs e)
        {
            int unityDisCompliantSign = System.Threading.Interlocked.Exchange(ref connectCompletedSignForUnityIl2CPPDisCompliantImplement, 1);

            if (unityDisCompliantSign != 0)
            {
                if (e.SocketError != SocketError.Success)
                {
                    lock (interLocker)
                    {
                        if (status == TCPClient.ClientStatus.Connecting)
                        {
                            requireClose = true;
                            return;
                        }
                        else if (status == TCPClient.ClientStatus.Closed)
                        {
                            return;
                        }
                    }

                    //-- status == TCPClient.ClientStatus.Connected
                    Close();
                }

                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                if (errorRecorder != null && !requireClose)
                {
                    errorRecorder.RecordError("Connect to " + endpoint + " failed. Due to SocketError: " + e.SocketError);
                }

                CloseWhenConnectingError();
                return;
            }

            if (requireClose)
            {
                CallConnectionConnectedDelegate(0, false, "Connecting cannel event exception. Remote endpoint: " + endpoint + ".");
                connectionCloseDelegate = null;
                Close();
                return;
            }

            receiveAsyncEventArgs.SetBuffer(receiver.buffer, receiver.offset, receiver.requireLength - receiver.offset);
            connectionId = socket.Handle.ToInt64();

            CallConnectionConnectedDelegate(connectionId, true, "Connected event exception. Remote endpoint: " + endpoint + ".");

            lock (interLocker)
            {
                status = TCPClient.ClientStatus.Connected;
            }

            if (requireClose)
            {
                Close();
                return;
            }

            if (ClientEngine.RegisterConnectedConnection(this) == false)
            {
                Close();
                return;
            }

            CheckSending();

            try
            {
                if (!socket.ReceiveAsync(receiveAsyncEventArgs))
                {
                    ReceiveCompleted(socket, receiveAsyncEventArgs);
                }
            }
            catch (ObjectDisposedException ex)
            {
                CloseByException("Receive data from " + endpoint + " exception. Connection is broken.", ex, true);
            }
            catch (SocketException ex)
            {
                CloseByException("Receive data from " + endpoint + " exception. Access socket is error.", ex, false);
            }
            catch (InvalidOperationException)
            {
                //-- Do nothings
            }
        }