예제 #1
0
 /// <summary>
 /// Creates a new instance of <see cref="LavalinkConfiguration"/>, copying the properties of another configuration.
 /// </summary>
 /// <param name="other">Configuration the properties of which are to be copied.</param>
 public LavalinkConfiguration(LavalinkConfiguration other)
 {
     this.RestEndpoint = new ConnectionEndpoint
     {
         Hostname = other.RestEndpoint.Hostname,
         Port     = other.RestEndpoint.Port
     };
     this.SocketEndpoint = new ConnectionEndpoint
     {
         Hostname = other.SocketEndpoint.Hostname,
         Port     = other.SocketEndpoint.Port
     };
     this.Password = other.Password;
 }
예제 #2
0
        internal LavalinkNodeConnection(DiscordClient client, LavalinkConfiguration config)
        {
            this.Discord         = client;
            this.Configuration   = new LavalinkConfiguration(config);
            this.ConnectedGuilds = new ConcurrentDictionary <ulong, LavalinkGuildConnection>();
            this.Statistics      = new LavalinkStatistics();

            this._lavalinkSocketError = new AsyncEvent <SocketErrorEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_SOCKET_ERROR");
            this._disconnected        = new AsyncEvent <NodeDisconnectedEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_NODE_DISCONNECTED");
            this._statsReceived       = new AsyncEvent <StatsReceivedEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_STATS_RECEIVED");
            this._playerUpdated       = new AsyncEvent <PlayerUpdateEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYER_UPDATED");
            this._playbackFinished    = new AsyncEvent <TrackFinishEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYBACK_FINISHED");
            this._trackStuck          = new AsyncEvent <TrackStuckEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_TRACK_STUCK");
            this._trackException      = new AsyncEvent <TrackExceptionEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_TRACK_EXCEPTION");

            this.VoiceServerUpdates          = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceServerUpdateEventArgs> >();
            this.VoiceStateUpdates           = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceStateUpdateEventArgs> >();
            this.Discord.VoiceStateUpdated  += this.Discord_VoiceStateUpdated;
            this.Discord.VoiceServerUpdated += this.Discord_VoiceServerUpdated;

            var httphandler = new HttpClientHandler
            {
                UseCookies             = false,
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
                UseProxy = client.Configuration.Proxy != null
            };

            if (httphandler.UseProxy) // because mono doesn't implement this properly
            {
                httphandler.Proxy = client.Configuration.Proxy;
            }

            this.Rest = new HttpClient(httphandler)
            {
                BaseAddress = new Uri($"http://{this.Configuration.RestEndpoint}/loadtracks")
            };
            this.Rest.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", $"DSharpPlus.LavaLink/{client.VersionString}");
            this.Rest.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", this.Configuration.Password);

            this.WebSocket                  = client.Configuration.WebSocketClientFactory(client.Configuration.Proxy);
            this.WebSocket.Connected       += this.WebSocket_OnConnect;
            this.WebSocket.Disconnected    += this.WebSocket_OnDisconnect;
            this.WebSocket.Errored         += this.WebSocket_OnError;
            this.WebSocket.MessageReceived += this.WebSocket_OnMessage;

            Volatile.Write(ref this._isDisposed, false);
        }
 /// <summary>
 /// Creates a new instance of <see cref="LavalinkConfiguration"/>, copying the properties of another configuration.
 /// </summary>
 /// <param name="other">Configuration the properties of which are to be copied.</param>
 public LavalinkConfiguration(LavalinkConfiguration other)
 {
     this.RestEndpoint = new ConnectionEndpoint
     {
         Hostname = other.RestEndpoint.Hostname,
         Port     = other.RestEndpoint.Port,
         Secured  = other.RestEndpoint.Secured
     };
     this.SocketEndpoint = new ConnectionEndpoint
     {
         Hostname = other.SocketEndpoint.Hostname,
         Port     = other.SocketEndpoint.Port,
         Secured  = other.SocketEndpoint.Secured
     };
     this.Password      = other.Password;
     this.ResumeKey     = other.ResumeKey;
     this.ResumeTimeout = other.ResumeTimeout;
 }
