async public static void TimerCallback(object objt) { while (true) { try { var tcpListen = objt as System.Net.Sockets.TcpListener; var listenList = new List <Task <System.Net.Sockets.TcpClient> >(); tcpListen.Server.UseOnlyOverlappedIO = true; while (tcpListen.Pending()) { listenList.Add(tcpListen.AcceptTcpClientAsync()); } //listenTimer.Change(100, 1); // Let's stop the timer from consuming too many threads bool restart = false; for (int i = 0; i < listenList.Count; i++) { var item = listenList[i]; var listen = await item; System.Threading.Interlocked.Increment(ref clientsConnected); if (item.IsCompleted) { TcpByteAgentHandler tcbah = new TcpByteAgentHandler(new TcpByteTransport(listen), TcpByteAgentHandler.HandlerType.ReceiveHeavy); tcbah.AddCommand(new ChainsAPM.Commands.Common.SendString("")); tcbah.HasData += tcbah_HasDataEvent; tcbah.Disconnected += tcbah_Disconnected; concurrentAgentHandlerList.GetOrAdd(tcbah.GetHashCode(), tcbah); } if (i == listenList.Count - 1 && restart == true) { i = 0; restart = false; } } } catch (Exception) { throw; } finally { } System.Threading.Thread.Sleep(10); } }
static void Main(string[] args) { Dictionary <int, ChainsAPM.Interfaces.ICommand <byte> > CommandList = new Dictionary <int, ICommand <byte> >(); var cmd1 = new ChainsAPM.Commands.Common.SendString(""); CommandList.Add(cmd1.Code, cmd1); CallContext.LogicalSetData("CommandProviders", CommandList); for (int i = 0; i < MAX; i++) { var whand = new System.Threading.ManualResetEventSlim(); System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((object o) => { var rand = new Random(); var hostname = System.Configuration.ConfigurationManager.AppSettings["hostname"]; var port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["port"]); System.Net.Sockets.TcpClient tcpC = new System.Net.Sockets.TcpClient(hostname, port); TcpByteAgentHandler tcbah = new TcpByteAgentHandler(new TcpByteTransport(tcpC), TcpByteAgentHandler.HandlerType.SendHeavy); tcbah.AddCommand(new ChainsAPM.Commands.Common.SendString("")); itemCounter.GetOrAdd(tcbah.GetHashCode(), System.Threading.Interlocked.Increment(ref counter2)); System.Threading.Interlocked.Increment(ref counter3); tcbah.HasData += tcbah_HasData; tcbah.Disconnected += tcbah_Disconnected; tcpC.NoDelay = true; int counter = 0; int msgCounter = 0; int stopCount = 10000; do { tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(5, 1000))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(5, 1000))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(5, 1000))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(5, 1000))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(5, 1000))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(10, 100))); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString(randString(5, 1000))); msgCounter += 72; System.Threading.Thread.Sleep(rand.Next(5, 25)); } while (++counter != stopCount); Console.WriteLine("Messages Sent: {0}", msgCounter); tcbah.SendCommand(new ChainsAPM.Commands.Common.SendString("Done!")); Console.WriteLine("Done Sent!"); lock (msgLock) { totalMessagesSent += msgCounter + 1; } })); } waitHandl.Wait(); Console.WriteLine("Messages Sent: {0}", totalMessagesSent); }