how to use bufzilla?

This commit is contained in:
vfs committed 2026-02-03 17:49:10 +01:00
1 parent fb238a1966
commit bab331083e
5 files changed
+96 -11

No files matched your search

+30 -5
View File
@@ -141,11 +141,7 @@ pub fn main() !void {
event.peer.*.data = @constCast(@ptrCast("Client information"));
},
enet.ENET_EVENT_TYPE_RECEIVE => {
std.log.info("A packet of length {d} containing {s} was received from {s} on channel {d}.", .{
event.packet.*.dataLength,
event.packet.*.data,
@as([*:0]const u8, @ptrCast(event.peer.*.data.?)),
event.channelID});
try on_packet(event.packet, event.peer, event.channelID);
enet.enet_packet_destroy(event.packet);
},
@@ -324,6 +320,35 @@ pub fn main() !void {
}
}
fn on_packet(packet: *enet.ENetPacket, peer: *enet.ENetPeer, channelID: i32) !void {
// std.log.info("A packet of length {d} containing {s} was received from {s} on channel {d}.", .{
// packet.*.dataLength,
// packet.*.data,
// @as([*:0]const u8, @ptrCast(peer.*.data.?)),
// channelID});
_ = peer;
_ = channelID;
const encoded: []const u8 = packet.*.data[0 .. packet.*.dataLength];
std.log.info("{d} bytes: {s}", .{encoded.len, encoded});
var reader = bufzilla.Reader(.{}).init(encoded);
// Read values sequentially
const val = try reader.read();
switch (val) {
.object => { },
.array => { },
.i32 => |n| std.debug.print("int: {d}\n", .{n}),
.i64 => |n| std.debug.print("int: {d}\n", .{n}),
.bytes => |s| std.debug.print("string: {s}\n", .{s}),
else => {}
}
}
fn connect() !void {
// const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);
+2 -2
View File
@@ -40,9 +40,9 @@ pub fn spawn(allocator: std.mem.Allocator, host: *enet.ENetHost, chunk: *shared.
var inspector = bufzilla.Inspect(.{}).init(fixed.buffered(), &fixed2, .{});
try inspector.inspect();
std.log.info("{}", .{msg});
// std.log.info("{}", .{msg});
std.debug.print("{s}\n", .{fixed2.buffered()});
// std.debug.print("{s}\n", .{fixed2.buffered()});
const data = fixed.buffered();
const packet = enet.enet_packet_create(data.ptr, data.len + 1, enet.ENET_PACKET_FLAG_RELIABLE);
+47 -1
View File
@@ -1,6 +1,7 @@
const std = @import("std");
const zm = @import("zmath");
const enet = @import("c.zig").enet;
const bufzilla = @import("bufzilla");
const shared = @import("shared");
@@ -77,7 +78,7 @@ pub fn main() !void {
while (enet.enet_host_service(host, &event, 0) > 0) {
switch (event.type) {
enet.ENET_EVENT_TYPE_CONNECT => {
std.log.info("connect", .{});
try on_connect(allocator, host, event.peer, &the_chunk);
},
enet.ENET_EVENT_TYPE_RECEIVE => {
std.log.info("receive", .{});
@@ -130,6 +131,51 @@ pub fn main() !void {
}
}
fn on_connect(allocator: std.mem.Allocator, host: *enet.ENetHost, peer: *enet.ENetPeer, the_chunk: *shared.chunk.Chunk) !void {
var aw = try std.io.Writer.Allocating.initCapacity(allocator, 128);
defer aw.deinit();
var writer = bufzilla.Writer.init(&aw.writer);
const fields = @typeInfo(shared.chunk.Chunk).@"struct".fields;
inline for (fields) |field| {
const list = &@field(the_chunk, field.name);
const itemsType = @FieldType(field.type, "items");
const T = std.meta.Child(itemsType);
for (list.items) |entity| {
const msg = shared.protocol.makeSpawnMessage(T, entity);
std.log.info("{}", .{entity});
try writer.writeAny(msg);
}
}
const encoded = aw.written();
std.log.info("{d} bytes: {s}", .{encoded.len, encoded});
var buffer2: [4096]u8 = undefined;
var fixed2 = std.io.Writer.fixed(&buffer2);
var inspector = bufzilla.Inspect(.{}).init(encoded, &fixed2, .{});
try inspector.inspect();
std.log.info("{s}\n", .{fixed2.buffered()});
const packet = enet.enet_packet_create(encoded.ptr, encoded.len, enet.ENET_PACKET_FLAG_RELIABLE);
// enet.enet_host_broadcast(host, 0, packet);
_ = host;
if (enet.enet_peer_send(peer, 0, packet) != 0) {
std.log.err("Could not send packet to peer.", .{});
}
}
//fn handle_connection(connection: std.net.Server.Connection) !void {}
// test "simple test" {