Switching: callbacks.
parent
0fe2712362
commit
1832065fa0
62
src/main.zig
62
src/main.zig
|
@ -27,37 +27,45 @@ const ipc = @cImport({
|
|||
// // Switch functions (for "protocol" services, such as TCPd).
|
||||
// int ipc_add_external (void* ctx, int newfd);
|
||||
//
|
||||
// // Returned "char" is a cb_event_types enum.
|
||||
// int ipc_set_switch_callbacks (void* ctx, int fd
|
||||
// , char (*in (int orig, const char *payload, uint32_t *mlen))
|
||||
// , char (*out(int dest, char *payload, uint32_t mlen)));
|
||||
|
||||
// TODO: noreturn error function: print a error message then crash.
|
||||
// TODO: connection to the proxied service
|
||||
// TODO: handle connections and disconnections
|
||||
// TODO: handle messages (proxy)
|
||||
|
||||
// TODO: find why "in" and "out" functions are detected as
|
||||
// returning "[*c]u8" while they should be returning u8.
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
var context : ?*anyopaque = undefined;
|
||||
|
||||
//pub fn in(origin : i32, payload : [*]u8, mlen : *u32) callconv(.C) u8
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//pub fn out(dest_fd : i32, , payload : [*]u8, mlen : u32) callconv(.C) u8
|
||||
//{
|
||||
//}
|
||||
// char (*in (int orig, const char *payload, uint32_t *mlen))
|
||||
pub fn in(origin : c_int, _ : [*c]const u8, _ : [*c]u32) callconv(.C) [*c]u8 // why this return type?!
|
||||
{
|
||||
stdout.print("we are IN origin is {}\n", .{origin}) catch {};
|
||||
return 1;
|
||||
}
|
||||
|
||||
// char (*out(int dest, char *payload, uint32_t mlen)));
|
||||
pub fn out(dest_fd : c_int, _ : [*c]u8, _ : u32) callconv(.C) [*c]u8 // why this return type?!
|
||||
{
|
||||
stdout.print("we are OUT target is {}\n", .{dest_fd}) catch {};
|
||||
return 1;
|
||||
}
|
||||
|
||||
var proxy_name : [:0]const u8 = "proxy";
|
||||
var proxied_service : [:0]const u8 = "pong";
|
||||
|
||||
pub fn connection_and_link(client_fd : i32) !void
|
||||
{
|
||||
try stdout.print("Connection of a new user ({})\n", .{client_fd});
|
||||
|
||||
// Connection to the proxied service.
|
||||
const proxied_service = "pong";
|
||||
var proxied_service_socket_fd : i32 = 0;
|
||||
|
||||
var ret = ipc.ipc_connect_service (context, &proxied_service_socket_fd, proxied_service, proxied_service.len);
|
||||
var ret = ipc.ipc_connect_service (context
|
||||
, &proxied_service_socket_fd
|
||||
, proxied_service
|
||||
, @intCast(proxied_service.len));
|
||||
if (ret != 0) {
|
||||
try stdout.print("Impossible to connect to the service {s}\n", .{proxied_service});
|
||||
return error.CannotConnectToService;
|
||||
|
@ -72,6 +80,14 @@ pub fn connection_and_link(client_fd : i32) !void
|
|||
}
|
||||
errdefer _ = ipc.ipc_close_fd(context, client_fd);
|
||||
|
||||
// Returned "char" is a cb_event_types enum.
|
||||
try stdout.print("Hello we will put some callbacks\n", .{});
|
||||
ret = ipc.ipc_set_switch_callbacks (context, client_fd, in, out);
|
||||
if (ret != 0) {
|
||||
try stdout.print("Impossible to set the callbacks {} -> {s}\n", .{client_fd, proxied_service});
|
||||
return error.CannotAddCallbacks;
|
||||
}
|
||||
|
||||
try stdout.print("Connection of a new user ({}): linked with '{s}' ({})\n",
|
||||
.{client_fd, proxied_service, proxied_service_socket_fd});
|
||||
}
|
||||
|
@ -94,11 +110,17 @@ pub fn message_tx(fd : i32) !void
|
|||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var service_socket_fd : i32 = 0;
|
||||
const args = try std.process.argsAlloc(std.heap.page_allocator);
|
||||
if (args.len != 3) {
|
||||
try stdout.print("usage: {s} proxy-name target-service\n", .{args[0]});
|
||||
try stdout.print("example: {s} mypongproxy pong\n", .{args[0]});
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialization of the hashmap.
|
||||
// hash = AutoArrayHashMap(i32, i32).init(std.heap.c_allocator);
|
||||
// defer hash.deinit(); // Free the hashmap structure memory.
|
||||
proxy_name = args[1];
|
||||
proxied_service = args[2];
|
||||
|
||||
var service_socket_fd : i32 = 0;
|
||||
|
||||
// Initialization of the proxy.
|
||||
switch(ipc.ipc_context_init(&context)) {
|
||||
|
@ -107,7 +129,7 @@ pub fn main() !void {
|
|||
}
|
||||
defer { _ = ipc.ipc_context_deinit(&context); } // Free the context structure memory.
|
||||
|
||||
switch(ipc.ipc_service_init(context, &service_socket_fd, "proxy", 5)) {
|
||||
switch(ipc.ipc_service_init(context, &service_socket_fd, proxy_name, @intCast(proxy_name.len))) {
|
||||
0 => try stdout.print("Service initialized\n", .{}),
|
||||
else => try stdout.print("Problem while initializing the service\n", .{}),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue