From 093d3be77091b92e455713c01633f2828b20d2ed Mon Sep 17 00:00:00 2001 From: Bruce Marriner Date: Sun, 10 Jan 2016 18:01:05 -0600 Subject: [PATCH] More improvements to voice support --- structs.go | 2 +- voice.go | 16 ++++++++-------- wsapi.go | 26 +++++++++++++------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/structs.go b/structs.go index a1217d0..3710037 100644 --- a/structs.go +++ b/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 diff --git a/voice.go b/voice.go index b834b59..060bc04 100644 --- a/voice.go +++ b/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) diff --git a/wsapi.go b/wsapi.go index 7c690ce..33d37d8 100644 --- a/wsapi.go +++ b/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