Last active
July 18, 2020 22:15
-
-
Save lithdew/7f4fc505abc2c85cf366866bfa396980 to your computer and use it in GitHub Desktop.
zig: evented i/o stream server accept not returning an error (?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub const io_mode = .evented; | |
var main_frame: ?*std.event.Loop.NextTickNode = undefined; | |
pub fn loop(server: *std.net.StreamServer) !void { | |
while (true) { | |
var conn = server.accept() catch |err| { | |
std.debug.print("Got loop error: {}\n", .{err}); | |
return err; | |
}; | |
} | |
} | |
pub fn main() !void { | |
// start server | |
var server = std.net.StreamServer.init(.{}); | |
try server.listen(try std.net.Address.parseIp("127.0.0.1", 9000)); | |
// run loop and store loop frame | |
var loopFrame = async loop(&server); | |
defer { | |
server.deinit(); | |
await loopFrame catch |err| { | |
std.debug.print("There was an error from the loop: {}\n", .{err}); | |
}; | |
std.debug.print("Shutdown successful.\n", .{}); | |
} | |
std.debug.print("Listening for new connections.\n", .{}); | |
// setup exit signal handler | |
const handler = struct { | |
fn impl(sig: i32, info: *std.os.siginfo_t, ucontext: ?*c_void) callconv(.C) void { | |
if (main_frame) |f| { | |
std.event.Loop.instance.?.onNextTick(f); | |
main_frame = null; | |
} | |
} | |
}; | |
const action = &std.os.Sigaction{ | |
.sigaction = handler.impl, | |
.mask = std.os.empty_sigset, | |
.flags = 0, | |
.restorer = null, | |
}; | |
std.os.sigaction(std.os.SIGINT, action, null); | |
suspend { | |
// std.event.Loop.instance.?.beginOneEvent(); | |
main_frame = &std.event.Loop.NextTickNode{ .next = null, .data = @frame() }; | |
} | |
// stop server | |
std.debug.print("Shutting down...\n", .{}); | |
// wait for loop frame to finish | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment