Ejemplo n.º 1
0
        /// <summary>
        /// Method responsible for start the clients.
        /// </summary>
        /// <param name="ip">The ip address from the server.</param>
        /// <param name="clientConnections">The number of client connections.</param>
        public static void StartClients(string ip, int clientConnections)
        {
            var kingBuffer = new KingBufferBase();

            kingBuffer.WriteString("Sometimes we just need a good networking library");

            _stopwatch = Stopwatch.StartNew();

            var clientFrequency = 14;

            var clients = new List <KingClient>();

            for (int i = 0; i < clientConnections; ++i)
            {
                var client = new KingClient();

                client.MessageReceivedHandler = OnMessageReceived;

                client.Connect(ip);
                clients.Add(client);

                Thread.Sleep(15);
            }

            Console.WriteLine("started all clients");

            var timer = new System.Timers.Timer(1000.0 / clientFrequency);

            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                foreach (var client in clients)
                {
                    if (client.HasConnected)
                    {
                        // send 2 messages each time
                        client.SendMessage(kingBuffer);
                        client.SendMessage(kingBuffer);

                        _messagesSent += 1;
                    }
                }

                // report every 10 seconds
                if (_stopwatch.ElapsedMilliseconds > 1000 * 10)
                {
                    long bandwithIn  = _dataReceived * 1000 / (_stopwatch.ElapsedMilliseconds * 1024);
                    long bandwithOut = _messagesSent * _bufferLength * 1000 / (_stopwatch.ElapsedMilliseconds * 1024);

                    Console.WriteLine(string.Format("Client in={0} ({1} KB/s)  out={2} ({3} KB/s) bufferL={4}",
                                                    _messagesReceived,
                                                    bandwithIn,
                                                    _messagesSent,
                                                    bandwithOut,
                                                    _bufferLength));
                    _stopwatch.Stop();
                    _stopwatch        = Stopwatch.StartNew();
                    _messagesSent     = 0;
                    _dataReceived     = 0;
                    _messagesReceived = 0;
                }
            };

            timer.AutoReset = true;
            timer.Enabled   = true;

            Console.ReadLine();
            timer.Stop();
            timer.Dispose();
        }