예제 #1
0
파일: Program.cs 프로젝트: riax/Clr
        private static void Main(string[] arg)
        {
            var options = new SocketOptions();
            options.ShutdownGrace = TimeSpan.FromSeconds(5);
            var unicastSocket = new HyperSocket(options);

            var stopwatch = new Stopwatch();

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

            unicastSocket.Disconnecting += (socket, args) => Console.WriteLine("DISCONNECTING" + args.Binding + " " + args.Reason);
            unicastSocket.Disconnected += (socket, args) => Console.WriteLine("DISCONNECTED " + args.Binding + " " + args.Reason);
            unicastSocket.Connected += (socket, args) => Console.WriteLine("CONNECTED " + args.Binding);

            int received = 0;
            unicastSocket.Received += (socket, letter) => {
                                          lock(unicastSocket) {
                                              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]);
            unicastSocket.Bind(IPAddress.Any, port);

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

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

            unicastSocket.Dispose();

            Thread.Sleep(500);
        }
예제 #2
0
        public HyperSocket(SocketOptions options)
        {
            Options = options;

            _factory          = new HyperletterFactory(BuildContainer());
            _letterDispatcher = _factory.CreateLetterDispatcher();

            _heartbeat = new Timer(Heartbeat, null, -1, -1);
            _heartbeat.Change(Options.Heartbeat.Interval, Options.Heartbeat.Interval);
        }
예제 #3
0
파일: HyperSocket.cs 프로젝트: riax/Clr
        public HyperSocket(SocketOptions options)
        {
            Options = options;

            _factory = new HyperletterFactory(BuildContainer());
            _letterDispatcher = _factory.CreateLetterDispatcher();

            _heartbeat = new Timer(Heartbeat);
            _heartbeat.Change(Options.Heartbeat.Interval, Options.Heartbeat.Interval);
        }
예제 #4
0
파일: Program.cs 프로젝트: riax/Clr
        private static void Main()
        {
            var socketOptions = new SocketOptions();
            var unicastSocket = new HyperSocket(socketOptions);

            int sent = 0;

            unicastSocket.Sent += (socket, letter) => {
                                      lock(SyncRoot) {
                                          sent++;

                                          if(sent%20000 == 0)
                                              Console.WriteLine("SENT: " + sent);
                                      }
                                  };

            int received = 0;
            unicastSocket.Received += (socket, letter) => {
                                          received++;

                                          if(received%20000 == 0)
                                              Console.WriteLine("RECEIVED: " + received);
                                      };

            unicastSocket.Discarded += (letter, args) => Console.WriteLine("DISCARDED: " + args.Binding + " " + Encoding.Unicode.GetString(letter.Parts[0]));
            unicastSocket.Requeued += (letter, args) => Console.WriteLine("REQUEUED: " + letter);

            unicastSocket.Disconnecting += (socket, args) => Console.WriteLine("DISCONNECTING" + args.Binding + " " + args.Reason);
            unicastSocket.Disconnected += (socket, args) => {
                {
                    Console.WriteLine("DISCONNECTED " + args.Binding + " " + args.Reason);
                }
                                          };
            unicastSocket.Connected += (socket, args) => Console.WriteLine("CONNECTED " + args.Binding);
            int x = 0;
            unicastSocket.Initialized += (socket, args) => {
                                             //    unicastSocket.Dispose();
                                         };
            unicastSocket.Disposed += (socket, args) => unicastSocket.Connect(IPAddress.Parse("127.0.0.1"), 8001);

            unicastSocket.Connect(IPAddress.Parse("127.0.0.1"), 8001);
            //unicastSocket.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") {
                    unicastSocket.Disconnect(IPAddress.Parse("127.0.0.1"), 8001);
                    unicastSocket.Connect(IPAddress.Parse("127.0.0.1"), 8001);
                } else if(line != "")
                    SendXLetters(unicastSocket, int.Parse(line));
                else
                    SendXLetters(unicastSocket, 1000000);
            }

            unicastSocket.Dispose();
        }