Ejemplo n.º 1
0
        public void Start()
        {
            Log.ConnectionStart(ConnectionId);

            // Start socket prior to applying the ConnectionFilter
            _socket.ReadStart(_allocCallback, _readCallback, this);

            if (ServerOptions.ConnectionFilter == null)
            {
                _frame.Start();
            }
            else
            {
                _libuvStream = new LibuvStream(SocketInput, SocketOutput);

                _filterContext = new ConnectionFilterContext
                {
                    Connection = _libuvStream,
                    Address    = ServerAddress
                };

                try
                {
                    ServerOptions.ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
                    {
                        var connection = (Connection)state;

                        if (task.IsFaulted)
                        {
                            connection.Log.LogError(0, task.Exception, "ConnectionFilter.OnConnection");
                            connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);
                        }
                        else if (task.IsCanceled)
                        {
                            connection.Log.LogError("ConnectionFilter.OnConnection Canceled");
                            connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);
                        }
                        else
                        {
                            connection.ApplyConnectionFilter();
                        }
                    }, this);
                }
                catch (Exception ex)
                {
                    Log.LogError(0, ex, "ConnectionFilter.OnConnection");
                    ConnectionControl.End(ProduceEndType.SocketDisconnect);
                }
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            Log.ConnectionStart(_connectionId);

            // Start socket prior to applying the ConnectionFilter
            _socket.ReadStart(_allocCallback, _readCallback, this);

            // Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
            if (ConnectionFilter == null)
            {
                SocketInput  = _rawSocketInput;
                SocketOutput = _rawSocketOutput;

                _frame = new Frame(this);
                _frame.Start();
            }
            else
            {
                var libuvStream = new LibuvStream(_rawSocketInput, _rawSocketOutput);

                _filterContext = new ConnectionFilterContext
                {
                    Connection = libuvStream,
                    Address    = ServerAddress
                };

                ConnectionFilter.OnConnection(_filterContext).ContinueWith((task, state) =>
                {
                    var connection = (Connection)state;

                    if (task.IsFaulted)
                    {
                        connection.Log.LogError("ConnectionFilter.OnConnection", task.Exception);
                        ConnectionControl.End(ProduceEndType.SocketDisconnect);
                    }
                    else if (task.IsCanceled)
                    {
                        connection.Log.LogError("ConnectionFilter.OnConnection Canceled");
                        ConnectionControl.End(ProduceEndType.SocketDisconnect);
                    }
                    else
                    {
                        connection.ApplyConnectionFilter();
                    }
                }, this);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Starts listening on this <see cref="Connection"/>.
        /// </summary>
        /// <returns>Whether the channel was started successfully or not.</returns>
        public bool Start()
        {
            if (this.State != ConnectionState.Creating)
            {
                return(false);
            }

            //NetTrace.WriteLine("Connection Starting", this, NetTraceCategory.Channel);
            this.OnAfterConstruct();

            // Start socket prior to applying the ConnectionFilter
            _socket.ReadStart(_allocCallback, _readCallback, this);

            var tcpHandle = _socket as UvTcpHandle;

            if (tcpHandle != null)
            {
                RemoteEndPoint = tcpHandle.GetPeerIPEndPoint();
                LocalEndPoint  = tcpHandle.GetSockIPEndPoint();
            }

            // Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
            if (Options.ConnectionFilter == null)
            {
                lock (_stateLock)
                {
                    if (State != ConnectionState.Creating)
                    {
                        throw new InvalidOperationException("Invalid connection state: " + State);
                    }

                    State        = ConnectionState.Open;
                    SocketInput  = _rawSocketInput;
                    SocketOutput = _rawSocketOutput;

                    this.OnConnect();
                    return(true);
                }
            }
            else
            {
                _libuvStream   = new LibuvStream(_rawSocketInput, _rawSocketOutput);
                _filterContext = new ConnectionFilterContext
                {
                    Connection = _libuvStream,
                    Address    = ServerAddress
                };

                try
                {
                    Options.ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
                    {
                        var connection = (Connection)state;

                        if (task.IsFaulted)
                        {
                            Service.Logger.Log(task.Exception);
                            connection.Close(CloseType.SocketDisconnect);
                        }
                        else if (task.IsCanceled)
                        {
                            //connection.Log.LogError("ConnectionFilter.OnConnection Canceled");
                            connection.Close(CloseType.SocketDisconnect);
                        }
                        else
                        {
                            connection.ApplyConnectionFilter();
                        }
                    }, this);
                    return(true);
                }
                catch (Exception ex)
                {
                    Service.Logger.Log(ex);
                    this.Close(CloseType.SocketDisconnect);
                    return(false);
                }
            }
        }
Ejemplo n.º 4
0
        public void Start()
        {
            Log.ConnectionStart(ConnectionId);

            // Start socket prior to applying the ConnectionFilter
            _socket.ReadStart(_allocCallback, _readCallback, this);

            var tcpHandle = _socket as UvTcpHandle;

            if (tcpHandle != null)
            {
                RemoteEndPoint = tcpHandle.GetPeerIPEndPoint();
                LocalEndPoint  = tcpHandle.GetSockIPEndPoint();
            }

            // Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
            if (ServerOptions.ConnectionFilter == null)
            {
                lock (_stateLock)
                {
                    if (_connectionState != ConnectionState.CreatingFrame)
                    {
                        throw new InvalidOperationException("Invalid connection state: " + _connectionState);
                    }

                    _connectionState = ConnectionState.Open;

                    SocketInput  = _rawSocketInput;
                    SocketOutput = _rawSocketOutput;

                    _frame = CreateFrame();
                    _frame.Start();
                }
            }
            else
            {
                _libuvStream = new LibuvStream(_rawSocketInput, _rawSocketOutput);

                _filterContext = new ConnectionFilterContext
                {
                    Connection = _libuvStream,
                    Address    = ServerAddress
                };

                try
                {
                    ServerOptions.ConnectionFilter.OnConnectionAsync(_filterContext).ContinueWith((task, state) =>
                    {
                        var connection = (Connection)state;

                        if (task.IsFaulted)
                        {
                            connection.Log.LogError(0, task.Exception, "ConnectionFilter.OnConnection");
                            connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);
                        }
                        else if (task.IsCanceled)
                        {
                            connection.Log.LogError("ConnectionFilter.OnConnection Canceled");
                            connection.ConnectionControl.End(ProduceEndType.SocketDisconnect);
                        }
                        else
                        {
                            connection.ApplyConnectionFilter();
                        }
                    }, this);
                }
                catch (Exception ex)
                {
                    Log.LogError(0, ex, "ConnectionFilter.OnConnection");
                    ConnectionControl.End(ProduceEndType.SocketDisconnect);
                }
            }
        }