Skip to content

Commit ee57385

Browse files
author
Steve🔆
committedJun 13, 2024
updates for zig 0.13
1 parent 9559b0c commit ee57385

File tree

5 files changed

+29
-45
lines changed

5 files changed

+29
-45
lines changed
 

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.DS_Store
2-
zig-cache
2+
.zig-cache
33
zig-out

‎build.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {
1010

1111
const exe = b.addExecutable(.{
1212
.name = "zig-zag-zoe",
13-
.root_source_file = .{ .path = "src/main.zig" },
13+
.root_source_file = b.path("src/main.zig"),
1414
.target = target,
1515
.optimize = optimize,
1616
});
@@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void {
2727
run_step.dependOn(&run_cmd.step);
2828

2929
const unit_tests = b.addTest(.{
30-
.root_source_file = .{ .path = "src/main.zig" },
30+
.root_source_file = b.path("src/main.zig"),
3131
.target = target,
3232
.optimize = optimize,
3333
});

‎build.zig.zon

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
.{
22
.name = "zig-zag-zoe",
3-
.version = "0.1.0",
3+
.version = "0.1.13",
44
.paths = .{""},
55

66
.dependencies = .{
77
.httpz = .{
8-
.url = "git+https://github.com/zigster64/http.zig#master",
9-
.hash = "1220a06b79fe8996ccfab3017f535f0ebc4976055e5b753fdfb2d8b0830aeb2d9a32",
10-
// .url = "git+https://github.com/karseguin/http.zig#blocking",
11-
// .hash = "1220b9cb100296f9ecbea1d5d335a074884e978c2f4fa9781cdeec47e2d222119b65",
8+
// .url = "git+https://github.com/zigster64/http.zig#master",
9+
.url = "git+https://github.com/karlseguin/http.zig#master",
10+
.hash = "12206d8d2a7f2535a7d48872eaf43911ead71ba05456f97363ae25e4f057a985975f",
1211
},
1312
.zts = .{
1413
.url = "git+https://github.com/zigster64/zts#main",
15-
.hash = "1220864472b1642b73155481ac7fe274c891816bd182a8853f378d0aefd42ef44c33",
14+
.hash = "122097f35c2d1aa158464a2c6e30f76ca09f747596881cba3f86627195135dcbe70e",
1615
},
1716
},
1817
}

‎src/game.zig

+11-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn init(grid_x: u8, grid_y: u8, players: u8, needed_to_win: u8, zero_wing: u
7070

7171
// seed the RNG
7272
var os_seed: u64 = undefined;
73-
try std.os.getrandom(std.mem.asBytes(&os_seed));
73+
try std.posix.getrandom(std.mem.asBytes(&os_seed));
7474

7575
var s = Self{
7676
.board = try Board.init(grid_x, grid_y),
@@ -170,7 +170,7 @@ pub fn logExtra(self: *Self, req: *httpz.Request, extra: []const u8) void {
170170
// do some memory logging and stats here
171171
self.game_mutex.lock();
172172
const player = self.getPlayer(req);
173-
const ru = std.os.getrusage(0);
173+
const ru = std.posix.getrusage(0);
174174
std.log.info("[{}:{s}:{}:{}:{}] {s} {s} {s}", .{ std.time.timestamp(), @tagName(self.state), player, ru.maxrss, ru.maxrss - self.last_rss, @tagName(req.method), req.url.raw, extra });
175175
self.last_rss = ru.maxrss;
176176
self.game_mutex.unlock();
@@ -180,7 +180,7 @@ pub fn log(self: *Self, req: *httpz.Request, elapsedUs: i128) void {
180180
// do some memory logging and stats here
181181
self.game_mutex.lock();
182182
const player = self.getPlayer(req);
183-
const ru = std.os.getrusage(0);
183+
const ru = std.posix.getrusage(0);
184184
std.log.info("[{}:{s}:{}:{}:{}] {s} {s} ({}µs)", .{ std.time.timestamp(), @tagName(self.state), player, ru.maxrss, ru.maxrss - self.last_rss, @tagName(req.method), req.url.raw, elapsedUs });
185185
self.game_mutex.unlock();
186186
self.last_rss = ru.maxrss;
@@ -685,12 +685,16 @@ fn events(self: *Self, req: *httpz.Request, res: *httpz.Response) !void {
685685
}
686686

687687
res.disown();
688-
const stream = try res.startEventStream();
689-
const thread = try std.Thread.spawn(.{}, eventsLoop, .{ self, stream });
690-
thread.detach();
688+
try res.startEventStream(self, eventsLoopWrapper);
691689
}
692690

693-
fn eventsLoop(self: *Self, stream: anytype) !void {
691+
fn eventsLoopWrapper(self: *Self, stream: std.net.Stream) void {
692+
eventsLoop(self, stream) catch |err| {
693+
std.log.err("eventsLoopWrapper: {}", .{err});
694+
};
695+
}
696+
697+
fn eventsLoop(self: *Self, stream: std.net.Stream) !void {
694698
// on initial connect, send the clock details, and send the last event processed
695699
try self.clock(stream);
696700
try stream.writer().print("event: update\ndata: {s}\n\n", .{@tagName(self.last_event)});

‎src/main.zig

+10-29
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ const default_port = 3000;
66

77
// always log .info, even in release modes
88
// change this to .debug if you want extreme debugging
9-
pub const std_options = struct {
10-
pub const log_level = .info;
9+
pub const std_options = std.Options{
10+
.log_level = .info,
1111
};
1212

1313
pub fn usage() void {
1414
std.debug.print("USAGE: zig-zag-zoe [-p PORTNUMBER]\n", .{});
15-
std.debug.print(" or use the PORT env var to set the port, for like Docker or whatever\n", .{});
15+
std.debug.print(" or use the PORT env var to set the port\n", .{});
1616
}
1717

1818
pub fn main() !void {
1919
var port: u16 = default_port;
2020

21-
const env_port = std.os.getenv("PORT");
21+
const env_port = std.posix.getenv("PORT");
2222
if (env_port != null and env_port.?.len > 0) {
2323
port = try std.fmt.parseInt(u16, env_port.?, 10);
2424
std.log.debug("Port set to {} via ENV\n", .{port});
@@ -60,31 +60,12 @@ pub fn main() !void {
6060
const zero_wing: u8 = 0;
6161

6262
var game = try Game.init(grid_x, grid_y, players, win, zero_wing);
63-
try game.startWatcher();
63+
// try game.startWatcher();
6464

65-
std.log.debug("Setting pool size to {}", .{Game.MAX_PLAYERS * 4});
65+
// std.log.debug("Setting pool size to {}", .{Game.MAX_PLAYERS * 4});
6666
var server = try httpz.ServerCtx(*Game, *Game).init(allocator, .{
6767
.address = "0.0.0.0",
6868
.port = port,
69-
// .pool_size = Game.MAX_PLAYERS * 32, // allow up to 32 req/res pairs buffered for each player
70-
// .pool = .{
71-
// .min = Game.MAX_PLAYERS * 4,
72-
// .max = Game.MAX_PLAYERS * 24,
73-
// .timeout = 5000,
74-
// },
75-
// .thread_pool = Game.MAX_PLAYERS * 2,
76-
.request = .{
77-
.max_body_size = 256,
78-
.buffer_size = 1024,
79-
.max_header_count = 32,
80-
.max_param_count = 2,
81-
.max_query_count = 1,
82-
},
83-
.response = .{
84-
.body_buffer_size = 48_000, // big enough for the biggest file
85-
.header_buffer_size = 256,
86-
.max_header_count = 8,
87-
},
8869
}, &game);
8970
server.notFound(notFound);
9071
server.errorHandler(errorHandler);
@@ -103,7 +84,7 @@ pub fn main() !void {
10384
// connect the game object to the router
10485
game.addRoutes(router);
10586

106-
const ru = std.os.getrusage(0);
87+
const ru = std.posix.getrusage(0);
10788
std.log.info("[{}:{s}:{}:{}:{}] {s} {s}", .{ std.time.timestamp(), @tagName(game.state), 0, ru.maxrss, ru.maxrss, "BOOT", "Initial Startup" });
10889
return server.listen();
10990
}
@@ -113,8 +94,8 @@ fn printValidAddresses(allocator: std.mem.Allocator, port: u16) !void {
11394

11495
// do some digging to get a list of IPv4 addresses that we are listening on
11596
std.log.info("- http://localhost:{}", .{port});
116-
var hostBuffer: [std.os.HOST_NAME_MAX]u8 = undefined;
117-
const hostname = try std.os.gethostname(&hostBuffer);
97+
var hostBuffer: [std.posix.HOST_NAME_MAX]u8 = undefined;
98+
const hostname = try std.posix.gethostname(&hostBuffer);
11899
std.log.info("- http://{s}:{}", .{ hostname, port });
119100

120101
var addressList = try std.net.getAddressList(allocator, hostname, port);
@@ -124,7 +105,7 @@ fn printValidAddresses(allocator: std.mem.Allocator, port: u16) !void {
124105
defer uniqueIPv4Addresses.deinit();
125106

126107
for (addressList.addrs) |address| {
127-
if (address.any.family == std.os.AF.INET) {
108+
if (address.any.family == std.posix.AF.INET) {
128109
try uniqueIPv4Addresses.put(address.in, true);
129110
}
130111
}

0 commit comments

Comments
 (0)
Please sign in to comment.