private static async Task SendTask(CancellationToken token) { await Task.Yield(); using (var socket = new HyperSocket()) { socket.Connected += (s, e) => Console.WriteLine("S: Connected"); socket.Connecting += (s, e) => Console.WriteLine("S: Connecting"); socket.Disconnected += (s, e) => Console.WriteLine("S: Disconnected"); socket.Disconnecting += (s, e) => Console.WriteLine("S: Disconnecting"); socket.Initialized += (s, e) => Console.WriteLine("S: Initialized"); socket.Disposed += (s, e) => Console.WriteLine("S: Disposed"); socket.Discarded += (l, e) => Console.WriteLine("S: Discarded"); socket.Queuing += (l, e) => Console.WriteLine($"S: Queuing: {l.UniqueId}"); socket.Received += (l, e) => Console.WriteLine("S: Received"); socket.Requeued += (l, e) => Console.WriteLine("S: Requeued"); socket.Sent += (l, e) => Console.WriteLine($"S: Sent: {l.UniqueId}"); socket.Send(new Letter { Type = LetterType.User, Parts = new[] { Encoding.UTF8.GetBytes("Letter before connect.") } }); socket.Connect(IPAddress.Loopback, 4711); socket.Send(new Letter { Type = LetterType.User, Parts = new[] { Encoding.UTF8.GetBytes("Letter after connect.") } }); token.WaitHandle.WaitOne(); socket.Disconnect(IPAddress.Loopback, 4711); } }
private static void Main() { var socketOptions = new SocketOptions { ReconnectInterval = TimeSpan.FromMilliseconds(100) }; var hyperSocket = new HyperSocket(socketOptions); int sent = 0; int received = 0; hyperSocket.Sent += (socket, letter) => { lock (SyncRoot) { sent++; if (sent % 20000 == 0) { Console.WriteLine("SENT: " + sent); } } }; hyperSocket.Received += (socket, letter) => { received++; if (received % 20000 == 0) { Console.WriteLine("RECEIVED: " + received); } }; hyperSocket.Discarded += (letter, args) => Console.WriteLine("DISCARDED: " + args.Binding + " " + Encoding.Unicode.GetString(letter.Parts[0])); hyperSocket.Requeued += (letter, args) => Console.WriteLine("REQUEUED: " + letter); hyperSocket.Queuing += (letter, args) => { }; hyperSocket.Disconnecting += (socket, args) => Console.WriteLine("DISCONNECTING" + args.Binding + " " + args.Reason); hyperSocket.Disconnected += (socket, args) => Console.WriteLine("DISCONNECTED " + args.Binding + " " + args.Reason); hyperSocket.Connected += (socket, args) => Console.WriteLine("CONNECTED " + args.Binding); hyperSocket.Initialized += (socket, args) => Console.WriteLine("INITIALIZED"); hyperSocket.Disposed += (socket, args) => {}; hyperSocket.Connect(IPAddress.Parse("127.0.0.1"), 8001); hyperSocket.Connect(IPAddress.Parse("127.0.0.1"), 8002); Console.WriteLine("Commands: "); Console.WriteLine("status\t\t- print number of sent and received letters"); Console.WriteLine("any number\t\t- send a number of letters"); Console.WriteLine("anything else\t- Send 1 000 000 letters"); Console.WriteLine("exit\t\t- exit"); string line; Console.Write("\n\nENTER COMMAND: "); while ((line = Console.ReadLine()) != null) { if (line == "exit") { return; } else if (line == "status") { WriteStatus(sent, received); } else if (line == "reconnect") { hyperSocket.Disconnect(IPAddress.Parse("127.0.0.1"), 8001); hyperSocket.Connect(IPAddress.Parse("127.0.0.1"), 8001); } else if (line != "") { SendXLetters(hyperSocket, int.Parse(line)); } else { SendXLetters(hyperSocket, 1000000); } } hyperSocket.Dispose(); }