Esempio n. 1
0
        private async Task <int> connect_task()
        {
            while (m_run == 0)
            {
                Console.Error.WriteLine(
                    "Task Connecting to {0}: ", m_uri.ToString());

                var cancellationTokenSource = new CancellationTokenSource(
                    TimeSpan.FromSeconds(10));

                try
                {
                    m_connect_sock = new ClientWebSocket();

                    ClientWebSocketOptions opt = m_connect_sock.Options;

                    Console.Error.WriteLine(opt.KeepAliveInterval.ToString());
                    opt.KeepAliveInterval = new TimeSpan(0, 0, 10);
                    Console.Error.WriteLine(opt.KeepAliveInterval.ToString());

                    m_connect_sock.ConnectAsync(
                        m_uri, cancellationTokenSource.Token).Wait(
                        cancellationTokenSource.Token);

                    Console.Error.WriteLine(
                        "Task Connected to {0}: ", m_uri.ToString());

                    iwsServiceHandler svc =
                        m_service_handler_strategy.makeServiceHandler();
                    m_run = await svc.open(m_connect_sock);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Connection failed: {0}", ex.Message);
                    // Async wait before re-trying
                    await Task.Delay(3000);
                }
                finally
                {
                    //await Task.Delay(3000);
                    //await m_connect_sock.CloseAsync();
                    m_connect_sock.Dispose();
                }
            }
            return(m_run);
        }
Esempio n. 2
0
        private async Task <bool> accept_task(int i)
        {
            while (m_run)
            {
                Console.WriteLine("Task {0} awaiting accept on {1}: ", i, m_prefix);
                HttpListenerContext ctx = await m_http_listener.GetContextAsync();

                string ipAddress = ctx.Request.RemoteEndPoint.Address.ToString();
                if (ctx.Request.IsWebSocketRequest)
                {
                    Console.Error.WriteLine(
                        "Websocket Request: IPAddress {0}", ipAddress);

                    HttpListenerWebSocketContext wsCtx =
                        await ctx.AcceptWebSocketAsync(
                            subProtocol : null, keepAliveInterval : new TimeSpan(0, 0, 10));

                    WebSocket ws = wsCtx.WebSocket;

                    Console.Error.WriteLine(WebSocket.DefaultKeepAliveInterval.ToString());


                    iwsServiceHandler svc = m_service_handler_strategy.makeServiceHandler();
                    Task t = Task.Factory.StartNew(async() =>
                    {
                        await svc.open(ws);
                    });
                }
                else
                {
                    Console.WriteLine("Http request: IPAddress {0}", ipAddress);
                    ctx.Response.StatusCode = 400;
                    ctx.Response.Close();
                }
            }
            return(m_run);
        }