Beispiel #1
0
        static void Main(string[] args)
        {
            string url = "";

            if (args.Length >= 1)
            {
                url = args[0];
            }
            else
            {
                url = "127.0.0.1";//"tyrenus.gamextasy.com";
            }

            SimpleDispatcher dispatcher;
            MessageTcpClient client;

            dispatcher = SimpleDispatcher.CurrentDispatcher;
            var message = new Message {
                ID = 7, Name = "abc"
            };
            var serializer = new MessageSerializer();

            serializer.Register <Message>();

            // 初始化client
            client = new MessageTcpClient(serializer, (session, o) =>
            {
                dispatcher.Invoke(() =>
                {
                    Console.WriteLine((o as Message).ID);
                    Console.WriteLine((o as Message).Name);
                });
            });
            client.Connected += () =>
            {
                Console.WriteLine("connected");
                client.Send(message);
            };
            client.Disconnected += (e) =>
            {
                Console.WriteLine("disconnected:" + e.Message);
            };

            var ip = System.Net.Dns.GetHostAddresses(url);

            Console.WriteLine(ip.Length);
            Console.WriteLine(ip[0]);
            client.Connect(new IPEndPoint(ip[0], 9528));
            dispatcher.Run();
        }
        public void TestNetwork()
        {
            var dispatcher = SimpleDispatcher.CurrentDispatcher;
            var message    = new Message {
                ID = 7, Name = "abc"
            };
            var serializer = new MessageSerializer();

            serializer.Register <Message>(new MessageTypeID {
                CategoryID = 1, TypeID = 1
            });

            bool done = false;

            // 初始化client
            client = new MessageTcpClient(serializer, (session, o) =>
            {
                dispatcher.Invoke(() =>
                {
                    Assert.AreEqual(message.ID, (o as Message).ID);
                    Assert.AreEqual(message.Name, (o as Message).Name);
                    done = true;
                    dispatcher.Shutdown();
                });
            });
            client.Connected += () =>
            {
                client.Send(message);
            };

            // 初始化service
            service = new MessageTcpService(serializer, (session, o) =>
            {
                dispatcher.Invoke(() =>
                {
                    Assert.AreEqual(message.ID, (o as Message).ID);
                    Assert.AreEqual(message.Name, (o as Message).Name);
                    session.Send(message);
                });
            });

            var ip = new IPAddress(new byte[] { 127, 0, 0, 1 });

            service.Listen(new IPEndPoint(ip, 9527));
            client.Connect(new IPEndPoint(ip, 9527));
            dispatcher.Run();
            Assert.IsTrue(done);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            string url = "127.0.0.1";// "tyrenus.gamextasy.com";
            int    num = 2048;

            if (args.Length >= 1)
            {
                url = args[0];
            }

            if (args.Length >= 2)
            {
                num = int.Parse(args[1]);
            }

            var clients    = new List <MessageTcpClient>();
            var dispatcher = SimpleDispatcher.CurrentDispatcher;
            var serializer = new MessageSerializer();

            serializer.Register <Message>();

            for (int i = 0; i < num; ++i)
            {
                var message = new Message {
                    ID = i, Name = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                };

                // 初始化client
                var client = new MessageTcpClient(serializer, (session, o) =>
                {
                    dispatcher.Invoke(() =>
                    {
                        //Console.WriteLine((o as Message).ID);
                        session.Send(o);
                    });
                });
                var id = i;
                client.Connected += () =>
                {
                    Console.WriteLine("connected" + id);
                    client.Send(message);
                };
                client.Disconnected += (e) =>
                {
                    Console.WriteLine("disconnected:" + id + " " + e.Message);
                };
                client.MessageDeserializeFailed += (e) =>
                {
                    Console.WriteLine("MessageDeserializeFailed:" + id + " " + e.Message);
                };

                var ip = System.Net.Dns.GetHostAddresses(url);
                //Console.WriteLine(ip.Length);
                //Console.WriteLine(ip[0]);
                client.Connect(new IPEndPoint(ip[0], 9528));
                clients.Add(client);
                System.Threading.Thread.Sleep(30);
            }
            var timer = new Eddy.Timers.Timer();

            timer.Tick += () =>
            {
                Console.WriteLine("{0} clients connected.", clients.Where(x => x.IsConnected).Count());
            };
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Start();
            dispatcher.Run();
        }