Esempio n. 1
0
        /// <summary>
        ///     A client has connected (nothing has been sent or received yet)
        /// </summary>
        /// <returns></returns>
        private async void OnClientConnected(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            if (args == null || args.Socket == null)
            {
                return;
            }

            _logger.Debug("Connection from {0}:{1} to {2}:{3} was opened",
                          args.Socket.Information.RemoteHostName.DisplayName,
                          args.Socket.Information.RemotePort,
                          args.Socket.Information.LocalAddress.DisplayName,
                          args.Socket.Information.LocalPort);

            _logger.Trace("Pipeline => HttpServer.OnClientConnected");

            ClientConnectedEventArgs connectedArgs = new ClientConnectedEventArgs(args);

            try
            {
                ClientConnected(sender, connectedArgs);

                if (connectedArgs.AllowConnect == false)
                {
                    if (connectedArgs.Response != null)
                    {
                        await connectedArgs.Response.FlushAsync();
                    }
                    _logger.Debug("Connection from {0}:{1} to {2}:{3} was denied access to connect",
                                  args.Socket.Information.RemoteHostName.DisplayName,
                                  args.Socket.Information.RemotePort,
                                  args.Socket.Information.LocalAddress.DisplayName,
                                  args.Socket.Information.LocalPort);
                }
                else
                {
                    OnMessageReceived(connectedArgs.Socket);
                }
            }
            catch (Exception ex)
            {
                // If this is an unknown status it means that the error is fatal and retry will likely fail.
                if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown)
                {
                    IsActive = false;
                    ListenerError(_listener, ex);

                    _logger.Error("Http request failed with error: {0}", ex.Message, ex);
                }
                else
                {
                    OnClientDisconnected(_listener, ex, SocketError.GetStatus(ex.HResult));
                }

                _logger.Debug("Connection from {0}:{1} to {2}:{3} was {4}",
                              args.Socket.Information.RemoteHostName.DisplayName,
                              args.Socket.Information.RemotePort,
                              args.Socket.Information.LocalAddress.DisplayName,
                              args.Socket.Information.LocalPort,
                              SocketError.GetStatus(ex.HResult).ToString());
            }
        }
        /// <summary>
        ///     A client has connected (nothing has been sent or received yet)
        /// </summary>
        /// <returns></returns>
        private async void OnClientConnected(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {

            if (args == null || args.Socket == null)
                return;

            _logger.Debug("Connection from {0}:{1} to {2}:{3} was opened",
                args.Socket.Information.RemoteHostName.DisplayName,
                args.Socket.Information.RemotePort,
                args.Socket.Information.LocalAddress.DisplayName,
                args.Socket.Information.LocalPort);

            _logger.Trace("Pipeline => HttpServer.OnClientConnected");

            ClientConnectedEventArgs connectedArgs = new ClientConnectedEventArgs(args);

            try
            {
                ClientConnected(sender, connectedArgs);

                if (connectedArgs.AllowConnect == false)
                {
                    if (connectedArgs.Response != null)
                    {
                        await connectedArgs.Response.FlushAsync();
                    }
                    _logger.Debug("Connection from {0}:{1} to {2}:{3} was denied access to connect",
                        args.Socket.Information.RemoteHostName.DisplayName,
                        args.Socket.Information.RemotePort,
                        args.Socket.Information.LocalAddress.DisplayName,
                        args.Socket.Information.LocalPort);
                }
                else
                {

                    OnMessageReceived(connectedArgs.Socket);
                }

            }
            catch (Exception ex)
            {
                // If this is an unknown status it means that the error is fatal and retry will likely fail.
                if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown)
                {
                    IsActive = false;
                    ListenerError(_listener, ex);

                    _logger.Error("Http request failed with error: {0}", ex.Message, ex);
                }
                else
                {
                    OnClientDisconnected(_listener, ex, SocketError.GetStatus(ex.HResult));
                }

                _logger.Debug("Connection from {0}:{1} to {2}:{3} was {4}",
                    args.Socket.Information.RemoteHostName.DisplayName,
                    args.Socket.Information.RemotePort,
                    args.Socket.Information.LocalAddress.DisplayName,
                    args.Socket.Information.LocalPort,
                    SocketError.GetStatus(ex.HResult).ToString());
            }
        }