예제 #4
0
        internal LavalinkNodeConnection(DiscordClient client, LavalinkConfiguration config)
        {
            this.Discord         = client;
            this.Configuration   = new LavalinkConfiguration(config);
            this.ConnectedGuilds = new ConcurrentDictionary <ulong, LavalinkGuildConnection>();
            this.Statistics      = new LavalinkStatistics();

            this._lavalinkSocketError = new AsyncEvent <SocketErrorEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_SOCKET_ERROR");
            this._disconnected        = new AsyncEvent <NodeDisconnectedEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_NODE_DISCONNECTED");
            this._statsReceived       = new AsyncEvent <StatisticsReceivedEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_STATS_RECEIVED");
            this._playerUpdated       = new AsyncEvent <PlayerUpdateEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYER_UPDATED");
            this._playbackStarted     = new AsyncEvent <TrackStartEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYBACK_STARTED");
            this._playbackFinished    = new AsyncEvent <TrackFinishEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYBACK_FINISHED");
            this._trackStuck          = new AsyncEvent <TrackStuckEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_TRACK_STUCK");
            this._trackException      = new AsyncEvent <TrackExceptionEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_TRACK_EXCEPTION");

            this.VoiceServerUpdates          = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceServerUpdateEventArgs> >();
            this.VoiceStateUpdates           = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceStateUpdateEventArgs> >();
            this.Discord.VoiceStateUpdated  += this.Discord_VoiceStateUpdated;
            this.Discord.VoiceServerUpdated += this.Discord_VoiceServerUpdated;

            var httphandler = new HttpClientHandler
            {
                UseCookies             = false,
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
                UseProxy = client.Configuration.Proxy != null
            };

            if (httphandler.UseProxy) // because mono doesn't implement this properly
            {
                httphandler.Proxy = client.Configuration.Proxy;
            }

            this.Rest = new LavalinkRest(this.Configuration, this.Discord);

            this.WebSocket                  = client.Configuration.WebSocketClientFactory(client.Configuration.Proxy);
            this.WebSocket.Connected       += this.WebSocket_OnConnect;
            this.WebSocket.Disconnected    += this.WebSocket_OnDisconnect;
            this.WebSocket.ExceptionThrown += this.WebSocket_OnException;
            this.WebSocket.MessageReceived += this.WebSocket_OnMessage;

            Volatile.Write(ref this._isDisposed, false);
        }
예제 #5
0
        internal LavalinkRest(LavalinkConfiguration config, DiscordClient client)
        {
            this.Configuration = config;
            this.Logger        = client.DebugLogger;

            var httphandler = new HttpClientHandler
            {
                UseCookies             = false,
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
                UseProxy = client.Configuration.Proxy != null
            };

            if (httphandler.UseProxy) // because mono doesn't implement this properly
            {
                httphandler.Proxy = client.Configuration.Proxy;
            }

            this.HttpClient = new HttpClient(httphandler);

            this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", $"DSharpPlus.LavaLink/{client.VersionString}");
            this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", config.Password);
        }
예제 #6
0
        internal LavalinkNodeConnection(DiscordClient client, LavalinkConfiguration config)
        {
            this.Discord       = client;
            this.Configuration = new LavalinkConfiguration(config);

            if (config.Region != null && this.Discord.VoiceRegions.Values.Contains(config.Region))
            {
                this.Region = config.Region;
            }

            this.ConnectedGuilds = new ReadOnlyConcurrentDictionary <ulong, LavalinkGuildConnection>(this._connectedGuilds);
            this.Statistics      = new LavalinkStatistics();

            this._lavalinkSocketError = new AsyncEvent <SocketErrorEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_SOCKET_ERROR");
            this._disconnected        = new AsyncEvent <NodeDisconnectedEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_NODE_DISCONNECTED");
            this._statsReceived       = new AsyncEvent <StatisticsReceivedEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_STATS_RECEIVED");
            this._playerUpdated       = new AsyncEvent <PlayerUpdateEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYER_UPDATED");
            this._playbackStarted     = new AsyncEvent <TrackStartEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYBACK_STARTED");
            this._playbackFinished    = new AsyncEvent <TrackFinishEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_PLAYBACK_FINISHED");
            this._trackStuck          = new AsyncEvent <TrackStuckEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_TRACK_STUCK");
            this._trackException      = new AsyncEvent <TrackExceptionEventArgs>(this.Discord.EventErrorHandler, "LAVALINK_TRACK_EXCEPTION");

            this.VoiceServerUpdates          = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceServerUpdateEventArgs> >();
            this.VoiceStateUpdates           = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceStateUpdateEventArgs> >();
            this.Discord.VoiceStateUpdated  += this.Discord_VoiceStateUpdated;
            this.Discord.VoiceServerUpdated += this.Discord_VoiceServerUpdated;

            this.Rest = new LavalinkRestClient(this.Configuration, this.Discord);

            this.WebSocket                  = client.Configuration.WebSocketClientFactory(client.Configuration.Proxy);
            this.WebSocket.Connected       += this.WebSocket_OnConnect;
            this.WebSocket.Disconnected    += this.WebSocket_OnDisconnect;
            this.WebSocket.ExceptionThrown += this.WebSocket_OnException;
            this.WebSocket.MessageReceived += this.WebSocket_OnMessage;

            Volatile.Write(ref this._isDisposed, false);
        }
