示例#1
0
 /// <summary>
 /// The try get.
 /// </summary>
 /// <param name="name">
 /// The world name.
 /// </param>
 /// <param name="world">
 /// The world.
 /// </param>
 /// <returns>
 /// true if found.
 /// </returns>
 public bool TryGet(string name, out MmoWorld world)
 {
     using (ReadLock.TryEnter(this.readWriteLock, Settings.MaxLockWaitTimeMilliseconds))
     {
         return(this.dict.TryGetValue(name, out world));
     }
 }
示例#2
0
        /// <summary>
        /// The try create.
        /// </summary>
        /// <param name="name">
        /// The world name.
        /// </param>
        /// <param name="topLeftCorner">
        /// The top left corner.
        /// </param>
        /// <param name="bottomRightCorner">
        /// The bottom right corner.
        /// </param>
        /// <param name="tileDimensions">
        /// The tile dimensions.
        /// </param>
        /// <param name="world">
        /// The world.
        /// </param>
        /// <returns>
        /// true if create.
        /// </returns>
        public bool TryCreate(string name, Vector topLeftCorner, Vector bottomRightCorner, Vector tileDimensions, out MmoWorld world)
        {
            using (WriteLock.TryEnter(this.readWriteLock, Settings.MaxLockWaitTimeMilliseconds))
            {
                if (this.dict.TryGetValue(name, out world))
                {
                    return(false);
                }

                world = new MmoWorld(name, topLeftCorner, bottomRightCorner, tileDimensions);
                this.dict.Add(name, world);
                return(true);
            }
        }
示例#3
0
        /// <summary>
        /// The setup clients. creates 2 clients per region.
        /// </summary>
        /// <param name="world">
        /// The world.
        /// </param>
        /// <returns>
        /// the list of created clients
        /// </returns>
        private static List<Client> SetupClients(MmoWorld world)
        {
            Stopwatch t = Stopwatch.StartNew();

            var clients = new List<Client>();
            for (int x = world.Area.Min.X + (world.TileDimensions.X / 2); x < world.Area.Max.X; x += world.TileDimensions.X)
            {
                for (int y = world.Area.Min.Y + (world.TileDimensions.Y / 2);
                     y < world.Area.Max.Y;
                     y += world.TileDimensions.Y)
                {
                    string name = string.Format("MyUsername{0}/{1}", x, y);
                    var client = new Client(name);
                    EnterWorldBegin(client, world, new[] { x / 100f, y / 100f });

                    clients.Add(client);
                    clientCount++;

                    Sleep();
                }

                for (int y = world.Area.Min.Y + (world.TileDimensions.Y / 2) + 1;
                     y < world.Area.Max.Y;
                     y += world.TileDimensions.Y)
                {
                    string name = string.Format("MyUsername{0}/{1}", x + 1, y);
                    var client = new Client(name);
                    EnterWorldBegin(client, world, new[] { (x + 1) / 100f, y / 100f });

                    clients.Add(client);
                    clientCount++;

                    Sleep();
                }
            }

            clients.ForEach(EnterWorldEnd);
            clients.ForEach(c => BeginReceiveEvent(c, EventCode.ItemSubscribed, d => true));
            clients.ForEach(c => EndReceiveEvent(c, EventCode.ItemSubscribed));
            PrintStats(t);

            Assert.AreEqual(0, Client.Exceptions, "Exceptions occured at start");
            log.Info("enter completed");

            Assert.AreEqual(0, Client.Exceptions, "Exceptions occured at exit");

            return clients;
        }
