/// <summary> /// Initializes a new instance of the <see cref="LavalinkSocket"/> class. /// </summary> /// <param name="options">the node options</param> /// <param name="client">the discord client</param> /// <param name="logger">the logger</param> /// <param name="cache">an optional cache that caches track requests</param> public LavalinkSocket(LavalinkNodeOptions options, IDiscordClientWrapper client, ILogger logger, ILavalinkCache cache = null) : base(options, logger, cache) { Logger = logger; if (options.BufferSize <= 0) { if (Logger is null) { throw new InvalidOperationException("The specified buffer size is zero or negative."); } Logger.Log(this, "The specified buffer size is zero or negative .. using 1048576 (1MiB).", LogLevel.Warning); options.BufferSize = 1024 * 1024; // 1 MiB buffer size } if (options.ReconnectStrategy is null) { throw new InvalidOperationException("No reconnection strategy specified in options."); } _client = client; _password = options.Password; _webSocketUri = new Uri(options.WebSocketUri); _receiveBuffer = new byte[options.BufferSize]; _overflowBuffer = new StringBuilder(); _resume = options.AllowResuming; _ioDebug = options.DebugPayloads; _resumeKey = Guid.NewGuid(); _queue = new Queue <IPayload>(); _reconnectionStrategy = options.ReconnectStrategy; _cancellationTokenSource = new CancellationTokenSource(); }
/// <summary> /// Called by sender/receiver when they disconnect unexpectedly /// </summary> private void HandleUnexpectedDisconnect() { if (!status.Set(TwitchChatStatus.Reconnecting)) { return; } OnReconnecting(); if (ReconnectStrategy == null) { return; } reconnectingTask = ReconnectStrategy .ExecuteAsync(DoReconnect, connectionToken.Token) .ContinueWith(t => { if (t.Result) { if (!status.SetConditional(TwitchChatStatus.Connected, s => s == TwitchChatStatus.Reconnecting)) { DoDisconnectUnsafeAsync().Wait(); } else { OnConnected(); } } else { if (connectionToken.IsCancellationRequested) { return; } if (status.Set(TwitchChatStatus.Disconnected)) { OnReconnectFailed(); } } reconnectingTask = null; }); }
/// <summary> /// Initializes a new instance of the <see cref="ReconnectAttemptEventArgs"/> class. /// </summary> /// <param name="uri">the URI connect / reconnected / disconnected from / to</param> /// <param name="attempt">the number of reconnect attempts already made (1 = first)</param> /// <param name="strategy">the reconnect strategy used</param> /// <exception cref="ArgumentNullException"> /// thrown if the specified <paramref name="uri"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentNullException"> /// thrown if the specified <paramref name="strategy"/> is <see langword="null"/>. /// </exception> public ReconnectAttemptEventArgs(Uri uri, int attempt, ReconnectStrategy strategy) : base(uri) { Attempt = attempt; Strategy = strategy ?? throw new ArgumentNullException(nameof(strategy)); }