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"); +}