Exemplo n.º 1
0
        public void AttachCamera()
        {
            using (var client = new Client("Test"))
            {
                SpawnItem(client);

                client.SendOperation(Operations.AttachCamera("MyItem", byte.MaxValue));
                client.BeginReceiveResponse();

                OperationResponse data;
                Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Event not received");
                Assert.AreEqual(data.ReturnCode, (int)ReturnCode.Ok);
                Func<OperationResponse, bool> checkAction =
                    e => (string)e.Parameters[(byte)ParameterCode.ItemId] == "MyItem" && (byte)e.Parameters[(byte)ParameterCode.ItemType] == byte.MaxValue;
                Assert.IsTrue(checkAction(data), "check action failed");
            }
        }
Exemplo n.º 2
0
        public void CreateWorld()
        {
            using (var client = new Client("Test"))
            {
                client.ResetEvent();
                client.SendOperation(Operations.CreateWorld("CreateWorld", new BoundingBox(new Vector(0f, 0f, 0f), new Vector(10f, 10f, 0f)), new Vector(1f, 1f, 0f)));
                client.BeginReceiveResponse();
                OperationResponse data;
                Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received"); // blocking
                Assert.AreEqual(data.ReturnCode, (int)ReturnCode.Ok);

                // "Test" defined in setup
                client.ResetEvent();
                client.SendOperation(Operations.CreateWorld("TestWorld", new BoundingBox(new Vector(0f, 0f, 0f), new Vector(10f, 10f, 0f)), new Vector(1f, 1f, 0f)));
                client.BeginReceiveResponse();
                Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received"); // blocking
                Assert.AreEqual(data.ReturnCode, (byte)ReturnCode.WorldAlreadyExists);
            }
        }
Exemplo n.º 3
0
        private static void SpawnItem(Client client)
        {
            EnterWorld(client, "TestWorld", new Vector(1f, 1f, 0f), new Vector(1f, 1f, 0f), new Vector(2f, 2f, 0f), null);

            client.ResetEvent();
            client.SendOperation(Operations.SpawnItem("MyItem", ItemType.Bot, new Vector(1f, 1f, 0f), null, true));
            OperationResponse data = ReceiveOperationResponse(client); // blocking

            Assert.AreEqual(data.ReturnCode, (int)ReturnCode.Ok, "SpawnItem op error:" + data.ReturnCode + " " + data.DebugMessage);

            // move item to view area
            client.ResetEvent();
            client.SendOperation(Operations.Move("MyItem", new Vector(1f, 1f, 0f)));
            NotReceiveOperationResponse(client); // blocking

            // test not existing item move
            client.ResetEvent();
            client.SendOperation(Operations.Move("NotExistsing", new Vector(1f, 1f, 0f)));
            ReceiveOperationResponse(client, ReturnCode.ItemNotFound); // blocking
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
0
 /// <summary>
 /// The client movement.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 private static void Move(Client client)
 {
     client.Peer.RequestFiber.Enqueue(() => MoveAction(client, 1));
 }
Exemplo n.º 6
0
 /// <summary>
 /// The exit world begin.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 private static void ExitWorldBegin(Client client)
 {
     ThreadPoolEnqueue(
         client,
         () =>
         {
             client.SendOperation(Operations.ExitWorld());
             BeginReceiveEvent(client, EventCode.WorldExited);
         });
 }
Exemplo n.º 7
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();
                });
        }
Exemplo n.º 8
0
 /// <summary>
 /// The begin receive event.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 /// <param name="eventCode">
 /// The event code.
 /// </param>
 private static void BeginReceiveEvent(Client client, EventCode eventCode)
 {
     client.BeginReceiveEvent(eventCode, d => true);
 }
Exemplo n.º 9
0
        public void SetProperties()
        {
            using (var client = new Client("Test"))
            {
                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);
                client.SendOperation(Operations.SetProperties(null, null, new Hashtable { { "Key", "Value" } }, null));

                NotReceiveOperationResponse(client);
            }
        }
