Example #1
0
 public void Unsubscribe(string id, OnTimedOut onTimedOut)
 {
     if (this.timers.ContainsKey(id))
     {
         this.timers[id].onTimedOut -= onTimedOut;
     }
 }
Example #2
0
        public async Task HeartbeatLoop(int heartbeatInterval)
        {
            receivedHeartbeatAck = true;

            while (State == WebSocketState.Open && !heartbeatCancellationSource.IsCancellationRequested)
            {
                if (!receivedHeartbeatAck)
                {
                    log.LogWarning("[HeartbeatLoop] Connection timed out (did not receive ack for last heartbeat).");

                    OnTimedOut?.Invoke(this, EventArgs.Empty);
                    break;
                }

                try
                {
                    receivedHeartbeatAck = false;

                    // Send heartbeat
                    await SendHeartbeatPayload().ConfigureAwait(false);
                }
                catch (InvalidOperationException)
                {
                    // Socket was closed between the loop check and sending the heartbeat
                    break;
                }
                catch (DiscordWebSocketException dwex)
                {
                    // Expected to be the socket closing while sending a heartbeat
                    if (dwex.Error != DiscordWebSocketError.ConnectionClosed)
                    {
                        // Unexpected errors may not be the socket closing/aborting, so just log and loop around.
                        log.LogError($"[HeartbeatLoop] Unexpected error occured while sending a heartbeat: {dwex}");
                    }
                    else
                    {
                        break;
                    }
                }

                try
                {
                    // Wait heartbeat interval
                    await Task.Delay(heartbeatInterval, heartbeatCancellationSource.Token)
                    .ConfigureAwait(false);
                }
                catch (ObjectDisposedException)
                {
                    // VoiceWebSocket was disposed between sending a heartbeat payload and beginning to wait
                    break;
                }
                catch (OperationCanceledException)
                {
                    // Socket is disconnecting
                    break;
                }
            }
        }
Example #3
0
 public Timer(string id, DateTime time, OnTimedOut onTimedOut, TimeManager timeMan)
 {
     this.confirmed  = false;
     this.confirming = false;
     this.lastCheck  = -60f;
     this.id         = id;
     this.time       = (float)time.Subtract(timeMan.CurrentTime).TotalSeconds;
     this.inited     = (float)timeMan.TimeFromStart.TotalSeconds;
     if (onTimedOut != null)
     {
         this.onTimedOut = (OnTimedOut)Delegate.Combine(this.onTimedOut, onTimedOut);
     }
     this.date  = time;
     this.fired = false;
 }
Example #4
0
        private void UpdateConnectionTimeout(Boolean alive)
        {
            var newTimeoutTask = Task.Delay(TimeOutMilliseonds);

            newTimeoutTask.ContinueWith(_ =>
            {
                // If this is the live task, swap it with null and continue with the timeout.
                if (Interlocked.CompareExchange(ref _timeoutTask, null, newTimeoutTask) != newTimeoutTask)
                {
                    return;
                }

                // Has timed out.
                OnTimedOut?.Invoke(this);
            });

            _timeoutTask = alive ? newTimeoutTask : null;
        }
Example #5
0
 public void CreateTimer(string id, DateTime time, OnTimedOut onTimedOut)
 {
     this.timers.Add(id, new Timer(id, time, onTimedOut, this));
     this.SaveTimers();
 }
Example #6
0
    public static int TryToActivateLootCrateAtSlot(int index, LootCrateType crateType, OnTimedOut onCrateUnlocked)
    {
        if (LootCrateSlots.IsUnlockingInProgress())
        {
            return(-1);
        }
        string slotIdentifier = LootCrateSlot.GetSlotIdentifier(index);

        GameProgress.SetString(slotIdentifier, string.Format("{0},{1}", LootCrateSlot.State.Locked.ToString(), crateType.ToString()), GameProgress.Location.Local);
        if (!Singleton <TimeManager> .Instance.HasTimer(slotIdentifier))
        {
            DateTime time = Singleton <TimeManager> .Instance.CurrentTime.AddSeconds((double)LootCrateSlots.GetOpenTimeForCrate(crateType));

            Singleton <TimeManager> .Instance.CreateTimer(slotIdentifier, time, onCrateUnlocked);

            GameProgress.SetString("LootCrateSlotOpening", slotIdentifier, GameProgress.Location.Local);
            if (LootCrateSlots.instance != null)
            {
                LootCrateSlots.instance.ShowFullBubble(LootCrateSlots.AreSlotsFull());
            }
            return(0);
        }
        return(1);
    }