コード例 #1
0
 public void StartServer(int i)
 {
     sockets[i] = HyperSocket.CreateServer("127.0.0.1", 8888, 500, new ServerListener(), new HyperSocketConfig()
     {
         UseSSL = true
     });
 }
コード例 #2
0
            public void OnTcpReceive(byte[] data, HyperSocket socket)
            {
                string str     = Encoding.UTF8.GetString(data);
                var    jsonObj = str.AsJObject();

                Console.WriteLine($"来自:{jsonObj["id"]}, 内容:{jsonObj["msg"]}");
            }
コード例 #3
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);
            }
        }
コード例 #4
0
 public void OnUdpReceive(byte[] data, HyperSocket socket)
 {
     // string str = Encoding.UTF8.GetString(data);
     // var num = int.Parse(str);
     // Console.WriteLine($"UDP Num:{str}");
     // socket.SendUdp((num + 1).ToString());
     // Console.WriteLine("Svr:" + Encoding.UTF8.GetString(data));
 }
コード例 #5
0
 public void StartClient(int i)
 {
     //threads[i] = new Thread(delegate ()
     {
         //Thread.Sleep(rd.Next(500, 5000));
         sockets[i] = HyperSocket.CreateClient("127.0.0.1", 8888, new ClientListener());
     }//);
      //threads[i].Start();
 }
コード例 #6
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) }
         });
     }
 }
コード例 #7
0
 public void SocketError(HyperSocket socket, Exception ex)
 {
     if (fff)
     {
         --num;
     }
     Console.WriteLine($"Connect Num:{num}");
     Console.WriteLine(ex.Message);
     Console.WriteLine(ex.StackTrace);
 }
コード例 #8
0
 public void StartClient()
 {
     socket = HyperSocket.CreateClient("127.0.0.1", 8888, new ClientListener());
     while (true)
     {
         var id      = Console.ReadLine();
         var str     = Console.ReadLine();
         var jsonObj = new JObject();
         jsonObj.Add("id", int.Parse(id));
         jsonObj.Add("msg", str);
         socket.SendUdp(jsonObj.AsBytes());
     }
 }
コード例 #9
0
 public void OnOpen(HyperSocket socket)
 {
     // timeCaller1[i] = new TimeCaller(rd.Next(1000, 5000), rd.Next(500, 1000), true, -1, () =>
     // {
     // socket.SendTcp(1.ToString());
     // });
     fff = true;
     Console.WriteLine($"Connect Num:{++num}");
     // Console.WriteLine($"Connect OK:{socket.SessionId}");
     timeCaller2[index++] = TimeCaller.Create(1000, 50, true, -1, (long count) =>
     {
         socket.SendUdp(count.ToString());
     });
 }
コード例 #10
0
            public void OnTcpReceive(byte[] data, HyperSocket socket)
            {
                // Console.WriteLine("Svr:" + Encoding.UTF8.GetString(data));
                if (data == null)
                {
                    Console.WriteLine("************************************"); return;
                }
                string str      = Encoding.UTF8.GetString(data);
                var    deltaNum = int.Parse(str) - lastNum - 1;

                lastNum = int.Parse(str);
                // var num = int.Parse(str);
                // if(num % 10000 == 0) Console.Clear();
                // Console.WriteLine($"SocketID:{socket.SessionId}, TCP Num:{str}, Delta:{deltaNum}");
                // socket.SendUdp((num + 1).ToString());
            }
コード例 #11
0
        private static async Task ReceiveTask(CancellationToken token)
        {
            await Task.Yield();

            using (var socket = new HyperSocket()) {
                socket.Connected     += (s, e) => Console.WriteLine("R: Connected");
                socket.Connecting    += (s, e) => Console.WriteLine("R: Connecting");
                socket.Disconnected  += (s, e) => Console.WriteLine("R: Disconnected");
                socket.Disconnecting += (s, e) => Console.WriteLine("R: Disconnecting");
                socket.Initialized   += (s, e) => Console.WriteLine("R: Initialized");
                socket.Disposed      += (s, e) => Console.WriteLine("R: Disposed");
                socket.Discarded     += (l, e) => Console.WriteLine("R: Discarded");
                socket.Queuing       += (l, e) => Console.WriteLine("R: Queuing");
                socket.Received      += (l, e) => Console.WriteLine($"R: Received({l.UniqueId}): {Encoding.UTF8.GetString(l.Parts[0])}");
                socket.Requeued      += (l, e) => Console.WriteLine("R: Requeued");
                socket.Sent          += (l, e) => Console.WriteLine("R: Sent");

                socket.Bind(IPAddress.Any, 4711);
                token.WaitHandle.WaitOne();

                socket.Unbind(IPAddress.Any, 4711);
            }
        }
コード例 #12
0
ファイル: BindProgram.cs プロジェクト: rossmurray/Hyperletter
        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);
        }
コード例 #13
0
        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();
        }
コード例 #14
0
 public void SocketError(HyperSocket socket, Exception ex)
 {
     Console.WriteLine($"客户端错误:{ex.Message}");
 }
コード例 #15
0
 public void OnUdpReceive(byte[] data, HyperSocket socket)
 {
     Console.WriteLine($"消息发送{Encoding.UTF8.GetString(data)}");
 }
コード例 #16
0
 public void OnOpen(HyperSocket socket)
 {
     Console.WriteLine($"连接服务器成功!聊天ID:{socket.SessionId}");
     Console.WriteLine($"发送信息:聊天ID + 回车 + 消息,聊天ID等于0为群发消息");
     Console.WriteLine($"例如:1 回车 HelloWorld");
 }