feat: drop use std.net instead of zig-network

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-12-14 21:56:15 -06:00
parent e5c1d4d2b7
commit 891b96acbd
1 changed files with 19 additions and 27 deletions

View File

@ -1,12 +1,12 @@
const std = @import("std"); const std = @import("std");
const network = @import("network");
const Packet = @import("Packet.zig"); const Packet = @import("Packet.zig");
const Socket = network.Socket;
const Allocator = std.mem.Allocator;
const Atomic = std.atomic.Atomic;
const Emulator = @import("lib.zig").Emulator; const Emulator = @import("lib.zig").Emulator;
const Atomic = std.atomic.Atomic;
const Allocator = std.mem.Allocator;
const Server = std.net.StreamServer;
const Connection = Server.Connection;
const Self = @This(); const Self = @This();
const log = std.log.scoped(.Server); const log = std.log.scoped(.Server);
const port: u16 = 2424; const port: u16 = 2424;
@ -57,32 +57,24 @@ pub const memory_map: []const u8 =
// FIXME: Shouldn't this be a Packet Struct? // FIXME: Shouldn't this be a Packet Struct?
pkt_cache: ?[]const u8 = null, pkt_cache: ?[]const u8 = null,
client: Socket, server: Server,
_socket: Socket, client: Server.Connection,
emu: Emulator, emu: Emulator,
pub fn init(emulator: Emulator) !Self { pub fn init(emulator: Emulator) !Self {
try network.init(); var server = std.net.StreamServer.init(.{});
try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port));
var socket = try Socket.create(.ipv4, .tcp); var client = try server.accept();
try socket.bindToPort(port); log.info("client connected from {}", .{client.address});
try socket.listen();
var client = try socket.accept(); // TODO: This blocks, is this OK? return .{ .emu = emulator, .server = server, .client = client };
const endpoint = try client.getLocalEndPoint();
log.info("client connected from {}", .{endpoint});
return .{ .emu = emulator, ._socket = socket, .client = client };
} }
pub fn deinit(self: *Self, allocator: Allocator) void { pub fn deinit(self: *Self, allocator: Allocator) void {
self.reset(allocator); self.reset(allocator);
self.server.deinit();
self.client.close();
self._socket.close();
network.deinit();
self.* = undefined; self.* = undefined;
} }
@ -99,7 +91,7 @@ pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void {
var buf: [Packet.max_len]u8 = undefined; var buf: [Packet.max_len]u8 = undefined;
while (true) { while (true) {
const len = try self.client.receive(&buf); const len = try self.client.stream.readAll(&buf);
if (len == 0) break; if (len == 0) break;
if (quit.load(.Monotonic)) break; if (quit.load(.Monotonic)) break;
@ -113,7 +105,7 @@ pub fn run(self: *Self, allocator: Allocator, quit: *Atomic(bool)) !void {
} }
fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action { fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
// log.debug("-> {s}", .{input}); log.debug("-> {s}", .{input});
return switch (input[0]) { return switch (input[0]) {
'+' => blk: { '+' => blk: {
@ -150,7 +142,7 @@ fn send(self: *Self, allocator: Allocator, action: Action) !void {
switch (action) { switch (action) {
.send => |pkt| { .send => |pkt| {
_ = try self.client.send(pkt); _ = try self.client.send(pkt);
// log.debug("<- {s}", .{pkt}); log.debug("<- {s}", .{pkt});
self.reset(allocator); self.reset(allocator);
self.pkt_cache = pkt; self.pkt_cache = pkt;
@ -160,18 +152,18 @@ fn send(self: *Self, allocator: Allocator, action: Action) !void {
if (self.pkt_cache) |pkt| { if (self.pkt_cache) |pkt| {
_ = try self.client.send(pkt); _ = try self.client.send(pkt);
// log.debug("<- {s}", .{pkt}); log.debug("<- {s}", .{pkt});
} }
}, },
.ack => { .ack => {
_ = try self.client.send("+"); _ = try self.client.send("+");
// log.debug("<- +", .{}); log.debug("<- +", .{});
self.reset(allocator); self.reset(allocator);
}, },
.nack => { .nack => {
_ = try self.client.send("-"); _ = try self.client.send("-");
// log.debug("<- -", .{}); log.debug("<- -", .{});
self.reset(allocator); self.reset(allocator);
}, },