1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
const base64_enc = std.base64.Base64Encoder.init(std.base64.standard_alphabet_chars, '=');
const base64_dec = std.base64.Base64Decoder.init(std.base64.standard_alphabet_chars, '=');
writer: *std.Io.Writer,
const Self = @This();
const max_message_size = 2048;
pub fn init(writer: *std.Io.Writer) !Self {
return .{
.writer = writer,
};
}
pub fn deinit(self: *Self) void {
self.writer.flush() catch {};
}
pub fn sendRelay(self: *Self, payload: []const u8, dest: [4]u8) !void {
const payload_len = base64_enc.calcSize(payload.len);
// Ensure the writer is in a valid state
std.debug.assert(self.writer.buffer.len - self.writer.end >= payload_len);
const headers_buf = try self.writer.writableSlice(@sizeOf(SaprusMessage) + @sizeOf(SaprusMessage.Relay));
const msg: *align(1) SaprusMessage = .init(.relay, headers_buf);
msg.length = @intCast(payload_len);
const relay = (try msg.getSaprusTypePayload()).relay;
relay.dest = dest;
try base64_enc.encodeWriter(self.writer, payload);
try msg.networkFromNativeEndian();
try self.writer.flush();
}
// pub fn sendInitialConnection(
// self: Self,
// payload: []const u8,
// output_bytes: []u8,
// initial_port: u16,
// ) !*align(1) SaprusMessage {
// const dest_port = self.randomPort();
// const msg_bytes = output_bytes[0..try SaprusMessage.calcSize(
// .connection,
// base64_enc.calcSize(payload.len),
// )];
// const msg: *align(1) SaprusMessage = .init(.connection, msg_bytes);
// const connection = (try msg.getSaprusTypePayload()).connection;
// connection.src_port = initial_port;
// connection.dest_port = dest_port;
// _ = base64_enc.encode(connection.getPayload(), payload);
// try broadcastSaprusMessage(msg_bytes, 8888);
// return msg;
// }
// pub fn connect(self: Self, payload: []const u8) !?SaprusConnection {
// const initial_port = self.randomPort();
// var initial_conn_res: ?*align(1) SaprusMessage = null;
// var sock = try network.Socket.create(.ipv4, .udp);
// defer sock.close();
// // Bind to 255.255.255.255:8888
// const bind_addr = network.EndPoint{
// .address = network.Address{ .ipv4 = network.Address.IPv4.broadcast },
// .port = 8888,
// };
// // timeout 1s
// try sock.setReadTimeout(1 * std.time.us_per_s);
// try sock.bind(bind_addr);
// var sent_msg_bytes: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
// const msg = try self.sendInitialConnection(payload, &sent_msg_bytes, initial_port);
// var response_buf: [max_message_size]u8 align(@alignOf(SaprusMessage)) = undefined;
// _ = try sock.receive(&response_buf); // Ignore message that I sent.
// const len = try sock.receive(&response_buf);
// initial_conn_res = try .networkBytesAsValue(response_buf[0..len]);
// // Complete handshake after awaiting response
// try broadcastSaprusMessage(msg.asBytes(), self.randomPort());
// if (false) {
// return initial_conn_res.?;
// }
// return null;
// }
const SaprusMessage = @import("message.zig").Message;
const SaprusConnection = @import("Connection.zig");
const std = @import("std");
const network = @import("network");
|