2020-04-25 02:36:33 +00:00
|
|
|
package voice
|
|
|
|
|
|
|
|
import (
|
2020-07-11 19:50:32 +00:00
|
|
|
"context"
|
2021-09-28 20:19:04 +00:00
|
|
|
"net"
|
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"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/internal/lazytime"
|
2021-06-02 02:53:19 +00:00
|
|
|
"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())
|
|
|
|
// Gateway returns the session's main Discord gateway.
|
|
|
|
Gateway() *gateway.Gateway
|
|
|
|
// Me returns the current user.
|
|
|
|
Me() (*discord.User, error)
|
|
|
|
// Channel queries for the channel with the given ID.
|
|
|
|
Channel(discord.ChannelID) (*discord.Channel, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ MainSession = (*session.Session)(nil)
|
|
|
|
_ MainSession = (*state.State)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// UDPDialer is the UDP dialer function type. It's the function signature for
|
|
|
|
// udp.DialConnection.
|
|
|
|
type UDPDialer = func(ctx context.Context, addr string, ssrc uint32) (*udp.Connection, error)
|
2020-07-11 19:50:32 +00:00
|
|
|
|
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()
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
voiceUDP *udp.Connection
|
2021-09-28 20:19:04 +00:00
|
|
|
gateway *voicegateway.Gateway
|
|
|
|
gwCancel context.CancelFunc
|
|
|
|
gwDone <-chan struct{}
|
|
|
|
|
|
|
|
// DialUDP is the custom function for dialing up a UDP connection.
|
|
|
|
DialUDP UDPDialer
|
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-09-28 20:19:04 +00:00
|
|
|
DialUDP: udp.DialConnection,
|
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-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-09-28 20:19:04 +00:00
|
|
|
select {
|
|
|
|
case <-s.disconnected:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
// ok
|
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-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.
|
|
|
|
if s.joining.Get() {
|
|
|
|
return errors.New("JoinChannel working elsewhere")
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
s.joining.Set(true)
|
|
|
|
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():
|
|
|
|
return err
|
|
|
|
}
|
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.
|
2021-09-28 20:19:04 +00:00
|
|
|
if err := s.session.Gateway().Send(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.")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if err := s.spinGateway(ctx, gwch); err != nil {
|
|
|
|
// 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})
|
|
|
|
return err
|
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-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
|
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 {
|
|
|
|
return s.gateway.LastError()
|
|
|
|
}
|
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:
|
|
|
|
// Prepare the UDP voice connection.
|
|
|
|
s.voiceUDP, err = s.DialUDP(ctx, data.Addr(), data.SSRC)
|
|
|
|
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{
|
|
|
|
Address: s.voiceUDP.GatewayIP,
|
|
|
|
Port: s.voiceUDP.GatewayPort,
|
|
|
|
Mode: Protocol,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to send SelectProtocolCommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
case *voicegateway.SessionDescriptionEvent:
|
|
|
|
// We're done.
|
|
|
|
s.voiceUDP.UseSecret(data.SecretKey)
|
|
|
|
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-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-09-28 20:19:04 +00:00
|
|
|
return gateway.Speaking(ctx, flag)
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
func (s *Session) useUDP(f func(c *udp.Connection) error) (err error) {
|
|
|
|
const maxAttempts = 5
|
|
|
|
const retryDelay = 250 * time.Millisecond // adds up to about 1.25s
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
var lazyWait lazytime.Timer
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Hack: loop until we no longer get an error closed or until the connection
|
|
|
|
// is dead. This is a workaround for when the session is trying to reconnect
|
|
|
|
// itself in the background, which would drop the UDP connection.
|
|
|
|
for i := 0; i < maxAttempts; i++ {
|
|
|
|
s.mut.RLock()
|
|
|
|
voiceUDP := s.voiceUDP
|
|
|
|
disconnected := s.disconnected
|
|
|
|
s.mut.RUnlock()
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
select {
|
|
|
|
case <-disconnected:
|
|
|
|
return net.ErrClosed
|
|
|
|
default:
|
|
|
|
if voiceUDP == nil {
|
|
|
|
// Session is still connected, but our voice UDP connection is
|
|
|
|
// nil, so we're probably in the process of reconnecting
|
|
|
|
// already.
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if err = f(voiceUDP); err != nil && errors.Is(err, net.ErrClosed) {
|
|
|
|
// Session is still connected, but our UDP connection is somehow
|
|
|
|
// closed, so we're probably waiting for the server to ask us to
|
|
|
|
// reconnect with a new session.
|
|
|
|
goto retry
|
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Unknown error or none at all; exit.
|
|
|
|
return err
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
retry:
|
|
|
|
// Wait a slight bit. We can probably make the caller wait a couple
|
|
|
|
// milliseconds without a wait.
|
|
|
|
lazyWait.Reset(retryDelay)
|
|
|
|
select {
|
|
|
|
case <-lazyWait.C:
|
|
|
|
continue
|
|
|
|
case <-disconnected:
|
|
|
|
return net.ErrClosed
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
return
|
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) {
|
|
|
|
var n int
|
|
|
|
err := s.useUDP(func(c *udp.Connection) (err error) {
|
|
|
|
n, err = c.Write(b)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
return n, err
|
|
|
|
}
|
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) {
|
|
|
|
var p *udp.Packet
|
|
|
|
err := s.useUDP(func(c *udp.Connection) (err error) {
|
|
|
|
p, err = c.ReadPacket()
|
|
|
|
return
|
|
|
|
})
|
|
|
|
return p, err
|
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.
|
|
|
|
if s.gateway == nil && s.voiceUDP == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// Notify Discord that we're leaving.
|
|
|
|
err := s.session.Gateway().Send(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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to update voice state")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// close ensures everything is closed. It does not acquire the mutex.
|
|
|
|
func (s *Session) ensureClosed() {
|
|
|
|
// Disconnect the UDP connection.
|
|
|
|
if s.voiceUDP != nil {
|
|
|
|
s.voiceUDP.Close()
|
|
|
|
s.voiceUDP = nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
if !s.disconnectClosed {
|
|
|
|
close(s.disconnected)
|
|
|
|
s.disconnectClosed = true
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
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
|
|
|
}
|