Exemple #1
0
        static void Main(string[] args)
        {
            var dispatcher = SimpleDispatcher.CurrentDispatcher;
            var serializer = new MessageSerializer();

            serializer.Register <Message>();
            serializer.Register <string>();
            HashSet <TcpSession> sessions = new HashSet <TcpSession>();

            // 初始化service
            var service = new MessageTcpService(serializer, (session, o) =>
            {
                dispatcher.Invoke(() =>
                {
                    session.Send(o);
                });
            },
                                                () =>
            {
                var session = new TcpSession();
                Console.WriteLine("connected");
                sessions.Add(session);
                session.Disconnected += (e) =>
                {
                    Console.WriteLine("Disconnected:" + e.Message);
                    sessions.Remove(session);
                };
                return(session);
            });

            service.MessageDeserializeFailed += (e) => { Console.WriteLine(e.Message); };
            var ip = IPAddress.Any;

            service.Listen(new IPEndPoint(ip, 9528));
            var timer = new Eddy.Timers.Timer();

            timer.Tick += () =>
            {
                Console.WriteLine("{0} clients connected.", sessions.Count);
            };
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Start();
            dispatcher.Run();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var dispatcher = SimpleDispatcher.CurrentDispatcher;
            var serializer = new MessageSerializer();
            serializer.Register<Message>();
            serializer.Register<string>();
            HashSet<TcpSession> sessions = new HashSet<TcpSession>();

            // 初始化service
            var service = new MessageTcpService(serializer, (session, o) =>
            {
                dispatcher.Invoke(() =>
                {
                    session.Send(o);
                });
            },
            () =>
            {
                var session = new TcpSession();
                Console.WriteLine("connected");
                sessions.Add(session);
                session.Disconnected += (e) =>
                {
                    Console.WriteLine("Disconnected:" + e.Message);
                    sessions.Remove(session);
                };
                return session;
            });
            service.MessageDeserializeFailed += (e) => { Console.WriteLine(e.Message); };
            var ip = IPAddress.Any;
            service.Listen(new IPEndPoint(ip, 9528));
            var timer = new Eddy.Timers.Timer();
            timer.Tick += () =>
            {
                Console.WriteLine("{0} clients connected.", sessions.Count);
            };
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Start();
            dispatcher.Run();
        }
Exemple #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();
        }
Exemple #4
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();
        }