-
Notifications
You must be signed in to change notification settings - Fork 41
feat: adopt with Zig 0.15 #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
//! Start a TCP server at an unused port. | ||||||
//! Start an echo TCP server at an unused port. | ||||||
//! | ||||||
//! Test with | ||||||
//! echo "hello zig" | nc localhost <port> | ||||||
|
@@ -8,10 +8,6 @@ const net = std.net; | |||||
const print = std.debug.print; | ||||||
|
||||||
pub fn main() !void { | ||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||||||
defer _ = gpa.deinit(); | ||||||
const allocator = gpa.allocator(); | ||||||
|
||||||
const loopback = try net.Ip4Address.parse("127.0.0.1", 0); | ||||||
const localhost = net.Address{ .in = loopback }; | ||||||
var server = try localhost.listen(.{ | ||||||
|
@@ -22,13 +18,31 @@ pub fn main() !void { | |||||
const addr = server.listen_address; | ||||||
print("Listening on {}, access this port to end the program\n", .{addr.getPort()}); | ||||||
|
||||||
var client = try server.accept(); | ||||||
defer client.stream.close(); | ||||||
|
||||||
print("Connection received! {} is sending data.\n", .{client.address}); | ||||||
|
||||||
const message = try client.stream.reader().readAllAlloc(allocator, 1024); | ||||||
defer allocator.free(message); | ||||||
const client = try server.accept(); | ||||||
// In real world, you'd want to handle multiple clients, probably in separate threads. | ||||||
try handleClient(client); | ||||||
} | ||||||
|
||||||
print("{} says {s}\n", .{ client.address, message }); | ||||||
fn handleClient(client: net.Server.Connection) !void { | ||||||
print("Accepted connection from {f}\n", .{client.address}); | ||||||
defer client.stream.close(); | ||||||
var stream_buf: [1024]u8 = undefined; | ||||||
var reader = client.stream.reader(&stream_buf); | ||||||
// Here we echo back what we read directly, so the writer buffer is empty | ||||||
var writer = client.stream.writer(&.{}); | ||||||
|
||||||
while (true) { | ||||||
print("Waiting for data from {f}...\n", .{client.address}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
const msg = reader.interface().takeDelimiterInclusive('\n') catch |err| { | ||||||
if (err == error.EndOfStream) { | ||||||
print("{f} closed the connection\n", .{client.address}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return; | ||||||
} else { | ||||||
return err; | ||||||
} | ||||||
}; | ||||||
print("{f} says {s}", .{ client.address, msg }); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
try writer.interface.writeAll(msg); | ||||||
// No need to flush, as writer buffer is empty | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,13 +16,13 @@ pub fn main() !void { | |||||
// Connect to peer | ||||||
const stream = try net.tcpConnectToAddress(peer); | ||||||
defer stream.close(); | ||||||
print("Connecting to {}\n", .{peer}); | ||||||
print("Connecting to {f}\n", .{peer}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
// Sending data to peer | ||||||
const data = "hello zig"; | ||||||
var writer = stream.writer(); | ||||||
const size = try writer.write(data); | ||||||
print("Sending '{s}' to peer, total written: {d} bytes\n", .{ data, size }); | ||||||
// Or just using `writer.writeAll` | ||||||
// try writer.writeAll("hello zig"); | ||||||
var buffer: [1024]u8 = undefined; | ||||||
var writer = stream.writer(buffer[0..]); | ||||||
try writer.interface.writeAll(data); | ||||||
try writer.interface.flush(); | ||||||
print("Sending '{s}' to peer, total written: {d} bytes\n", .{ data, data.len }); | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,7 +30,7 @@ pub fn main() !void { | |||||
|
||||||
var buf: [1024]u8 = undefined; | ||||||
|
||||||
print("Listen on {any}...\n", .{addr}); | ||||||
print("Listen on {f}\n", .{addr}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
// we did not set the NONBLOCK flag (socket type flag), | ||||||
// so the program will wait until data is received | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,5 @@ | ||||||
const std = @import("std"); | ||||||
const log = std.log; | ||||||
const WebSocket = std.http.WebSocket; | ||||||
const Request = std.http.Server.Request; | ||||||
const Connection = std.net.Server.Connection; | ||||||
|
||||||
|
@@ -11,7 +10,7 @@ pub fn main() !void { | |||||
var server = try std.net.Address.listen(addr, .{ .reuse_address = true }); | ||||||
defer server.deinit(); | ||||||
|
||||||
log.info("Start HTTP server at {any}", .{addr}); | ||||||
log.info("Start HTTP server at {f}", .{addr}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
while (true) { | ||||||
const conn = server.accept() catch |err| { | ||||||
|
@@ -29,31 +28,31 @@ pub fn main() !void { | |||||
fn accept(conn: Connection) !void { | ||||||
defer conn.stream.close(); | ||||||
|
||||||
log.info("Got new client({any})!", .{conn.address}); | ||||||
log.info("Got new client({f})!", .{conn.address}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format specifier
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
var read_buffer: [MAX_BUF]u8 = undefined; | ||||||
var server = std.http.Server.init(conn, &read_buffer); | ||||||
while (server.state == .ready) { | ||||||
var recv_buffer: [1024]u8 = undefined; | ||||||
var send_buffer: [100]u8 = undefined; | ||||||
var connection_br = conn.stream.reader(&recv_buffer); | ||||||
var connection_bw = conn.stream.writer(&send_buffer); | ||||||
var server = std.http.Server.init(connection_br.interface(), &connection_bw.interface); | ||||||
while (server.reader.state == .ready) { | ||||||
var request = server.receiveHead() catch |err| switch (err) { | ||||||
error.HttpConnectionClosing => return, | ||||||
else => return err, | ||||||
}; | ||||||
|
||||||
var ws: WebSocket = undefined; | ||||||
var send_buf: [MAX_BUF]u8 = undefined; | ||||||
var recv_buf: [MAX_BUF]u8 align(4) = undefined; | ||||||
|
||||||
if (try ws.init(&request, &send_buf, &recv_buf)) { | ||||||
// Upgrade to web socket successfully. | ||||||
serveWebSocket(&ws) catch |err| switch (err) { | ||||||
error.ConnectionClose => { | ||||||
log.info("Client({any}) closed!", .{conn.address}); | ||||||
break; | ||||||
}, | ||||||
else => return err, | ||||||
}; | ||||||
} else { | ||||||
try serveHTTP(&request); | ||||||
switch (request.upgradeRequested()) { | ||||||
.other => |other_protocol| { | ||||||
log.err("Not supported protocol, {s}", .{other_protocol}); | ||||||
return; | ||||||
}, | ||||||
.websocket => |key| { | ||||||
var ws = try request.respondWebSocket(.{ .key = key orelse "" }); | ||||||
try serveWebSocket(&ws); | ||||||
}, | ||||||
.none => { | ||||||
try serveHTTP(&request); | ||||||
}, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -63,16 +62,20 @@ fn serveHTTP(request: *Request) !void { | |||||
"Hello World from Zig HTTP server", | ||||||
.{ | ||||||
.extra_headers = &.{ | ||||||
.{ .name = "custom header", .value = "custom value" }, | ||||||
.{ .name = "custom-header", .value = "custom value" }, | ||||||
}, | ||||||
}, | ||||||
); | ||||||
} | ||||||
|
||||||
fn serveWebSocket(ws: *WebSocket) !void { | ||||||
try ws.writeMessage("Message from zig", .text); | ||||||
fn serveWebSocket(ws: *std.http.Server.WebSocket) !void { | ||||||
try ws.writeMessage("Hello from Zig WebSocket server", .text); | ||||||
while (true) { | ||||||
const msg = try ws.readSmallMessage(); | ||||||
if (msg.opcode == .connection_close) { | ||||||
log.info("Client closed the WebSocket", .{}); | ||||||
return; | ||||||
} | ||||||
try ws.writeMessage(msg.data, msg.opcode); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format specifier
{f}
is for floating point numbers, butclient.address
is a network address. This should be{any}
to properly format the address.Copilot uses AI. Check for mistakes.