feat: try add socket
This commit is contained in:
15
src/main.zig
15
src/main.zig
@@ -1,18 +1,13 @@
|
||||
const std = @import("std");
|
||||
const x = @import("protocol.zig");
|
||||
const protocol = @import("protocol.zig");
|
||||
const socket = @import("socket.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||
const alloc = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
socket.listen(alloc);
|
||||
}
|
||||
|
||||
test {
|
||||
_ = @import("protocol.zig");
|
||||
}
|
||||
|
||||
test "simple test" {
|
||||
const gpa = std.testing.allocator;
|
||||
var list: std.ArrayList(i32) = .empty;
|
||||
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
|
||||
try list.append(gpa, 42);
|
||||
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
||||
_ = @import("socket.zig");
|
||||
}
|
||||
|
||||
22
src/socket.zig
Normal file
22
src/socket.zig
Normal file
@@ -0,0 +1,22 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn listen(alloc: std.mem.Allocator) !void {
|
||||
const addr = std.net.Address.initIp4(.{ 0, 0, 0, 0 }, 3667);
|
||||
var server = try addr.listen(.{});
|
||||
|
||||
std.log.info("Server listening on port 3667", .{});
|
||||
|
||||
var client = try server.accept();
|
||||
defer client.stream.close();
|
||||
|
||||
const client_reader = client.stream.reader();
|
||||
const client_writer = client.stream.writer();
|
||||
while (true) {
|
||||
const msg = try client_reader.readUntilDelimiterOrEofAlloc(alloc, '\n', 65536) orelse break;
|
||||
defer alloc.free(msg);
|
||||
|
||||
std.log.info("Recieved message: \"{}\"", .{std.zig.fmtEscapes(msg)});
|
||||
|
||||
try client_writer.writeAll(msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user