示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");


            ILogger           logger           = new SerilogLogger();
            ISerializer       serializer       = new ProtobufSerializer();
            IMessagePublisher messagePublisher = new MessagePublisher(logger, serializer);
            IOutgoingQueue    outgoingQueue    = new OutgoingQueue(logger, messagePublisher);

            messagePublisher.Start(9193);
            outgoingQueue.Start();

            IOrderRepository          orderRepository          = new OrderRepository();
            IOrderDispatcher          orderDispatcher          = new OrderDispatcher(outgoingQueue, logger, new DateService(), orderRepository);
            IIncomingMessageProcessor incomingMessageProcessor = new IncomingMessageProcessor(orderRepository, outgoingQueue, new DateService(), orderDispatcher, new ProtobufSerializer());
            IPerformanceRecorder      performanceRecorder      = new PerformanceRecorderDirectConsoleOutput(new DateService());
            IIncomingMessageQueue     incomingMessageQueue     = new IncomingMessageQueue(logger, incomingMessageProcessor, performanceRecorder);
            IClientMessagePuller      clientMessagePuller      = new ClientMessagePuller(logger, new ProtobufSerializer(), incomingMessageQueue);

            incomingMessageQueue.Start();
            clientMessagePuller.Start(9192);

            Console.WriteLine("Started. Hit any key to quit.");
            Console.ReadKey();

            Console.WriteLine("Stopping...");

            clientMessagePuller.Stop();
            incomingMessageQueue.Stop();
            outgoingQueue.Stop();
            messagePublisher.Stop();

            Console.WriteLine("Stopped");
        }
		public void TestIncomingMessageQueue()
		{
			var processed = new List<MessageSet>();
			var testQueue = new IncomingMessageQueue(processed.Add);

			var testSets = new[]
			{
				new MessageSet {Number = 2},
				new MessageSet {Number = 1},
				new MessageSet {Number = 3},
				new MessageSet {Number = 4},
				new MessageSet {Number = 5},
				new MessageSet {Number = 7},
				new MessageSet {Number = 8},
				new MessageSet {Number = 6},
				new MessageSet {Number = 9},
				new MessageSet {Number = 10}
			};

			foreach (var messageSet in testSets)
				testQueue.ProcessMessageSet(messageSet);

			Assert.AreEqual(processed.Count, 10);

			int numberTest = 1;
			foreach (var eventSet in processed)
				Assert.AreEqual(eventSet.Number, numberTest++);

			Assert.AreEqual(testQueue.NextMessageSetNumber, 11);
		}
示例#3
0
 public void Start()
 {
     outgoingMessageQueue = CreateOutgoingMessageQueue();
     outgoingMessageQueue.WaitForFirstMessage();
     incomingMessageQueue = CreateIncomingMessageQueue(outgoingMessageQueue);
     incomingMessageQueue.MessageReceived += OnMessageReceived;
     incomingMessageQueue.WaitForFirstMessage();
 }
示例#4
0
 private void StartCommunication(PipeStream readStream, PipeStream writeStream)
 {
     outgoingMessageQueue = CreateOutgoingMessageQueue(writeStream);
     outgoingMessageQueue.WaitForFirstMessage();
     incomingMessageQueue = CreateIncomingMessageQueue(readStream);
     incomingMessageQueue.MessageReceived += OnMessageReceived;
     incomingMessageQueue.WaitForFirstMessage();
 }
        public void TestIncomingMessageQueue()
        {
            var processed = new List <MessageSet>();
            var testQueue = new IncomingMessageQueue(processed.Add);

            var testSets = new[]
            {
                new MessageSet {
                    Number = 2
                },
                new MessageSet {
                    Number = 1
                },
                new MessageSet {
                    Number = 3
                },
                new MessageSet {
                    Number = 4
                },
                new MessageSet {
                    Number = 5
                },
                new MessageSet {
                    Number = 7
                },
                new MessageSet {
                    Number = 8
                },
                new MessageSet {
                    Number = 6
                },
                new MessageSet {
                    Number = 9
                },
                new MessageSet {
                    Number = 10
                }
            };

            foreach (var messageSet in testSets)
            {
                testQueue.ProcessMessageSet(messageSet);
            }

            Assert.AreEqual(processed.Count, 10);

            int numberTest = 1;

            foreach (var eventSet in processed)
            {
                Assert.AreEqual(eventSet.Number, numberTest++);
            }

            Assert.AreEqual(testQueue.NextMessageSetNumber, 11);
        }
示例#6
0
 /// <inheritdoc />
 public override Task <NetworkIncomingMessage <TPayloadReadType> > ReadMessageAsync(CancellationToken token)
 {
     return(IncomingMessageQueue.DequeueAsync(token));
 }
示例#7
0
        /// <summary>
        /// Reading the incoming messages from the network client and schedules them
        /// with the incoming message queue.
        /// </summary>
        /// <returns>A future which will complete when the client disconnects.</returns>
        private async Task EnqueueIncomingMessages()
        {
            //We need a token for canceling this task when a user disconnects
            CancellationToken incomingCancellationToken = CreateNewManagedCancellationTokenSource().Token;

            try
            {
                while (!incomingCancellationToken.IsCancellationRequested)
                {
                    NetworkIncomingMessage <TPayloadReadType> message = await UnmanagedClient.ReadAsync(incomingCancellationToken)
                                                                        .ConfigureAwait(false);

                    //If the message is null then the connection is no longer valid
                    //The socket likely disconnected so we should stop the network thread
                    if (message == null)
                    {
                        //We have to publish a null so it can be consumed by the user
                        //to know that the socket is dead
                        await IncomingMessageQueue.EnqueueAsync(null, CancellationToken.None)
                        .ConfigureAwait(false);

                        StopNetwork();
                    }


                    //if have to check the token again because the message may be null and may have been canceled mid-read
                    if (incomingCancellationToken.IsCancellationRequested)
                    {
                        continue;
                    }

                    //Try to notify interceptors of a payload that has come in. They may want it
                    if (!InterceptorManager.TryNotifyOutstandingInterceptors(message.Payload))
                    {
                        await IncomingMessageQueue.EnqueueAsync(message, incomingCancellationToken)
                        .ConfigureAwait(false);
                    }
                }
            }
            catch (TaskCanceledException e)
            {
                //This is an expected exception that happens when the token is canceled
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Expected Task Canceled Exception: {e.Message}\n\n Stack: {e.StackTrace}");
                }

                //We cannot rethrow because this can cause application instability on threadpools
            }
            catch (Exception e)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error($"Error: {e.Message}\n\n Stack: {e.StackTrace}");
                }

                //We cannot rethrow because this can cause application instability on threadpools
            }
            finally
            {
                try
                {
                    await DisconnectAsync(0);
                }
                catch (Exception)
                {
                }
            }

            //TODO: Should we do anything after the dispatch has stopped?
        }