github.com/diamondburned/arikawa@v1.3.14/voice/voicegateway/op.go (about) 1 package voicegateway 2 3 import ( 4 "fmt" 5 "sync" 6 7 "github.com/diamondburned/arikawa/utils/json" 8 "github.com/diamondburned/arikawa/utils/wsutil" 9 "github.com/pkg/errors" 10 ) 11 12 // OPCode represents a Discord Gateway Gateway operation code. 13 type OPCode = wsutil.OPCode 14 15 const ( 16 IdentifyOP OPCode = 0 // send 17 SelectProtocolOP OPCode = 1 // send 18 ReadyOP OPCode = 2 // receive 19 HeartbeatOP OPCode = 3 // send 20 SessionDescriptionOP OPCode = 4 // receive 21 SpeakingOP OPCode = 5 // send/receive 22 HeartbeatAckOP OPCode = 6 // receive 23 ResumeOP OPCode = 7 // send 24 HelloOP OPCode = 8 // receive 25 ResumedOP OPCode = 9 // receive 26 // ClientDisconnectOP OPCode = 13 // receive 27 ) 28 29 func (c *Gateway) HandleOP(op *wsutil.OP) error { 30 switch op.Code { 31 // Gives information required to make a UDP connection 32 case ReadyOP: 33 if err := unmarshalMutex(op.Data, &c.ready, &c.mutex); err != nil { 34 return errors.Wrap(err, "failed to parse READY event") 35 } 36 37 // Gives information about the encryption mode and secret key for sending voice packets 38 case SessionDescriptionOP: 39 // ? 40 // Already handled by Session. 41 42 // Someone started or stopped speaking. 43 case SpeakingOP: 44 // ? 45 // TODO: handler in Session 46 47 // Heartbeat response from the server 48 case HeartbeatAckOP: 49 c.EventLoop.Echo() 50 51 // Hello server, we hear you! :) 52 case HelloOP: 53 // ? 54 // Already handled on initial connection. 55 56 // Server is saying the connection was resumed, no data here. 57 case ResumedOP: 58 wsutil.WSDebug("Gateway connection has been resumed.") 59 60 default: 61 return fmt.Errorf("unknown OP code %d", op.Code) 62 } 63 64 return nil 65 } 66 67 func unmarshalMutex(d []byte, v interface{}, m *sync.RWMutex) error { 68 m.Lock() 69 err := json.Unmarshal(d, v) 70 m.Unlock() 71 return err 72 }