public async Task Process()
        {
            Trace.WriteLine("Starting Controller.Process");

            await Connection.Open();

            ConnectionReader = new Reader(Connection.ConnectionStream);
            ConnectionWriter = new Writer(Connection.ConnectionStream);

            Trace.WriteLine("Connection open");

            RequestReader  requestReader  = new RequestReader(ConnectionReader, ProtocolFactory);
            ResponseWriter responseWriter = new ResponseWriter(ConnectionWriter);

            Trace.WriteLine("Starting to listen for requests");

            try
            {
                while (Connection.Connected)
                {
                    Trace.WriteLine("Listening for request");
                    var protocolObject = await requestReader.ParseNextRequest().ConfigureAwait(false);

                    await protocolObject.Process().ConfigureAwait(false);

                    await responseWriter.WriteResponseAsync(protocolObject).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Exception thrown {ex.Message}\n{ex.StackTrace}");
            }

            Trace.WriteLine("Connection no longer active, Controller.Process ending");
        }
Example #2
0
        public async Task <IProtocolObject> TryConsumeStreamObjectOfType(Type type)
        {
            //Read the next incoming request message
            await RequestReader.ParseNextRequest().ConfigureAwait(false);

            //Is it of the correct type
            if (RequestReader.GetObjectType() != type)
            {
                return(null);
            }

            //Create and return an object from the request message
            return(ProtocolObjectFactory.CreateObject(RequestReader.GetObjectType(), RequestReader.CurrentObjectData));
        }
        public async Task ProcessStreamObjects()
        {
            BreakProcessLoop = false;

            while (!BreakProcessLoop && await RequestReader.ParseNextRequest().ConfigureAwait(false))
            {
                var protocolObject = ProtocolObjectFactory.CreateObject(RequestReader.GetObjectType(), RequestReader.CurrentObjectData);
                protocolObject.ProtocolEvent += BreakLoopEvent;

                await protocolObject.Process(this).ConfigureAwait(false);
                await SendResponse(protocolObject).ConfigureAwait(false);

                Trace.Flush();
            }

            BreakProcessLoop = false;   //Ensure that any process loops that this one is running within still continue.
        }