예제 #1
0
        async Task CompleteAcceptAsync(RelayedHttpListenerContext listenerContext, Uri rendezvousUri, bool shouldAccept)
        {
            try
            {
                if (shouldAccept)
                {
                    var webSocketStream = await listenerContext.AcceptAsync(rendezvousUri).ConfigureAwait(false);

                    lock (this.ThisLock)
                    {
                        if (this.closeCalled)
                        {
                            RelayEventSource.Log.RelayListenerRendezvousFailed(this, listenerContext.TrackingContext.TrackingId, SR.ObjectClosedOrAborted);
                            return;
                        }

                        this.connectionInputQueue.EnqueueAndDispatch(webSocketStream, null, canDispatchOnThisThread: false);
                    }
                }
                else
                {
                    RelayEventSource.Log.RelayListenerRendezvousRejected(
                        listenerContext.TrackingContext, listenerContext.Response.StatusCode, listenerContext.Response.StatusDescription);
                    await listenerContext.RejectAsync(rendezvousUri).ConfigureAwait(false);
                }
            }
            catch (Exception exception) when(!Fx.IsFatal(exception))
            {
                RelayEventSource.Log.RelayListenerRendezvousFailed(this, listenerContext.TrackingContext.TrackingId, exception);
            }
            finally
            {
                RelayEventSource.Log.RelayListenerRendezvousStop();
            }
        }
        async Task OnAcceptClientCommand(ListenerCommand.AcceptCommand acceptCommand)
        {
            var listenerContext = new RelayedHttpListenerContext(this, acceptCommand);

            RelayEventSource.Log.RelayListenerRendezvousStart(listenerContext, acceptCommand.Address);
            try
            {
                bool shouldAccept       = true;
                var  acceptHandlerValue = this.AcceptHandler;
                if (acceptHandlerValue != null)
                {
                    // Invoke and await the user's AcceptHandler method
                    try
                    {
                        shouldAccept = await acceptHandlerValue(listenerContext).ConfigureAwait(false);
                    }
                    catch (Exception userException) when(!Fx.IsFatal(userException))
                    {
                        string description = SR.GetString(SR.AcceptHandlerException, listenerContext.TrackingContext.TrackingId);

                        RelayEventSource.Log.RelayListenerRendezvousFailed(listenerContext, description + " " + userException);
                        listenerContext.Response.StatusCode        = HttpStatusCode.BadGateway;
                        listenerContext.Response.StatusDescription = description;
                        shouldAccept = false;
                    }
                }

                if (shouldAccept)
                {
                    var webSocketStream = await listenerContext.AcceptAsync().ConfigureAwait(false);

                    lock (this.ThisLock)
                    {
                        if (this.closeCalled)
                        {
                            RelayEventSource.Log.RelayListenerRendezvousFailed(listenerContext, SR.ObjectClosedOrAborted);
                            return;
                        }

                        this.connectionInputQueue.EnqueueAndDispatch(webSocketStream, null, canDispatchOnThisThread: false);
                    }
                }
                else
                {
                    RelayEventSource.Log.RelayListenerRendezvousRejected(listenerContext);
                    await listenerContext.RejectAsync().ConfigureAwait(false);
                }
            }
            catch (Exception exception) when(!Fx.IsFatal(exception))
            {
                RelayEventSource.Log.RelayListenerRendezvousFailed(listenerContext, exception.ToString());
            }
            finally
            {
                RelayEventSource.Log.RelayListenerRendezvousStop();
            }
        }