fixes
This commit is contained in:
parent
aea624ea64
commit
425c8646d7
@ -197,7 +197,7 @@ pub fn login(allocator: mem.Allocator, args: struct {
|
|||||||
defer parsed.deinit();
|
defer parsed.deinit();
|
||||||
const url = parsed.value.url["wss://".len..];
|
const url = parsed.value.url["wss://".len..];
|
||||||
|
|
||||||
return .{
|
var self: Self = .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.token = args.token,
|
.token = args.token,
|
||||||
.intents = args.intents,
|
.intents = args.intents,
|
||||||
@ -210,6 +210,11 @@ pub fn login(allocator: mem.Allocator, args: struct {
|
|||||||
.packets = std.ArrayList(u8).init(allocator),
|
.packets = std.ArrayList(u8).init(allocator),
|
||||||
.inflator = try zlib.Decompressor.init(allocator, .{ .header = .zlib_or_gzip }),
|
.inflator = try zlib.Decompressor.init(allocator, .{ .header = .zlib_or_gzip }),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const event_listener = try std.Thread.spawn(.{}, Self.readMessage, .{ &self, null });
|
||||||
|
event_listener.join();
|
||||||
|
|
||||||
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn _connect_ws(allocator: mem.Allocator, url: []const u8) !ws.Client {
|
inline fn _connect_ws(allocator: mem.Allocator, url: []const u8) !ws.Client {
|
||||||
@ -301,7 +306,7 @@ pub fn readMessage(self: *Self, _: anytype) !void {
|
|||||||
|
|
||||||
var prng = std.Random.DefaultPrng.init(0);
|
var prng = std.Random.DefaultPrng.init(0);
|
||||||
const jitter = std.Random.float(prng.random(), f64);
|
const jitter = std.Random.float(prng.random(), f64);
|
||||||
|
self.heart.lastBeat = std.time.milliTimestamp();
|
||||||
const heartbeat_writer = try std.Thread.spawn(.{}, Self.heartbeat, .{ self, jitter });
|
const heartbeat_writer = try std.Thread.spawn(.{}, Self.heartbeat, .{ self, jitter });
|
||||||
heartbeat_writer.detach();
|
heartbeat_writer.detach();
|
||||||
},
|
},
|
||||||
@ -371,7 +376,7 @@ pub fn heartbeat(self: *Self, initial_jitter: f64) !void {
|
|||||||
try self.send(.{ .op = @intFromEnum(Opcode.Heartbeat), .d = seq });
|
try self.send(.{ .op = @intFromEnum(Opcode.Heartbeat), .d = seq });
|
||||||
self.ws_mutex.unlock();
|
self.ws_mutex.unlock();
|
||||||
|
|
||||||
if (last > (5100 * self.heart.heartbeatInterval)) {
|
if ((std.time.milliTimestamp() - last) > (5000 * self.heart.heartbeatInterval)) {
|
||||||
self.close(ShardSocketCloseCodes.ZombiedConnection, "Zombied connection") catch unreachable;
|
self.close(ShardSocketCloseCodes.ZombiedConnection, "Zombied connection") catch unreachable;
|
||||||
@panic("zombied conn\n");
|
@panic("zombied conn\n");
|
||||||
}
|
}
|
||||||
|
14
src/test.zig
14
src/test.zig
@ -21,19 +21,14 @@ fn message_create(session: *Shard, message: Discord.Message) void {
|
|||||||
defer session.allocator.free(json);
|
defer session.allocator.free(json);
|
||||||
const path = std.fmt.allocPrint(session.allocator, "/channels/{d}/messages", .{message.channel_id.value()}) catch unreachable;
|
const path = std.fmt.allocPrint(session.allocator, "/channels/{d}/messages", .{message.channel_id.value()}) catch unreachable;
|
||||||
|
|
||||||
_ = req.makeRequest(
|
_ = req.makeRequest(.POST, path, json) catch unreachable;
|
||||||
.POST,
|
|
||||||
path,
|
|
||||||
json,
|
|
||||||
) catch unreachable;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var tsa = std.heap.ThreadSafeAllocator{ .child_allocator = std.heap.c_allocator };
|
||||||
const allocator = gpa.allocator();
|
|
||||||
|
|
||||||
var handler = try Shard.login(allocator, .{
|
var handler = try Shard.login(tsa.allocator(), .{
|
||||||
.token = std.posix.getenv("TOKEN") orelse unreachable,
|
.token = std.posix.getenv("TOKEN") orelse unreachable,
|
||||||
.intents = Intents.fromRaw(37379),
|
.intents = Intents.fromRaw(37379),
|
||||||
.run = Internal.GatewayDispatchEvent(*Shard){
|
.run = Internal.GatewayDispatchEvent(*Shard){
|
||||||
@ -43,7 +38,4 @@ pub fn main() !void {
|
|||||||
.log = .yes,
|
.log = .yes,
|
||||||
});
|
});
|
||||||
errdefer handler.deinit();
|
errdefer handler.deinit();
|
||||||
|
|
||||||
const event_listener = try std.Thread.spawn(.{}, Shard.readMessage, .{ &handler, null });
|
|
||||||
event_listener.join();
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user