You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiscordRpcApiClient.cs 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #pragma warning disable CS1591
  2. using Discord.API.Rpc;
  3. using Discord.Net.Queue;
  4. using Discord.Net.Rest;
  5. using Discord.Net.WebSockets;
  6. using Discord.Rpc;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.IO.Compression;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace Discord.API
  18. {
  19. public class DiscordRpcApiClient : DiscordRestApiClient, IDisposable
  20. {
  21. private abstract class RpcRequest
  22. {
  23. public abstract Task SetResultAsync(JToken data, JsonSerializer serializer);
  24. public abstract Task SetExceptionAsync(JToken data, JsonSerializer serializer);
  25. }
  26. private class RpcRequest<T> : RpcRequest
  27. {
  28. public TaskCompletionSource<T> Promise { get; set; }
  29. public RpcRequest(RequestOptions options)
  30. {
  31. Promise = new TaskCompletionSource<T>();
  32. Task.Run(async () =>
  33. {
  34. await Task.Delay(options?.Timeout ?? 15000).ConfigureAwait(false);
  35. Promise.TrySetCanceled(); //Doesn't need to be async, we're already in a separate task
  36. });
  37. }
  38. public override Task SetResultAsync(JToken data, JsonSerializer serializer)
  39. {
  40. return Promise.TrySetResultAsync(data.ToObject<T>(serializer));
  41. }
  42. public override Task SetExceptionAsync(JToken data, JsonSerializer serializer)
  43. {
  44. var error = data.ToObject<ErrorEvent>(serializer);
  45. return Promise.TrySetExceptionAsync(new RpcException(error.Code, error.Message));
  46. }
  47. }
  48. private object _eventLock = new object();
  49. public event Func<string, Task> SentRpcMessage { add { _sentRpcMessageEvent.Add(value); } remove { _sentRpcMessageEvent.Remove(value); } }
  50. private readonly AsyncEvent<Func<string, Task>> _sentRpcMessageEvent = new AsyncEvent<Func<string, Task>>();
  51. public event Func<string, Optional<string>, Optional<object>, Task> ReceivedRpcEvent { add { _receivedRpcEvent.Add(value); } remove { _receivedRpcEvent.Remove(value); } }
  52. private readonly AsyncEvent<Func<string, Optional<string>, Optional<object>, Task>> _receivedRpcEvent = new AsyncEvent<Func<string, Optional<string>, Optional<object>, Task>>();
  53. public event Func<Exception, Task> Disconnected { add { _disconnectedEvent.Add(value); } remove { _disconnectedEvent.Remove(value); } }
  54. private readonly AsyncEvent<Func<Exception, Task>> _disconnectedEvent = new AsyncEvent<Func<Exception, Task>>();
  55. private readonly ConcurrentDictionary<Guid, RpcRequest> _requests;
  56. private readonly RequestQueue _requestQueue;
  57. private readonly IWebSocketClient _webSocketClient;
  58. private readonly SemaphoreSlim _connectionLock;
  59. private readonly string _clientId;
  60. private CancellationTokenSource _stateCancelToken;
  61. private string _origin;
  62. public ConnectionState ConnectionState { get; private set; }
  63. public DiscordRpcApiClient(string clientId, string userAgent, string origin, RestClientProvider restClientProvider, WebSocketProvider webSocketProvider,
  64. JsonSerializer serializer = null, RequestQueue requestQueue = null)
  65. : base(restClientProvider, userAgent, serializer, requestQueue)
  66. {
  67. _connectionLock = new SemaphoreSlim(1, 1);
  68. _clientId = clientId;
  69. _origin = origin;
  70. FetchCurrentUser = false;
  71. _requestQueue = requestQueue ?? new RequestQueue();
  72. _requests = new ConcurrentDictionary<Guid, RpcRequest>();
  73. _webSocketClient = webSocketProvider();
  74. //_webSocketClient.SetHeader("user-agent", DiscordConfig.UserAgent); (Causes issues in .Net 4.6+)
  75. _webSocketClient.SetHeader("origin", _origin);
  76. _webSocketClient.BinaryMessage += async (data, index, count) =>
  77. {
  78. using (var compressed = new MemoryStream(data, index + 2, count - 2))
  79. using (var decompressed = new MemoryStream())
  80. {
  81. using (var zlib = new DeflateStream(compressed, CompressionMode.Decompress))
  82. zlib.CopyTo(decompressed);
  83. decompressed.Position = 0;
  84. using (var reader = new StreamReader(decompressed))
  85. using (var jsonReader = new JsonTextReader(reader))
  86. {
  87. var msg = _serializer.Deserialize<API.Rpc.RpcFrame>(jsonReader);
  88. await _receivedRpcEvent.InvokeAsync(msg.Cmd, msg.Event, msg.Data).ConfigureAwait(false);
  89. if (msg.Nonce.IsSpecified && msg.Nonce.Value.HasValue)
  90. ProcessMessage(msg);
  91. }
  92. }
  93. };
  94. _webSocketClient.TextMessage += async text =>
  95. {
  96. using (var reader = new StringReader(text))
  97. using (var jsonReader = new JsonTextReader(reader))
  98. {
  99. var msg = _serializer.Deserialize<API.Rpc.RpcFrame>(jsonReader);
  100. await _receivedRpcEvent.InvokeAsync(msg.Cmd, msg.Event, msg.Data).ConfigureAwait(false);
  101. if (msg.Nonce.IsSpecified && msg.Nonce.Value.HasValue)
  102. ProcessMessage(msg);
  103. }
  104. };
  105. _webSocketClient.Closed += async ex =>
  106. {
  107. await DisconnectAsync().ConfigureAwait(false);
  108. await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
  109. };
  110. }
  111. internal override void Dispose(bool disposing)
  112. {
  113. if (!_isDisposed)
  114. {
  115. if (disposing)
  116. {
  117. _stateCancelToken?.Dispose();
  118. (_webSocketClient as IDisposable)?.Dispose();
  119. }
  120. _isDisposed = true;
  121. }
  122. }
  123. public async Task ConnectAsync()
  124. {
  125. await _connectionLock.WaitAsync().ConfigureAwait(false);
  126. try
  127. {
  128. await ConnectInternalAsync().ConfigureAwait(false);
  129. }
  130. finally { _connectionLock.Release(); }
  131. }
  132. internal override async Task ConnectInternalAsync()
  133. {
  134. /*if (LoginState != LoginState.LoggedIn)
  135. throw new InvalidOperationException("Client is not logged in.");*/
  136. ConnectionState = ConnectionState.Connecting;
  137. try
  138. {
  139. _stateCancelToken = new CancellationTokenSource();
  140. if (_webSocketClient != null)
  141. _webSocketClient.SetCancelToken(_stateCancelToken.Token);
  142. bool success = false;
  143. int port;
  144. string uuid = Guid.NewGuid().ToString();
  145. for ( port = DiscordRpcConfig.PortRangeStart; port <= DiscordRpcConfig.PortRangeEnd; port++)
  146. {
  147. try
  148. {
  149. string url = $"wss://{uuid}.discordapp.io:{port}/?v={DiscordRpcConfig.RpcAPIVersion}&client_id={_clientId}";
  150. await _webSocketClient.ConnectAsync(url).ConfigureAwait(false);
  151. success = true;
  152. break;
  153. }
  154. catch (Exception)
  155. {
  156. }
  157. }
  158. if (!success)
  159. throw new Exception("Unable to connect to the RPC server.");
  160. SetBaseUrl($"https://{uuid}.discordapp.io:{port}/");
  161. ConnectionState = ConnectionState.Connected;
  162. }
  163. catch (Exception)
  164. {
  165. await DisconnectInternalAsync().ConfigureAwait(false);
  166. throw;
  167. }
  168. }
  169. public async Task DisconnectAsync()
  170. {
  171. await _connectionLock.WaitAsync().ConfigureAwait(false);
  172. try
  173. {
  174. await DisconnectInternalAsync().ConfigureAwait(false);
  175. }
  176. finally { _connectionLock.Release(); }
  177. }
  178. internal override async Task DisconnectInternalAsync()
  179. {
  180. if (_webSocketClient == null)
  181. throw new NotSupportedException("This client is not configured with websocket support.");
  182. if (ConnectionState == ConnectionState.Disconnected) return;
  183. ConnectionState = ConnectionState.Disconnecting;
  184. try { _stateCancelToken?.Cancel(false); }
  185. catch { }
  186. await _webSocketClient.DisconnectAsync().ConfigureAwait(false);
  187. ConnectionState = ConnectionState.Disconnected;
  188. }
  189. //Core
  190. public async Task<TResponse> SendRpcAsync<TResponse>(string cmd, object payload, Optional<string> evt = default(Optional<string>), RequestOptions options = null)
  191. where TResponse : class
  192. {
  193. return await SendRpcAsyncInternal<TResponse>(cmd, payload, evt, options).ConfigureAwait(false);
  194. }
  195. private async Task<TResponse> SendRpcAsyncInternal<TResponse>(string cmd, object payload, Optional<string> evt, RequestOptions options)
  196. where TResponse : class
  197. {
  198. if (!options.IgnoreState)
  199. CheckState();
  200. byte[] bytes = null;
  201. var guid = Guid.NewGuid();
  202. payload = new API.Rpc.RpcFrame { Cmd = cmd, Event = evt, Args = payload, Nonce = guid };
  203. if (payload != null)
  204. {
  205. var json = SerializeJson(payload);
  206. bytes = Encoding.UTF8.GetBytes(json);
  207. }
  208. var requestTracker = new RpcRequest<TResponse>(options);
  209. _requests[guid] = requestTracker;
  210. await _requestQueue.SendAsync(new WebSocketRequest(_webSocketClient, null, bytes, true, options)).ConfigureAwait(false);
  211. await _sentRpcMessageEvent.InvokeAsync(cmd).ConfigureAwait(false);
  212. return await requestTracker.Promise.Task.ConfigureAwait(false);
  213. }
  214. //Rpc
  215. public async Task<AuthenticateResponse> SendAuthenticateAsync(RequestOptions options = null)
  216. {
  217. options = RequestOptions.CreateOrClone(options);
  218. var msg = new AuthenticateParams
  219. {
  220. AccessToken = _authToken
  221. };
  222. options.IgnoreState = true;
  223. return await SendRpcAsync<AuthenticateResponse>("AUTHENTICATE", msg, options: options).ConfigureAwait(false);
  224. }
  225. public async Task<AuthorizeResponse> SendAuthorizeAsync(IReadOnlyCollection<string> scopes, string rpcToken = null, RequestOptions options = null)
  226. {
  227. options = RequestOptions.CreateOrClone(options);
  228. var msg = new AuthorizeParams
  229. {
  230. ClientId = _clientId,
  231. Scopes = scopes,
  232. RpcToken = rpcToken != null ? rpcToken : Optional.Create<string>()
  233. };
  234. if (options.Timeout == null)
  235. options.Timeout = 60000; //This requires manual input on the user's end, lets give them more time
  236. options.IgnoreState = true;
  237. return await SendRpcAsync<AuthorizeResponse>("AUTHORIZE", msg, options: options).ConfigureAwait(false);
  238. }
  239. public async Task<GetGuildsResponse> SendGetGuildsAsync(RequestOptions options = null)
  240. {
  241. options = RequestOptions.CreateOrClone(options);
  242. return await SendRpcAsync<GetGuildsResponse>("GET_GUILDS", null, options: options).ConfigureAwait(false);
  243. }
  244. public async Task<Rpc.Guild> SendGetGuildAsync(ulong guildId, RequestOptions options = null)
  245. {
  246. options = RequestOptions.CreateOrClone(options);
  247. var msg = new GetGuildParams
  248. {
  249. GuildId = guildId
  250. };
  251. return await SendRpcAsync<Rpc.Guild>("GET_GUILD", msg, options: options).ConfigureAwait(false);
  252. }
  253. public async Task<GetChannelsResponse> SendGetChannelsAsync(ulong guildId, RequestOptions options = null)
  254. {
  255. options = RequestOptions.CreateOrClone(options);
  256. var msg = new GetChannelsParams
  257. {
  258. GuildId = guildId
  259. };
  260. return await SendRpcAsync<GetChannelsResponse>("GET_CHANNELS", msg, options: options).ConfigureAwait(false);
  261. }
  262. public async Task<Rpc.Channel> SendGetChannelAsync(ulong channelId, RequestOptions options = null)
  263. {
  264. options = RequestOptions.CreateOrClone(options);
  265. var msg = new GetChannelParams
  266. {
  267. ChannelId = channelId
  268. };
  269. return await SendRpcAsync<Rpc.Channel>("GET_CHANNEL", msg, options: options).ConfigureAwait(false);
  270. }
  271. public async Task<Rpc.Channel> SendSelectTextChannelAsync(ulong channelId, RequestOptions options = null)
  272. {
  273. options = RequestOptions.CreateOrClone(options);
  274. var msg = new SelectChannelParams
  275. {
  276. ChannelId = channelId
  277. };
  278. return await SendRpcAsync<Rpc.Channel>("SELECT_TEXT_CHANNEL", msg, options: options).ConfigureAwait(false);
  279. }
  280. public async Task<Rpc.Channel> SendSelectVoiceChannelAsync(ulong channelId, bool force = false, RequestOptions options = null)
  281. {
  282. options = RequestOptions.CreateOrClone(options);
  283. var msg = new SelectChannelParams
  284. {
  285. ChannelId = channelId,
  286. Force = force
  287. };
  288. return await SendRpcAsync<Rpc.Channel>("SELECT_VOICE_CHANNEL", msg, options: options).ConfigureAwait(false);
  289. }
  290. public async Task<SubscriptionResponse> SendGlobalSubscribeAsync(string evt, RequestOptions options = null)
  291. {
  292. options = RequestOptions.CreateOrClone(options);
  293. return await SendRpcAsync<SubscriptionResponse>("SUBSCRIBE", null, evt: evt, options: options).ConfigureAwait(false);
  294. }
  295. public async Task<SubscriptionResponse> SendGlobalUnsubscribeAsync(string evt, RequestOptions options = null)
  296. {
  297. options = RequestOptions.CreateOrClone(options);
  298. return await SendRpcAsync<SubscriptionResponse>("UNSUBSCRIBE", null, evt: evt, options: options).ConfigureAwait(false);
  299. }
  300. public async Task<SubscriptionResponse> SendGuildSubscribeAsync(string evt, ulong guildId, RequestOptions options = null)
  301. {
  302. options = RequestOptions.CreateOrClone(options);
  303. var msg = new GuildSubscriptionParams
  304. {
  305. GuildId = guildId
  306. };
  307. return await SendRpcAsync<SubscriptionResponse>("SUBSCRIBE", msg, evt: evt, options: options).ConfigureAwait(false);
  308. }
  309. public async Task<SubscriptionResponse> SendGuildUnsubscribeAsync(string evt, ulong guildId, RequestOptions options = null)
  310. {
  311. options = RequestOptions.CreateOrClone(options);
  312. var msg = new GuildSubscriptionParams
  313. {
  314. GuildId = guildId
  315. };
  316. return await SendRpcAsync<SubscriptionResponse>("UNSUBSCRIBE", msg, evt: evt, options: options).ConfigureAwait(false);
  317. }
  318. public async Task<SubscriptionResponse> SendChannelSubscribeAsync(string evt, ulong channelId, RequestOptions options = null)
  319. {
  320. options = RequestOptions.CreateOrClone(options);
  321. var msg = new ChannelSubscriptionParams
  322. {
  323. ChannelId = channelId
  324. };
  325. return await SendRpcAsync<SubscriptionResponse>("SUBSCRIBE", msg, evt: evt, options: options).ConfigureAwait(false);
  326. }
  327. public async Task<SubscriptionResponse> SendChannelUnsubscribeAsync(string evt, ulong channelId, RequestOptions options = null)
  328. {
  329. options = RequestOptions.CreateOrClone(options);
  330. var msg = new ChannelSubscriptionParams
  331. {
  332. ChannelId = channelId
  333. };
  334. return await SendRpcAsync<SubscriptionResponse>("UNSUBSCRIBE", msg, evt: evt, options: options).ConfigureAwait(false);
  335. }
  336. public async Task<API.Rpc.VoiceSettings> GetVoiceSettingsAsync(RequestOptions options = null)
  337. {
  338. options = RequestOptions.CreateOrClone(options);
  339. return await SendRpcAsync<API.Rpc.VoiceSettings>("GET_VOICE_SETTINGS", null, options: options).ConfigureAwait(false);
  340. }
  341. public async Task SetVoiceSettingsAsync(API.Rpc.VoiceSettings settings, RequestOptions options = null)
  342. {
  343. options = RequestOptions.CreateOrClone(options);
  344. await SendRpcAsync<API.Rpc.VoiceSettings>("SET_VOICE_SETTINGS", settings, options: options).ConfigureAwait(false);
  345. }
  346. public async Task SetUserVoiceSettingsAsync(ulong userId, API.Rpc.UserVoiceSettings settings, RequestOptions options = null)
  347. {
  348. options = RequestOptions.CreateOrClone(options);
  349. settings.UserId = userId;
  350. await SendRpcAsync<API.Rpc.UserVoiceSettings>("SET_USER_VOICE_SETTINGS", settings, options: options).ConfigureAwait(false);
  351. }
  352. private bool ProcessMessage(API.Rpc.RpcFrame msg)
  353. {
  354. RpcRequest requestTracker;
  355. if (_requests.TryGetValue(msg.Nonce.Value.Value, out requestTracker))
  356. {
  357. if (msg.Event.GetValueOrDefault("") == "ERROR")
  358. {
  359. var _ = requestTracker.SetExceptionAsync(msg.Data.GetValueOrDefault() as JToken, _serializer);
  360. }
  361. else
  362. {
  363. var _ = requestTracker.SetResultAsync(msg.Data.GetValueOrDefault() as JToken, _serializer);
  364. }
  365. return true;
  366. }
  367. else
  368. return false;
  369. }
  370. }
  371. }