mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 03:34:48 +00:00
Implemented client and connection
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
socket: RawSocket,
|
||||
headers: EthIpUdp,
|
||||
connection: SaprusMessage,
|
||||
|
||||
const Connection = @This();
|
||||
|
||||
pub fn init(socket: RawSocket, headers: EthIpUdp, connection: SaprusMessage) Connection {
|
||||
return .{
|
||||
.socket = socket,
|
||||
.headers = headers,
|
||||
.connection = connection,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn next(self: Connection, io: Io, buf: []u8) ![]const u8 {
|
||||
_ = io;
|
||||
const res = try self.socket.receive(buf);
|
||||
const connection_res = blk: {
|
||||
const msg: SaprusMessage = try .parse(res[42..]);
|
||||
break :blk msg.connection;
|
||||
};
|
||||
|
||||
return connection_res.payload;
|
||||
}
|
||||
|
||||
pub fn send(self: *Connection, io: Io, buf: []const u8) !void {
|
||||
const rand = blk: {
|
||||
const io_source: std.Random.IoSource = .{ .io = io };
|
||||
break :blk io_source.interface();
|
||||
};
|
||||
|
||||
self.connection.connection.payload = buf;
|
||||
const connection_bytes = blk: {
|
||||
var connection_bytes: [2048]u8 = undefined;
|
||||
break :blk self.connection.toBytes(&connection_bytes);
|
||||
};
|
||||
|
||||
self.headers.setPayloadLen(connection_bytes.len);
|
||||
self.headers.ip.id = rand.int(u16);
|
||||
|
||||
const full_msg = blk: {
|
||||
var msg_buf: [2048]u8 = undefined;
|
||||
var msg_w: Writer = .fixed(&msg_buf);
|
||||
try msg_w.writeAll(&self.headers.toBytes());
|
||||
try msg_w.writeAll(connection_bytes);
|
||||
break :blk msg_w.buffered();
|
||||
};
|
||||
|
||||
try self.socket.send(full_msg);
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const Writer = std.Io.Writer;
|
||||
|
||||
const SaprusMessage = @import("./message.zig").Message;
|
||||
|
||||
const EthIpUdp = @import("./EthIpUdp.zig").EthIpUdp;
|
||||
const RawSocket = @import("./RawSocket.zig");
|
||||
|
||||
Reference in New Issue
Block a user