websocket: use Unix socket for internal server

And add an option for TCP only server

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-11-27 21:16:16 +01:00
parent 27b50c46c3
commit 8449d5ab22
3 changed files with 49 additions and 25 deletions

View File

@@ -23,20 +23,22 @@ const ws = @import("websocket");
const log = std.log.scoped(.handler);
pub const Stream = struct {
addr: std.net.Address,
socket: std.posix.socket_t = undefined,
ws_conn: *ws.Conn = undefined,
fn connectCDP(self: *Stream) !void {
const address = try std.net.Address.parseIp("127.0.0.1", 3245);
const flags: u32 = std.posix.SOCK.STREAM;
const proto = std.posix.IPPROTO.TCP;
const socket = try std.posix.socket(address.any.family, flags, proto);
const proto = blk: {
if (self.addr.any.family == std.posix.AF.UNIX) break :blk @as(u32, 0);
break :blk std.posix.IPPROTO.TCP;
};
const socket = try std.posix.socket(self.addr.any.family, flags, proto);
try std.posix.connect(
socket,
&address.any,
address.getOsSockLen(),
&self.addr.any,
self.addr.getOsSockLen(),
);
log.debug("connected to Stream server", .{});
self.socket = socket;