serialization getting better maybe??
This commit is contained in:
1 parent
da8589a700
commit
b686052636
7 files changed
+131
-88
No files matched your search
+25
-9
@@ -1,8 +1,13 @@
|
||||
const std = @import("std");
|
||||
const znet = @import("znet");
|
||||
const bufzilla = @import("bufzilla");
|
||||
|
||||
const shared = @import("shared");
|
||||
const server = @import("server.zig");
|
||||
|
||||
pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.Allocator, value: T, w: *std.Io.Writer) !void {
|
||||
pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.Allocator, value: T) !void {
|
||||
std.debug.assert(value.id == shared.entity.INVALID_ID);
|
||||
|
||||
const id = server.next_entity_id;
|
||||
server.next_entity_id += 1;
|
||||
|
||||
@@ -17,14 +22,25 @@ pub fn spawn(chunk: *shared.chunk.Chunk(), comptime T: type, allocator: std.mem.
|
||||
}
|
||||
|
||||
// serialize entity
|
||||
const messaged = entity.to_message();
|
||||
var buffer: [64]u8 = undefined;
|
||||
var fbs = std.io.fixedBufferStream(&buffer);
|
||||
try T.encode(entity, fbs.writer());
|
||||
var fixed = std.io.Writer.fixed(&buffer);
|
||||
var writer = bufzilla.Writer.init(&fixed);
|
||||
try writer.writeAny(messaged);
|
||||
|
||||
try shared.protocol.write_message(
|
||||
w,
|
||||
.spawn_entity,
|
||||
fbs.getWritten(),
|
||||
);
|
||||
try w.flush();
|
||||
const encoded = fixed.buffered();
|
||||
|
||||
std.log.info("\nsending the boy ( {s} )\n", .{encoded});
|
||||
|
||||
var iterator = server.host.iterPeers();
|
||||
|
||||
std.log.info("iterator length?? ({})", .{iterator.peers.len});
|
||||
|
||||
while (iterator.next()) |peer| {
|
||||
if (peer.state() == .connected) {
|
||||
std.log.info("peer?? ({})", .{peer});
|
||||
const packet = try znet.Packet.init(encoded, 0, .reliable);
|
||||
try peer.send(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -24,7 +24,7 @@ pub fn main() !void {
|
||||
try znet.init();
|
||||
defer znet.deinit();
|
||||
|
||||
const host = try znet.Host.init(.{
|
||||
server.host = try znet.Host.init(.{
|
||||
.addr = try .init(.{
|
||||
.ip = .any,
|
||||
.port = .{ .uint = 5000 },
|
||||
@@ -44,7 +44,7 @@ pub fn main() !void {
|
||||
defer shared.chunk.deinitChunk(&the_chunk, allocator);
|
||||
|
||||
while (true) {
|
||||
while (try host.service(500)) |event| switch (event) {
|
||||
while (try server.host.service(500)) |event| switch (event) {
|
||||
.connect => |data| {
|
||||
_ = data;
|
||||
// std.log.info("{}", .{data.peer});
|
||||
@@ -73,10 +73,10 @@ pub fn main() !void {
|
||||
// const w = &writer.interface;
|
||||
// try shared.protocol.sendHello(w, .{ .msg = "Hello from server!" });
|
||||
|
||||
// try chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{
|
||||
// .pos = zm.f32x4(1, 1, 0, 0),
|
||||
// .hp = 10,
|
||||
// }, w);
|
||||
try chunk.spawn(&the_chunk, shared.entity.Elf, allocator, .{
|
||||
.pos = zm.f32x4(1, 1, 0, 0),
|
||||
.hp = 10,
|
||||
});
|
||||
|
||||
// try chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
|
||||
// .pos = zm.f32x4(1, 1, 0, 0),
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
const znet = @import("znet");
|
||||
|
||||
const shared = @import("shared");
|
||||
|
||||
pub var next_entity_id: shared.entity.entity_id = 1;
|
||||
pub var host: znet.Host = undefined;
|
||||
|
||||
pub var next_entity_id: shared.entity.id = 1;
|
||||
Reference in new issue
Block a user