示例#4
0
        /// <summary>
        /// The enter world.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <param name="world">
        /// The world.
        /// </param>
        /// <param name="position">
        /// The position.
        /// </param>
        private static void EnterWorldBegin(Client client, MmoWorld world, float[] position)
        {
            var viewDistanceEnter = new[] { 1f, 1f };
            var viewDistanceExit = new[] { 2f, 2f };

            client.Position = position;

            ThreadPoolEnqueue(
                client,
                () =>
                {
                    client.SendOperation(Operations.EnterWorld(world.Name, client.Username, null, position, viewDistanceEnter, viewDistanceExit));
                    client.BeginReceiveResponse();
                });
        }
 /// <summary>
 /// The try get.
 /// </summary>
 /// <param name="name">
 /// The world name.
 /// </param>
 /// <param name="world">
 /// The world.
 /// </param>
 /// <returns>
 /// true if found.
 /// </returns>
 public bool TryGet(string name, out MmoWorld world)
 {
     using (ReadLock.TryEnter(this.readWriteLock, Settings.MaxLockWaitTimeMilliseconds))
     {
         return this.dict.TryGetValue(name, out world);
     }
 }
        /// <summary>
        /// The try create.
        /// </summary>
        /// <param name="name">
        /// The world name.
        /// </param>
        /// <param name="topLeftCorner">
        /// The top left corner.
        /// </param>
        /// <param name="bottomRightCorner">
        /// The bottom right corner.
        /// </param>
        /// <param name="tileDimensions">
        /// The tile dimensions.
        /// </param>
        /// <param name="world">
        /// The world.
        /// </param>
        /// <returns>
        /// true if create.
        /// </returns>
        public bool TryCreate(string name, Vector topLeftCorner, Vector bottomRightCorner, Vector tileDimensions, out MmoWorld world)
        {
            using (WriteLock.TryEnter(this.readWriteLock, Settings.MaxLockWaitTimeMilliseconds))
            {
                if (this.dict.TryGetValue(name, out world))
                {
                    return false;
                }

                world = new MmoWorld(name, topLeftCorner, bottomRightCorner, tileDimensions);
                this.dict.Add(name, world);
                return true;
            }
        }
示例#7
0
        /// <summary>
        ///   The setup clients.
        /// </summary>
        /// <param name = "world">
        ///   The world.
        /// </param>
        /// <param name = "clients">
        ///   The clients.
        /// </param>
        /// <param name = "t">
        ///   The stopwatch.
        /// </param>
        private static void SetupClients(MmoWorld world, List<Client> clients, Stopwatch t)
        {
            for (int x = world.Area.Min.X + (world.TileDimensions.X / 2); x < world.Area.Max.X; x += world.TileDimensions.X)
            {
                for (int y = world.Area.Min.Y + (world.TileDimensions.Y / 2); y < world.Area.Max.Y; y += world.TileDimensions.Y)
                {
                    string name = string.Format("MyUsername{0}/{1}", x, y);
                    var client = new Client(name, new[] { x / 100f, y / 100f });
                    client.BeginConnect();
                    clients.Add(client);
                    clientCount++;
                }

                for (int y = world.Area.Min.Y + (world.TileDimensions.Y / 2) + 1; y < world.Area.Max.Y; y += world.TileDimensions.Y)
                {
                    string name = string.Format("MyUsername{0}/{1}", x + 1, y);
                    var client = new Client(name, new[] { (x + 1) / 100f, y / 100f });
                    client.BeginConnect();
                    clients.Add(client);
                    clientCount++;
                }
            }

            clients.ForEach(c => Assert.IsTrue(c.EndConnect()));
            log.InfoFormat("connect completed, {0} clients", clientCount);

            clients.ForEach(c => EnterWorldBegin(c, world));
            clients.ForEach(EnterWorldEnd);
            clients.ForEach(c => BeginReceiveEvent(c, EventCode.ItemSubscribed, d => (string)d[(byte)ParameterCode.ItemId] != c.Username, 500));
            clients.ForEach(c => EndReceiveEvent(c, EventCode.ItemSubscribed));
            PrintStats(t);
            Client.ResetStats();
            Assert.AreEqual(0, Client.Exceptions, "Exceptions occured at start");
            log.Info("enter completed");
        }
示例#8
0
        /// <summary>
        ///   The enter world.
        /// </summary>
        /// <param name = "client">
        ///   The client.
        /// </param>
        /// <param name = "world">
        ///   The world.
        /// </param>
        private static void EnterWorldBegin(Client client, MmoWorld world)
        {
            var viewDistanceEnter = new[] { 1f, 1f };
            var viewDistanceExit = new[] { 2f, 2f };

            ThreadPoolEnqueue(
                client,
                () =>
                    {
                        Operations.EnterWorld(client, world.Name, client.Username, null, client.Position, viewDistanceEnter, viewDistanceExit);
                        client.BeginReceiveResponse(10);
                    });
        }
示例#9
0
        /// <summary>
        ///   The create world.
        /// </summary>
        /// <param name = "world">
        ///   The world.
        /// </param>
        /// <param name = "client">
        ///   The client.
        /// </param>
        private static void CreateWorld(MmoWorld world, Client client)
        {
            Operations.CreateWorld(client, world.Name, world.Area.Min.ToFloatArray(2), world.Area.Max.ToFloatArray(2), world.TileDimensions.ToFloatArray(2));

            client.BeginReceiveResponse(0);

            OperationResponse data;
            Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received");
            Assert.IsTrue(data.ReturnCode == (int)ReturnCode.Ok || data.ReturnCode == (int)ReturnCode.WorldAlreadyExists);
        }