Example #1
0
        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);
            }
        }
Example #2
0
 private static void SendXLetters(HyperSocket unicastSocket, int numberToSend)
 {
     for (int i = 0; i < numberToSend; i++)
     {
         unicastSocket.Send(new Letter {
             Options = LetterOptions.Ack | LetterOptions.Requeue, Type = LetterType.User, Parts = new[] { Encoding.Unicode.GetBytes("Hej " + i) }
         });
     }
 }
Example #3
0
        private static void Main(string[] arg)
        {
            var options = new SocketOptions {
                ShutdownGrace = TimeSpan.FromMilliseconds(250)
            };
            var hyperSocket = new HyperSocket(options);

            var stopwatch = new Stopwatch();

            int sent = 0;

            hyperSocket.Sent += (letter, args) => {
                lock (SyncRoot) {
                    sent++;
                    if (sent % 1000 == 0)
                    {
                        Console.WriteLine("->" + sent);
                    }
                }
            };

            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);

            int received = 0;

            hyperSocket.Received += (letter, args) => {
                lock (SyncRoot) {
                    if (received == 0)
                    {
                        stopwatch.Restart();
                    }
                    received++;

                    if (received % 20000 == 0)
                    {
                        Console.WriteLine("<-" + received);
                    }
                    if (received % 100000 == 0)
                    {
                        Console.WriteLine("Received: " + received + " in " + stopwatch.ElapsedMilliseconds + " ms" + ". " + (received / stopwatch.ElapsedMilliseconds) + " letter/millisecond");
                        received = 0;
                    }
                }
            };

            int port = int.Parse(arg[0]);

            hyperSocket.Bind(IPAddress.Any, port);

            string line;

            while ((line = Console.ReadLine()) != null)
            {
                if (line == "exit")
                {
                    break;
                }

                if (line == "k")
                {
                    hyperSocket.Dispose();
                    Console.WriteLine("KILLED SOCKET");
                }
                else
                {
                    for (int m = 0; m < 1000; m++)
                    {
                        hyperSocket.Send(new Letter {
                            Type = LetterType.User, Options = LetterOptions.Multicast, Parts = new[] { new[] { (byte)'A' } }
                        });
                    }
                }
            }

            hyperSocket.Dispose();

            Thread.Sleep(500);
        }