2020-01-15 04:43:34 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
2020-07-11 19:50:32 +00:00
|
|
|
"context"
|
2020-01-15 04:43:34 +00:00
|
|
|
"fmt"
|
2020-01-18 07:40:44 +00:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
2020-01-15 04:43:34 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/wsutil"
|
2020-01-15 04:43:34 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-04-24 22:09:05 +00:00
|
|
|
type OPCode = wsutil.OPCode
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
const (
|
2020-03-16 05:55:45 +00:00
|
|
|
DispatchOP OPCode = 0 // recv
|
|
|
|
HeartbeatOP OPCode = 1 // send/recv
|
|
|
|
IdentifyOP OPCode = 2 // send...
|
|
|
|
StatusUpdateOP OPCode = 3 //
|
|
|
|
VoiceStateUpdateOP OPCode = 4 //
|
|
|
|
VoiceServerPingOP OPCode = 5 //
|
|
|
|
ResumeOP OPCode = 6 //
|
|
|
|
ReconnectOP OPCode = 7 // recv
|
|
|
|
RequestGuildMembersOP OPCode = 8 // send
|
|
|
|
InvalidSessionOP OPCode = 9 // recv...
|
|
|
|
HelloOP OPCode = 10
|
|
|
|
HeartbeatAckOP OPCode = 11
|
|
|
|
CallConnectOP OPCode = 13
|
|
|
|
GuildSubscriptionsOP OPCode = 14
|
2020-01-15 04:43:34 +00:00
|
|
|
)
|
|
|
|
|
2020-10-17 10:21:23 +00:00
|
|
|
// ErrReconnectRequest is returned by HandleOP if a ReconnectOP is given. This
|
|
|
|
// is used mostly internally to signal the heartbeat loop to reconnect, if
|
|
|
|
// needed. It is not a fatal error.
|
|
|
|
var ErrReconnectRequest = errors.New("ReconnectOP received")
|
|
|
|
|
2020-04-24 22:09:05 +00:00
|
|
|
func (g *Gateway) HandleOP(op *wsutil.OP) error {
|
2020-01-15 04:43:34 +00:00
|
|
|
switch op.Code {
|
|
|
|
case HeartbeatAckOP:
|
|
|
|
// Heartbeat from the server?
|
2020-04-24 22:09:05 +00:00
|
|
|
g.PacerLoop.Echo()
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
case HeartbeatOP:
|
2020-07-11 19:50:32 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
// Server requesting a heartbeat.
|
2020-10-22 17:47:27 +00:00
|
|
|
if err := g.PacerLoop.Pace(ctx); err != nil {
|
|
|
|
return wsutil.ErrBrokenConnection(errors.Wrap(err, "failed to pace"))
|
|
|
|
}
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
case ReconnectOP:
|
2021-04-07 18:38:26 +00:00
|
|
|
// Server requests to Reconnect, die and retry.
|
2020-04-24 06:34:08 +00:00
|
|
|
wsutil.WSDebug("ReconnectOP received.")
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2020-10-17 10:21:23 +00:00
|
|
|
// Exit with the ReconnectOP error to force the heartbeat event loop to
|
2021-04-07 18:38:26 +00:00
|
|
|
// Reconnect synchronously. Not really a fatal error.
|
2020-10-22 17:47:27 +00:00
|
|
|
return wsutil.ErrBrokenConnection(ErrReconnectRequest)
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
case InvalidSessionOP:
|
2020-01-18 07:40:44 +00:00
|
|
|
// Discord expects us to sleep for no reason
|
|
|
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-22 21:45:19 +00:00
|
|
|
// Invalid session, try and Identify.
|
2020-07-11 19:50:32 +00:00
|
|
|
if err := g.IdentifyCtx(ctx); err != nil {
|
2021-04-07 18:38:26 +00:00
|
|
|
// Can't identify, Reconnect.
|
2020-10-22 17:47:27 +00:00
|
|
|
return wsutil.ErrBrokenConnection(ErrReconnectRequest)
|
2020-04-22 21:45:19 +00:00
|
|
|
}
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2020-04-22 21:45:19 +00:00
|
|
|
return nil
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
case HelloOP:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case DispatchOP:
|
2020-01-15 07:34:18 +00:00
|
|
|
// Set the sequence
|
2020-01-16 03:28:21 +00:00
|
|
|
if op.Sequence > 0 {
|
|
|
|
g.Sequence.Set(op.Sequence)
|
|
|
|
}
|
2020-01-15 07:34:18 +00:00
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
// Check if we know the event
|
|
|
|
fn, ok := EventCreator[op.EventName]
|
|
|
|
if !ok {
|
2020-02-16 05:29:25 +00:00
|
|
|
return fmt.Errorf(
|
2020-07-29 23:29:01 +00:00
|
|
|
"unknown event %s: %s",
|
2020-02-16 05:29:25 +00:00
|
|
|
op.EventName, string(op.Data),
|
|
|
|
)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new pointer to the event
|
|
|
|
var ev = fn()
|
|
|
|
|
|
|
|
// Try and parse the event
|
2020-04-24 06:34:08 +00:00
|
|
|
if err := json.Unmarshal(op.Data, ev); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to parse event "+op.EventName)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 03:28:21 +00:00
|
|
|
// If the event is a ready, we'll want its sessionID
|
|
|
|
if ev, ok := ev.(*ReadyEvent); ok {
|
2020-12-31 08:35:37 +00:00
|
|
|
g.sessionMu.Lock()
|
|
|
|
g.sessionID = ev.SessionID
|
|
|
|
g.sessionMu.Unlock()
|
2020-01-16 03:28:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 10:21:23 +00:00
|
|
|
// Throw the event into a channel; it's valid now.
|
2020-01-15 04:43:34 +00:00
|
|
|
g.Events <- ev
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
2020-05-16 21:14:49 +00:00
|
|
|
return fmt.Errorf("unknown OP code %d (event %s)", op.Code, op.EventName)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|