Ejemplo n.º 1
0
        private async Task InitialUDPConnection()
        {
            try
            {
                _udp = new UdpClient(Params.port); //passes in proper port
                //_udp.
                _udp.DontFragment = false;
                _udp.Connect(VoiceEndpoint.Replace(":80", ""), Params.port);

                VoiceDebugLogger.Log($"Initialized UDP Client at {VoiceEndpoint.Replace(":80", "")}:{Params.port}");

                udpEndpoint = new IPEndPoint(Dns.GetHostAddresses(VoiceEndpoint.Replace(":80", ""))[0], 80);


                byte[] packet = new byte[70]; //the initial packet
                packet[0]     = (byte)((Params.ssrc >> 24) & 0xFF);
                packet[1]     = (byte)((Params.ssrc >> 16) & 0xFF);
                packet[2]     = (byte)((Params.ssrc >> 8) & 0xFF);
                packet[3]     = (byte)((Params.ssrc >> 0) & 0xFF);

#if NETFX4_5
                await _udp.SendAsync(packet, packet.Length).ConfigureAwait(false); //sends this initial packet.

                VoiceDebugLogger.Log("Sent ssrc packet.");

                UdpReceiveResult resultingMessage = await _udp.ReceiveAsync().ConfigureAwait(false); //receive a response packet

                Console.WriteLine($"Receiving");
#else
                _udp.Send(packet, packet.Length, udpEndpoint);
                VoiceDebugLogger.Log("Sent ssrc packet.");

                byte[] resultingMessage = _udp.Receive(ref udpEndpoint);
#endif

#if NETFX4_5
                if (resultingMessage != null && resultingMessage.Buffer.Length > 0)
#else
                if (resultingMessage != null && resultingMessage.Length > 0)
#endif
                {
                    VoiceDebugLogger.Log("Received IP packet, reading..");
#if NETFX4_5
                    await SendIPOverUDP(GetIPAndPortFromPacket(resultingMessage.Buffer)).ConfigureAwait(false);
#else
                    var ipAndPort = GetIPAndPortFromPacket(resultingMessage);
                    udpEndpoint   = new IPEndPoint(ipAndPort.Address, ipAndPort.port);
                    await SendIPOverUDP(ipAndPort).ConfigureAwait(false);
#endif
                }
                else
                {
                    VoiceDebugLogger.Log("No IP packet received.", MessageLevel.Critical);
                }
            }
            catch (Exception ex)
            {
                VoiceDebugLogger.Log("UDP Client Error: " + ex.Message, MessageLevel.Critical);
            }
        }
Ejemplo n.º 2
0
        private async Task InitialUDPConnection()
        {
            try
            {
                _udp = new UdpClient(Params.port); //passes in proper port
                _udp.DontFragment = false;
                _udp.Connect(VoiceEndpoint.Replace(":80", ""), Params.port);

                VoiceDebugLogger.Log($"Initialized UDP Client at {VoiceEndpoint.Replace(":80", "")}:{Params.port}");

                byte[] packet = new byte[70]; //the initial packet
                packet[0] = (byte)((Params.ssrc >> 24) & 0xFF);
                packet[1] = (byte)((Params.ssrc >> 16) & 0xFF);
                packet[2] = (byte)((Params.ssrc >> 8) & 0xFF);
                packet[3] = (byte)((Params.ssrc >> 0) & 0xFF);

                await _udp.SendAsync(packet, packet.Length).ConfigureAwait(false); //sends this initial packet.

                VoiceDebugLogger.Log("Sent ssrc packet.");

                UdpReceiveResult resultingMessage = await _udp.ReceiveAsync().ConfigureAwait(false); //receive a response packet

                if (resultingMessage != null || resultingMessage.Buffer.Length > 0)
                {
                    VoiceDebugLogger.Log("Received IP packet, reading..");
                    await SendIPOverUDP(GetIPAndPortFromPacket(resultingMessage.Buffer)).ConfigureAwait(false);
                }
                else
                {
                    VoiceDebugLogger.Log("No IP packet received.", MessageLevel.Critical);
                }
            }
            catch (Exception ex)
            {
                VoiceDebugLogger.Log("UDP Client Error: " + ex.Message, MessageLevel.Critical);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Begins the voice client connection.
        /// </summary>
        public void Initiate()
        {
            if (Me == null)
            {
                if (_parent != null)
                {
                    Me = _parent.Me;
                }
                else
                {
                    throw new NullReferenceException("VoiceClient's main client reference was null!");
                }
            }

            MembersInChannel.Add(Me);

            VoiceDebugLogger.LogMessageReceived += (sender, e) =>
            {
                if (DebugMessageReceived != null)
                {
                    DebugMessageReceived(this, e);
                }
            };

            VoiceWebSocket = new WebSocket(VoiceEndpoint.StartsWith("wss://") ? VoiceEndpoint.Replace(":80", "") :
                                           "wss://" + VoiceEndpoint.Replace(":80", ""));
            VoiceWebSocket.EnableAutoSendPing         = false;
            VoiceWebSocket.AllowUnstrustedCertificate = true;
            VoiceWebSocket.NoDelay = true;
            VoiceWebSocket.Closed += VoiceWebSocket_OnClose;
            VoiceWebSocket.Error  += VoiceWebSocket_OnError;

            VoiceWebSocket.MessageReceived += (s, e) =>
            {
                try
                {
                    VoiceWebSocket_OnMessage(s, e);
                }
                catch (Exception ex)
                {
                    VoiceDebugLogger.Log($"Exception while awaiting OnMessage?!\n\tMessage: {ex.Message}\n\tStack: {ex.StackTrace}", MessageLevel.Critical);
                }
            };
            VoiceWebSocket.Opened += (sender, e) =>
            {
                string initMsg = JsonConvert.SerializeObject(new
                {
                    op = 0,
                    d  = new
                    {
                        server_id  = Guild.ID,
                        user_id    = Me.ID,
                        session_id = SessionID,
                        token      = Token
                    }
                });

                VoiceDebugLogger.Log("VoiceWebSocket opened, sending initial json. ( " + initMsg + ") ");

                VoiceWebSocket.Send(initMsg);
            };

            VoiceWebSocket.Open();
        }