Exemplo n.º 10
0
        public void RaiseGenericEvent()
        {
            using (var client = new Client("Test"))
            {
                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);

                ////client.SendOperation(Operations.RaiseGenericEvent(null, null, byte.MaxValue, null, Reliability.Reliable, EventReceiver.WorldRegion));
                Func<EventData, bool> checkAction =
                    d =>
                    (string)d.Parameters[(byte)ParameterCode.ItemId] == client.Username && (byte)d.Parameters[(byte)ParameterCode.ItemType] == (byte)ItemType.Avatar &&
                    (byte)d.Parameters[(byte)ParameterCode.CustomEventCode] == byte.MaxValue;

                ////client.BeginReceiveEvent(EventCode.ItemGeneric, checkAction);
                EventData data;

                ////Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out data), "Event not received - target avatar world region");
                ////Assert.AreEqual((byte)data.Code, (byte)EventCode.ItemGeneric);
                ////Assert.IsTrue(checkAction(data), "check action failed - target avatar world region");
                client.SendOperation(Operations.RaiseGenericEvent(null, null, byte.MaxValue, null, Reliability.Reliable, EventReceiver.ItemOwner));
                client.BeginReceiveEvent(EventCode.ItemGeneric, checkAction);
                Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out data), "Event not received - target avatar");
                Assert.AreEqual(data.Code, (byte)EventCode.ItemGeneric);
                Assert.IsTrue(checkAction(data), "check action failed - target avatar");

                client.SendOperation(Operations.RaiseGenericEvent(null, null, byte.MaxValue, null, Reliability.Reliable, EventReceiver.ItemSubscriber));
                client.BeginReceiveEvent(EventCode.ItemGeneric, checkAction);
                Assert.IsFalse(client.EndReceiveEvent(Settings.WaitTime, out data), "Event received - but avatar not subscribed");

                // subscribe own avatar
                client.SendOperation(Operations.SubscribeItem(client.Username, (byte)ItemType.Avatar, null));
                client.SendOperation(Operations.RaiseGenericEvent(null, null, byte.MaxValue, null, Reliability.Reliable, EventReceiver.ItemSubscriber));
                client.BeginReceiveEvent(EventCode.ItemGeneric, checkAction);
                Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out data), "Event not received - target avatar subscriber");
                Assert.AreEqual(data.Code, (byte)EventCode.ItemGeneric);
                Assert.IsTrue(checkAction(data), "check action failed - target avatar subscriber");
            }
        }
Exemplo n.º 11
0
        public void ExitWorld()
        {
            using (var client = new Client("Test"))
            {
                client.SendOperation(Operations.ExitWorld());
                ReceiveOperationResponse(client, ReturnCode.InvalidOperation);

                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);
                ExitWorld(client);
            }
        }
Exemplo n.º 12
0
        public void EnterWorld()
        {
            using (var client = new Client("Test"))
            {
                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);

                client.SendOperation(Operations.EnterWorld("TestWorld", client.Username, null, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }));
                ReceiveOperationResponse(client, ReturnCode.InvalidOperation);

                using (var client2 = new Client("Test"))
                {
                    EnterWorld(client2, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);

                    EventData @event;
                    client.BeginReceiveEvent(EventCode.WorldExited, d => true);
                    Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out @event), "Event not received");
                    Assert.AreEqual((byte)EventCode.WorldExited, @event.Code);

                    Assert.IsFalse(client.Peer.Connected);
                    //// client.SendOperation(Operations.Move(null, null, new[] { 2f, 2f }));
                    //// ReceiveOperationResponse(client, ReturnCode.InvalidOperation);
                }
            }
        }
Exemplo n.º 13
0
        public void DetachCamera()
        {
            using (var client = new Client("Test"))
            {
                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);

                client.SendOperation(Operations.DetachCamera());
                client.BeginReceiveResponse();

                OperationResponse data;
                Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received");
                Assert.AreEqual(data.ReturnCode, (int)ReturnCode.Ok);
            }
        }
Exemplo n.º 14
0
        public void DestroyItem()
        {
            using (var client = new Client("Test"))
            {
                SpawnItem(client);
                client.SendOperation(Operations.DestroyItem("MyItem", byte.MaxValue));
                Func<EventData, bool> checkAction =
                    e => (string)e.Parameters[(byte)ParameterCode.ItemId] == "MyItem" && (byte)e.Parameters[(byte)ParameterCode.ItemType] == byte.MaxValue;
                client.BeginReceiveEvent(EventCode.ItemDestroyed, checkAction);

                EventData data;
                Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out data), "Event not received");
                Assert.AreEqual(data.Code, (byte)EventCode.ItemDestroyed);
                Assert.IsTrue(checkAction(data), "check action failed");
                client.EndReceiveEvent(Settings.WaitTime, out data);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// The receive operation response.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <returns>
        /// the operation response
        /// </returns>
        private static OperationResponse ReceiveOperationResponse(Client client)
        {
            client.BeginReceiveResponse();

            OperationResponse data;
            Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received");
            return data;
        }
Exemplo n.º 16
0
        public void SetViewDistance()
        {
            using (var client = new Client("Test"))
            {
                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);
                client.SendOperation(Operations.SetViewDistance(new[] { 2f, 2f }, new[] { 3f, 3f }));

                NotReceiveOperationResponse(client);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// The spawn item.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        private static void SpawnItem(Client client)
        {
            EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);
            client.SendOperation(Operations.SpawnItem("MyItem", byte.MaxValue, new[] { 1f, 1f }, null, true));

            OperationResponse data = ReceiveOperationResponse(client);

            Assert.AreEqual("MyItem", data.Parameters[(byte)ParameterCode.ItemId]);
            Assert.AreEqual(byte.MaxValue, data.Parameters[(byte)ParameterCode.ItemType]);

            if (data.ReturnCode != (int)ReturnCode.Ok)
            {
                // move item to view area
                client.SendOperation(Operations.Move("MyItem", byte.MaxValue, new[] { 1f, 1f }));

                ReceiveOperationResponse(client, ReturnCode.Ok);
            }
        }
Exemplo n.º 18
0
 public void SpawnItem()
 {
     using (var client = new Client("Test"))
     {
         SpawnItem(client);
     }
 }
Exemplo n.º 19
0
 /// <summary>
 /// The begin receive event.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 /// <param name="eventCode">
 /// The event code.
 /// </param>
 /// <param name="checkAction">
 /// The check action.
 /// </param>
 private static void BeginReceiveEvent(Client client, EventCode eventCode, Func<EventData, bool> checkAction)
 {
     client.BeginReceiveEvent(eventCode, checkAction);
 }
Exemplo n.º 20
0
        public void UnsubscribeItem()
        {
            using (var client = new Client("Test"))
            {
                EnterWorld(client, "TestWorld", new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 2f, 2f }, null);
                client.SendOperation(Operations.SubscribeItem(client.Username, (byte)ItemType.Avatar, null));
                client.BeginReceiveEvent(EventCode.ItemSubscribed, d => (string)d.Parameters[(byte)ParameterCode.ItemId] == client.Username);
                EventData data;
                client.EndReceiveEvent(Settings.WaitTime, out data);
                Assert.AreEqual(data.Code, (byte)EventCode.ItemSubscribed);

                client.SendOperation(Operations.UnsubscribeItem(client.Username, (byte)ItemType.Avatar));
                client.BeginReceiveEvent(EventCode.ItemUnsubscribed, d => (string)d.Parameters[(byte)ParameterCode.ItemId] == client.Username);
                client.EndReceiveEvent(Settings.WaitTime, out data);
                Assert.AreEqual(data.Code, (byte)EventCode.ItemUnsubscribed);

                // check if subscription worked
                client.SendOperation(Operations.Move(null, null, new[] { 1f, 2f }));

                Func<EventData, bool> checkAction = d => true;
                client.BeginReceiveEvent(EventCode.ItemMoved, checkAction);
                Assert.IsFalse(client.EndReceiveEvent(Settings.WaitTime, out data), "Event received");
            }
        }
Exemplo n.º 21
0
 /// <summary>
 /// The end receive event.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 /// <param name="eventCode">
 /// The event EventCode.
 /// </param>
 /// <returns>
 /// the received event
 /// </returns>
 private static EventData EndReceiveEvent(Client client, EventCode eventCode)
 {
     EventData data;
     Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out data), "Event not received");
     Assert.AreEqual(eventCode, (EventCode)data.Code);
     return data;
 }
Exemplo n.º 22
0
        /// <summary>
        /// The enter world.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <param name="worldName">
        /// The world name.
        /// </param>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <param name="viewDistanceEnter">
        /// The view Distance Enter.
        /// </param>
        /// <param name="viewDistanceExit">
        /// The view Distance Exit.
        /// </param>
        /// <param name="properties">
        /// The properties.
        /// </param>
        private static void EnterWorld(
            Client client, string worldName, float[] position, float[] viewDistanceEnter, float[] viewDistanceExit, Hashtable properties)
        {
            client.SendOperation(Operations.EnterWorld(worldName, client.Username, properties, position, viewDistanceEnter, viewDistanceExit));

            ReceiveOperationResponse(client, ReturnCode.Ok);
        }
Exemplo n.º 23
0
 /// <summary>
 /// The enter world end.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 private static void EnterWorldEnd(Client client)
 {
     OperationResponse data;
     Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data));
     Assert.AreEqual(0, data.ReturnCode);
 }
Exemplo n.º 24
0
 /// <summary>
 /// The exit world.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 private static void ExitWorld(Client client)
 {
     EventData data;
     client.SendOperation(Operations.ExitWorld());
     client.BeginReceiveEvent(EventCode.WorldExited, d => true);
     Assert.IsTrue(client.EndReceiveEvent(Settings.WaitTime, out data), "Event not received");
     Assert.AreEqual(data.Code, (byte)EventCode.WorldExited);
 }
Exemplo n.º 25
0
 /// <summary>
 /// The exit world end.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 private static void ExitWorldEnd(Client client)
 {
     EndReceiveEvent(client, EventCode.WorldExited);
 }
Exemplo n.º 26
0
        /// <summary>
        /// The not receive operation response.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        private static void NotReceiveOperationResponse(Client client)
        {
            client.BeginReceiveResponse();

            OperationResponse data;
            Assert.IsFalse(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received");
        }
Exemplo n.º 27
0
 /// <summary>
 /// The move action.
 /// </summary>
 /// <param name="client">
 /// The client.
 /// </param>
 /// <param name="number">
 /// The number.
 /// </param>
 private static void MoveAction(Client client, int number)
 {
     float[] pos = client.Position;
     client.SendOperation(Operations.Move(null, null, pos));
     number++;
     if (number < 6)
     {
         client.Peer.RequestFiber.Schedule(() => MoveAction(client, number), 100);
     }
 }
Exemplo n.º 28
0
        /// <summary>
        /// The receive operation response.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <param name="expectedReturn">
        /// The expected Return.
        /// </param>
        private static void ReceiveOperationResponse(Client client, ReturnCode expectedReturn)
        {
            client.BeginReceiveResponse();

            OperationResponse data;
            Assert.IsTrue(client.EndReceiveResponse(Settings.WaitTime, out data), "Response not received");
            var errorCode = (ReturnCode)data.ReturnCode;
            Assert.AreEqual(errorCode, expectedReturn);
        }
Exemplo n.º 29
0
        /////// <summary>
        /////// The receive event.
        /////// </summary>
        /////// <param name="client">
        /////// The client.
        /////// </param>
        /////// <param name="eventCode">
        /////// The event code.
        /////// </param>
        /////// <param name="checkAction">
        /////// The check action.
        /////// </param>
        /////// <returns>
        /////// </returns>
        ////private static EventData ReceiveEvent(Client client, Code eventCode, Func<EventData, bool> checkAction)
        ////{
        ////    BeginReceiveEvent(client, eventCode, checkAction);
        ////    return EndReceiveEvent(client);
        ////}

        /////// <summary>
        /////// The receive event.
        /////// </summary>
        /////// <param name="client">
        /////// The client.
        /////// </param>
        /////// <param name="eventCode">
        /////// The event code.
        /////// </param>
        /////// <returns>
        /////// </returns>
        ////private static EventData ReceiveEvent(Client client, Code eventCode)
        ////{
        ////    return ReceiveEvent(client, eventCode, d => true);
        ////}

        /////// <summary>
        /////// The receive operation response.
        /////// </summary>
        /////// <param name="client">
        /////// The client.
        /////// </param>
        /////// <param name="operationCode">
        /////// The operation code.
        /////// </param>
        /////// <returns>
        /////// </returns>
        ////private static EventData ReceiveOperationResponse(Client client, OperationCode operationCode)
        ////{
        ////    Func<EventData, bool> checkAction =
        ////        d => (byte)d.Parameters[(short)ParameterCode.OperationCode] == (byte)operationCode;
        ////    EventData data = ReceiveEvent(client, EventCode.OperationSuccess, checkAction);
        ////    var eventCode = (EventCode)(byte)data.Code;
        ////    Assert.IsTrue(eventCode == EventCode.OperationSuccess || eventCode == EventCode.OperationError);
        ////    Assert.AreEqual((OperationCode)(byte)data.Parameters[(short)ParameterCode.OperationCode], operationCode);
        ////    return data;
        ////}

        /// <summary>
        /// The thread pool enqueue.
        /// </summary>
        /// <param name="client">
        /// The client.
        /// </param>
        /// <param name="action">
        /// The action.
        /// </param>
        private static void ThreadPoolEnqueue(Client client, Action action)
        {
            ThreadPool.QueueUserWorkItem(
                o =>
                {
                    try
                    {
                        action();
                    }
                    catch (Exception e)
                    {
                        client.HandleException(e);
                    }
                });
        }
Exemplo n.º 30
0
 private static void EnterWorld(
     Client client, string worldName, Vector position, Vector viewDistanceEnter, Vector viewDistanceExit, Hashtable properties)
 {
     client.ResetEvent();
     client.SendOperation(Operations.EnterWorld(worldName, client.Username, properties, position, viewDistanceEnter, viewDistanceExit));
     ReceiveOperationResponse(client, ReturnCode.Ok); // blocking
 }