Example #1
0
        private async Task ListenAsync(HttpListener listener)
        {
            if (Handler is null)
            {
                return;
            }

            try
            {
                HttpListenerContext ctx = await listener.GetContextAsync();

                byte[] content = await ctx.Request.InputStream.ToBytesAsync();

                WWSResponse resp = await Handler(ctx.Request, content, ctx.Response);

                byte[] ret = resp?.Bytes ?? new byte[0];

                ctx.Response.ContentEncoding = resp.Codepage;
                ctx.Response.ContentLength64 = ret.Length;

                using (Stream os = ctx.Response.OutputStream)
                {
                    await os.WriteAsync(ret, 0, ret.Length);

                    os.Flush();
                    os.Close();
                }
            }
            catch (Exception ex)
            {
                OnInternalError?.Invoke(this, ex);
            }
        }
Example #2
0
        protected internal async override Task MainLoop()
        {
            LOG("MainLoop()");

            try
            {
                while (true)
                {
                    ASSERT(!string.IsNullOrWhiteSpace(HandlerUrl), "Property 'HandlerUrl' is supposed to be set here");
                    ASSERT(!string.IsNullOrWhiteSpace(ConnectionID), "Property 'ConnectionID' is supposed to be set here");

                    LOG("Send POLL request");
                    var request  = CreateRootMessage_Poll(ConnectionID);
                    var response = await SendHttpRequest(request, isPollConnection : true);

                    var responseType = response[RootMessageKeys.KeyType] as string;
                    LOG("Root message received: " + responseType);
                    switch (responseType)
                    {
                    case RootMessageKeys.TypeReset:
                        // TCP connection refresh asked (just send another request)
                        continue;

                    case RootMessageType_Logout:
                        // Terminate MainLoop
                        goto EXIT_LOOP;

                    case RootMessageKeys.TypeMessages:
                        ReceiveMessages(response).FireAndForget();
                        break;

                    default:
                        throw new NotImplementedException("Unsupported response message type '" + responseType + "'");
                    }
                }
                EXIT_LOOP :;
            }
            catch (System.Exception ex)
            {
                switch (Status)
                {
                case ConnectionStatus.Closing      :
                case ConnectionStatus.Disconnected :
                    // Error received while closing the connections => No need to report
                    break;

                default:
                    await OnInternalError.Invoke("Error while reading message from the HTTP request", ex);

                    break;
                }
            }

            await Stop();
        }