2020-04-25 02:36:33 +00:00
|
|
|
package voicegateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/wsutil"
|
2020-04-25 02:36:33 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OPCode represents a Discord Gateway Gateway operation code.
|
|
|
|
type OPCode = wsutil.OPCode
|
|
|
|
|
|
|
|
const (
|
2020-11-15 19:45:47 +00:00
|
|
|
IdentifyOP OPCode = 0 // send
|
|
|
|
SelectProtocolOP OPCode = 1 // send
|
|
|
|
ReadyOP OPCode = 2 // receive
|
|
|
|
HeartbeatOP OPCode = 3 // send
|
|
|
|
SessionDescriptionOP OPCode = 4 // receive
|
|
|
|
SpeakingOP OPCode = 5 // send/receive
|
|
|
|
HeartbeatAckOP OPCode = 6 // receive
|
|
|
|
ResumeOP OPCode = 7 // send
|
|
|
|
HelloOP OPCode = 8 // receive
|
|
|
|
ResumedOP OPCode = 9 // receive
|
|
|
|
ClientConnectOP OPCode = 12 // receive
|
|
|
|
ClientDisconnectOP OPCode = 13 // receive
|
2020-04-25 02:36:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Gateway) HandleOP(op *wsutil.OP) error {
|
2020-11-15 19:45:47 +00:00
|
|
|
wsutil.WSDebug("Handle OP", op.Code)
|
2020-04-25 02:36:33 +00:00
|
|
|
switch op.Code {
|
|
|
|
// Gives information required to make a UDP connection
|
|
|
|
case ReadyOP:
|
|
|
|
if err := unmarshalMutex(op.Data, &c.ready, &c.mutex); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to parse READY event")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 19:45:47 +00:00
|
|
|
c.Events <- &c.ready
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Gives information about the encryption mode and secret key for sending voice packets
|
|
|
|
case SessionDescriptionOP:
|
|
|
|
// ?
|
|
|
|
// Already handled by Session.
|
|
|
|
|
|
|
|
// Someone started or stopped speaking.
|
|
|
|
case SpeakingOP:
|
2020-11-15 19:45:47 +00:00
|
|
|
ev := new(SpeakingEvent)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(op.Data, ev); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to parse Speaking event")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Events <- ev
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
// Heartbeat response from the server
|
|
|
|
case HeartbeatAckOP:
|
|
|
|
c.EventLoop.Echo()
|
|
|
|
|
|
|
|
// Hello server, we hear you! :)
|
|
|
|
case HelloOP:
|
|
|
|
// ?
|
|
|
|
// Already handled on initial connection.
|
|
|
|
|
|
|
|
// Server is saying the connection was resumed, no data here.
|
|
|
|
case ResumedOP:
|
|
|
|
wsutil.WSDebug("Gateway connection has been resumed.")
|
|
|
|
|
2020-11-15 19:45:47 +00:00
|
|
|
case ClientConnectOP:
|
|
|
|
ev := new(ClientConnectEvent)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(op.Data, ev); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to parse Speaking event")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Events <- ev
|
|
|
|
|
|
|
|
case ClientDisconnectOP:
|
|
|
|
ev := new(ClientDisconnectEvent)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(op.Data, ev); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to parse Speaking event")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Events <- ev
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown OP code %d", op.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalMutex(d []byte, v interface{}, m *sync.RWMutex) error {
|
|
|
|
m.Lock()
|
|
|
|
err := json.Unmarshal(d, v)
|
|
|
|
m.Unlock()
|
|
|
|
return err
|
|
|
|
}
|