Exemple #1
0
        private async Task <ITransport> AcceptWebSocketAsync(HttpListenerContext httpListenerContext)
        {
            try
            {
                if (!httpListenerContext.Request.IsWebSocketRequest)
                {
                    httpListenerContext.Response.StatusCode = 400;
                    httpListenerContext.Response.Close();
                    return(null);
                }

                var context = await httpListenerContext.AcceptWebSocketAsync(
                    LimeUri.LIME_URI_SCHEME, _bufferSize, _keepAliveInterval)
                              .WithCancellation(_acceptTransportCts.Token)
                              .ConfigureAwait(false);

                return(new ServerWebSocketTransport(context, _envelopeSerializer, _traceWriter, _bufferSize));
            }
            catch (OperationCanceledException) when(_acceptTransportCts.IsCancellationRequested)
            {
            }
            catch (Exception ex)
            {
                ListenerException.RaiseEvent(this, new ExceptionEventArgs(ex));
            }

            return(null);
        }
Exemple #2
0
        private async Task AcceptTransportsAsync()
        {
            while (!_acceptTransportCts.IsCancellationRequested)
            {
                try
                {
                    var httpContext = await _httpListener
                                      .GetContextAsync()
                                      .WithCancellation(_acceptTransportCts.Token)
                                      .ConfigureAwait(false);

                    await _httpListenerContextBufferBlock
                    .SendAsync(httpContext, _acceptTransportCts.Token)
                    .ConfigureAwait(false);
                }
                catch (OperationCanceledException) when(_acceptTransportCts.IsCancellationRequested)
                {
                    break;
                }
                catch (Exception ex)
                {
                    var args = new ExceptionEventArgs(ex);
                    ListenerException.RaiseEvent(this, new ExceptionEventArgs(ex));
                    await args.WaitForDeferralsAsync();
                }
            }
        }
Exemple #3
0
        private void ListeningLoop(object stopEventArg)
        {
            if (stopEventArg == null)
            {
                throw new ArgumentNullException("stopEventArg");
            }

            var stopEvent = (ManualResetEvent)stopEventArg;

            var cancel = Task.Factory.StartNew(() =>
            {
                stopEvent.WaitOne();
            }, TaskCreationOptions.LongRunning);

            try
            {
                while (true)
                {
                    Task <TcpClient> conn = ReceiveAsync();

                    switch (Task.WaitAny(new[] { conn, cancel }))
                    {
                    case 0:
                        HandleConnection(conn);
                        break;

                    case 1:
                        return;

                    default:
                        throw new NotImplementedException("WTF????");
                    }
                }
            }
            catch (Exception ex)
            {
                ListenerException callback = OnListenerException;
                if (callback != null)
                {
                    callback(this, new ListenerExceptionEventArgs(ex));
                }
            }
        }
Exemple #4
0
 public static void ListenerException(this ILog logger, ListenerException exception, string trackingId, object data)
 {
     logger.Error(string.Format("Exception of type {0} is occurred with message: {1}", exception.GetType().Name, exception.Message), exception);
     logger.Error(string.Format("Exception data: [{0}]{1}", trackingId, data.ToJson()));
 }