Clean up voice.go, should work better on other systems now, closes #31

This commit is contained in:
Bruce Marriner 2015-12-20 17:04:31 -06:00
parent b87add9152
commit 8c301a4126

View file

@ -176,9 +176,6 @@ type voiceUDPOp struct {
// and can be used to send or receive audio. // and can be used to send or receive audio.
func (s *Session) VoiceOpenUDP() { func (s *Session) VoiceOpenUDP() {
// TODO: add code to convert hostname into an IP address to avoid problems
// with frequent DNS lookups. ??
udpHost := fmt.Sprintf("%s:%d", strings.TrimSuffix(s.VEndpoint, ":80"), s.Vop2.Port) udpHost := fmt.Sprintf("%s:%d", strings.TrimSuffix(s.VEndpoint, ":80"), s.Vop2.Port)
serverAddr, err := net.ResolveUDPAddr("udp", udpHost) serverAddr, err := net.ResolveUDPAddr("udp", udpHost)
if err != nil { if err != nil {
@ -206,16 +203,22 @@ func (s *Session) VoiceOpenUDP() {
fmt.Println("Voice RLEN should be 70 but isn't") fmt.Println("Voice RLEN should be 70 but isn't")
} }
// TODO need serious changes, this will likely not work on all IPs! // Loop over position 4 though 20 to grab the IP address
ip := string(rb[4:16]) // must be a better way. TODO: NEEDS TESTING // Should never be beyond position 20.
port := make([]byte, 2) var ip string
port[0] = rb[68] for i := 4; i < 20; i++ {
port[1] = rb[69] if rb[i] == 0 {
p := binary.LittleEndian.Uint16(port) break
}
ip += string(rb[i])
}
// Grab port from postion 68 and 69
port := binary.LittleEndian.Uint16(rb[68:70])
// Take the parsed data from above and send it back to Discord // Take the parsed data from above and send it back to Discord
// to finalize the UDP handshake. // to finalize the UDP handshake.
jsondata := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, p, "plain"}}} jsondata := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, port, "plain"}}}
err = s.VwsConn.WriteJSON(jsondata) err = s.VwsConn.WriteJSON(jsondata)
if err != nil { if err != nil {
@ -223,9 +226,6 @@ func (s *Session) VoiceOpenUDP() {
return return
} }
s.UDPReady = true s.UDPReady = true
// continue to listen for future packets
// go s.VoiceListenUDP()
} }
// VoiceCloseUDP closes the voice UDP connection. // VoiceCloseUDP closes the voice UDP connection.
@ -267,7 +267,7 @@ func (s *Session) VoiceListenUDP() {
// go s.VoiceUDPKeepalive(s.Vop2.HeartbeatInterval) // lets try the ws timer // go s.VoiceUDPKeepalive(s.Vop2.HeartbeatInterval) // lets try the ws timer
for { for {
b := make([]byte, 1024) b := make([]byte, 1024) //TODO DO NOT PUT MAKE INSIDE LOOP
rlen, _, err := s.UDPConn.ReadFromUDP(b) rlen, _, err := s.UDPConn.ReadFromUDP(b)
if err != nil { if err != nil {
fmt.Println("Error reading from UDP:", err) fmt.Println("Error reading from UDP:", err)
@ -330,14 +330,11 @@ func (s *Session) VoiceHeartbeat(i time.Duration) {
ticker := time.NewTicker(i * time.Millisecond) ticker := time.NewTicker(i * time.Millisecond)
for { for {
timestamp := int(time.Now().Unix()) err := s.VwsConn.WriteJSON(voiceHeartbeatOp{3, int(time.Now().Unix())})
json := voiceHeartbeatOp{3, timestamp}
err := s.VwsConn.WriteJSON(json)
if err != nil { if err != nil {
s.VoiceReady = false s.VoiceReady = false
fmt.Println(err) fmt.Println(err)
return // log error? return // TODO LOG ERROR
} }
s.VoiceReady = true s.VoiceReady = true