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,103 +31,136 @@ 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) {
nothing,
send: []const u8,
retry,
ack,
nack,
};
pub fn run(self: *Self, allocator: Allocator) !void {
var buf: [Packet.max_len]u8 = undefined;
while (true) {
const len = try self.client.receive(&buf);
if (len == 0) break;
const action = try Self.parse(allocator, buf[0..len]);
try self.send(allocator, action);
} }
} }
}
const Action = union(enum) { fn parse(allocator: Allocator, input: []const u8) !Action {
send: []const u8, return switch (input[0]) {
recv_nack, '+' => .nothing,
recv_ack, '-' => .retry,
'$' => blk: {
// Packet
var packet = Packet.from(allocator, input) catch return .nack;
defer packet.deinit(allocator);
var string = packet.parse(allocator) catch return .nack;
defer string.deinit(allocator);
const reply = string.inner();
// deallocated by the caller
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
break :blk .{ .send = response };
},
else => std.debug.panic("Unknown: {s}", .{input}),
};
}
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;
}
}; };
fn parse(allocator: Allocator, client: Socket, previous: ?[]const u8, input: []const u8) !Action {
const log = std.log.scoped(.GdbStubParser);
return switch (input[0]) {
'+' => .recv_ack,
'-' => blk: {
if (previous) |packet| {
log.warn("received nack, resending: \"{s}\"", .{packet});
_ = try client.send(packet);
} else {
log.err("received nack, but we don't recall sending anything", .{});
}
break :blk .recv_nack;
},
'$' => blk: {
// Packet
var packet = try Packet.from(allocator, input);
defer packet.deinit(allocator);
var string = try packet.parse(allocator);
defer string.deinit(allocator);
const reply = string.inner();
_ = try client.send("+"); // Acknowledge
// deallocated by the caller
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
_ = try client.send(response);
break :blk .{ .send = response };
},
else => std.debug.panic("Unknown: {s}", .{input}),
};
}
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 };
} }