예제 #7
0
        /// <summary>
        /// Connect to a Lavalink node.
        /// </summary>
        /// <param name="config">Lavalink client configuration.</param>
        /// <returns>The established Lavalink connection.</returns>
        public async Task <LavalinkNodeConnection> ConnectAsync(LavalinkConfiguration config)
        {
            if (this.ConnectedNodes.ContainsKey(config.SocketEndpoint))
            {
                return(this.ConnectedNodes[config.SocketEndpoint]);
            }

            var con = new LavalinkNodeConnection(this.Client, config);

            con.NodeDisconnected += this.Con_NodeDisconnected;
            con.Disconnected     += this.Con_Disconnected;
            this.ConnectedNodes[con.NodeEndpoint] = con;
            try
            {
                await con.StartAsync().ConfigureAwait(false);
            }
            catch
            {
                this.Con_NodeDisconnected(con);
                throw;
            }

            return(con);
        }
예제 #8
0
        internal LavalinkNodeConnection(DiscordClient client, LavalinkExtension extension, LavalinkConfiguration config)
        {
            this.Discord       = client;
            this.Parent        = extension;
            this.Configuration = new LavalinkConfiguration(config);

            if (config.Region != null && this.Discord.VoiceRegions.Values.Contains(config.Region))
            {
                this.Region = config.Region;
            }

            this.ConnectedGuilds = new ReadOnlyConcurrentDictionary <ulong, LavalinkGuildConnection>(this._connectedGuilds);
            this.Statistics      = new LavalinkStatistics();

            this._lavalinkSocketError = new AsyncEvent <LavalinkNodeConnection, SocketErrorEventArgs>("LAVALINK_SOCKET_ERROR", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._disconnected        = new AsyncEvent <LavalinkNodeConnection, NodeDisconnectedEventArgs>("LAVALINK_NODE_DISCONNECTED", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._statsReceived       = new AsyncEvent <LavalinkNodeConnection, StatisticsReceivedEventArgs>("LAVALINK_STATS_RECEIVED", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._playerUpdated       = new AsyncEvent <LavalinkGuildConnection, PlayerUpdateEventArgs>("LAVALINK_PLAYER_UPDATED", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._playbackStarted     = new AsyncEvent <LavalinkGuildConnection, TrackStartEventArgs>("LAVALINK_PLAYBACK_STARTED", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._playbackFinished    = new AsyncEvent <LavalinkGuildConnection, TrackFinishEventArgs>("LAVALINK_PLAYBACK_FINISHED", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._trackStuck          = new AsyncEvent <LavalinkGuildConnection, TrackStuckEventArgs>("LAVALINK_TRACK_STUCK", TimeSpan.Zero, this.Discord.EventErrorHandler);
            this._trackException      = new AsyncEvent <LavalinkGuildConnection, TrackExceptionEventArgs>("LAVALINK_TRACK_EXCEPTION", TimeSpan.Zero, this.Discord.EventErrorHandler);

            this.VoiceServerUpdates          = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceServerUpdateEventArgs> >();
            this.VoiceStateUpdates           = new ConcurrentDictionary <ulong, TaskCompletionSource <VoiceStateUpdateEventArgs> >();
            this.Discord.VoiceStateUpdated  += this.Discord_VoiceStateUpdated;
            this.Discord.VoiceServerUpdated += this.Discord_VoiceServerUpdated;

            this.Rest = new LavalinkRestClient(this.Configuration, this.Discord);

            Volatile.Write(ref this._isDisposed, false);
        }
 internal LavalinkRestClient(LavalinkConfiguration config, BaseDiscordClient client)
 {
     this.RestEndpoint = config.RestEndpoint;
     this._logger      = client.Logger;
     this.ConfigureHttpHandling(config.Password, client);
 }