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);
        }
Пример #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();
        }