Esempio n. 1
0
        public async void ListenEx(Connection dataConnection)
        {
            // accept inside try so EndService could clean up.
            try
            {
                await dataConnection.AcceptAsync();
                var protocolHandler = BeginService(dataConnection);
                do
                {
                    // process requests from this connection.
                    protocolHandler.BeginRequest();
                    while (protocolHandler.KeepReading)
                    {
                        await dataConnection.ReadAsync();
                        protocolHandler.CheckRequestData();
                    }

                    // execute collected requests.
                    var stillExecuting = protocolHandler.ParseExecuteRequest();
                    while (stillExecuting > 0)
                    {
                        await dataConnection.FlushAsync(false);
                        stillExecuting = await protocolHandler.WaitForExecuting();
                    }
                    // send last piece of accumulated response to the client.
                    await dataConnection.FlushAsync(true);
                } 
                while (dataConnection.KeepAlive);
                
                // allow chance of graceful close.
                await dataConnection.CloseAsync();
            }
            finally
            {
                //todo: pre-close connection on errors ??
                EndService(dataConnection);
            }
        }