public void SocketReceive() { const int port = 5050; var sockets = SocketEvents.GetTcpStreamSockets(port); try { var tcs = new TaskCompletionSource <object>(); int count = 0; sockets.Subscribe(s => { var data = s.CreateReceiver(); data.Subscribe( d => count += d.Count, tcs.SetException, () => tcs.SetResult(null)); }); var sender = SocketTestUtility.Connect(port); var bytes = 0; for (int i = 0; i < 100; i++) { bytes += SocketTestUtility.Send(sender, "This is a test<EOF>"); } sender.Close(5); tcs.Task.Wait(Defaults.MediumTestWaitTime); Console.WriteLine("Sent Bytes: {0} Received Bytes {1}", bytes, count); Assert.IsTrue(count == bytes); } finally { sockets.Dispose(); } }
public void SocketConnect() { const int port = 5050; const int clientCount = 10; var listener = SocketEvents.GetTcpStreamSockets(port); var countdown = new CountdownEvent(clientCount); try { var tcs = new TaskCompletionSource <object>(); int count = 0; listener.Subscribe(s => { count++; countdown.Signal(); s.Close(); }, tcs.SetException, () => tcs.TrySetResult(null)); for (int i = 0; i < clientCount; i++) { SocketTestUtility.Connect(port); } countdown.WaitEx(); Assert.IsTrue(count == clientCount); } finally { listener.Dispose(); } }
private IDisposable FixedLenghtWithAck() { var sockets = SocketEvents.GetTcpStreamSockets(_port); var monitor = new ConnectionRateMonitor(); sockets.GetConnections() .Subscribe(connection => { monitor.OnConnect(); connection .ToLengthPrefixed() .Subscribe(context => { var response = HandleMessage(context.Message); monitor.OnMessage(); context.Publish(response); }, ex => monitor.OnDisconnect(), monitor.OnDisconnect); }); monitor.Start(); return(sockets); }
public void SocketSend() { const int port = 5050; using (var sockets = SocketEvents.GetTcpStreamSockets(port)) { var tcs = new TaskCompletionSource <object>(); int count = 0; sockets.Subscribe(s => { var data = s.CreateReceiver(); data.Subscribe( d => count += d.Count, tcs.SetException, () => tcs.SetResult(null)); }); var socket = SocketTestUtility.Connect(port); var sender = socket.CreateSender(); var bytes = Encoding.ASCII.GetBytes("This is a test<EOF>"); sender.Publish(new ArraySegment <byte>(bytes, 0, bytes.Length)); sender.Dispose(); tcs.Task.Wait(Defaults.MediumTestWaitTime); Console.WriteLine("Sent Bytes: {0} Received Bytes {1}", bytes, count); Assert.IsTrue(count == bytes.Length); } }
private IDisposable SimpleReceiver() { var sockets = SocketEvents.GetTcpStreamSockets(_port); var monitor = new ConnectionRateMonitor(); sockets.Subscribe(s => { monitor.OnConnect(); var receiver = s.CreateReceiver(); receiver.Subscribe( d => monitor.OnMessage(), ex => monitor.OnDisconnect(), monitor.OnDisconnect); }); monitor.Start(); return(sockets); }
public void SocketReceive1Gb() { const int port = 5050; var payload = new byte[1024]; const int totalSize = 1024 * 1024 * 1024; var random = new Random(); random.NextBytes(payload); using (var sockets = SocketEvents.GetTcpStreamSockets(port)) { var tcs = new TaskCompletionSource <object>(); int count = 0; sockets.Subscribe(s => { var data = s.CreateReceiver(); data.Do(d => count += d.Count) .TakeWhile(_ => count <= totalSize) .Subscribe(_ => { }, tcs.SetException, () => tcs.SetResult(null)); }); var sender = SocketTestUtility.Connect(port); var bytes = 0; do { bytes += sender.Send(payload); } while (bytes < totalSize); sender.Close(5); tcs.Task.Wait(Defaults.MediumTestWaitTime); Console.WriteLine("Sent Bytes: {0} Received Bytes {1}", bytes, count); Assert.IsTrue(count == totalSize); } }