private void RunClientSubscriber(IConnection <SimpleMessage <int>, int> connection, IConnectionStubListener <SimpleMessage <int>, int> listener, ushort port) { listener.BindTo(new IPEndPoint(IPAddress.Any, port)); var listenerTask = Task.Run(() => RunListenerSubscriber(listener)); var ready = false; connection.Subscribe(0, (message, size) => ready = true); connection.Subscribe(1, (message, size) => TextMessageHandler("Client", message, size)); connection.Connect(new IPEndPoint(IPAddress.Loopback, port)); while (!ready) { Thread.CurrentThread.Join(10); } const int messages = 500; Console.WriteLine(@"Sending {0} messages with random data", 5); var rnd = new Random(); for (var i = 0; i < messages; ++i) { var data = new byte[rnd.Next(16, 256)]; rnd.NextBytes(data); connection.Send(new SimpleMessage <int>(1, Encoding.ASCII.GetBytes(Convert.ToBase64String(data)))); } Console.WriteLine(@"Client: Done, sending exit message"); connection.Send(new SimpleMessage <int>(2)); connection.Disconnect(); Console.WriteLine(@"Waiting for listener thread to exit"); listenerTask.Wait(); Console.WriteLine(@"Listener thread has exited"); Console.WriteLine(); Console.WriteLine(@"Press any key to exit"); Console.ReadKey(); }
private void RunClientSubscriber(IConnection<SimpleMessage<int>, int> connection, IConnectionStubListener<SimpleMessage<int>, int> listener, ushort port) { listener.BindTo(new IPEndPoint(IPAddress.Any, port)); var listenerTask = Task.Run(() => RunListenerSubscriber(listener)); var ready = false; connection.Subscribe(0, (message, size) => ready = true); connection.Subscribe(1, (message, size) => TextMessageHandler("Client", message, size)); connection.Connect(new IPEndPoint(IPAddress.Loopback, port)); while (!ready) Thread.CurrentThread.Join(10); const int messages = 500; Console.WriteLine(@"Sending {0} messages with random data", 5); var rnd = new Random(); for (var i = 0; i < messages; ++i) { var data = new byte[rnd.Next(16, 256)]; rnd.NextBytes(data); connection.Send(new SimpleMessage<int>(1, Encoding.ASCII.GetBytes(Convert.ToBase64String(data)))); } Console.WriteLine(@"Client: Done, sending exit message"); connection.Send(new SimpleMessage<int>(2)); connection.Disconnect(); Console.WriteLine(@"Waiting for listener thread to exit"); listenerTask.Wait(); Console.WriteLine(@"Listener thread has exited"); Console.WriteLine(); Console.WriteLine(@"Press any key to exit"); Console.ReadKey(); }
public void Start(EndPoint localEndpoint) { if (isStarted) { return; } isStarted = true; listener.BindTo(localEndpoint); connectionWorker = threadManager.StartNew(AcceptConnections); }