From 049e69a88007acbc8e7a03b5d31a9ba2ed68736d Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Fri, 25 Dec 2020 02:33:23 +0000 Subject: [PATCH] some async stuff --- colorblind/README.md | 2 ++ colorblind/send.zig | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 colorblind/README.md create mode 100644 colorblind/send.zig diff --git a/colorblind/README.md b/colorblind/README.md new file mode 100644 index 0000000..558625a --- /dev/null +++ b/colorblind/README.md @@ -0,0 +1,2 @@ +following along with this blog post: +https://kristoff.it/blog/zig-colorblind-async-await/ diff --git a/colorblind/send.zig b/colorblind/send.zig new file mode 100644 index 0000000..50b8ef3 --- /dev/null +++ b/colorblind/send.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const net = std.net; + +const stdout = std.io.getStdOut().writer(); +pub const io_mode = .evented; + +pub fn main() !void { + const addr = try net.Address.parseIp("127.0.0.1", 7000); + try stdout.print("about to send message\n", .{}); + + var sendFrame = async send_message(addr); + // you can do some other stuff while waiting for send_message to resolve + try stdout.print("message sent, can do other stuff now\n", .{}); + try stdout.print("alright let's wait\n", .{}); + + // now we await the result from that prior async operation + try await sendFrame; + try stdout.print("we finished with sendFrame\n", .{}); +} + +fn send_message(addr: net.Address) !void { + var socket = try net.tcpConnectToAddress(addr); + defer socket.close(); + + _ = try socket.write("Hello World!\n"); +}