Exemplo n.º 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="LavalinkCluster"/> class.
        /// </summary>
        /// <param name="options">the cluster options</param>
        /// <param name="client">the discord client</param>
        /// <param name="logger">the logger</param>
        /// <param name="cache">
        ///     a cache that is shared between the different lavalink rest clients. If the cache is
        ///     <see langword="null"/>, no cache will be used.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="options"/> parameter is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="client"/> is <see langword="null"/>.
        /// </exception>
        public LavalinkCluster(LavalinkClusterOptions options, IDiscordClientWrapper client, ILogger logger = null, ILavalinkCache cache = null)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _client = client ?? throw new ArgumentNullException(nameof(client));

            _loadBalacingStrategy = options.LoadBalacingStrategy;
            _logger    = logger;
            _cache     = cache;
            _nodesLock = new object();
            StayOnline = options.StayOnline;
            _nodes     = options.Nodes.Select(CreateNode).ToList();
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="LavalinkRestClient"/> class.
        /// </summary>
        /// <param name="options">the rest client options</param>
        /// <param name="logger">the logger</param>
        /// <param name="cache">an optional cache that caches track requests</param>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="options"/> parameter is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///     thrown if the track cache time ( <see cref="RestClientOptions.CacheTime"/>) is equal
        ///     or less than <see cref="TimeSpan.Zero"/>.
        /// </exception>
        public LavalinkRestClient(LavalinkRestOptions options, ILogger logger = null, ILavalinkCache cache = null)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.CacheTime <= TimeSpan.Zero)
            {
                throw new InvalidOperationException("The track cache time is negative or zero. Please do not " +
                                                    "specify a cache in the constructor instead of using a zero cache time.");
            }

            // initialize HTTP client handler
            var httpHandler = new HttpClientHandler();

            // check if automatic decompression should be used
            if (options.Decompression)
            {
                // setup compression
                httpHandler.AutomaticDecompression = DecompressionMethods.GZip
                                                     | DecompressionMethods.Deflate;
            }

            // disables cookies
            httpHandler.UseCookies = false;

            // initialize HTTP client
            _httpClient = new HttpClient(httpHandler)
            {
                BaseAddress = new Uri(options.RestUri)
            };
            _httpClient.DefaultRequestHeaders.Add("Authorization", options.Password);

            // add user-agent request header
            if (!string.IsNullOrWhiteSpace(options.UserAgent))
            {
                _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", options.UserAgent);
            }

            _logger        = logger;
            _cache         = cache;
            _cacheTime     = options.CacheTime;
            _debugPayloads = options.DebugPayloads;
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="LavalinkNode"/> class.
        /// </summary>
        /// <param name="cluster">the cluster</param>
        /// <param name="options">the node options for connecting</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>
        /// <param name="id">the node number</param>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="cluster"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="options"/> parameter is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="client"/> is <see langword="null"/>.
        /// </exception>
        public LavalinkClusterNode(LavalinkCluster cluster, LavalinkNodeOptions options, IDiscordClientWrapper client,
                                   ILogger logger, ILavalinkCache cache, int id)
            : base(options, client, logger, cache)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            Cluster    = cluster ?? throw new ArgumentNullException(nameof(cluster));
            Identifier = "Cluster Node-" + id;
            LastUsage  = DateTimeOffset.MinValue;
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="LavalinkNode"/> class.
        /// </summary>
        /// <param name="options">the node options for connecting</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>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="options"/> parameter is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     thrown if the specified <paramref name="client"/> is <see langword="null"/>.
        /// </exception>
        public LavalinkNode(LavalinkNodeOptions options, IDiscordClientWrapper client, ILogger logger = null, ILavalinkCache cache = null)
            : base(options, client, logger, cache)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _discordClient = client ?? throw new ArgumentNullException(nameof(client));
            Players        = new ConcurrentDictionary <ulong, LavalinkPlayer>();

            _disconnectOnStop = options.DisconnectOnStop;
            _discordClient.VoiceServerUpdated += VoiceServerUpdated;
            _discordClient.VoiceStateUpdated  += VoiceStateUpdated;
        }
Exemplo n.º 5
0
        /// <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();
        }