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.

DiscordVoiceApiClient.cs 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #pragma warning disable CS1591
  2. using Discord.API;
  3. using Discord.API.Voice;
  4. using Discord.Net.Converters;
  5. using Discord.Net.Udp;
  6. using Discord.Net.WebSockets;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Diagnostics;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.IO.Compression;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace Discord.Audio
  17. {
  18. internal class DiscordVoiceAPIClient
  19. {
  20. public const int MaxBitrate = 128 * 1024;
  21. public const string Mode = "xsalsa20_poly1305";
  22. public event Func<string, string, double, Task> SentRequest { add { _sentRequestEvent.Add(value); } remove { _sentRequestEvent.Remove(value); } }
  23. private readonly AsyncEvent<Func<string, string, double, Task>> _sentRequestEvent = new AsyncEvent<Func<string, string, double, Task>>();
  24. public event Func<VoiceOpCode, Task> SentGatewayMessage { add { _sentGatewayMessageEvent.Add(value); } remove { _sentGatewayMessageEvent.Remove(value); } }
  25. private readonly AsyncEvent<Func<VoiceOpCode, Task>> _sentGatewayMessageEvent = new AsyncEvent<Func<VoiceOpCode, Task>>();
  26. public event Func<Task> SentDiscovery { add { _sentDiscoveryEvent.Add(value); } remove { _sentDiscoveryEvent.Remove(value); } }
  27. private readonly AsyncEvent<Func<Task>> _sentDiscoveryEvent = new AsyncEvent<Func<Task>>();
  28. public event Func<int, Task> SentData { add { _sentDataEvent.Add(value); } remove { _sentDataEvent.Remove(value); } }
  29. private readonly AsyncEvent<Func<int, Task>> _sentDataEvent = new AsyncEvent<Func<int, Task>>();
  30. public event Func<VoiceOpCode, object, Task> ReceivedEvent { add { _receivedEvent.Add(value); } remove { _receivedEvent.Remove(value); } }
  31. private readonly AsyncEvent<Func<VoiceOpCode, object, Task>> _receivedEvent = new AsyncEvent<Func<VoiceOpCode, object, Task>>();
  32. public event Func<byte[], Task> ReceivedPacket { add { _receivedPacketEvent.Add(value); } remove { _receivedPacketEvent.Remove(value); } }
  33. private readonly AsyncEvent<Func<byte[], Task>> _receivedPacketEvent = new AsyncEvent<Func<byte[], Task>>();
  34. public event Func<Exception, Task> Disconnected { add { _disconnectedEvent.Add(value); } remove { _disconnectedEvent.Remove(value); } }
  35. private readonly AsyncEvent<Func<Exception, Task>> _disconnectedEvent = new AsyncEvent<Func<Exception, Task>>();
  36. private readonly JsonSerializer _serializer;
  37. private readonly SemaphoreSlim _connectionLock;
  38. private CancellationTokenSource _connectCancelToken;
  39. private IUdpSocket _udp;
  40. private bool _isDisposed;
  41. private ulong _nextKeepalive;
  42. public ulong GuildId { get; }
  43. internal IWebSocketClient WebSocketClient { get; }
  44. public ConnectionState ConnectionState { get; private set; }
  45. public ushort UdpPort => _udp.Port;
  46. internal DiscordVoiceAPIClient(ulong guildId, WebSocketProvider webSocketProvider, UdpSocketProvider udpSocketProvider, JsonSerializer serializer = null)
  47. {
  48. GuildId = guildId;
  49. _connectionLock = new SemaphoreSlim(1, 1);
  50. _udp = udpSocketProvider();
  51. _udp.ReceivedDatagram += async (data, index, count) =>
  52. {
  53. if (index != 0 || count != data.Length)
  54. {
  55. var newData = new byte[count];
  56. Buffer.BlockCopy(data, index, newData, 0, count);
  57. data = newData;
  58. }
  59. await _receivedPacketEvent.InvokeAsync(data).ConfigureAwait(false);
  60. };
  61. WebSocketClient = webSocketProvider();
  62. //_gatewayClient.SetHeader("user-agent", DiscordConfig.UserAgent); //(Causes issues in .Net 4.6+)
  63. WebSocketClient.BinaryMessage += async (data, index, count) =>
  64. {
  65. using (var compressed = new MemoryStream(data, index + 2, count - 2))
  66. using (var decompressed = new MemoryStream())
  67. {
  68. using (var zlib = new DeflateStream(compressed, CompressionMode.Decompress))
  69. zlib.CopyTo(decompressed);
  70. decompressed.Position = 0;
  71. using (var reader = new StreamReader(decompressed))
  72. {
  73. var msg = JsonConvert.DeserializeObject<SocketFrame>(reader.ReadToEnd());
  74. await _receivedEvent.InvokeAsync((VoiceOpCode)msg.Operation, msg.Payload).ConfigureAwait(false);
  75. }
  76. }
  77. };
  78. WebSocketClient.TextMessage += async text =>
  79. {
  80. var msg = JsonConvert.DeserializeObject<SocketFrame>(text);
  81. await _receivedEvent.InvokeAsync((VoiceOpCode)msg.Operation, msg.Payload).ConfigureAwait(false);
  82. };
  83. WebSocketClient.Closed += async ex =>
  84. {
  85. await DisconnectAsync().ConfigureAwait(false);
  86. await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
  87. };
  88. _serializer = serializer ?? new JsonSerializer { ContractResolver = new DiscordContractResolver() };
  89. }
  90. private void Dispose(bool disposing)
  91. {
  92. if (!_isDisposed)
  93. {
  94. if (disposing)
  95. {
  96. _connectCancelToken?.Dispose();
  97. (_udp as IDisposable)?.Dispose();
  98. (WebSocketClient as IDisposable)?.Dispose();
  99. }
  100. _isDisposed = true;
  101. }
  102. }
  103. public void Dispose() => Dispose(true);
  104. public async Task SendAsync(VoiceOpCode opCode, object payload, RequestOptions options = null)
  105. {
  106. byte[] bytes = null;
  107. payload = new SocketFrame { Operation = (int)opCode, Payload = payload };
  108. if (payload != null)
  109. bytes = Encoding.UTF8.GetBytes(SerializeJson(payload));
  110. await WebSocketClient.SendAsync(bytes, 0, bytes.Length, true).ConfigureAwait(false);
  111. await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
  112. }
  113. public async Task SendAsync(byte[] data, int offset, int bytes)
  114. {
  115. await _udp.SendAsync(data, offset, bytes).ConfigureAwait(false);
  116. await _sentDataEvent.InvokeAsync(bytes).ConfigureAwait(false);
  117. }
  118. //WebSocket
  119. public async Task SendHeartbeatAsync(RequestOptions options = null)
  120. {
  121. await SendAsync(VoiceOpCode.Heartbeat, DateTimeUtils.ToUnixMilliseconds(DateTimeOffset.UtcNow), options: options).ConfigureAwait(false);
  122. }
  123. public async Task SendIdentityAsync(ulong userId, string sessionId, string token)
  124. {
  125. await SendAsync(VoiceOpCode.Identify, new IdentifyParams
  126. {
  127. GuildId = GuildId,
  128. UserId = userId,
  129. SessionId = sessionId,
  130. Token = token
  131. }).ConfigureAwait(false);
  132. }
  133. public async Task SendSelectProtocol(string externalIp, int externalPort)
  134. {
  135. await SendAsync(VoiceOpCode.SelectProtocol, new SelectProtocolParams
  136. {
  137. Protocol = "udp",
  138. Data = new UdpProtocolInfo
  139. {
  140. Address = externalIp,
  141. Port = externalPort,
  142. Mode = Mode
  143. }
  144. }).ConfigureAwait(false);
  145. }
  146. public async Task SendSetSpeaking(bool value)
  147. {
  148. await SendAsync(VoiceOpCode.Speaking, new SpeakingParams
  149. {
  150. IsSpeaking = value,
  151. Delay = 0
  152. }).ConfigureAwait(false);
  153. }
  154. public async Task ConnectAsync(string url)
  155. {
  156. await _connectionLock.WaitAsync().ConfigureAwait(false);
  157. try
  158. {
  159. await ConnectInternalAsync(url).ConfigureAwait(false);
  160. }
  161. finally { _connectionLock.Release(); }
  162. }
  163. private async Task ConnectInternalAsync(string url)
  164. {
  165. ConnectionState = ConnectionState.Connecting;
  166. try
  167. {
  168. _connectCancelToken = new CancellationTokenSource();
  169. var cancelToken = _connectCancelToken.Token;
  170. WebSocketClient.SetCancelToken(cancelToken);
  171. await WebSocketClient.ConnectAsync(url).ConfigureAwait(false);
  172. _udp.SetCancelToken(cancelToken);
  173. await _udp.StartAsync().ConfigureAwait(false);
  174. ConnectionState = ConnectionState.Connected;
  175. }
  176. catch (Exception)
  177. {
  178. await DisconnectInternalAsync().ConfigureAwait(false);
  179. throw;
  180. }
  181. }
  182. public async Task DisconnectAsync()
  183. {
  184. await _connectionLock.WaitAsync().ConfigureAwait(false);
  185. try
  186. {
  187. await DisconnectInternalAsync().ConfigureAwait(false);
  188. }
  189. finally { _connectionLock.Release(); }
  190. }
  191. private async Task DisconnectInternalAsync()
  192. {
  193. if (ConnectionState == ConnectionState.Disconnected) return;
  194. ConnectionState = ConnectionState.Disconnecting;
  195. try { _connectCancelToken?.Cancel(false); }
  196. catch { }
  197. //Wait for tasks to complete
  198. await _udp.StopAsync().ConfigureAwait(false);
  199. await WebSocketClient.DisconnectAsync().ConfigureAwait(false);
  200. ConnectionState = ConnectionState.Disconnected;
  201. }
  202. //Udp
  203. public async Task SendDiscoveryAsync(uint ssrc)
  204. {
  205. var packet = new byte[70];
  206. packet[0] = (byte)(ssrc >> 24);
  207. packet[1] = (byte)(ssrc >> 16);
  208. packet[2] = (byte)(ssrc >> 8);
  209. packet[3] = (byte)(ssrc >> 0);
  210. await SendAsync(packet, 0, 70).ConfigureAwait(false);
  211. await _sentDiscoveryEvent.InvokeAsync().ConfigureAwait(false);
  212. }
  213. public async Task<ulong> SendKeepaliveAsync()
  214. {
  215. var value = _nextKeepalive++;
  216. var packet = new byte[8];
  217. packet[0] = (byte)(value >> 0);
  218. packet[1] = (byte)(value >> 8);
  219. packet[2] = (byte)(value >> 16);
  220. packet[3] = (byte)(value >> 24);
  221. packet[4] = (byte)(value >> 32);
  222. packet[5] = (byte)(value >> 40);
  223. packet[6] = (byte)(value >> 48);
  224. packet[7] = (byte)(value >> 56);
  225. await SendAsync(packet, 0, 8).ConfigureAwait(false);
  226. return value;
  227. }
  228. public void SetUdpEndpoint(string ip, int port)
  229. {
  230. _udp.SetDestination(ip, port);
  231. }
  232. //Helpers
  233. private static double ToMilliseconds(Stopwatch stopwatch) => Math.Round((double)stopwatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000.0, 2);
  234. private string SerializeJson(object value)
  235. {
  236. var sb = new StringBuilder(256);
  237. using (TextWriter text = new StringWriter(sb, CultureInfo.InvariantCulture))
  238. using (JsonWriter writer = new JsonTextWriter(text))
  239. _serializer.Serialize(writer, value);
  240. return sb.ToString();
  241. }
  242. private T DeserializeJson<T>(Stream jsonStream)
  243. {
  244. using (TextReader text = new StreamReader(jsonStream))
  245. using (JsonReader reader = new JsonTextReader(text))
  246. return _serializer.Deserialize<T>(reader);
  247. }
  248. }
  249. }