2020-04-25 02:36:33 +00:00
|
|
|
package voice
|
|
|
|
|
|
|
|
import (
|
2020-07-11 19:50:32 +00:00
|
|
|
"context"
|
2020-04-25 02:36:33 +00:00
|
|
|
"sync"
|
2020-07-11 19:50:32 +00:00
|
|
|
"time"
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/handler"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/ws/ophandler"
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2020-07-29 20:10:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v3/internal/moreatomic"
|
|
|
|
"github.com/diamondburned/arikawa/v3/session"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/ws"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/voice/udp"
|
|
|
|
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
|
2020-04-25 02:36:33 +00:00
|
|
|
)
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
// Protocol is the encryption protocol that this library uses.
|
2020-04-25 02:36:33 +00:00
|
|
|
const Protocol = "xsalsa20_poly1305"
|
|
|
|
|
2020-11-17 20:09:15 +00:00
|
|
|
// ErrAlreadyConnecting is returned when the session is already connecting.
|
|
|
|
var ErrAlreadyConnecting = errors.New("already connecting")
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
// ErrCannotSend is an error when audio is sent to a closed channel.
|
|
|
|
var ErrCannotSend = errors.New("cannot send audio to closed channel")
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// WSTimeout is the duration to wait for a gateway operation including Session
|
|
|
|
// to complete before erroring out. This only applies to functions that don't
|
|
|
|
// take in a context already.
|
2021-09-28 20:19:04 +00:00
|
|
|
const WSTimeout = 25 * time.Second
|
|
|
|
|
|
|
|
// ReconnectError is emitted into Session.Handler everytime the voice gateway
|
|
|
|
// fails to be reconnected. It implements the error interface.
|
|
|
|
type ReconnectError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error.
|
|
|
|
func (e ReconnectError) Error() string {
|
|
|
|
return "voice reconnect error: " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap returns e.Err.
|
|
|
|
func (e ReconnectError) Unwrap() error { return e.Err }
|
|
|
|
|
|
|
|
// MainSession abstracts both session.Session and state.State.
|
|
|
|
type MainSession interface {
|
|
|
|
// AddHandler describes the method in handler.Handler.
|
|
|
|
AddHandler(handler interface{}) (rm func())
|
|
|
|
// Me returns the current user.
|
|
|
|
Me() (*discord.User, error)
|
|
|
|
// Channel queries for the channel with the given ID.
|
|
|
|
Channel(discord.ChannelID) (*discord.Channel, error)
|
2022-11-25 19:16:13 +00:00
|
|
|
// SendGateway is a helper to send messages over the gateway.
|
|
|
|
SendGateway(ctx context.Context, m ws.Event) error
|
2021-09-28 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ MainSession = (*session.Session)(nil)
|
|
|
|
_ MainSession = (*state.State)(nil)
|
|
|
|
)
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
// Session is a single voice session that wraps around the voice gateway and UDP
|
|
|
|
// connection.
|
2020-04-25 02:36:33 +00:00
|
|
|
type Session struct {
|
2020-11-15 19:45:47 +00:00
|
|
|
*handler.Handler
|
2021-09-28 20:19:04 +00:00
|
|
|
session MainSession
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
mut sync.RWMutex
|
|
|
|
// connected is a non-nil blocking channel after Join is called and is
|
|
|
|
// closed once Leave is called.
|
|
|
|
disconnected chan struct{}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2020-11-17 20:09:15 +00:00
|
|
|
state voicegateway.State // guarded except UserID
|
2021-09-28 20:19:04 +00:00
|
|
|
|
|
|
|
detachReconnect []func()
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// udpManager is the manager for a UDP connection. The user can use this to
|
|
|
|
// plug in a custom UDP dialer.
|
|
|
|
udpManager *udp.Manager
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
gateway *voicegateway.Gateway
|
|
|
|
gwCancel context.CancelFunc
|
|
|
|
gwDone <-chan struct{}
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
WSTimeout time.Duration // global WSTimeout
|
|
|
|
WSMaxRetry int // 2
|
|
|
|
WSRetryDelay time.Duration // 2s
|
|
|
|
WSWaitDuration time.Duration // 5s
|
|
|
|
|
|
|
|
// joining determines the behavior of incoming event callbacks (Update).
|
|
|
|
// If this is true, incoming events will just send into Updated channels. If
|
|
|
|
// false, events will trigger a reconnection.
|
2021-09-28 20:19:04 +00:00
|
|
|
joining moreatomic.Bool
|
|
|
|
// disconnectClosed is true if connected is already closed. It is only used
|
|
|
|
// to keep track of closing connected.
|
|
|
|
disconnectClosed bool
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
// NewSession creates a new voice session for the current user.
|
2021-09-28 20:19:04 +00:00
|
|
|
func NewSession(state MainSession) (*Session, error) {
|
2020-12-01 01:49:18 +00:00
|
|
|
u, err := state.Me()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get me")
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return NewSessionCustom(state, u.ID), nil
|
2020-12-01 01:49:18 +00:00
|
|
|
}
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
// NewSessionCustom creates a new voice session from the given session and user
|
|
|
|
// ID.
|
2021-09-28 20:19:04 +00:00
|
|
|
func NewSessionCustom(ses MainSession, userID discord.UserID) *Session {
|
|
|
|
closed := make(chan struct{})
|
|
|
|
close(closed)
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
session := &Session{
|
2021-09-28 20:19:04 +00:00
|
|
|
Handler: handler.New(),
|
2020-04-25 02:36:33 +00:00
|
|
|
session: ses,
|
|
|
|
state: voicegateway.State{
|
|
|
|
UserID: userID,
|
|
|
|
},
|
2021-07-02 09:42:00 +00:00
|
|
|
udpManager: udp.NewManager(),
|
2021-11-04 20:07:18 +00:00
|
|
|
WSTimeout: WSTimeout,
|
|
|
|
WSMaxRetry: 2,
|
|
|
|
WSRetryDelay: 2 * time.Second,
|
|
|
|
WSWaitDuration: 5 * time.Second,
|
2021-09-28 20:19:04 +00:00
|
|
|
|
|
|
|
// Set this pair of value in so we never have to nil-check the channel.
|
|
|
|
// We can just assume that it's either closed or connected.
|
|
|
|
disconnected: closed,
|
|
|
|
disconnectClosed: true,
|
2020-12-01 01:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return session
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// SetUDPDialer sets the given dialer to be used for dialing UDP voice
|
|
|
|
// connections.
|
2022-04-04 00:48:15 +00:00
|
|
|
func (s *Session) SetUDPDialer(d udp.DialFunc) {
|
2021-07-02 09:42:00 +00:00
|
|
|
s.udpManager.SetDialer(d)
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (s *Session) acquireUpdate(f func()) bool {
|
2020-04-25 02:36:33 +00:00
|
|
|
if s.joining.Get() {
|
2021-09-28 20:19:04 +00:00
|
|
|
return false
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
// Ignore if we haven't connected yet or we're still joining.
|
2021-07-02 09:42:00 +00:00
|
|
|
if s.udpManager.IsClosed() {
|
2021-09-28 20:19:04 +00:00
|
|
|
return false
|
2020-11-17 20:09:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
f()
|
|
|
|
return true
|
|
|
|
}
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// updateServer is specifically used to monitor for reconnects.
|
|
|
|
func (s *Session) updateServer(ev *gateway.VoiceServerUpdateEvent) {
|
|
|
|
s.acquireUpdate(func() {
|
|
|
|
if s.state.GuildID != ev.GuildID {
|
|
|
|
return
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
s.state.Endpoint = ev.Endpoint
|
|
|
|
s.state.Token = ev.Token
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.reconnectCtx(ctx)
|
|
|
|
})
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// updateState is specifically used after connecting to monitor when the bot is
|
|
|
|
// forced across channels.
|
|
|
|
func (s *Session) updateState(ev *gateway.VoiceStateUpdateEvent) {
|
|
|
|
s.acquireUpdate(func() {
|
|
|
|
if s.state.GuildID != ev.GuildID || s.state.UserID != ev.UserID {
|
|
|
|
return
|
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
s.state.ChannelID = ev.ChannelID
|
|
|
|
s.state.SessionID = ev.SessionID
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.reconnectCtx(ctx)
|
|
|
|
})
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// JoinChannelAndSpeak is a convenient function that calls JoinChannel then
|
|
|
|
// Speaking.
|
|
|
|
func (s *Session) JoinChannelAndSpeak(ctx context.Context, chID discord.ChannelID, mute, deaf bool) error {
|
|
|
|
if err := s.JoinChannel(ctx, chID, mute, deaf); err != nil {
|
|
|
|
return errors.Wrap(err, "cannot join channel")
|
|
|
|
}
|
|
|
|
return s.Speaking(ctx, voicegateway.Microphone)
|
|
|
|
}
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
type waitEventChs struct {
|
|
|
|
serverUpdate chan *gateway.VoiceServerUpdateEvent
|
|
|
|
stateUpdate chan *gateway.VoiceStateUpdateEvent
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// JoinChannel joins the given voice channel with the default timeout.
|
|
|
|
func (s *Session) JoinChannel(ctx context.Context, chID discord.ChannelID, mute, deaf bool) error {
|
|
|
|
var ch *discord.Channel
|
|
|
|
|
|
|
|
if chID.IsValid() {
|
|
|
|
var err error
|
|
|
|
ch, err = s.session.Channel(chID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "invalid channel ID")
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
// Error out if we're already joining. JoinChannel shouldn't be called
|
|
|
|
// concurrently.
|
2021-07-02 09:42:00 +00:00
|
|
|
if !s.joining.Acquire() {
|
2021-11-04 20:07:18 +00:00
|
|
|
return errors.New("JoinChannel working elsewhere")
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
defer s.joining.Set(false)
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
// Set the state.
|
2021-09-28 20:19:04 +00:00
|
|
|
if ch != nil {
|
|
|
|
s.state.ChannelID = ch.ID
|
|
|
|
s.state.GuildID = ch.GuildID
|
|
|
|
} else {
|
|
|
|
s.state.GuildID = 0
|
|
|
|
// Ensure that if `cID` is zero that it passes null to the update event.
|
|
|
|
s.state.ChannelID = discord.NullChannelID
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.detachReconnect == nil {
|
|
|
|
s.detachReconnect = []func(){
|
|
|
|
s.session.AddHandler(s.updateServer),
|
|
|
|
s.session.AddHandler(s.updateState),
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
chs := waitEventChs{
|
|
|
|
serverUpdate: make(chan *gateway.VoiceServerUpdateEvent),
|
|
|
|
stateUpdate: make(chan *gateway.VoiceStateUpdateEvent),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind the handlers.
|
|
|
|
cancels := []func(){
|
|
|
|
s.session.AddHandler(chs.serverUpdate),
|
|
|
|
s.session.AddHandler(chs.stateUpdate),
|
|
|
|
}
|
|
|
|
// Disconnects the handlers once the function exits.
|
|
|
|
defer func() {
|
|
|
|
for _, cancel := range cancels {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Ensure gateway and voiceUDP are already closed.
|
|
|
|
s.ensureClosed()
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// https://discord.com/developers/docs/topics/voice-connections#retrieving-voice-server-information
|
|
|
|
// Send a Voice State Update event to the gateway.
|
|
|
|
data := &gateway.UpdateVoiceStateCommand{
|
|
|
|
GuildID: s.state.GuildID,
|
|
|
|
ChannelID: s.state.ChannelID,
|
2020-12-01 01:49:18 +00:00
|
|
|
SelfMute: mute,
|
|
|
|
SelfDeaf: deaf,
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
var err error
|
|
|
|
var timer *time.Timer
|
|
|
|
|
|
|
|
// Retry 3 times maximum.
|
|
|
|
for i := 0; i < s.WSMaxRetry; i++ {
|
|
|
|
if err = s.askDiscord(ctx, data, chs); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the first attempt and the context timed out, it's
|
|
|
|
// probably the context that's waiting for gateway events. Retry the
|
|
|
|
// loop.
|
|
|
|
if i == 0 && errors.Is(err, ctx.Err()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if timer == nil {
|
|
|
|
// Set up a timer.
|
|
|
|
timer = time.NewTimer(s.WSRetryDelay)
|
|
|
|
defer timer.Stop()
|
|
|
|
} else {
|
|
|
|
timer.Reset(s.WSRetryDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
continue
|
|
|
|
case <-ctx.Done():
|
2021-07-02 09:42:00 +00:00
|
|
|
return errors.Wrap(err, "cannot ask Discord for events")
|
2021-11-04 20:07:18 +00:00
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
// These 2 methods should've updated s.state before sending into these
|
|
|
|
// channels. Since s.state is already filled, we can go ahead and connect.
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
// Mark the session as connected and move on. This allows one of the
|
|
|
|
// connected handlers to reconnect on its own.
|
2021-09-28 20:19:04 +00:00
|
|
|
s.disconnected = make(chan struct{})
|
2021-11-04 20:07:18 +00:00
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
return s.reconnectCtx(ctx)
|
|
|
|
}
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
func (s *Session) askDiscord(
|
2021-09-28 20:19:04 +00:00
|
|
|
ctx context.Context, data *gateway.UpdateVoiceStateCommand, chs waitEventChs) error {
|
2021-11-04 20:07:18 +00:00
|
|
|
|
|
|
|
// https://discord.com/developers/docs/topics/voice-connections#retrieving-voice-server-information
|
|
|
|
// Send a Voice State Update event to the gateway.
|
2022-11-25 19:16:13 +00:00
|
|
|
if err := s.session.SendGateway(ctx, data); err != nil {
|
2021-11-04 20:07:18 +00:00
|
|
|
return errors.Wrap(err, "failed to send Voice State Update event")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for 2 replies. The above command should reply with these 2 events.
|
|
|
|
if err := s.waitForIncoming(ctx, chs); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to wait for needed gateway events")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) waitForIncoming(ctx context.Context, chs waitEventChs) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, s.WSWaitDuration)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
state := false
|
|
|
|
// server is true when we already have the token and endpoint, meaning that
|
|
|
|
// we don't have to wait for another such event.
|
|
|
|
server := s.state.Token != "" && s.state.Endpoint != ""
|
|
|
|
|
|
|
|
// Loop until timeout or until we have all the information that we need.
|
|
|
|
for !(server && state) {
|
2020-07-11 19:50:32 +00:00
|
|
|
select {
|
2021-11-04 20:07:18 +00:00
|
|
|
case ev := <-chs.serverUpdate:
|
|
|
|
if s.state.GuildID != ev.GuildID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.state.Endpoint = ev.Endpoint
|
|
|
|
s.state.Token = ev.Token
|
|
|
|
server = true
|
|
|
|
|
|
|
|
case ev := <-chs.stateUpdate:
|
|
|
|
if s.state.GuildID != ev.GuildID || s.state.UserID != ev.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.state.SessionID = ev.SessionID
|
|
|
|
s.state.ChannelID = ev.ChannelID
|
|
|
|
state = true
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reconnect uses the current state to reconnect to a new gateway and UDP
|
|
|
|
// connection.
|
2021-09-28 20:19:04 +00:00
|
|
|
func (s *Session) reconnectCtx(ctx context.Context) error {
|
|
|
|
ws.WSDebug("Sending stop handle.")
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
if err := s.udpManager.Pause(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "cannot pause UDP manager")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if !s.udpManager.Continue() {
|
|
|
|
panic("UDP manager continued but invalid lock ownership")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
s.ensureClosed()
|
2020-11-15 19:45:47 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
ws.WSDebug("Start gateway.")
|
2020-04-25 02:36:33 +00:00
|
|
|
s.gateway = voicegateway.New(s.state)
|
|
|
|
|
|
|
|
// Open the voice gateway. The function will block until Ready is received.
|
2021-09-28 20:19:04 +00:00
|
|
|
gwctx, gwcancel := context.WithCancel(context.Background())
|
|
|
|
s.gwCancel = gwcancel
|
|
|
|
|
|
|
|
gwch := s.gateway.Connect(gwctx)
|
2021-07-02 09:42:00 +00:00
|
|
|
ws.WSDebug("Voice Gateway connected")
|
2021-09-28 20:19:04 +00:00
|
|
|
|
|
|
|
if err := s.spinGateway(ctx, gwch); err != nil {
|
2021-07-02 09:42:00 +00:00
|
|
|
ws.WSDebug("Voice spinGateway error:", err)
|
2021-09-28 20:19:04 +00:00
|
|
|
// Early cancel the gateway.
|
|
|
|
gwcancel()
|
|
|
|
// Nil this so future reconnects don't use the invalid gwDone.
|
|
|
|
s.gwCancel = nil
|
|
|
|
// Emit the error. It's fine to do this here since this is the only
|
|
|
|
// place that can error out.
|
|
|
|
s.Handler.Call(&ReconnectError{err})
|
2021-07-02 09:42:00 +00:00
|
|
|
return errors.Wrap(err, "cannot wait for event sequence from voice gateway")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Start dispatching.
|
|
|
|
s.gwDone = ophandler.Loop(gwch, s.Handler)
|
2020-11-15 19:45:47 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
ws.WSDebug("Voice reconnectCtx finished with no error")
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (s *Session) spinGateway(ctx context.Context, gwch <-chan ws.Op) error {
|
|
|
|
var err error
|
2021-07-02 09:42:00 +00:00
|
|
|
var conn *udp.Connection
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case ev, ok := <-gwch:
|
|
|
|
if !ok {
|
2021-07-02 09:42:00 +00:00
|
|
|
return errors.Wrap(s.gateway.LastError(), "voice gateway error")
|
2021-09-28 20:19:04 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
switch data := ev.Data.(type) {
|
|
|
|
case *ws.CloseEvent:
|
|
|
|
return errors.Wrap(err, "voice gateway error")
|
|
|
|
|
|
|
|
case *voicegateway.ReadyEvent:
|
2021-07-02 09:42:00 +00:00
|
|
|
ws.WSDebug("Got ready from voice gateway, SSRC:", data.SSRC)
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Prepare the UDP voice connection.
|
2021-07-02 09:42:00 +00:00
|
|
|
conn, err = s.udpManager.Dial(ctx, data.Addr(), data.SSRC)
|
2021-09-28 20:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to open voice UDP connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.gateway.Send(ctx, &voicegateway.SelectProtocolCommand{
|
|
|
|
Protocol: "udp",
|
|
|
|
Data: voicegateway.SelectProtocolData{
|
2021-07-02 09:42:00 +00:00
|
|
|
Address: conn.GatewayIP,
|
|
|
|
Port: conn.GatewayPort,
|
2021-09-28 20:19:04 +00:00
|
|
|
Mode: Protocol,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to send SelectProtocolCommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
case *voicegateway.SessionDescriptionEvent:
|
2021-07-02 09:42:00 +00:00
|
|
|
if conn == nil {
|
|
|
|
return errors.New("server bug: SessionDescription before Ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.WSDebug("Received secret key from voice gateway")
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// We're done.
|
2021-07-02 09:42:00 +00:00
|
|
|
conn.UseSecret(data.SecretKey)
|
2021-09-28 20:19:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Dispatch this event to the handler.
|
|
|
|
s.Handler.Call(ev.Data)
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 22:43:00 +00:00
|
|
|
// Speaking tells Discord we're speaking. This method should not be called
|
2020-11-17 20:09:15 +00:00
|
|
|
// concurrently.
|
2021-07-02 09:42:00 +00:00
|
|
|
//
|
|
|
|
// If only NotSpeaking (0) is given, then even if the gateway cannot be reached,
|
|
|
|
// a nil error will be returned. This is because sending Discord a not-speaking
|
|
|
|
// event is a destruction command that doesn't affect the outcome of anything
|
|
|
|
// done after whatsoever.
|
2021-09-28 20:19:04 +00:00
|
|
|
func (s *Session) Speaking(ctx context.Context, flag voicegateway.SpeakingFlag) error {
|
|
|
|
s.mut.Lock()
|
2020-11-17 20:09:15 +00:00
|
|
|
gateway := s.gateway
|
2021-09-28 20:19:04 +00:00
|
|
|
s.mut.Unlock()
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
if err := gateway.Speaking(ctx, flag); err != nil && flag != 0 {
|
2021-09-28 20:19:04 +00:00
|
|
|
return err
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
return nil
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Write writes into the UDP voice connection. This method is thread safe as far
|
|
|
|
// as calling other methods of Session goes; HOWEVER it is not thread safe to
|
|
|
|
// call Write itself concurrently.
|
|
|
|
func (s *Session) Write(b []byte) (int, error) {
|
2021-07-02 09:42:00 +00:00
|
|
|
return s.udpManager.Write(b)
|
2021-09-28 20:19:04 +00:00
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// ReadPacket reads a single packet from the UDP connection. This is NOT at all
|
|
|
|
// thread safe, and must be used very carefully. The backing buffer is always
|
|
|
|
// reused.
|
|
|
|
func (s *Session) ReadPacket() (*udp.Packet, error) {
|
2021-07-02 09:42:00 +00:00
|
|
|
return s.udpManager.ReadPacket()
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Leave disconnects the current voice session from the currently connected
|
|
|
|
// channel.
|
|
|
|
func (s *Session) Leave(ctx context.Context) error {
|
2020-04-25 02:36:33 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
s.ensureClosed()
|
2021-11-04 20:07:18 +00:00
|
|
|
|
|
|
|
// Unbind the handlers.
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.detachReconnect != nil {
|
|
|
|
for _, detach := range s.detachReconnect {
|
|
|
|
detach()
|
|
|
|
}
|
|
|
|
s.detachReconnect = nil
|
2021-11-04 20:07:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// If we're already closed.
|
2021-07-02 09:42:00 +00:00
|
|
|
if s.gateway == nil && s.udpManager.IsClosed() {
|
2020-04-25 02:36:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Notify Discord that we're leaving.
|
2022-11-25 19:16:13 +00:00
|
|
|
sendErr := s.session.SendGateway(ctx, &gateway.UpdateVoiceStateCommand{
|
2020-04-25 02:36:33 +00:00
|
|
|
GuildID: s.state.GuildID,
|
2020-07-21 20:27:59 +00:00
|
|
|
ChannelID: discord.ChannelID(discord.NullSnowflake),
|
2020-04-25 02:36:33 +00:00
|
|
|
SelfMute: true,
|
|
|
|
SelfDeaf: true,
|
|
|
|
})
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Wait for the gateway to exit first before we tell the user of the gateway
|
|
|
|
// send error.
|
|
|
|
if err := s.cancelGateway(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
if sendErr != nil {
|
|
|
|
return errors.Wrap(sendErr, "failed to update voice state")
|
2021-09-28 20:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) cancelGateway(ctx context.Context) error {
|
|
|
|
if s.gwCancel != nil {
|
|
|
|
s.gwCancel()
|
|
|
|
s.gwCancel = nil
|
|
|
|
|
|
|
|
// Wait for the previous gateway to finish closing up, but make sure to
|
|
|
|
// bail if the context expires.
|
|
|
|
if err := ophandler.WaitForDone(ctx, s.gwDone); err != nil {
|
|
|
|
return errors.Wrap(err, "cannot wait for gateway to close")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
const (
|
|
|
|
permanentClose = true
|
|
|
|
temporaryClose = false
|
|
|
|
)
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// close ensures everything is closed. It does not acquire the mutex.
|
|
|
|
func (s *Session) ensureClosed() {
|
2021-07-02 09:42:00 +00:00
|
|
|
// Disconnect the UDP connection. If not permanent, then pause.
|
|
|
|
s.udpManager.Close()
|
2020-11-30 01:32:18 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if s.gwCancel != nil {
|
|
|
|
s.gwCancel()
|
|
|
|
// Don't actually clear this field, because we still want the caller to
|
|
|
|
// be able to wait for the gateway to completely exit using
|
|
|
|
// cancelGateway.
|
|
|
|
}
|
2020-11-30 01:32:18 +00:00
|
|
|
}
|