-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgateway.jl
487 lines (431 loc) · 13.7 KB
/
gateway.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
export request_guild_members,
update_voice_status,
update_status,
update_presence
const conn_properties = Dict(
"\$os" => string(Sys.KERNEL),
"\$browser" => "Discord.jl",
"\$device" => "Discord.jl",
)
const OPCODES = Dict(
0 => :DISPATCH,
1 => :HEARTBEAT,
2 => :IDENTIFY,
3 => :STATUS_UPDATE,
4 => :VOICE_STATUS_UPDATE,
6 => :RESUME,
7 => :RECONNECT,
8 => :REQUEST_GUILD_MEMBERS,
9 => :INVALID_SESSION,
10 => :HELLO,
11 => :HEARTBEAT_ACK,
)
struct Empty <: Exception end
# Connection.
"""
open(c::Client; delay::Period=Second(7))
Connect a [`Client`](@ref) to the Discord gateway.
The `delay` keyword is the time between shards connecting. It can be increased from its
default if you are using multiple shards and frequently experiencing invalid sessions upon
connection.
"""
function Base.open(c::Client; resume::Bool=false, delay::Period=Second(7))
isopen(c) && throw(ArgumentError("Client is already connected"))
c.ready = false
# Clients can only identify once per 5 seconds.
resume || sleep(c.shard * delay)
@debug "Requesting gateway URL" logkws(c; conn=undef)...
resp = try
HTTP.get("$DISCORD_API/v$(c.version)/gateway")
catch e
kws = logkws(c; conn=undef, exception=(e, catch_backtrace()))
@error "Getting gateway URL failed" kws...
rethrow(e)
end
data = JSON.parse(String(resp.body))
url = "$(data["url"])?v=$(c.version)&encoding=json"
c.conn.v += 1
@debug "Connecting to gateway" logkws(c; conn=c.conn.v, url=url)...
c.conn.io = opentrick(HTTP.WebSockets.open, url)
@debug "Receiving HELLO" logkws(c)...
data, e = readjson(c.conn.io)
e === nothing || throw(e)
op = get(OPCODES, data[:op], data[:op])
if op !== :HELLO
msg = "Expected opcode HELLO, received $op"
@error msg logkws(c)...
error(msg)
end
hello(c, data)
data = if resume
Dict("op" => 6, "d" => Dict(
"token" => c.token,
"session_id" => c.state.session_id,
"seq" => c.hb_seq,
))
else
d = Dict("op" => 2, "s" => c.hb_seq, "d" => Dict(
"token" => c.token,
"properties" => conn_properties,
))
isempty(c.presence) || (d["d"]["presence"] = c.presence)
c.shards > 1 && (d["d"]["shard"] = [c.shard, c.shards])
d
end
op = resume ? :RESUME : :IDENTIFY
@debug "Writing $op" logkws(c)...
try
writejson(c.conn.io, data)
catch e
kws = logkws(c; exception=(e, catch_backtrace()))
@error "Writing $op failed" kws...
rethrow(e)
end
@debug "Starting background maintenance tasks" logkws(c)...
@async heartbeat_loop(c)
@async read_loop(c)
c.ready = true
end
"""
isopen(c::Client) -> Bool
Determine whether the [`Client`](@ref) is connected to the gateway.
"""
Base.isopen(c::Client) = c.ready && c.conn.io !== nothing && isopen(c.conn.io)
"""
close(c::Client)
Disconnect the [`Client`](@ref) from the gateway.
"""
function Base.close(c::Client; statuscode::Int=1000, zombie::Bool=false)
isopen(c) || return
c.ready = false
if zombie
# It seems that Discord doesn't send a closing frame for zombie connections
# (which makes sense). However, close waits for one forever (see HTTP.jl#350).
@async close(c.conn.io; statuscode=statuscode)
sleep(1)
else
close(c.conn.io; statuscode=statuscode)
end
end
"""
wait(c::Client)
Wait for an open [`Client`](@ref) to close.
"""
function Base.wait(c::Client)
while isopen(c)
wait(c.conn.io.cond)
# This is an arbitrary amount of time to wait,
# but we want to wait long enough to potentially reconnect.
sleep(10)
end
end
# Gateway commands.
"""
request_guild_members(
c::Client,
guilds::Union{Integer, Vector{<:Integer};
query::AbstractString="",
limit::Int=0,
) -> Bool
Request offline guild members of one or more [`Guild`](@ref)s. [`GuildMembersChunk`](@ref)
events are sent by the gateway in response.
More details [here](https://discordapp.com/developers/docs/topics/gateway#request-guild-members).
"""
function request_guild_members(
c::Client,
guild::Integer;
query::AbstractString="",
limit::Int=0,
)
return request_guild_members(c, [guild]; query=query, limit=limit)
end
function request_guild_members(
c::Client,
guilds::Vector{<:Integer};
query::AbstractString="",
limit::Int=0,
)
e, bt = trywritejson(c.conn.io, Dict("op" => 8, "d" => Dict(
"guild_id" => guilds,
"query" => query,
"limit" => limit,
)))
show_gateway_error(c, e, bt)
return e === nothing
end
"""
update_voice_state(
c::Client,
guild::Integer,
channel::Nullable{Integer},
mute::Bool,
deaf::Bool,
) -> Bool
Join, move, or disconnect from a voice channel. A [`VoiceStateUpdate`](@ref) event is sent
by the gateway in response.
More details [here](https://discordapp.com/developers/docs/topics/gateway#update-voice-state).
"""
function update_voice_state(
c::Client,
guild::Integer,
channel::Nullable{Integer},
mute::Bool,
deaf::Bool,
)
e, bt = trywritejson(c.conn.io, Dict("op" => 4, "d" => Dict(
"guild_id" => guild,
"channel_id" => channel,
"self_mute" => mute,
"self_deaf" => deaf,
)))
show_gateway_error(c, e, bt)
return e === nothing
end
"""
update_status(
c::Client,
since::Nullable{Int},
activity::Nullable{Activity},
status::Union{PresenceStatus, AbstractString},
afk::Bool,
) -> Bool
Indicate a presence or status update. A [`PresenceUpdate`](@ref) event is sent by the
gateway in response.
More details [here](https://discordapp.com/developers/docs/topics/gateway#update-status).
"""
function update_status(
c::Client,
since::Nullable{Int},
game::Union{Dict, NamedTuple, Activity, Nothing},
status::Union{PresenceStatus, AbstractString},
afk::Bool,
)
# Update defaults for future calls to set_game.
c.presence["since"] = since
c.presence["game"] = game
c.presence["status"] = status
c.presence["afk"] = afk
e, bt = trywritejson(c.conn.io, Dict("op" => 3, "d" => Dict(
"since" => since,
"game" => game,
"status" => status,
"afk" => afk,
)))
show_gateway_error(c, e, bt)
return e === nothing
end
# Client maintenance.
# Continuously send the heartbeat.
function heartbeat_loop(c::Client)
v = c.conn.v
try
sleep(rand(1:round(Int, c.hb_interval / 1000)))
heartbeat(c) || (isopen(c) && @error "Writing HEARTBEAT failed" logkws(c)...)
while c.conn.v == v && isopen(c)
sleep(c.hb_interval / 1000)
if c.last_hb > c.last_ack && isopen(c) && c.conn.v == v
@debug "Encountered zombie connection" logkws(c)...
reconnect(c; zombie=true)
elseif !heartbeat(c) && c.conn.v == v && isopen(c)
@error "Writing HEARTBEAT failed" logkws(c)...
end
end
@debug "Heartbeat loop exited" logkws(c; conn=v)...
catch e
kws = logkws(c; conn=v, exception=(e, catch_backtrace()))
@warn "Heartbeat loop exited unexpectedly" kws...
end
end
# Continuously read and respond to messages.
function read_loop(c::Client)
v = c.conn.v
try
while c.conn.v == v && isopen(c)
data, e = readjson(c.conn.io)
if e !== nothing
if c.conn.v == v
handle_read_exception(c, e)
else
@debug "Read failed, but the connection is outdated" logkws(c; error=e)...
end
elseif haskey(HANDLERS, data[:op])
@async HANDLERS[data[:op]](c, data)
else
@warn "Unkown opcode" logkws(c; op=data[:op])...
end
end
@debug "Read loop exited" logkws(c; conn=v)...
catch e
kws = logkws(c; conn=v, exception=(e, catch_backtrace()))
@warn "Read loop exited unexpectedly" kws...
c.ready && reconnect(c; zombie=true)
end
end
# Event handlers.
# Dispatch an event to its handlers.
function dispatch(c::Client, data::Dict)
c.hb_seq = data[:s]
T = get(EVENT_TYPES, data[:t], UnknownEvent)
handlers = allhandlers(c, T)
# If there are no handlers to call, don't bother parsing the event.
isempty(handlers) && return
evt = if T === UnknownEvent
UnknownEvent(data)
else
val, e = tryparse(c, T, data[:d])
if e === nothing
val
else
T = UnknownEvent
handlers = allhandlers(c, T)
UnknownEvent(data)
end
end
for (t, h) in sort(handlers; by=p -> priority(p.second), rev=true)
# TODO: There are race conditions here.
isexpired(h) && delete_handler!(c, eltype(h), tag)
dec!(h)
pred = try
predicate(h)(c, evt)
catch e
kws = logkws(c; event=T, handler=t, exception=(e, catch_backtrace()))
@warn "Predicate function threw an exception" kws...
return # Predicate throws -> do nothing.
end
if pred === true
try
result = handler(h)(c, evt)
iscollecting(h) && push!(results(h), result)
catch e
kws = logkws(c; event=T, handler=t, exception=(e, catch_backtrace()))
@warn "Handler function threw an exception" kws...
end
else
reason = pred in instances(FallbackReason) ? pred : FB_PREDICATE
try
fallback(h, reason)(c, evt)
catch e
kws = logkws(c; event=T, handler=t, exception=(e, catch_backtrace()))
@warn "Fallback function threw an exception" kws...
end
end
end
end
# Send a heartbeat.
function heartbeat(c::Client, ::Dict=Dict())
isopen(c) || return false
e, bt = trywritejson(c.conn.io, Dict("op" => 1, "d" => c.hb_seq))
show_gateway_error(c, e, bt)
ok = e === nothing
ok && (c.last_hb = now())
return ok
end
# Reconnect to the gateway.
function reconnect(c::Client, ::Dict=Dict(); resume::Bool=true, zombie::Bool=false)
@info "Reconnecting" logkws(c; resume=resume, zombie=zombie)...
try
close(c; zombie=zombie, statuscode=resume ? 4000 : 1000)
catch ex
@warn "Unable to close existing connection" ex
end
open(c; resume=resume)
end
# React to an invalid session.
function invalid_session(c::Client, data::Dict)
@warn "Received INVALID_SESSION" logkws(c; resumable=data[:d])...
sleep(rand(1:5))
reconnect(c; resume=data[:d])
end
# React to a hello message.
hello(c::Client, data::Dict) = c.hb_interval = data[:d][:heartbeat_interval]
# React to a heartbeack ack.
heartbeat_ack(c::Client, ::Dict) = c.last_ack = now()
# Gateway opcodes => handler function.
const HANDLERS = Dict(
0 => dispatch,
1 => heartbeat,
7 => reconnect,
9 => invalid_session,
10 => hello,
11 => heartbeat_ack,
)
# Error handling.
const CLOSE_CODES = Dict(
1000 => :NORMAL,
4000 => :UNKNOWN_ERROR,
4001 => :UNKNOWN_OPCODE, # Probably a library bug.
4002 => :DECODE_ERROR, # Probably a library bug.
4003 => :NOT_AUTHENTICATED, # Probably a library bug.
4004 => :AUTHENTICATION_FAILED,
4005 => :ALREADY_AUTHENTICATED, # Probably a library bug.
4007 => :INVALID_SEQ, # Probably a library bug.
4008 => :RATE_LIMITED, # Probably a library bug.
4009 => :SESSION_TIMEOUT,
4010 => :INVALID_SHARD,
4011 => :SHARDING_REQUIRED,
)
# Deal with an error from reading a message.
function handle_read_exception(c::Client, e::Exception)
@debug "Handling a $(typeof(e))" logkws(c; error=e)...
c.ready && handle_specific_exception(c, e)
end
handle_specific_exception(::Client, ::Empty) = nothing
handle_specific_exception(c::Client, ::EOFError) = reconnect(c)
function handle_specific_exception(c::Client, e::HTTP.WebSockets.WebSocketError)
err = get(CLOSE_CODES, e.status, :UNKNOWN_ERROR)
if err === :NORMAL
close(c)
elseif err === :AUTHENTICATION_FAILED
@error "Authentication failed" logkws(c)...
close(c)
elseif err === :INVALID_SHARD
@error "Invalid shard" logkws(c)...
close(c)
elseif err === :SHARDING_REQUIRED
@error "Sharding required" logkws(c)...
close(c)
else
@debug "Gateway connection was closed" logkws(c; code=e.status, error=err)...
reconnect(c)
end
end
function handle_specific_exception(c::Client, e::Exception)
@error sprint(showerror, e) logkws(c)...
isopen(c) || reconnect(c)
end
# Helpers.
# Read a JSON message.
function readjson(io)
return try
data = readavailable(io)
if isempty(data)
nothing, Empty()
else
JSON.parse(String(data); dicttype=Dict{Symbol, Any}), nothing
end
catch e
nothing, e
end
end
# Write a JSON message.
writejson(::Nothing, body) = error("Tried to write to an uninitialized connection")
writejson(io, body) = write(io, json(body))
# Write a JSON message, but don't throw an exception.
function trywritejson(io, body)
return try
writejson(io, body)
nothing, nothing
catch e
e, catch_backtrace()
end
end
# Display a gateway error.
show_gateway_error(c::Client, ::Nothing, ::Nothing) = nothing
function show_gateway_error(c::Client, e::Exception, bt)
kws = logkws(c; exception=(e, bt))
if isopen(c)
@error "Gateway error" kws...
else
@debug "Gateway error" kws...
end
end