2022-02-05 13:16:44 +01:00
|
|
|
const std = @import("std");
|
2022-12-23 00:39:12 +01:00
|
|
|
const hexdump = @import("./hexdump.zig");
|
2022-02-05 13:16:44 +01:00
|
|
|
const testing = std.testing;
|
2022-02-07 18:30:06 +01:00
|
|
|
const net = std.net;
|
2022-05-07 13:13:40 +02:00
|
|
|
const fmt = std.fmt;
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
// TODO: file descriptors should have a specific type (but i32 is used in std.net...).
|
2022-02-06 18:11:01 +01:00
|
|
|
|
|
|
|
// TODO: path => std.XXX.YYY, not simple [] const u8
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
// TODO: both Connection and pollfd store file descriptors.
|
|
|
|
// Connection stores either Stream (server) or Address (client).
|
2022-02-08 05:59:18 +01:00
|
|
|
|
|
|
|
// TODO: API should completely obfuscate the inner structures.
|
|
|
|
// Only structures in this file should be necessary.
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-12-23 01:53:07 +01:00
|
|
|
const CBEvent = @import("./callback.zig").CBEvent;
|
|
|
|
const Connection = @import("./connection.zig").Connection;
|
|
|
|
const Message = @import("./message.zig").Message;
|
|
|
|
const Event = @import("./event.zig").Event;
|
|
|
|
const Switch = @import("./switch.zig").Switch;
|
|
|
|
const print_eq = @import("./util.zig").print_eq;
|
|
|
|
|
2022-02-05 13:16:44 +01:00
|
|
|
const print = std.debug.print;
|
|
|
|
|
2022-12-23 01:53:07 +01:00
|
|
|
const Messages = @import("./message.zig").Messages;
|
|
|
|
const Switches = @import("./switch.zig").Switches;
|
|
|
|
const Connections = @import("./connection.zig").Connections;
|
|
|
|
const Context = @import("./context.zig").Context;
|
2022-05-07 13:13:40 +02:00
|
|
|
pub const PollFD = std.ArrayList(i32);
|
|
|
|
|
2022-12-23 01:53:07 +01:00
|
|
|
test {
|
|
|
|
_ = @import("./callback.zig");
|
|
|
|
_ = @import("./connection.zig");
|
|
|
|
_ = @import("./context.zig");
|
|
|
|
_ = @import("./event.zig");
|
|
|
|
_ = @import("./message.zig");
|
|
|
|
_ = @import("./switch.zig");
|
|
|
|
_ = @import("./util.zig");
|
2022-02-06 16:57:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
// FIRST
|
|
|
|
fn create_service() !void {
|
|
|
|
const config = .{.safety = true};
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(config){};
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
|
|
|
|
const allocator = gpa.allocator();
|
2022-05-08 02:33:02 +02:00
|
|
|
var ctx = try Context.init(allocator);
|
2022-02-08 05:59:18 +01:00
|
|
|
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
|
|
|
|
|
|
|
|
const path = "/tmp/.TEST_USOCK";
|
|
|
|
|
|
|
|
// SERVER SIDE: creating a service.
|
2022-05-01 00:42:17 +02:00
|
|
|
_ = try ctx.server_init(path);
|
2022-12-23 01:53:07 +01:00
|
|
|
var some_event = try ctx.wait_event();
|
|
|
|
switch (some_event.t) {
|
2022-05-01 00:42:17 +02:00
|
|
|
.CONNECTION => {
|
|
|
|
print("New connection!\n", .{});
|
|
|
|
},
|
|
|
|
else => {
|
2022-12-23 01:53:07 +01:00
|
|
|
print("New event: {}\n", .{some_event.t});
|
2022-05-01 00:42:17 +02:00
|
|
|
},
|
|
|
|
}
|
2022-02-08 05:59:18 +01:00
|
|
|
|
|
|
|
// Server.accept returns a net.Connection (handle = fd, addr = net.Address).
|
2022-05-01 00:42:17 +02:00
|
|
|
// var client = try server.accept();
|
|
|
|
// var buf: [4096]u8 = undefined;
|
|
|
|
// const n = try ctx.read_ (client, &buf);
|
2022-02-08 05:59:18 +01:00
|
|
|
|
2022-05-01 00:42:17 +02:00
|
|
|
// print("new client: {}\n", .{client});
|
|
|
|
// print("{} bytes: {s}\n", .{n, buf});
|
|
|
|
print("End the create_service function\n", .{});
|
2022-02-08 05:59:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !u8 {
|
|
|
|
try create_service();
|
2022-02-05 13:16:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// export fn add(a: i32, b: i32) i32 {
|
|
|
|
// return a + b;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// test "basic add functionality" {
|
|
|
|
// try testing.expect(add(3, 7) == 10);
|
|
|
|
// }
|