/// <summary>Connects the specified server endpoint.</summary> /// <param name="serverEndpoint">The server endpoint.</param> /// <param name="serverPort">The server port.</param> /// <param name="clientPort">The client port.</param> /// <param name="signingKeyBase64">Signing key for token generation.</param> /// <param name="clientId">The client identifier.</param> /// <param name="location">The location.</param> public void Connect(string serverEndpoint, int serverPort, int clientPort, string signingKeyBase64, uint clientId, AVector3 location) { this.location = location; this.ClientId = clientId; this.signingKeyBase64 = signingKeyBase64; if (this.ClientId == 0) { var random = new System.Random(); this.ClientId = (uint)random.Next(); } //send IPAddress ipAddr; if (IPAddress.TryParse(serverEndpoint, out ipAddr)) { serverEndPoint = new IPEndPoint(IPAddress.Parse(serverEndpoint), serverPort); } else { var hostEntry = Dns.GetHostEntry(serverEndpoint).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork); serverEndPoint = new IPEndPoint(hostEntry, serverPort); } this.client = new UdpClient(clientPort); //receive this.localEndPoint = new IPEndPoint(IPAddress.Any, ((IPEndPoint)client.Client.LocalEndPoint).Port); InitializeCommunication((uint)localEndPoint.Port); this.thread = new Thread(Run); this.thread.Start(); }
private IActorRef CreateDefaultObjectActor(TestProbe areaRegionProbe, AVector3 pos) { //setup var localShardRegionResolver = new Mock <IShardRegionResolver>(); localShardRegionResolver.Setup(m => m.GetShardRegion(ShardRegions.AREAREGION)).Returns(areaRegionProbe); var testeeRef = Sys.ActorOf(Props.Create(() => new ObjectActor(localShardRegionResolver.Object)), DEFAULTOBJECTID.ToString(CultureInfo.InvariantCulture)); Watch(testeeRef); var cmdCreate = new ObjectCreateCommand( frameTick: 0, objectId: DEFAULTOBJECTID, parentObjectId: 0, ownerId: 0, typeId: 0, targetPosition: pos, targetOrientation: new AQuaternion { X = 40.0f, Y = 50.0f, Z = 60.0f, W = 70.0f }); var cmdEnvCreate = new ObjectCommandEnvelope(0, cmdCreate, DEFAULTOBJECTID); //execute testeeRef.Tell(cmdEnvCreate); //verify areaRegionProbe.ExpectMsg <ObjectEnterAreaCommand>(); areaRegionProbe.ExpectMsg <AreaCommandEnvelope>(); return(testeeRef); }
/// <summary>Initializes a new instance of the <see cref="EventLineCommand" /> class.</summary> /// <param name="eventType">The event type.</param> /// <param name="eventId">The event id.</param> /// <param name="startPosition">The start position.</param> /// <param name="endPosition">The end position.</param> /// <param name="frameTick">The frame tick.</param> /// <param name="data">The data.</param> /// <exception cref="ArgumentNullException">Data is null.</exception> public EventLineCommand(ushort eventType, ulong eventId, AVector3 startPosition, AVector3 endPosition, ulong frameTick, byte[] data) : base(CommandType.EventLine) { this.EventType = eventType; this.EventId = eventId; this.StartPosition = startPosition; this.EndPosition = endPosition; this.FrameTick = frameTick; this.Data = data ?? throw new ArgumentNullException(nameof(data)); using (MemoryStream stream = new MemoryStream(sizeof(ushort) + data.Length)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(this.EventType); bw.Write(this.EventId); Write(bw, this.StartPosition); Write(bw, this.EndPosition); bw.Write(this.FrameTick); bw.Write(this.Data.Length); bw.Write(this.Data); } stream.Flush(); this.Body = stream.ToArray(); } }
public static AVector3 Max(AVector3 a, AVector3 b) { return(new AVector3() { X = Mathf.Max(a.X, b.X), Y = Mathf.Max(a.Y, b.Y), Z = Mathf.Max(a.Z, b.Z) }); }
public void GetAreaIdFromWorldPositionTest(float x, float y, float z, long expectedArea) { AVector3 wp = new AVector3 { X = x, Y = y, Z = z, }; ulong actual = Locator.GetAreaIdFromWorldPosition(wp); Assert.AreEqual(expectedArea, actual); }
private Task <ClientStatistic> RunStatisticsClient(object state, MessageControlFlags qos) { var task = new Task <ClientStatistic>(() => { uint clientId = (uint)state; CliClient client = new CliClient(); AVector3 location = new AVector3 { X = clientId * Locator.AREASIZE, Y = clientId * Locator.AREASIZE, Z = clientId * Locator.AREASIZE, }; client.Connect(Host, Port, 0, TokenSigningKey, clientId, location); AutoResetEvent eventFinished = new AutoResetEvent(false); var statistics = new ClientStatistic(); statistics.ClientId = clientId; client.ReliableMessaging.OnDeadLetter = (m) => statistics.DeadLetteredMessages++; client.OnConnected = () => { ulong areaId = Locator.GetAreaIdFromWorldPosition(location); _testCountdownEventStart.Signal(); _testCountdownEventStart.Wait(); statistics.MessageStatistic.Add("CREATE", RunStatistics(client, qos, "Create objects", MessageCount, _ => new ObjectCreateCommand(0, _, 0, 0, 0, location, AQuaternion.Zero))); _testCountdownEventCreated.Signal(); _testCountdownEventCreated.Wait(); statistics.MessageStatistic.Add("UPDATE", RunStatistics(client, qos, "Update positions", MessageCount, _ => new ObjectUpdatePositionCommand(_, location, AQuaternion.Zero, AVector3.Zero, AVector3.Zero, 0, 0))); _testCountdownEventUpdate.Signal(); _testCountdownEventUpdate.Wait(); //statistics.MessageStatistic.Add("DELETE", RunStatistics(client, qos, "Destroy objects", MessageCount, _ => new ObjectDestroyCommand(0, 0, locationX, locationY, locationZ))); _testCountdownEventDestroy.Signal(); _testCountdownEventDestroy.Wait(); statistics.UnackedMessages = client.ReliableMessaging.MessageUnackedCount; statistics.MessagesSent = client.ReliableMessaging.MessageSentCount; statistics.MessagesReceived = client.ReliableMessaging.MessageReceivedCount; statistics.ResentMessages = client.ReliableMessaging.MessageResentCount; eventFinished.Set(); }; eventFinished.WaitOne(); return(statistics); }); return(task); }
/// <summary>Initializes a new instance of the <see cref="ObjectUpdatePositionCommand" /> class.</summary> /// <param name="objectId">The object identifier.</param> /// <param name="targetPosition">The target position x.</param> /// <param name="targetOrientation">The quaternion .</param> /// <param name="startFrameTick">The start frame tick.</param> /// <param name="stopFrameTick">The stop frame tick.</param> public ObjectUpdatePositionCommand( ulong objectId, AVector3 targetPosition, AQuaternion targetOrientation, ulong startFrameTick, ulong stopFrameTick) : this( objectId, targetPosition, targetOrientation, AVector3.Zero, AVector3.Zero, startFrameTick, stopFrameTick) { }
/// <summary> /// Writes a vector 3 instance. /// </summary> /// <param name="writer">Binary write to byte buffer.</param> /// <param name="vector">Vector to write.</param> protected static void Write(BinaryWriter writer, AVector3 vector) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (vector == null) { throw new ArgumentNullException(nameof(vector)); } writer.Write(vector.X); writer.Write(vector.Y); writer.Write(vector.Z); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectCreateCommand"/> class. /// </summary> /// <param name="frameTick">Frame tick when the object was created.</param> /// <param name="objectId">Id of the new object.</param> /// <param name="parentObjectId">Id of the objects parent.</param> /// <param name="ownerId">Id of the owner.</param> /// <param name="typeId">Type of the object.</param> /// <param name="targetPosition">Position where object is located.</param> /// <param name="targetOrientation">Quaternion orientation of object.</param> public ObjectCreateCommand( ulong frameTick, ulong objectId, ulong parentObjectId, uint ownerId, uint typeId, AVector3 targetPosition, AQuaternion targetOrientation) : base(CommandType.ObjectCreate) { if (targetPosition == null) { throw new ArgumentNullException(nameof(targetPosition)); } if (targetOrientation == null) { throw new ArgumentNullException(nameof(targetOrientation)); } this.FrameTick = frameTick; this.ObjectId = objectId; this.ParentObjectId = parentObjectId; this.OwnerId = ownerId; this.TypeId = typeId; this.TargetPosition = targetPosition; this.TargetOrientation = targetOrientation; int floatSize = sizeof(float) * 7; using (MemoryStream stream = new MemoryStream(sizeof(ulong) + floatSize)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(this.FrameTick); bw.Write(this.ObjectId); bw.Write(this.ParentObjectId); bw.Write(this.OwnerId); bw.Write(this.TypeId); Write(bw, this.TargetPosition); Write(bw, this.TargetOrientation); } stream.Flush(); this.Body = stream.ToArray(); } }
public void GetAreaIdsWithinWorldBoundariesTest(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, int expectedAreaCount) { AVector3 min = new AVector3 { X = minX, Y = minY, Z = minZ, }; AVector3 max = new AVector3 { X = maxX, Y = maxY, Z = maxZ, }; var actual = Locator.GetAreaIdsWithinWorldBoundaries(min, max); Assert.AreEqual(expectedAreaCount, actual.Count); }
/// <summary>Gets all area identifiers within the given world boundaries.</summary> /// <param name="a">The world position start.</param> /// <param name="b">The world position end.</param> /// <returns>List of area identifiers.</returns> public static List <ulong> GetAreaIdsWithinWorldBoundaries(AVector3 a, AVector3 b) { List <ulong> areas = new List <ulong>(); ulong aArea = GetAreaIdFromWorldPosition(a); ulong bArea = GetAreaIdFromWorldPosition(b); GetPartsFromAreaId(aArea, out var aXPart, out var aYPart, out var aZPart); GetPartsFromAreaId(bArea, out var bXPart, out var bYPart, out var bZPart); for (ulong xPart = Math.Min(aXPart, bXPart); xPart <= Math.Max(aXPart, bXPart); xPart++) { for (ulong yPart = Math.Min(aYPart, bYPart); yPart <= Math.Max(aYPart, bYPart); yPart++) { for (ulong zPart = Math.Min(aZPart, bZPart); zPart <= Math.Max(aZPart, bZPart); zPart++) { ulong areaId = zPart + (yPart * (ulong)MAXPOSITION) + (xPart * (ulong)MAXPOSITION * (ulong)MAXPOSITION); areas.Add(areaId); } } } return(areas); }
/// <summary>Initializes a new instance of the <see cref="ObjectDestroyCommand" /> class.</summary> /// <param name="frameTick">The frame tick.</param> /// <param name="objectId">The object identifier.</param> /// <param name="targetPosition">The target position.</param> public ObjectDestroyCommand(ulong frameTick, ulong objectId, AVector3 targetPosition) : base(CommandType.ObjectDestroy) { this.FrameTick = frameTick; this.ObjectId = objectId; this.TargetPosition = targetPosition; int sizeUlongs = sizeof(ulong) * 2; int sizeFloats = sizeof(float) * 3; using (MemoryStream stream = new MemoryStream(sizeUlongs + sizeFloats)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(this.FrameTick); bw.Write(this.ObjectId); Write(bw, this.TargetPosition); } stream.Flush(); this.Body = stream.ToArray(); } }
/// <summary>Initializes a new instance of the <see cref="ObjectUpdatePositionCommand" /> class.</summary> /// <param name="objectId">The object identifier.</param> /// <param name="targetPosition">The target position.</param> /// <param name="targetOrientation">The quaternion.</param> /// <param name="velocity">The velocity.</param> /// <param name="angularVelocity">The angular velocity.</param> /// <param name="startFrameTick">The start frame tick.</param> /// <param name="stopFrameTick">The stop frame tick.</param> public ObjectUpdatePositionCommand( ulong objectId, AVector3 targetPosition, AQuaternion targetOrientation, AVector3 velocity, AVector3 angularVelocity, ulong startFrameTick, ulong stopFrameTick) : base(CommandType.ObjectUpdatePosition) { this.ObjectId = objectId; this.TargetPosition = targetPosition; this.TargetOrientation = targetOrientation; this.Velocity = velocity; this.AngularVelocity = angularVelocity; this.StartFrameTick = startFrameTick; this.StopFrameTick = stopFrameTick; int sizeUlongs = sizeof(ulong) * 2; int sizeFloats = sizeof(float) * 13; int size = sizeUlongs + sizeFloats; using (MemoryStream stream = new MemoryStream(size)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(this.ObjectId); Write(bw, this.TargetPosition); Write(bw, this.TargetOrientation); Write(bw, this.Velocity); Write(bw, this.AngularVelocity); bw.Write(this.StartFrameTick); bw.Write(this.StopFrameTick); } stream.Flush(); this.Body = stream.ToArray(); } }
/// <summary>Gets the area identifier from world position.</summary> /// <param name="worldPosition">The world position.</param> /// <returns>The area identifier.</returns> public static ulong GetAreaIdFromWorldPosition(AVector3 worldPosition) { float x = worldPosition.X; float y = worldPosition.Y; float z = worldPosition.Z; if (Math.Abs(x) > MAXPOSITION) { x = x > 0 ? MAXPOSITION : -MAXPOSITION; } if (Math.Abs(y) > MAXPOSITION) { y = y > 0 ? MAXPOSITION : -MAXPOSITION; } if (Math.Abs(z) > MAXPOSITION) { z = z > 0 ? MAXPOSITION : -MAXPOSITION; } return(GetAreaId(x, y, z)); }