Exemple #1
0
 public void SetConnectionCloseDelegate(ConnectionCloseDelegate cwcd)
 {
     lock (interLocker)
     {
         connectionCloseDelegate = cwcd;
     }
 }
Exemple #2
0
        private void ConfigWillCloseDelegate(TCPConnection conn, ConnectionCloseDelegate cb)
        {
            conn.SetCloseDelegate((Int64 connectionId, string endpoint, bool causedByError) =>
            {
                SetClientStatus(conn, ClientStatus.Closed);

                cb?.Invoke(connectionId, endpoint, causedByError);
            });
        }
        private void CloseWhenConnectingError()
        {
            ClientEngine.UnregisterConnection(this);

            lock (interLocker)
            {
                if (status == TCPClient.ClientStatus.Closed)
                {
                    return;
                }

                status       = TCPClient.ClientStatus.Closed;
                beginClosing = true;
            }

            CallConnectionConnectedDelegate(0, false, "Connecting failed event exception. Remote endpoint: " + endpoint + ".");
            connectionCloseDelegate = null;

            ClearAllCallback(ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION);

            socket.Close();
        }
        private void CleanForClose(bool callCloseEvent, bool causedByError)
        {
            ClientEngine.UnregisterConnection(this);

            ClearAllCallback(ErrorCode.FPNN_EC_CORE_CONNECTION_CLOSED);

            if (callCloseEvent && connectionCloseDelegate != null)
            {
                try
                {
                    connectionCloseDelegate(connectionId, endpoint.ToString(), causedByError);
                }
                catch (Exception ex)
                {
                    if (errorRecorder != null)
                    {
                        errorRecorder.RecordError("Close event exception. Remote endpoint: " + endpoint + ".", ex);
                    }
                }
            }
            connectionCloseDelegate = null;
        }
 public void SetCloseDelegate(ConnectionCloseDelegate cb)
 {
     connectionCloseDelegate = cb;
 }
        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);
            }
        }
        //----------------[ I/O Operations ]-----------------------//

        public void AsyncConnect(int connectTimeout)
        {
            lock (interLocker)
            {
                if (status != TCPClient.ClientStatus.Closed)
                {
                    return;
                }

                status = TCPClient.ClientStatus.Connecting;
            }

            if (ClientEngine.RegisterConnectingConnection(this, connectTimeout) == false)
            {
                lock (interLocker)
                {
                    status       = TCPClient.ClientStatus.Closed;
                    beginClosing = true;
                }

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

            connectionId       = 0;
            receiver           = new StandardReceiver();
            sendAsyncEventArgs = new SocketAsyncEventArgs {
                RemoteEndPoint = endpoint
            };
            sendAsyncEventArgs.Completed += IO_Completed;

            recursionDeepOfReceiveFunction = 0;
            recursionDeepOfSendFunction    = 0;

            try
            {
                if (!socket.ConnectAsync(receiveAsyncEventArgs))      //-- Synchronous
                {
                    ConnectCompleted(socket, receiveAsyncEventArgs);
                }
                else
                {
                    lock (interLocker)
                    {
                        if (requireClose)
                        {
                            CannelConnecting();
                        }
                        else
                        {
                            connectingCanBeCannelled = true;
                        }
                    }
                }
            }
            catch (SocketException e)
            {
                if (errorRecorder != null)
                {
                    errorRecorder.RecordError("Connect to " + endpoint + " failed.", e);
                }

                CloseWhenConnectingError();
            }
            catch (ObjectDisposedException e)
            {
                if (errorRecorder != null)
                {
                    errorRecorder.RecordError("Connect to " + endpoint + " failed.", e);
                }

                CloseWhenConnectingError();
            }
        }
Exemple #8
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
            }
        }