treesummaryrefslogcommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig5
-rw-r--r--build.zig.zon41
-rw-r--r--src/main.zig20
3 files changed, 25 insertions, 41 deletions
diff --git a/build.zig b/build.zig
index 37ebe2e..f046fad 100644
--- a/build.zig
+++ b/build.zig
@@ -20,6 +20,10 @@ pub fn build(b: *std.Build) void {
// of this build script using `b.option()`. All defined flags (including
// target and optimize options) will be listed when running `zig build --help`
// in this directory.
+ const zio = b.dependency("zio", .{
+ .target = target,
+ .optimize = optimize,
+ });
// Here we define an executable. An executable needs to have a root module
// which needs to expose a `main` function. While we could add a main function
@@ -44,6 +48,7 @@ pub fn build(b: *std.Build) void {
// unlike b.addModule, it does not expose the module to consumers of
// this package, which is why in this case we don't have to give it a name.
.root_source_file = b.path("src/main.zig"),
+ .imports = &[_]std.Build.Module.Import{.{ .name = "zio", .module = zio.module("zio") }},
// Target and optimization levels must be explicitly wired in when
// defining an executable or library (in the root module), and you
// can also hardcode a specific target for an executable or library
diff --git a/build.zig.zon b/build.zig.zon
index fd2f5a2..51f0077 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -32,44 +32,11 @@
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
- // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
- //.example = .{
- // // When updating this field to a new URL, be sure to delete the corresponding
- // // `hash`, otherwise you are communicating that you expect to find the old hash at
- // // the new URL. If the contents of a URL change this will result in a hash mismatch
- // // which will prevent zig from using it.
- // .url = "https://example.com/foo.tar.gz",
- //
- // // This is computed from the file contents of the directory of files that is
- // // obtained after fetching `url` and applying the inclusion rules given by
- // // `paths`.
- // //
- // // This field is the source of truth; packages do not come from a `url`; they
- // // come from a `hash`. `url` is just one of many possible mirrors for how to
- // // obtain a package matching this `hash`.
- // //
- // // Uses the [multihash](https://multiformats.io/multihash/) format.
- // .hash = "...",
- //
- // // When this is provided, the package is found in a directory relative to the
- // // build root. In this case the package's hash is irrelevant and therefore not
- // // computed. This field and `url` are mutually exclusive.
- // .path = "foo",
- //
- // // When this is set to `true`, a package is declared to be lazily
- // // fetched. This makes the dependency only get fetched if it is
- // // actually used.
- // .lazy = false,
- //},
+ .zio = .{
+ .url = "git+https://github.com/lalinsky/zio?ref=zig-0.17#9005f50f8cc5e5fa54341c5053f7b3aec7263e8b",
+ .hash = "zio-0.11.0-xHbVVPUAHAAJWxcnqslC7N7gyFTPDGtuc14ISmpdrzY4",
+ },
},
- // Specifies the set of files and directories that are included in this package.
- // Only files and directories listed here are included in the `hash` that
- // is computed for this package. Only files listed here will remain on disk
- // when using the zig package manager. As a rule of thumb, one should list
- // files required for compilation plus any license(s).
- // Paths are relative to the build root. Use the empty string (`""`) to refer to
- // the build root itself.
- // A directory listed here means that all files within, recursively, are included.
.paths = .{
"build.zig",
"build.zig.zon",
diff --git a/src/main.zig b/src/main.zig
index d1acc32..d80a7d3 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,4 +1,5 @@
const std = @import("std");
+const zio = @import("zio");
const Io = std.Io;
const WebSocket = std.http.Server.WebSocket;
@@ -21,7 +22,12 @@ fn handle_websocket(alloc: std.mem.Allocator, websocket: *WebSocket) void {
m.unlock();
defer {
- for (0..clients.items.len) |i| { if (std.mem.eql(u8, websocket.key, clients.items[i].key)) { _ = clients.swapRemove(i); break; } }
+ for (0..clients.items.len) |i| {
+ if (std.mem.eql(u8, websocket.key, clients.items[i].key)) {
+ _ = clients.swapRemove(i);
+ break;
+ }
+ }
std.debug.print("{} clients\n", .{clients.items.len});
}
@@ -105,6 +111,7 @@ pub fn main(init: std.process.Init) !void {
// This is appropriate for anything that lives as long as the process.
const arena: std.mem.Allocator = init.arena.allocator();
+ defer init.arena.deinit();
clients = try .initCapacity(arena, 10);
@@ -114,8 +121,9 @@ pub fn main(init: std.process.Init) !void {
std.log.info("arg: {s}", .{arg});
}
- // In order to do I/O operations need an `Io` instance.
- const io = init.io;
+ const rt = try zio.Runtime.init(arena, .{});
+ defer rt.deinit();
+ const io = rt.io();
var port: u16 = 10010;
if (init.environ_map.get("PORT")) |s| {
@@ -129,10 +137,14 @@ pub fn main(init: std.process.Init) !void {
const address = try std.Io.net.IpAddress.parseIp4("0.0.0.0", port);
var net_server = try address.listen(io, .{ .reuse_address = true });
+ var group: Io.Group = .init;
+ defer group.cancel(io);
+
while (true) {
const stream = try net_server.accept(io);
+ errdefer stream.close(io);
- _ = io.async(handle_request, .{ arena, io, stream });
+ try group.concurrent(io, handle_request, .{ arena, io, stream });
// std.debug.print("created http thread\n", .{});
}