private DiscordIpPort GetIPAndPortFromPacket(byte[] packet) { DiscordIpPort returnVal = new DiscordIpPort(); //quoth thy danny //#the ip is ascii starting at the 4th byte and ending at the first null int startingIPIndex = 4; int endingIPIndex = 4; for (int i = startingIPIndex; i < packet.Length; i++) { if (packet[i] != (byte)0) { endingIPIndex++; } else { break; } } byte[] ipArray = new byte[endingIPIndex - startingIPIndex]; Buffer.BlockCopy(packet, startingIPIndex, ipArray, 0, ipArray.Length); //quoth thy wise danny part two: //# the port is a little endian unsigned short in the last two bytes //# yes, this is different endianness from everything else int port = packet[packet.Length - 2] | packet[packet.Length - 1] << 8; returnVal.Address = IPAddress.Parse(System.Text.Encoding.ASCII.GetString(ipArray)); returnVal.port = port; VoiceDebugLogger.Log($"Our IP is {returnVal.Address} and we're using port {returnVal.port}."); return(returnVal); }
/// <summary> /// Sends our IP over UDP for Discord's voice server to process. Also sends op 1 /// </summary> /// <param name="buffer">The byte[] returned after sending your ssrc.</param> /// <returns></returns> private async Task SendIPOverUDP(DiscordIpPort ipPort) { string msg = JsonConvert.SerializeObject(new { op = 1, d = new { protocol = "udp", data = new { address = ipPort.Address.ToString(), port = ipPort.port, mode = encryptionMode } } }); VoiceDebugLogger.Log("Sending our IP over WebSocket ( " + msg.ToString() + " ) "); VoiceWebSocket.Send(msg); //await Task.Run(() => VoiceWebSocket.Send(msg)).ConfigureAwait(false); //idk lets try it }