mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
This commit is contained in:
@@ -77,7 +77,7 @@ pub const ClientState = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return true if the value was put in the clients buffer to process, else false.
|
/// Return true if the value was put in the clients buffer to process, else false.
|
||||||
pub fn send(self: *ClientState, io: std.Io, msg: Message) (std.Io.Cancelable||std.Io.QueueClosedError)!bool {
|
pub fn send(self: *ClientState, io: std.Io, msg: Message) (std.Io.Cancelable || std.Io.QueueClosedError)!bool {
|
||||||
try self.recv_queue.putOne(io, msg);
|
try self.recv_queue.putOne(io, msg);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -110,6 +110,16 @@ pub fn writeInfo(out: *std.Io.Writer, info: Message.ServerInfo) !void {
|
|||||||
|
|
||||||
fn writeMsg(out: *std.Io.Writer, msg: Message.Msg) !void {
|
fn writeMsg(out: *std.Io.Writer, msg: Message.Msg) !void {
|
||||||
std.debug.print("PRINTING MESSAGE\n\n\n\n", .{});
|
std.debug.print("PRINTING MESSAGE\n\n\n\n", .{});
|
||||||
|
std.debug.print(
|
||||||
|
"MSG {s} {s} {s} {d}\r\n{s}\r\n-\n\n\n",
|
||||||
|
.{
|
||||||
|
msg.subject,
|
||||||
|
msg.sid,
|
||||||
|
msg.reply_to orelse "",
|
||||||
|
msg.payload.len,
|
||||||
|
msg.payload,
|
||||||
|
},
|
||||||
|
);
|
||||||
try out.print(
|
try out.print(
|
||||||
"MSG {s} {s} {s} {d}\r\n{s}\r\n",
|
"MSG {s} {s} {s} {d}\r\n{s}\r\n",
|
||||||
.{
|
.{
|
||||||
@@ -135,6 +145,7 @@ test {
|
|||||||
var from_client_queue: std.Io.Queue(Message) = .init(&from_client_buf);
|
var from_client_queue: std.Io.Queue(Message) = .init(&from_client_buf);
|
||||||
|
|
||||||
{
|
{
|
||||||
|
// Simulate stream
|
||||||
while (Message.next(gpa, &from_client)) |msg| {
|
while (Message.next(gpa, &from_client)) |msg| {
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
.eos => {
|
.eos => {
|
||||||
@@ -145,7 +156,10 @@ test {
|
|||||||
try from_client_queue.putOne(io, msg);
|
try from_client_queue.putOne(io, msg);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else |_| {}
|
} else |err| switch (err) {
|
||||||
|
error.EndOfStream => try from_client_queue.close(io),
|
||||||
|
else => return err,
|
||||||
|
}
|
||||||
|
|
||||||
while (from_client_queue.getOne(io)) |msg| {
|
while (from_client_queue.getOne(io)) |msg| {
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
|
|||||||
@@ -10,7 +10,24 @@ clients: std.AutoHashMapUnmanaged(usize, *ClientState) = .empty,
|
|||||||
/// Map of subjects to a map of (client ID => SID)
|
/// Map of subjects to a map of (client ID => SID)
|
||||||
subscriptions: std.StringHashMapUnmanaged(std.AutoHashMapUnmanaged(usize, []const u8)) = .empty,
|
subscriptions: std.StringHashMapUnmanaged(std.AutoHashMapUnmanaged(usize, []const u8)) = .empty,
|
||||||
|
|
||||||
|
var keep_running = std.atomic.Value(bool).init(true);
|
||||||
|
|
||||||
|
fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void {
|
||||||
|
_ = sig;
|
||||||
|
keep_running.store(false, .monotonic);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
|
pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
|
||||||
|
// Configure the signal action
|
||||||
|
// const act = std.posix.Sigaction{
|
||||||
|
// .handler = .{ .handler = handleSigInt },
|
||||||
|
// .mask = std.posix.sigemptyset(),
|
||||||
|
// .flags = 0,
|
||||||
|
// };
|
||||||
|
|
||||||
|
// // Register the handler for SIGINT (Ctrl+C)
|
||||||
|
// std.posix.sigaction(std.posix.SIG.INT, &act, null);
|
||||||
|
|
||||||
var server: Server = .{
|
var server: Server = .{
|
||||||
.info = server_config,
|
.info = server_config,
|
||||||
};
|
};
|
||||||
@@ -26,7 +43,8 @@ pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
|
|||||||
defer tcp_server.deinit(io);
|
defer tcp_server.deinit(io);
|
||||||
|
|
||||||
var id: usize = 0;
|
var id: usize = 0;
|
||||||
while (true) : (id +%= 1) {
|
// Run until SIGINT is handled, then exit gracefully
|
||||||
|
while (keep_running.load(.monotonic)) : (id +%= 1) {
|
||||||
std.debug.print("in server loop\n", .{});
|
std.debug.print("in server loop\n", .{});
|
||||||
if (server.clients.contains(id)) continue;
|
if (server.clients.contains(id)) continue;
|
||||||
const stream = try tcp_server.accept(io);
|
const stream = try tcp_server.accept(io);
|
||||||
@@ -36,6 +54,8 @@ pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
|
|||||||
stream.close(io);
|
stream.close(io);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std.debug.print("Exiting gracefully\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn addClient(server: *Server, allocator: std.mem.Allocator, id: usize, client: *ClientState) !void {
|
fn addClient(server: *Server, allocator: std.mem.Allocator, id: usize, client: *ClientState) !void {
|
||||||
@@ -58,8 +78,8 @@ fn handleConnection(
|
|||||||
id: usize,
|
id: usize,
|
||||||
stream: std.Io.net.Stream,
|
stream: std.Io.net.Stream,
|
||||||
) !void {
|
) !void {
|
||||||
_ = server_allocator;
|
|
||||||
var client_allocator: std.heap.DebugAllocator(.{}) = .init;
|
var client_allocator: std.heap.DebugAllocator(.{}) = .init;
|
||||||
|
client_allocator.backing_allocator = server_allocator;
|
||||||
defer {
|
defer {
|
||||||
std.debug.print("deinitializing debug allocator\n", .{});
|
std.debug.print("deinitializing debug allocator\n", .{});
|
||||||
_ = client_allocator.deinit();
|
_ = client_allocator.deinit();
|
||||||
@@ -113,9 +133,6 @@ fn handleConnection(
|
|||||||
.unsub => |unsub| {
|
.unsub => |unsub| {
|
||||||
try server.unsubscribe(client_state.id, unsub);
|
try server.unsubscribe(client_state.id, unsub);
|
||||||
},
|
},
|
||||||
.eos => {
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
else => |e| {
|
else => |e| {
|
||||||
std.debug.panic("Unimplemented message: {any}\n", .{e});
|
std.debug.panic("Unimplemented message: {any}\n", .{e});
|
||||||
},
|
},
|
||||||
@@ -123,7 +140,10 @@ fn handleConnection(
|
|||||||
|
|
||||||
std.debug.print("processed message from client\n", .{});
|
std.debug.print("processed message from client\n", .{});
|
||||||
std.debug.print("awaiting next message from client\n", .{});
|
std.debug.print("awaiting next message from client\n", .{});
|
||||||
} else |_| {}
|
} else |err| {
|
||||||
|
// This is probably going to be normal on disconnect
|
||||||
|
std.debug.print("Ran into error in client process loop: {}\n", .{err});
|
||||||
|
}
|
||||||
|
|
||||||
// client_state.task.await(io);
|
// client_state.task.await(io);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ pub const MessageType = enum {
|
|||||||
pong,
|
pong,
|
||||||
@"+ok",
|
@"+ok",
|
||||||
@"-err",
|
@"-err",
|
||||||
eos,
|
|
||||||
|
|
||||||
fn parseMemEql(input: []const u8) ?MessageType {
|
fn parseMemEql(input: []const u8) ?MessageType {
|
||||||
// if (std.mem.eql(u8, "INFO", input)) return .info;
|
// if (std.mem.eql(u8, "INFO", input)) return .info;
|
||||||
@@ -45,9 +44,6 @@ pub const Message = union(MessageType) {
|
|||||||
pong,
|
pong,
|
||||||
@"+ok": void,
|
@"+ok": void,
|
||||||
@"-err": void,
|
@"-err": void,
|
||||||
// Not an actual NATS message, but used to signal end of stream was reached in the input,
|
|
||||||
// and we should close the reader.
|
|
||||||
eos: void,
|
|
||||||
pub const ServerInfo = struct {
|
pub const ServerInfo = struct {
|
||||||
/// The unique identifier of the NATS server.
|
/// The unique identifier of the NATS server.
|
||||||
server_id: []const u8,
|
server_id: []const u8,
|
||||||
@@ -176,14 +172,7 @@ pub const Message = union(MessageType) {
|
|||||||
try operation_string.appendBounded(byte);
|
try operation_string.appendBounded(byte);
|
||||||
try in.discardAll(1);
|
try in.discardAll(1);
|
||||||
} else break;
|
} else break;
|
||||||
} else |err| switch (err) {
|
} else |err| return err;
|
||||||
error.EndOfStream => {
|
|
||||||
return .{ .eos = {} };
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
return err;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const operation = parse(operation_string.items) orelse {
|
const operation = parse(operation_string.items) orelse {
|
||||||
return error.InvalidOperation;
|
return error.InvalidOperation;
|
||||||
|
|||||||
Reference in New Issue
Block a user