Cleaner code.
parent
85b042bec9
commit
0fe2712362
|
@ -72,6 +72,17 @@ pub fn hexdump(stream: anytype, header: []const u8, buffer: []const u8) std.posi
|
|||
try stream.writeAll("\n");
|
||||
}
|
||||
|
||||
/// Debug function: print some bytes' hexdump on standard output.
|
||||
pub fn print_hex(title: []const u8, buffer : []const u8) !void
|
||||
{
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
var hexbuf: [100_000]u8 = undefined;
|
||||
var hexfbs = std.io.fixedBufferStream(&hexbuf);
|
||||
const hexwriter = hexfbs.writer();
|
||||
try hexdump(hexwriter, title, buffer);
|
||||
try stdout.print("{s}\n", .{hexfbs.getWritten()});
|
||||
}
|
||||
|
||||
const print = std.debug.print;
|
||||
|
||||
test "36-byte hexdump test" {
|
||||
|
|
64
src/main.zig
64
src/main.zig
|
@ -1,5 +1,5 @@
|
|||
const std = @import("std");
|
||||
const hexdump = @import("./hexdump.zig");
|
||||
const hexdump = @import("./hexdump.zig").print_hex;
|
||||
const AutoArrayHashMap = std.AutoArrayHashMap;
|
||||
const allocator = std.mem.Allocator;
|
||||
const ipc = @cImport({
|
||||
|
@ -37,8 +37,9 @@ const ipc = @cImport({
|
|||
// TODO: handle connections and disconnections
|
||||
// TODO: handle messages (proxy)
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
var context : ?*anyopaque = undefined;
|
||||
var hash : AutoArrayHashMap(i32, i32) = undefined;
|
||||
|
||||
//pub fn in(origin : i32, payload : [*]u8, mlen : *u32) callconv(.C) u8
|
||||
//{
|
||||
|
@ -50,15 +51,13 @@ var hash : AutoArrayHashMap(i32, i32) = undefined;
|
|||
|
||||
pub fn connection_and_link(client_fd : i32) !void
|
||||
{
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
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;
|
||||
|
||||
const 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, proxied_service.len);
|
||||
if (ret != 0) {
|
||||
try stdout.print("Impossible to connect to the service {s}\n", .{proxied_service});
|
||||
return error.CannotConnectToService;
|
||||
|
@ -66,75 +65,40 @@ pub fn connection_and_link(client_fd : i32) !void
|
|||
errdefer _ = ipc.ipc_close_fd(context, proxied_service_socket_fd);
|
||||
|
||||
// Link the user to the proxied service.
|
||||
//ret = ipc.ipc_add_switch (context, client_fd, proxied_service_socket_fd);
|
||||
//if (ret != 0) {
|
||||
// try stdout.print("Impossible switch both client {} and service {s}\n", .{client_fd, proxied_service});
|
||||
// return error.CannotAddSwitch;
|
||||
//}
|
||||
ret = ipc.ipc_add_switch (context, client_fd, proxied_service_socket_fd);
|
||||
if (ret != 0) {
|
||||
try stdout.print("Impossible switch both client {} and service {s}\n", .{client_fd, proxied_service});
|
||||
return error.CannotAddSwitch;
|
||||
}
|
||||
errdefer _ = ipc.ipc_close_fd(context, client_fd);
|
||||
|
||||
try hash.put(client_fd, proxied_service_socket_fd);
|
||||
try hash.put(proxied_service_socket_fd, client_fd);
|
||||
|
||||
try stdout.print("Connection of a new user ({}): linked with '{s}' ({})\n",
|
||||
.{client_fd, proxied_service, proxied_service_socket_fd});
|
||||
}
|
||||
|
||||
pub fn disconnection_and_unlink(fd : i32) !void
|
||||
{
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("Disconnection of user ({})\n", .{fd});
|
||||
|
||||
const related_fd = hash.get(fd) orelse return error.noRelatedFD;
|
||||
_ = ipc.ipc_close_fd(context, related_fd);
|
||||
_ = hash.swapRemove(related_fd);
|
||||
_ = hash.swapRemove(fd);
|
||||
}
|
||||
|
||||
fn print_hex(buffer : [100_000]u8, buflen : u64) !void
|
||||
pub fn message_rx(_ : i32, buffer : [100_000]u8, buflen : u64) !void
|
||||
{
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
var hexbuf: [100_000]u8 = undefined;
|
||||
var hexfbs = std.io.fixedBufferStream(&hexbuf);
|
||||
const hexwriter = hexfbs.writer();
|
||||
try hexdump.hexdump(hexwriter, "Message received", buffer[0..buflen]);
|
||||
try stdout.print("{s}\n", .{hexfbs.getWritten()});
|
||||
}
|
||||
|
||||
pub fn message_rx(fd : i32, buffer : [100_000]u8, buflen : u64) !void
|
||||
{
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
// There is no need to "read a message", it's already done.
|
||||
// _ = ipc.ipc_read_fd (context, fd, &buffer, &buflen);
|
||||
|
||||
try stdout.print("Message received from {}, size: {}\n", .{fd, buflen});
|
||||
try print_hex(buffer, buflen);
|
||||
|
||||
// Search for the related fd.
|
||||
const related_fd = hash.get(fd) orelse return error.no_related_fd;
|
||||
|
||||
// Send a message to the related service or client.
|
||||
_ = ipc.ipc_schedule(context, related_fd, &buffer, @intCast(buflen));
|
||||
try stdout.print("Message scheduled for {}\n", .{fd});
|
||||
try hexdump("received message", buffer[0..buflen]);
|
||||
}
|
||||
|
||||
pub fn message_tx(fd : i32) !void
|
||||
{
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("Message sent to ({})\n", .{fd});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var service_socket_fd : i32 = 0;
|
||||
|
||||
const stdout_file = std.io.getStdOut().writer();
|
||||
var bw = std.io.bufferedWriter(stdout_file);
|
||||
const stdout = bw.writer();
|
||||
|
||||
// Initialization of the hashmap.
|
||||
hash = AutoArrayHashMap(i32, i32).init(std.heap.c_allocator);
|
||||
defer hash.deinit(); // Free the hashmap structure memory.
|
||||
// hash = AutoArrayHashMap(i32, i32).init(std.heap.c_allocator);
|
||||
// defer hash.deinit(); // Free the hashmap structure memory.
|
||||
|
||||
// Initialization of the proxy.
|
||||
switch(ipc.ipc_context_init(&context)) {
|
||||
|
@ -148,7 +112,6 @@ pub fn main() !void {
|
|||
else => try stdout.print("Problem while initializing the service\n", .{}),
|
||||
}
|
||||
defer { _ = ipc.ipc_close_all(context); } // Close all established connections.
|
||||
try bw.flush(); // don't forget to flush!
|
||||
|
||||
while(true) {
|
||||
var t : u8 = undefined; // event type
|
||||
|
@ -174,7 +137,6 @@ pub fn main() !void {
|
|||
ipc.SWITCH_TX => try stdout.print("SWITCH_TX\n", .{}), // Message sent to a switched fd.
|
||||
else => try stdout.print("ELSE CASE\n", .{}), // Should never happen.
|
||||
}
|
||||
try bw.flush(); // don't forget to flush!
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue