Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/zig-impl/src/main.zig

518 lines
16 KiB
Zig
Raw Normal View History

2022-02-05 13:16:44 +01:00
const std = @import("std");
const testing = std.testing;
const net = std.net;
2022-02-05 13:16:44 +01:00
2022-02-06 18:11:01 +01:00
// TODO: file descriptors should have a specific type
// (however, usize is pointer size).
// TODO: path => std.XXX.YYY, not simple [] const u8
2022-02-05 13:16:44 +01:00
pub const RUNDIR = "/run/ipc/";
pub const IPC_HEADER_SIZE = 6;
pub const IPC_BASE_SIZE = 2000000; // 2 MB, plenty enough space for messages
pub const IPC_MAX_MESSAGE_SIZE = IPC_BASE_SIZE-IPC_HEADER_SIZE;
pub const IPC_VERSION = 4;
const print = std.debug.print;
pub const IPC_TYPE = enum {
UNIX_SOCKETS
};
pub const MessageType = enum {
SERVER_CLOSE,
ERR,
DATA,
NETWORK_LOOKUP,
};
pub const Message = struct {
2022-02-06 00:32:16 +01:00
@"type": MessageType, // Internal message type.
user_type: u8, // User-defined message type (arbitrary).
fd: usize, // File descriptor concerned about this message.
2022-02-05 13:16:44 +01:00
payload: []const u8,
const Self = @This();
pub fn init(fd: usize,
@"type": MessageType,
user_type: u8,
payload: []const u8) Self {
return Message {
.fd = fd,
.@"type" = @"type",
.user_type = user_type,
.payload = payload,
};
}
2022-02-06 00:32:16 +01:00
pub fn format(
self: Self,
comptime _: []const u8, // No need.
_: std.fmt.FormatOptions, // No need.
out_stream: anytype,
) !void {
try std.fmt.format(out_stream, "fd: {}, type {}, usertype {}, payload: {s}",
.{self.fd, self.@"type", self.user_type, self.payload} );
}
2022-02-05 13:16:44 +01:00
// del == list.swapRemove(index)
};
test "Message - creation and display" {
print("\n", .{});
2022-02-05 13:16:44 +01:00
// fd type usertype payload
2022-02-06 00:32:16 +01:00
var s = "hello!!";
2022-02-05 13:16:44 +01:00
var m = Message.init(1, MessageType.DATA, 3, s);
2022-02-06 16:57:23 +01:00
print("message:\t[{}]\n", .{m});
print("\n", .{});
2022-02-05 13:16:44 +01:00
}
pub const Messages = std.ArrayList(Message);
// Event types.
// In the main event loop, servers and clients can receive connections,
// disconnections, errors or messages from their pairs. They also can
// set a timer so the loop will allow a periodic routine (sending ping
// messages for websockets, for instance).
//
// A few other events can occur.
//
// Extra socket
// The main loop waiting for an event can be used as an unique entry
// point for socket management. libipc users can register sockets via
// ipc_add_fd allowing them to trigger an event, so events unrelated
// to libipc are managed the same way.
// Switch
// libipc can be used to create protocol-related programs, such as a
// websocket proxy allowing libipc services to be accessible online.
// To help those programs (with TCP-complient sockets), two sockets
// can be bound together, each message coming from one end will be
// automatically transfered to the other socket and a Switch event
// will be triggered.
// Look Up
// When a client establishes a connection to a service, it asks the
// ipc daemon (ipcd) to locate the service and establish a connection
// to it. This is a lookup.
pub const EventType = enum {
2022-02-06 00:32:16 +01:00
NOT_SET, // Default. TODO: should we keep this?
ERROR, // A problem occured.
2022-02-05 13:16:44 +01:00
EXTRA_SOCKET, // Message received from a non IPC socket.
SWITCH, // Message to send to a corresponding fd.
CONNECTION, // New user.
DISCONNECTION, // User disconnected.
MESSAGE, // New message.
LOOKUP, // Client asking for a service through ipcd.
TIMER, // Timeout in the poll(2) function.
TX, // Message sent.
};
2022-02-06 00:32:16 +01:00
// For IO callbacks (switching).
pub const EventCallBack = enum {
NO_ERROR, // No error. A message was generated.
FD_CLOSING, // The fd is closing.
FD_ERROR, // Generic error.
PARSING_ERROR, // The message was read but with errors.
IGNORE, // The message should be ignored (protocol specific).
};
2022-02-05 13:16:44 +01:00
2022-02-06 00:32:16 +01:00
pub const Event = struct {
2022-02-05 13:16:44 +01:00
@"type": EventType,
index: u32,
origin: usize,
m: ?*Message, // message pointer
const Self = @This();
pub fn init(@"type": EventType,
index: u32,
origin: usize,
m: ?*Message) Self {
return Self {
.@"type" = @"type",
.index = index,
.origin = origin,
.m = m,
};
}
pub fn set(self: *Self,
@"type": EventType,
index: u32,
origin: usize,
m: ?*Message) void {
self.@"type" = @"type";
self.index = index;
self.origin = origin;
self.m = m;
}
pub fn clean(self: *Self) void {
self.@"type" = EventType.NOT_SET;
self.index = @as(u8,0);
self.origin = @as(usize,0);
if (self.m) |message| {
message.deinit();
}
self.m = null;
}
2022-02-06 16:57:23 +01:00
pub fn format(
self: Self,
comptime _: []const u8, // No need.
_: std.fmt.FormatOptions, // No need.
out_stream: anytype,
) !void {
try std.fmt.format(out_stream
, "type {}, origin: {}, index {}, message: [{}]"
, .{ self.@"type", self.origin, self.index, self.m} );
}
2022-02-05 13:16:44 +01:00
};
2022-02-06 16:57:23 +01:00
test "Event - creation and display" {
print("\n", .{});
2022-02-06 16:57:23 +01:00
var s = "hello!!";
// fd type usertype payload
var m = Message.init(1, MessageType.DATA, 3, s);
// type index origin message
var e = Event.init(EventType.CONNECTION, 5, 8, &m);
print("event:\t[{}]\n", .{e});
print("\n", .{});
2022-02-06 16:57:23 +01:00
}
2022-02-05 13:16:44 +01:00
2022-02-06 00:32:16 +01:00
pub const ConnectionType = enum {
IPC, // Standard connection.
2022-02-06 16:57:23 +01:00
EXTERNAL, // Non IPC connection (TCP, UDP, etc.).
2022-02-06 00:32:16 +01:00
SERVER, // Messages received = new connections.
SWITCHED, // IO operations should go through registered callbacks.
};
// RATIONALE: a connection is mostly a file descriptor,
// but with a few other pieces of information.
// Storing all data related to a connection in a single place
// would be logical but very inefficient.
// File descriptors are stored elsewhere (in the context),
// packed together, in a single dedicated structure.
pub const ConnectionInfos = struct {
2022-02-06 16:57:23 +01:00
@"type": ConnectionType,
2022-02-05 13:16:44 +01:00
more_to_read: bool,
2022-02-06 16:57:23 +01:00
path: ?[] const u8, // Not always needed.
// TODO: socket: ?net.Stream, // Not always needed.
2022-02-06 16:57:23 +01:00
const Self = @This();
pub fn init(@"type": ConnectionType, path: ?[] const u8) Self {
return Self {
.@"type" = @"type",
.more_to_read = false,
.path = path,
};
}
pub fn format(
self: Self,
comptime _: []const u8, // No need.
_: std.fmt.FormatOptions, // No need.
out_stream: anytype,
) !void {
try std.fmt.format(out_stream
, "connection type {}, more_to_read {}, path {s}"
, .{ self.@"type", self.more_to_read, self.path} );
}
2022-02-05 13:16:44 +01:00
};
test "ConnectionInfos - creation and display" {
print("\n", .{});
2022-02-06 16:57:23 +01:00
// origin destination
var path = "/some/path";
var c1 = ConnectionInfos.init(ConnectionType.EXTERNAL, path);
var c2 = ConnectionInfos.init(ConnectionType.IPC , null);
2022-02-06 16:57:23 +01:00
print("connection 1:\t[{}]\n", .{c1});
print("connection 2:\t[{}]\n", .{c2});
print("\n", .{});
2022-02-06 16:57:23 +01:00
}
// TODO: callbacks.
2022-02-05 13:16:44 +01:00
pub const Switch = struct {
2022-02-06 16:57:23 +01:00
origin : usize,
destination : usize,
2022-02-05 13:16:44 +01:00
// enum ipccb (*orig_in) (int origin_fd, struct ipc_message *m, short int *more_to_read);
// enum ipccb (*orig_out) (int origin_fd, struct ipc_message *m);
// enum ipccb (*dest_in) (int origin_fd, struct ipc_message *m, short int *more_to_read);
// enum ipccb (*dest_out) (int origin_fd, struct ipc_message *m);
2022-02-06 16:57:23 +01:00
const Self = @This();
pub fn init(origin: usize, destination: usize) Self {
return Self {
.origin = origin,
.destination = destination,
};
}
pub fn format(
self: Self,
comptime _: []const u8, // No need.
_: std.fmt.FormatOptions, // No need.
out_stream: anytype,
) !void {
try std.fmt.format(out_stream
, "switch {} <-> {}"
, .{ self.origin, self.destination} );
}
2022-02-05 13:16:44 +01:00
};
2022-02-06 16:57:23 +01:00
test "Switch - creation and display" {
// origin destination
var s = Switch.init(3,8);
print("switch:\t[{}]\n", .{s});
print("\n", .{});
2022-02-06 16:57:23 +01:00
}
2022-02-05 13:16:44 +01:00
pub const Switches = std.ArrayList(Switch);
pub const Connections = std.ArrayList(ConnectionInfos);
pub const PollFD = std.ArrayList(i32);
2022-02-05 13:16:44 +01:00
// Context of the whole networking state.
pub const Context = struct {
2022-02-06 18:11:01 +01:00
allocator: std.mem.Allocator, // Memory allocator.
connections: Connections, // Keep track of connections.
// TODO: List of "pollfd" structures within cinfos,
// so we can pass it to poll(2). Share indexes with 'connections'.
// For now, this list doesn't do anything.
// Can even be replaced in a near future.
pollfd: PollFD, // File descriptors.
2022-02-06 18:11:01 +01:00
tx: Messages, // Messages to send, once their fd is available.
switchdb: ?Switches, // Relations between fd.
const Self = @This();
// Context initialization:
// - init structures (provide the allocator)
2022-02-06 18:11:01 +01:00
pub fn init(allocator: std.mem.Allocator) Self {
print("Context init\n", .{});
2022-02-06 18:11:01 +01:00
return Self {
.connections = Connections.init(allocator)
, .pollfd = PollFD.init(allocator)
2022-02-06 18:11:01 +01:00
, .tx = Messages.init(allocator)
, .switchdb = null
, .allocator = allocator
};
}
pub fn deinit(self: *Self) void {
print("connection deinit\n", .{});
self.close_all() catch |err| switch(err){
error.IndexOutOfBounds => {
print("context.deinit(): IndexOutOfBounds\n", .{});
},
};
self.connections.deinit();
self.pollfd.deinit();
self.tx.deinit();
if (self.switchdb) |sdb| { sdb.deinit(); }
}
fn connect_ (self: *Self, ctype: ConnectionType, path: []const u8) !i32 {
var stream = try net.connectUnixSocket(path);
const newfd = stream.handle;
errdefer std.os.closeSocket(newfd);
var newcon = ConnectionInfos.init(ctype, path);
2022-02-06 18:11:01 +01:00
try self.connections.append(newcon);
try self.pollfd.append(newfd);
return newfd;
}
// Return the new fd. Can be useful to the caller.
pub fn connect(self: *Self, path: []const u8) !i32 {
print("connection to:\t{s}\n", .{path});
return self.connect_ (ConnectionType.IPC, path);
}
// Connection to a service, but with switched with the client fd.
pub fn connection_switched(self: *Self
, path: [] const u8
, clientfd: i32) !i32 {
print("connection switched from {} to path {s}\n", .{clientfd, path});
var newfd = try self.connect_ (ConnectionType.SWITCHED, path);
// TODO: record switch.
return newfd;
}
// Create a unix socket.
pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer {
print("context server init {s}\n", .{path});
var server = net.StreamServer.init(.{});
var socket_addr = try net.Address.initUnix(path);
try server.listen(socket_addr);
const newfd = server.sockfd orelse return error.SocketLOL;
var newcon = ConnectionInfos.init(ConnectionType.SERVER, path);
try self.connections.append(newcon);
try self.pollfd.append(newfd);
return server;
}
/// ipc_write (ctx *, const struct ipc_message *m);
pub fn write (self: *Self, m: Message) !void {
print("write fd {}\n", .{m.fd});
self.tx.append(m);
}
pub fn read (self: *Self, index: u32) !Message {
// TODO: read the actual content.
if (index >= self.pollfd.items.len) {
return error.IndexOutOfBounds;
}
var fd = self.pollfd[index];
print("read fd {} index {}\n", .{fd, index});
var payload = "hello!!";
// fd type usertype payload
var m = Message.init(0, MessageType.DATA, 1, payload);
return m;
}
pub fn read_fd (_: *Self, fd: i32) !Message {
// TODO: read the actual content.
print("read fd {}\n", .{fd});
var payload = "hello!!";
// fd type usertype payload
var m = Message.init(0, MessageType.DATA, 1, payload);
return m;
}
// Wait an event.
pub fn wait_event(self: *Self, timer: *i32) !Event {
for (self.pollfd.items) |fd| {
print("listening to fd {}\n", .{fd});
}
print("listening for MAXIMUM {} us\n", .{timer});
// TODO: listening to these file descriptors.
var event = Event.init(EventType.CONNECTION, 5, 8, null);
return event;
2022-02-06 18:11:01 +01:00
}
pub fn close(self: *Self, index: usize) !void {
if (index >= self.pollfd.items.len) {
return error.IndexOutOfBounds;
}
// close the connection and remove it from the two structures
if (self.connections.items.len > 0) {
// TODO: actually close the file descriptor.
_ = self.connections.swapRemove(index);
_ = self.pollfd.swapRemove(index);
}
}
pub fn close_all(self: *Self) !void {
while(self.connections.items.len > 0) { try self.close(0); }
2022-02-07 01:21:46 +01:00
}
2022-02-06 18:11:01 +01:00
pub fn format(
self: Self,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
out_stream: anytype,
) !void {
try std.fmt.format(out_stream
, "context ({} connections and {} messages):"
, .{self.connections.items.len, self.tx.items.len});
2022-02-05 13:16:44 +01:00
2022-02-06 18:11:01 +01:00
for (self.connections.items) |con| {
2022-02-07 01:21:46 +01:00
try std.fmt.format(out_stream, "\n- ", .{});
2022-02-06 18:11:01 +01:00
try con.format(fmt, options, out_stream);
}
2022-02-05 13:16:44 +01:00
2022-02-06 18:11:01 +01:00
for (self.tx.items) |tx| {
2022-02-07 01:21:46 +01:00
try std.fmt.format(out_stream, "\n- ", .{});
2022-02-06 18:11:01 +01:00
try tx.format(fmt, options, out_stream);
}
}
2022-02-05 13:16:44 +01:00
};
2022-02-06 18:11:01 +01:00
// TODO
test "Context - creation, display and memory check" {
print("\n", .{});
2022-02-06 18:11:01 +01:00
// origin destination
//var s = Switch.init(3,8);
const config = .{.safety = true};
var gpa = std.heap.GeneralPurposeAllocator(config){};
2022-02-07 01:21:46 +01:00
defer _ = gpa.deinit();
2022-02-06 18:11:01 +01:00
const allocator = gpa.allocator();
// var payload = "hello!!";
// // fd type usertype payload
// var m = Message.init(0, MessageType.DATA, 1, payload);
//
// // type index origin message
// var e = Event.init(EventType.CONNECTION, 5, 8, &m);
var c = Context.init(allocator);
2022-02-07 01:21:46 +01:00
defer c.deinit(); // There. Can't leak. Isn't Zig wonderful?
2022-02-06 18:11:01 +01:00
const path = "/tmp/.TEST_USOCK";
// SERVER SIDE: creating a service.
var server = try c.server_init(path);
defer server.deinit();
defer std.fs.cwd().deleteFile(path) catch {}; // Once done, remove file.
2022-02-06 18:11:01 +01:00
// CLIENT SIDE: connection to a service.
//_ = try c.connect(path);
// TODO: connection to a server, but switched with clientfd "3".
// _ = try c.connection_switched(path, 3);
// print ("Context: {}\n", .{c});
// print("\n", .{});
const S = struct {
fn clientFn() !void {
const socket = try net.connectUnixSocket(path);
defer socket.close();
print("So we're a client now... path: {s}\n", .{path});
_ = try socket.writer().writeAll("Hello world!");
}
};
const t = try std.Thread.spawn(.{}, S.clientFn, .{});
defer t.join();
// Server.accept returns a net.Connection (handle = fd, addr = net.Address).
var client = try server.accept();
defer client.stream.close();
var buf: [16]u8 = undefined;
const n = try client.stream.reader().read(&buf);
try testing.expectEqual(@as(usize, 12), n);
try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]);
2022-02-06 18:11:01 +01:00
}
2022-02-05 13:16:44 +01:00
pub fn main() u8 {
return 0;
}
// export fn add(a: i32, b: i32) i32 {
// return a + b;
// }
// test "basic add functionality" {
// try testing.expect(add(3, 7) == 10);
// }