private MessageStatistic RunStatistics(CliClient client, MessageControlFlags flags, string domain, int messageCount, Func <ulong, BaseCommand> createCommand) { ConcurrentDictionary <ulong, MessageEntry> messages = new ConcurrentDictionary <ulong, MessageEntry>(); client.OnCommand = (cmd) => ProcessCommandMessage(client, domain, messages, cmd); DateTime startTimeStamp = DateTime.Now; ulong objectId = (ulong)(client.ClientId * messageCount); for (int i = 0; i < messageCount; i++) { objectId++; BaseCommand command = createCommand(objectId); ObjectCommandEnvelope commandEnv = new ObjectCommandEnvelope(client.ClientId, command, objectId); messages.TryAdd(objectId, new MessageEntry { RequestTimeStamp = DateTime.Now, ResponseTimeStamp = DateTime.MinValue, CommandEnvelope = commandEnv }); client.ReliableMessaging.SendCommandEnvelope(commandEnv, flags); TimeSpan waitTime = client.ReliableMessaging.SynchronizeMessages(); if (waitTime > TimeSpan.Zero) { Thread.Sleep((int)waitTime.TotalMilliseconds); } } for (; ;) { if (DateTime.Now - client.LastMessageReceived > TimeSpan.FromSeconds(5)) { break; } Thread.Sleep(200); TimeSpan waitTime = client.ReliableMessaging.SynchronizeMessages(); if (waitTime > TimeSpan.Zero) { Thread.Sleep((int)waitTime.TotalMilliseconds); } } TimeSpan sumLatency = TimeSpan.Zero; int countSuccess = 0; foreach (var value in messages.Values) { if (value.ResponseTimeStamp == DateTime.MinValue) { break; } countSuccess++; sumLatency += value.ResponseTimeStamp - value.RequestTimeStamp; } MessageStatistic messageStatistic = new MessageStatistic(); messageStatistic.Duration = client.LastMessageReceived - startTimeStamp; messageStatistic.MessagesTotal = messageCount; messageStatistic.MessagesAnswered = countSuccess; return(messageStatistic); }
/// <summary>Sends a command envelope.</summary> /// <param name="commandEnvelope">The command envelope.</param> /// <param name="qualityOfService">The quality of service.</param> /// <exception cref="ArgumentNullException">commandEnvelope is null.</exception> public void SendCommandEnvelope(ObjectCommandEnvelope commandEnvelope, MessageControlFlags qualityOfService = MessageControlFlags.QOS0) { if (commandEnvelope == null) { throw new ArgumentNullException(nameof(commandEnvelope)); } var message = new UdpMessage(this.sequenceId, MessageControlFlags.COMMAND | qualityOfService, commandEnvelope.Serialize()); this.sequenceId++; this.pendingMessages.Enqueue(message); }
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>Sends a command.</summary> /// <param name="senderId">The sender identifier.</param> /// <param name="command">The command.</param> /// <param name="toObjectId">To object identifier.</param> /// <param name="qualityOfService">The quality of service.</param> public void SendCommand(uint senderId, BaseCommand command, ulong toObjectId, MessageControlFlags qualityOfService = MessageControlFlags.QOS0) { var commandEnvelope = new ObjectCommandEnvelope(senderId, command, toObjectId); this.SendCommandEnvelope(commandEnvelope, qualityOfService); }
/// <summary>Initializes a new instance of the <see cref="UdpMessage" /> class.</summary> /// <param name="sequenceId">The sequence identifier.</param> /// <param name="controlFlags">The control flags.</param> /// <param name="commandBody">The command body.</param> public UdpMessage(uint sequenceId, MessageControlFlags controlFlags, byte[] commandBody) { this.SequenceId = sequenceId; this.ControlFlags = controlFlags; this.CommandBody = commandBody; }