Forráskód Böngészése

More improvements to voice support

Bruce Marriner 9 éve
szülő
commit
093d3be770
3 módosított fájl, 22 hozzáadás és 22 törlés
  1. 1 1
      structs.go
  2. 8 8
      voice.go
  3. 13 13
      wsapi.go

+ 1 - 1
structs.go

@@ -74,7 +74,7 @@ type Session struct {
 	// Everything below here is used for Voice testing.
 	// This stuff is almost guarenteed to change a lot
 	// and is even a bit hackish right now.
-	Voice *voice // Stores all details related to voice connections
+	Voice *Voice // Stores all details related to voice connections
 
 	// Managed state object, updated with events.
 	State        *State

+ 8 - 8
voice.go

@@ -29,7 +29,7 @@ import (
 // ------------------------------------------------------------------------------------------------
 
 // A Voice struct holds all data and functions related to Discord Voice support.
-type voice struct {
+type Voice struct {
 	sync.Mutex
 	Ready   bool
 	Debug   bool
@@ -75,7 +75,7 @@ type voiceHandshakeOp struct {
 // Open opens a voice connection.  This should be called
 // after VoiceChannelJoin is used and the data VOICE websocket events
 // are captured.
-func (v *voice) Open() (err error) {
+func (v *Voice) Open() (err error) {
 
 	// TODO: How do we handle changing channels?
 	// Don't open a websocket if one is already open
@@ -108,7 +108,7 @@ func (v *voice) Open() (err error) {
 }
 
 // Close closes the voice connection
-func (v *voice) Close() {
+func (v *Voice) Close() {
 
 	if v.UDPConn != nil {
 		v.UDPConn.Close()
@@ -122,7 +122,7 @@ func (v *voice) Close() {
 // wsListen listens on the voice websocket for messages and passes them
 // to the voice event handler.  This is automaticly called by the WS.Open
 // func when needed.
-func (v *voice) wsListen() {
+func (v *Voice) wsListen() {
 
 	for {
 		messageType, message, err := v.wsConn.ReadMessage()
@@ -142,7 +142,7 @@ func (v *voice) wsListen() {
 
 // wsEvent handles any voice websocket events. This is only called by the
 // wsListen() function.
-func (v *voice) wsEvent(messageType int, message []byte) {
+func (v *Voice) wsEvent(messageType int, message []byte) {
 
 	if v.Debug {
 		fmt.Println("wsEvent received: ", messageType)
@@ -195,7 +195,7 @@ type voiceHeartbeatOp struct {
 // wsHeartbeat sends regular heartbeats to voice Discord so it knows the client
 // is still connected.  If you do not send these heartbeats Discord will
 // disconnect the websocket connection after a few seconds.
-func (v *voice) wsHeartbeat(i time.Duration) {
+func (v *Voice) wsHeartbeat(i time.Duration) {
 
 	ticker := time.NewTicker(i * time.Millisecond)
 	for {
@@ -223,7 +223,7 @@ type voiceSpeakingOp struct {
 // This must be sent as true prior to sending audio and should be set to false
 // once finished sending audio.
 //  b  : Send true if speaking, false if not.
-func (v *voice) Speaking(b bool) (err error) {
+func (v *Voice) Speaking(b bool) (err error) {
 
 	if v.wsConn == nil {
 		return fmt.Errorf("No Voice websocket.")
@@ -263,7 +263,7 @@ type voiceUDPOp struct {
 // initial required handshake.  This connect is left open in the session
 // and can be used to send or receive audio.  This should only be called
 // from voice.wsEvent OP2
-func (v *voice) udpOpen() (err error) {
+func (v *Voice) udpOpen() (err error) {
 
 	host := fmt.Sprintf("%s:%d", strings.TrimSuffix(v.endpoint, ":80"), v.OP2.Port)
 	addr, err := net.ResolveUDPAddr("udp", host)

+ 13 - 13
wsapi.go

@@ -532,8 +532,9 @@ func (s *Session) Heartbeat(i time.Duration) {
 	}
 }
 
-// Everything below is experimental Voice support code
-// all of it will get changed and moved around.
+// ------------------------------------------------------------------------------------------------
+// Code related to voice connections that initiate over the data websocket
+// ------------------------------------------------------------------------------------------------
 
 // A VoiceServerUpdate stores the data received during the Voice Server Update
 // data websocket event. This data is used during the initial Voice Channel
@@ -571,8 +572,10 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (err error)
 	}
 
 	// Create new voice{} struct if one does not exist.
+	// If you create this prior to calling this func then you can manually
+	// set some variables if needed, such as to enable debugging.
 	if s.Voice == nil {
-		s.Voice = &voice{}
+		s.Voice = &Voice{}
 	}
 	// TODO : Determine how to properly change channels and change guild
 	// and channel when you are already connected to an existing channel.
@@ -588,10 +591,6 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (err error)
 	s.Voice.guildID = gID
 	s.Voice.channelID = cID
 
-	// NOTE: This could remain open and monitor for the followup
-	// websocket events and then the voice ws/udp
-	// connection then if that fails, return with an error
-	// but doing so would add a lot of delay to the response..
 	return
 }
 
@@ -600,6 +599,12 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (err error)
 // for the session user.
 func (s *Session) onVoiceStateUpdate(st *VoiceState) {
 
+	// If s.Voice is nil, we must not have even requested to join
+	// a voice channel yet, so this shouldn't be processed.
+	if s.Voice == nil {
+		return
+	}
+
 	// Need to have this happen at login and store it in the Session
 	// TODO : This should be done upon connecting to Discord, or
 	// be moved to a small helper function
@@ -616,11 +621,6 @@ func (s *Session) onVoiceStateUpdate(st *VoiceState) {
 		return
 	}
 
-	// This shouldn't ever be the case, I don't think.
-	if s.Voice == nil {
-		s.Voice = &voice{}
-	}
-
 	// Store the SessionID for later use.
 	s.Voice.userID = self.ID // TODO: Review
 	s.Voice.sessionID = st.SessionID
@@ -633,7 +633,7 @@ func (s *Session) onVoiceServerUpdate(st *VoiceServerUpdate) {
 
 	// This shouldn't ever be the case, I don't think.
 	if s.Voice == nil {
-		s.Voice = &voice{}
+		return
 	}
 
 	// Store values for later use