コード例 #1
0
        private static void StartServer(int totalMessages, int totalClients)
        {
            var server = new MqServer <SimpleMqSession, MqConfig>(new MqConfig()
            {
                Ip   = "127.0.0.1",
                Port = 2828
            });

            var builder = new MqMessageWriter((MqConfig)server.Config);

            builder.Write("COMPLETE");

            var completeMessage = builder.ToMessage(true);

            builder.Write("START");
            var startMessage = builder.ToMessage(true);

            ConcurrentDictionary <SimpleMqSession, ClientRunInfo> clientsInfo =
                new ConcurrentDictionary <SimpleMqSession, ClientRunInfo>();


            server.Connected += (sender, session) =>
            {
                var currentInfo = new ClientRunInfo()
                {
                    Session = session.Session,
                    Runs    = 0
                };
                clientsInfo.TryAdd(session.Session, currentInfo);

                if (clientsInfo.Count == totalClients)
                {
                    foreach (var mqSession in clientsInfo.Keys)
                    {
                        mqSession.Send(startMessage);
                    }
                }
            };

            server.Closed += (session, value) =>
            {
                ClientRunInfo info;
                clientsInfo.TryRemove(value.Session, out info);
            };

            server.IncomingMessage += (sender, args) =>
            {
                var clientInfo = clientsInfo[args.Session];

                // Count the total messages.
                clientInfo.Runs += args.Messages.Count;

                if (clientInfo.Runs == totalMessages)
                {
                    args.Session.Send(completeMessage);
                    args.Session.Send(startMessage);
                    clientInfo.Runs = 0;
                }
            };


            server.Start();
        }
コード例 #2
0
        public void MessageWriter_writes_bool_true()
        {
            var expectedValue = (bool)true;

            _messageBuilder.Write(expectedValue);
            var message = _messageBuilder.ToMessage();

            _messageReader.Message = message;

            Assert.Equal(expectedValue, _messageReader.ReadBoolean());
            Assert.True(_messageReader.IsAtEnd);
        }