Example #1
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();
        }
Example #2
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;
        }