how to use bufzilla?

This commit is contained in:
Vicente Ferrari Smith 2026-02-03 17:49:10 +01:00
parent fb238a1966
commit bab331083e
5 changed files with 96 additions and 11 deletions

6
.vscode/launch.json vendored
View File

@ -13,7 +13,8 @@
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"internalConsoleOptions": "openOnSessionStart", "internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": "build", "preLaunchTask": "build",
"sourceLanguages": ["zig"] "sourceLanguages": ["zig"],
"terminal":"external",
}, },
{ {
"name": "Server", "name": "Server",
@ -24,7 +25,8 @@
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"internalConsoleOptions": "openOnSessionStart", "internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": "build", "preLaunchTask": "build",
"sourceLanguages": ["zig"] "sourceLanguages": ["zig"],
"terminal":"external",
} }
] ]
} }

14
.vscode/tasks.json vendored
View File

@ -6,7 +6,19 @@
{ {
"label": "build", "label": "build",
"type": "shell", "type": "shell",
"command": "zig build", "command": "zig",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"problemMatcher": [], "problemMatcher": [],
"group": { "group": {
"kind": "build", "kind": "build",

View File

@ -141,11 +141,7 @@ pub fn main() !void {
event.peer.*.data = @constCast(@ptrCast("Client information")); event.peer.*.data = @constCast(@ptrCast("Client information"));
}, },
enet.ENET_EVENT_TYPE_RECEIVE => { enet.ENET_EVENT_TYPE_RECEIVE => {
std.log.info("A packet of length {d} containing {s} was received from {s} on channel {d}.", .{ try on_packet(event.packet, event.peer, event.channelID);
event.packet.*.dataLength,
event.packet.*.data,
@as([*:0]const u8, @ptrCast(event.peer.*.data.?)),
event.channelID});
enet.enet_packet_destroy(event.packet); 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 { fn connect() !void {
// const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT); // const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);

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, .{}); var inspector = bufzilla.Inspect(.{}).init(fixed.buffered(), &fixed2, .{});
try inspector.inspect(); 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 data = fixed.buffered();
const packet = enet.enet_packet_create(data.ptr, data.len + 1, enet.ENET_PACKET_FLAG_RELIABLE); const packet = enet.enet_packet_create(data.ptr, data.len + 1, enet.ENET_PACKET_FLAG_RELIABLE);

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const zm = @import("zmath"); const zm = @import("zmath");
const enet = @import("c.zig").enet; const enet = @import("c.zig").enet;
const bufzilla = @import("bufzilla");
const shared = @import("shared"); const shared = @import("shared");
@ -77,7 +78,7 @@ pub fn main() !void {
while (enet.enet_host_service(host, &event, 0) > 0) { while (enet.enet_host_service(host, &event, 0) > 0) {
switch (event.type) { switch (event.type) {
enet.ENET_EVENT_TYPE_CONNECT => { enet.ENET_EVENT_TYPE_CONNECT => {
std.log.info("connect", .{}); try on_connect(allocator, host, event.peer, &the_chunk);
}, },
enet.ENET_EVENT_TYPE_RECEIVE => { enet.ENET_EVENT_TYPE_RECEIVE => {
std.log.info("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 {} //fn handle_connection(connection: std.net.Server.Connection) !void {}
// test "simple test" { // test "simple test" {