chore: reorganize code

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-12-14 22:54:08 -04:00
parent feb222c262
commit 278a5adf25
1 changed files with 110 additions and 79 deletions

View File

@ -4,8 +4,6 @@ const network = @import("network");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Socket = network.Socket; const Socket = network.Socket;
const port: u16 = 2424;
const target: []const u8 = const target: []const u8 =
\\<target version="1.0"> \\<target version="1.0">
\\ <architecture>armv4t</architecture> \\ <architecture>armv4t</architecture>
@ -33,92 +31,93 @@ const target: []const u8 =
; ;
pub fn main() !void { pub fn main() !void {
const log = std.log.scoped(.Server); const log = std.log.scoped(.Main);
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit()); defer std.debug.assert(!gpa.deinit());
const allocator = gpa.allocator(); const allocator = gpa.allocator();
try network.init(); var server = try Server.init();
defer network.deinit(); defer server.deinit(allocator);
var socket = try Socket.create(.ipv4, .tcp); try server.run(allocator);
defer socket.close();
try socket.bindToPort(port);
try socket.listen();
var client = try socket.accept();
defer client.close();
const endpoint = try client.getLocalEndPoint();
log.info("client connected from {}", .{endpoint});
try gdbStubServer(allocator, client);
log.info("Client disconnected", .{}); log.info("Client disconnected", .{});
} }
fn gdbStubServer(allocator: Allocator, client: Socket) !void { const Server = struct {
var buf: [Packet.size]u8 = undefined; const Self = @This();
var previous: ?[]const u8 = null; const log = std.log.scoped(.Server);
defer if (previous) |packet| allocator.free(packet); const port: u16 = 2424;
while (true) { // FIXME: Shouldn't this be a Packet Struct?
const len = try client.receive(&buf); pkt_cache: ?[]const u8 = null,
if (len == 0) break;
switch (try parse(allocator, client, previous, buf[0..len])) { client: Socket,
.send => |response| { _socket: Socket,
if (previous) |packet| allocator.free(packet);
previous = response; pub fn init() !Self {
}, try network.init();
.recv_ack => {
if (previous) |packet| allocator.free(packet); var socket = try Socket.create(.ipv4, .tcp);
previous = null; try socket.bindToPort(port);
}, try socket.listen();
.recv_nack => {},
} var client = try socket.accept(); // TODO: This blocks, is this OK?
const endpoint = try client.getLocalEndPoint();
log.info("client connected from {}", .{endpoint});
return .{ ._socket = socket, .client = client };
} }
pub fn deinit(self: *Self, allocator: Allocator) void {
self.reset(allocator);
self.client.close();
self._socket.close();
network.deinit();
self.* = undefined;
} }
const Action = union(enum) { const Action = union(enum) {
nothing,
send: []const u8, send: []const u8,
recv_nack, retry,
recv_ack, ack,
nack,
}; };
fn parse(allocator: Allocator, client: Socket, previous: ?[]const u8, input: []const u8) !Action { pub fn run(self: *Self, allocator: Allocator) !void {
const log = std.log.scoped(.GdbStubParser); var buf: [Packet.max_len]u8 = undefined;
return switch (input[0]) { while (true) {
'+' => .recv_ack, const len = try self.client.receive(&buf);
'-' => blk: { if (len == 0) break;
if (previous) |packet| {
log.warn("received nack, resending: \"{s}\"", .{packet}); const action = try Self.parse(allocator, buf[0..len]);
_ = try client.send(packet); try self.send(allocator, action);
} else { }
log.err("received nack, but we don't recall sending anything", .{});
} }
break :blk .recv_nack; fn parse(allocator: Allocator, input: []const u8) !Action {
}, return switch (input[0]) {
'+' => .nothing,
'-' => .retry,
'$' => blk: { '$' => blk: {
// Packet // Packet
var packet = try Packet.from(allocator, input); var packet = Packet.from(allocator, input) catch return .nack;
defer packet.deinit(allocator); defer packet.deinit(allocator);
var string = try packet.parse(allocator); var string = packet.parse(allocator) catch return .nack;
defer string.deinit(allocator); defer string.deinit(allocator);
const reply = string.inner(); const reply = string.inner();
_ = try client.send("+"); // Acknowledge
// deallocated by the caller // deallocated by the caller
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) }); const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
_ = try client.send(response);
break :blk .{ .send = response }; break :blk .{ .send = response };
}, },
@ -126,10 +125,42 @@ fn parse(allocator: Allocator, client: Socket, previous: ?[]const u8, input: []c
}; };
} }
fn send(self: *Self, allocator: Allocator, action: Action) !void {
switch (action) {
.send => |pkt| {
_ = try self.client.send("+"); // ACK
_ = try self.client.send(pkt);
self.reset(allocator);
self.pkt_cache = pkt;
},
.retry => {
log.warn("received nack, resending: \"{?s}\"", .{self.pkt_cache});
if (self.pkt_cache) |pkt| _ = try self.client.send(pkt); // FIXME: is an ack to a nack necessary?
},
.ack => {
_ = try self.client.send("+");
self.reset(allocator);
},
.nack => {
_ = try self.client.send("-");
self.reset(allocator);
},
.nothing => self.reset(allocator),
}
}
fn reset(self: *Self, allocator: Allocator) void {
if (self.pkt_cache) |pkt| allocator.free(pkt);
self.pkt_cache = null;
}
};
const Packet = struct { const Packet = struct {
const Self = @This(); const Self = @This();
const log = std.log.scoped(.Packet); const log = std.log.scoped(.Packet);
const size: usize = 0x1000; const max_len: usize = 0x1000;
contents: []const u8, contents: []const u8,
@ -212,7 +243,7 @@ const Packet = struct {
const format = "PacketSize={x:};qXfer:features:read+;qXfer:memory-map:read+"; const format = "PacketSize={x:};qXfer:features:read+;qXfer:memory-map:read+";
// TODO: Anything else? // TODO: Anything else?
const ret = try std.fmt.allocPrint(allocator, format, .{Packet.size}); const ret = try std.fmt.allocPrint(allocator, format, .{Packet.max_len});
return .{ .alloc = ret }; return .{ .alloc = ret };
} }