/// <summary> /// Disposes the client /// </summary> public void Dispose() { MessageCounter.Dispose(); Redis.Dispose(); Discord.Dispose(); DbContext.Dispose(); }
/// <summary> /// Ensures the <paramref name="count"/> number of messages of type <typeparamref name="TMessage"/> and matching predicate <paramref name="predicate"/> are received.<br/> /// If all required messages are already received by listener, the method returns immediately.<br/> /// If none or not all messages are received yet, the method listens for upcoming messages and returns when all are received, timeout occurs or <paramref name="cancellationToken"/> is cancelled.<br/> /// If more than <paramref name="count"/> messages matches the criteria, the latest <paramref name="count"/> messages will be returned. /// </summary> /// <typeparam name="TMessage">Type of message to receive.</typeparam> /// <param name="count">Number of messages that should be received</param> /// <param name="predicate">Predicate that message have to match to be returned.</param> /// <param name="timeout">Timeout for how long the method should await for matching messages to arrive. If <c>null</c>, a default value of 10s will be used.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>List of <paramref name="count"/> requested messages, ordered from latest to oldest.</returns> public async Task <IReadOnlyList <TMessage> > EnsureReceivedMany <TMessage>(int count, Func <TMessage, bool> predicate, TimeSpan?timeout = null, CancellationToken cancellationToken = default) { bool PredicateFn(TMessage m) => IsValidMessage(m, predicate); using var counter = new MessageCounter <TMessage>(this, count, PredicateFn); using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _listenerDisposedTokenSource.Token); return(await counter.WaitAsync(timeout ?? DefaultTimeout, cts.Token)); }
public void Should_calculate_max_messages_per_minute_correct() { // Arrange var ss = new MessageCounter(7000); // Act var m = ss.MaxMessagesPerSampleSlot; // Assert m.ShouldBeInRange(4.86, 4.87); }
public Text m_Text; // Reference to the component that displays the mps. private void Awake() { if (instance != null) { Debug.LogError("More than one MessageCounter in the scene"); } else { instance = this; } }
private void AckArrivedHandler(MessageCounter arg1, short arg2) { lock (_messageBuffer) { var toRemove = _messageBuffer.FirstOrDefault(m => m.Msg.MessageId == arg1 && m.Msg.Header.Type == arg2); if (toRemove != null) { _messageBuffer.Remove(toRemove); } } }
public void OrderedIfPacketsLost() { ActionDispatcher.Start(1); var server = new Host(8080); var client = new Host(8081); var messageCount = 0; MessageCounter lastMsg = 0; server.AddHandler(1023, (m, e) => { Assert.AreEqual(lastMsg, m.MessageId); lastMsg++; messageCount++; }); var connected = false; client.OnConnect = () => { connected = true; }; var ts1 = new TestSocketRnd(0.85); var ts2 = new TestSocketRnd(0.85); server.StartListen(ts1); client.StartListen(ts2); var t1 = new Timer((o) => ServerTimer.Tick()); t1.Change(10, 10); Thread.Sleep(1000); TestSocket.Join(ts1, ts2); while (!connected) { client.BeginConnect(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8080)); Thread.Sleep(1000); } for (var i = 0; i < 500; i++) { ActionDispatcher.Enqueue(() => { client.Send(new Message(1023, Mode.Reliable | Mode.Ordered, new byte[100])); }); } Thread.Sleep(5000); Assert.AreEqual(messageCount, 500); foreach (var connection in client.GetConnections()) { Assert.AreEqual(0, connection.BufferCount, "client buffer not empty"); } foreach (var connection in server.GetConnections()) { Assert.AreEqual(0, connection.BufferCount, "server buffer not empty"); } ActionDispatcher.Stop(); }
public void MessageCounterTest() { MessageCounter mc14 = 14; MessageCounter mc100 = 100; MessageCounter almostMax = short.MaxValue - 100; MessageCounter halfMax = short.MaxValue / 2 + 15; MessageCounter almostMin = short.MinValue + 100; MessageCounter min = short.MinValue; Assert.Greater(mc100, mc14, "100 < 14"); Assert.Greater(halfMax, mc100, "halfMax < 100"); Assert.Greater(mc14, halfMax, "14 < halfMax"); Assert.Greater(almostMax, halfMax, "almostMax < halfMax"); Assert.Greater(mc100, almostMax, "100 < almostMax"); Assert.Greater(mc14, almostMax, "14 < almostMax"); Assert.AreEqual(min - 1, 1); Assert.AreEqual(almostMin - 130, 30); Assert.AreEqual(mc100 - 100, 0); }
internal bool IsMessageInItsOrder(short type, MessageCounter num) { lock (_lastSequencedMessageNumPerType) { if (_lastSequencedMessageNumPerType.ContainsKey(type)) { if (_lastSequencedMessageNumPerType[type] < num) { _lastSequencedMessageNumPerType[type] = num; return(true); } else { return(false); } } else { _lastSequencedMessageNumPerType.Add(type, num); return(true); } } }
/// <summary> /// Used by the derived type to incremenet the message counter /// </summary> /// <param name="incrementBy">The number of events to increment the counter by</param> protected void IncrementMessageCount(int incrementBy) { MessageCounter.IncrementBy(incrementBy); _messageCount += incrementBy; _checkpointPolicy.IncrementBy(incrementBy); }
static MessageCounter() { Instance = new MessageCounter(); }