2020-01-15 04:43:34 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-15 07:34:18 +00:00
|
|
|
"log"
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/json"
|
|
|
|
"github.com/diamondburned/arikawa/wsutil"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OPCode uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
DispatchOP OPCode = iota // recv
|
|
|
|
HeartbeatOP // send/recv
|
|
|
|
IdentifyOP // send...
|
|
|
|
StatusUpdateOP //
|
|
|
|
VoiceStateUpdateOP //
|
|
|
|
VoiceServerPingOP //
|
|
|
|
ResumeOP //
|
|
|
|
ReconnectOP // recv
|
|
|
|
RequestGuildMembersOP // send
|
|
|
|
InvalidSessionOP // recv...
|
|
|
|
HelloOP
|
|
|
|
HeartbeatAckOP
|
|
|
|
_
|
|
|
|
CallConnectOP
|
|
|
|
GuildSubscriptionsOP
|
|
|
|
)
|
|
|
|
|
|
|
|
type OP struct {
|
|
|
|
Code OPCode `json:"op"`
|
|
|
|
Data json.Raw `json:"d,omitempty"`
|
|
|
|
|
|
|
|
// Only for Dispatch (op 0)
|
2020-01-15 07:34:18 +00:00
|
|
|
Sequence int64 `json:"s,omitempty"`
|
2020-01-15 04:43:34 +00:00
|
|
|
EventName string `json:"t,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrInvalidSession = errors.New("Invalid session")
|
|
|
|
|
|
|
|
func DecodeOP(driver json.Driver, ev wsutil.Event) (*OP, error) {
|
|
|
|
if ev.Error != nil {
|
|
|
|
return nil, ev.Error
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:34:18 +00:00
|
|
|
log.Println("<-", string(ev.Data))
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
var op *OP
|
|
|
|
if err := driver.Unmarshal(ev.Data, &op); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to decode payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
if op.Code == InvalidSessionOP {
|
|
|
|
return op, ErrInvalidSession
|
|
|
|
}
|
|
|
|
|
|
|
|
return op, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeEvent(driver json.Driver,
|
|
|
|
ev wsutil.Event, v interface{}) (OPCode, error) {
|
|
|
|
|
|
|
|
op, err := DecodeOP(driver, ev)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Unmarshal(op.Data, v); err != nil {
|
|
|
|
return 0, errors.Wrap(err, "Failed to decode data")
|
|
|
|
}
|
|
|
|
|
|
|
|
return op.Code, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertEvent(driver json.Driver,
|
2020-01-15 07:34:18 +00:00
|
|
|
ev wsutil.Event, code OPCode, v interface{}) (*OP, error) {
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
op, err := DecodeOP(driver, ev)
|
|
|
|
if err != nil {
|
2020-01-15 07:34:18 +00:00
|
|
|
return nil, err
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if op.Code != code {
|
2020-01-15 07:34:18 +00:00
|
|
|
return op, fmt.Errorf(
|
2020-01-15 04:43:34 +00:00
|
|
|
"Unexpected OP Code: %d, expected %d (%s)",
|
|
|
|
op.Code, code, op.Data,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.Unmarshal(op.Data, v); err != nil {
|
2020-01-15 07:34:18 +00:00
|
|
|
return op, errors.Wrap(err, "Failed to decode data")
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 07:34:18 +00:00
|
|
|
return op, nil
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleEvent(g *Gateway, data []byte) error {
|
|
|
|
// Parse the raw data into an OP struct
|
|
|
|
var op *OP
|
|
|
|
if err := g.Driver.Unmarshal(data, &op); err != nil {
|
|
|
|
return errors.Wrap(err, "OP error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.OP != nil {
|
|
|
|
g.OP <- op
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op.Code {
|
|
|
|
case HeartbeatAckOP:
|
|
|
|
// Heartbeat from the server?
|
|
|
|
g.Pacemaker.Echo()
|
|
|
|
|
|
|
|
case HeartbeatOP:
|
|
|
|
// Server requesting a heartbeat.
|
|
|
|
return g.Pacemaker.Pace()
|
|
|
|
|
|
|
|
case ReconnectOP:
|
|
|
|
// Server requests to reconnect, die and retry.
|
|
|
|
return g.Reconnect()
|
|
|
|
|
|
|
|
case InvalidSessionOP:
|
|
|
|
// Invalid session, respond with Identify.
|
|
|
|
return g.Identify()
|
|
|
|
|
|
|
|
case HelloOP:
|
|
|
|
// What is this OP doing here???
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case DispatchOP:
|
2020-01-15 07:34:18 +00:00
|
|
|
// Set the sequence
|
|
|
|
g.Sequence.Set(op.Sequence)
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
// Check if we know the event
|
|
|
|
fn, ok := EventCreator[op.EventName]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Unknown event: " + op.EventName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new pointer to the event
|
|
|
|
var ev = fn()
|
|
|
|
|
|
|
|
// Try and parse the event
|
|
|
|
if err := g.Driver.Unmarshal(op.Data, ev); err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to parse event "+op.EventName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw the event into a channel, it's valid now.
|
|
|
|
g.Events <- ev
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Unknown OP code %d (event %s)", op.Code, op.EventName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|