Exemple #1
0
        static void Main(string[] args)
        {
            INetContext ctx = new NetContext();

            ctx.ApplicationName = "Testbed";
            ctx.Port            = 11111;
            ctx.MaxConnections  = 10;
            ctx.IPAddress       = "localhost";
            ctx.Services.Register <UserListService>();
            ctx.Services.Register <Distributed>();
            ctx.Services.Register <ModelTrackerService <TestDistModel> >();

            Router router = new Router();

            router.InitializeServer(ctx, new LidgrenServerProvider(), 11111, new LidgrenServerProvider(), 11112);
            router.CreatePassthrough <TestPacket>();

            router.StartShardServer();
            router.StartUserServer();

            Func <Shard> makeShard = () => {
                var sh = new Shard();
                var c  = ctx.Clone();
                c.Port = 11111;

                sh.InitializeClient(c, new LidgrenClientProvider());
                sh.Channel          = sh.CreateUserChannel <TestPacket>();
                sh.Channel.Receive += (e) => { Console.WriteLine("Recv" + e.Data.Data); };

                sh.StartClient();
                return(sh);
            };

            Func <User> makeUser = () => {
                var us = new User();
                var c  = ctx.Clone();
                c.Port = 11112;

                us.InitializeClient(c, new LidgrenClientProvider());
                us.Channel          = us.CreateShardChannel <TestPacket>();
                us.Channel.Receive += (e) => { Console.WriteLine("Recv " + e.Data.Data); };

                us.StartClient();
                return(us);
            };

            List <Shard> shards = new List <Shard>();
            List <User>  users  = new List <User>();

            for (int i = 0; i < 5; i++)
            {
                shards.Add(makeShard());
            }

            for (int i = 0; i < 5; i++)
            {
                users.Add(makeUser());
            }

            var svc  = users.First().Context.Services.Get <Distributed>();
            var svc2 = router.ShardController.Context.Services.Get <ModelTrackerService <TestDistModel> >();

            for (int i = 0; i < 100; i++)
            {
                svc2.Models.Add(new TestDistModel()
                {
                    Data = "Hello world " + i.ToString(),
                });
            }

            //svc.Channel.Send(new TestPacket() {
            //	Data = 10112
            //});

            var t = Environment.TickCount;

            while (true)
            {
                router.UpdateServer();
                foreach (var s in shards)
                {
                    s.UpdateClient();
                }
                foreach (var u in users)
                {
                    u.UpdateClient();
                }

                if (Environment.TickCount - t > 300)
                {
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
Exemple #2
0
        public void Initialize()
        {
            INetContext ctx = new NetContext();

            ctx.IPAddress = Guid.NewGuid().ToString();
            ctx.Port      = 0;

            var router = new RouterServer <TestRouterModel, TestShardModel, TestUserModel>();

            router.InitializeServer(ctx, new DirectServerProvider(), 0, new DirectServerProvider(), 1);
            router.CreatePassthrough <TestPacket>();

            router.StartShardServer();
            router.StartUserServer();

            this.Router = new RouterContainer()
            {
                Router = router,
            };

            Shards = new List <ShardContainer>();
            Users  = new List <UserContainer>();

            ShardsReceived = new List <ShardContainer>();
            UsersReceived  = new List <UserContainer>();

            for (int i = 0; i < 5; i++)
            {
                var shard = new ShardClient <TestRouterModel, TestShardModel, TestUserModel>();
                var user  = new UserClient <TestRouterModel, TestShardModel, TestUserModel>();

                var shctx = ctx.Clone();
                var usctx = ctx.Clone();
                shctx.Port = 0;
                usctx.Port = 1;

                shard.InitializeClient(shctx, new DirectClientProvider());
                user.InitializeClient(usctx, new DirectClientProvider());

                var shch = shard.CreateUserChannel <TestPacket>();
                var usch = user.CreateShardChannel <TestPacket>();

                var shcont = new ShardContainer()
                {
                    Shard       = shard,
                    Passthrough = shch,
                };

                var uscont = new UserContainer()
                {
                    User        = user,
                    Passthrough = usch,
                };

                shch.Receive += (e) => ShardsReceived.Add(shcont);
                usch.Receive += (e) => UsersReceived.Add(uscont);
                shard.StartClient();
                user.StartClient();

                Shards.Add(shcont);
                Users.Add(uscont);
            }

            var  tick = Environment.TickCount;
            bool run  = true;

            while (run)
            {
                int ct = 0;
                Router.Router.UpdateServer();
                foreach (var sh in Shards)
                {
                    sh.Shard.UpdateClient();
                    if (sh.Shard.Model != null)
                    {
                        ct++;
                    }
                }

                foreach (var us in Users)
                {
                    us.User.UpdateClient();
                    if (us.User.Model != null)
                    {
                        ct++;
                    }
                }

                if (ct == Users.Count + Shards.Count)
                {
                    run = false;
                }

                Assert.IsFalse(Environment.TickCount - tick > 200);
            }
        }