how to use bufzilla?
This commit is contained in:
parent
fb238a1966
commit
bab331083e
6
.vscode/launch.json
vendored
6
.vscode/launch.json
vendored
@ -13,7 +13,8 @@
|
||||
"cwd": "${workspaceFolder}",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"preLaunchTask": "build",
|
||||
"sourceLanguages": ["zig"]
|
||||
"sourceLanguages": ["zig"],
|
||||
"terminal":"external",
|
||||
},
|
||||
{
|
||||
"name": "Server",
|
||||
@ -24,7 +25,8 @@
|
||||
"cwd": "${workspaceFolder}",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"preLaunchTask": "build",
|
||||
"sourceLanguages": ["zig"]
|
||||
"sourceLanguages": ["zig"],
|
||||
"terminal":"external",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
14
.vscode/tasks.json
vendored
14
.vscode/tasks.json
vendored
@ -6,7 +6,19 @@
|
||||
{
|
||||
"label": "build",
|
||||
"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": [],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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" {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user