added client server shared

This commit is contained in:
vfs committed 2025-12-21 23:02:59 +01:00
1 parent 6f9de0f566
commit 7d60843598
10 files changed
+482

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
const std = @import("std");
const zm = @import("zmath");
const shared = @import("shared");
var stdout: *std.io.Writer = undefined;
pub fn main() !void {
std.log.info("Helllo Server!", .{});
var dbg_allocator = std.heap.DebugAllocator(.{}).init;
defer _ = dbg_allocator.deinit();
const allocator = dbg_allocator.allocator();
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
stdout = &stdout_writer.interface;
std.log.info("{s}", .{@typeName(shared.chunk.Chunk)});
try stdout.flush();
const address = try std.net.Address.parseIp4("127.0.0.1", shared.protocol.SERVER_PORT);
var server = try address.listen(.{});
defer server.deinit();
var the_chunk = try shared.chunk.initChunk(allocator);
defer shared.chunk.deinitChunk(&the_chunk, allocator);
shared.chunk.spawn(&the_chunk, shared.entity.Player, allocator, .{
.pos = zm.f32x4(1, 1, 0, 0),
.hp = 10,
});
shared.chunk.spawn(&the_chunk, shared.entity.Monster, allocator, .{
.pos = zm.f32x4(1, 1, 0, 0),
.hp = 20,
});
shared.chunk.spawn(&the_chunk, shared.entity.Projectile, allocator, .{
.pos = zm.f32x4(0, 0, 0, 0),
.vel = zm.f32x4(0.2, 0, 0, 0),
});
shared.chunk.updateChunk(&the_chunk);
while (true) {
const connection = try server.accept();
defer connection.stream.close();
std.log.info("Client connected", .{});
var recv_buffer: [4096]u8 = undefined;
var send_buffer: [4096]u8 = undefined;
var reader = connection.stream.reader(&recv_buffer);
var writer = connection.stream.writer(&send_buffer);
const line = try shared.protocol.recvLine(reader.interface());
std.log.info("Received: {s}", .{line});
try shared.protocol.sendHello(&writer.interface, .{ .msg = "Hello from server!" });
}
}
//fn handle_connection(connection: std.net.Server.Connection) !void {}
// test "simple test" {
// const gpa = std.testing.allocator;
// var list: std.ArrayList(i32) = .empty;
// defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
// try list.append(gpa, 42);
// try std.testing.expectEqual(@as(i32, 42), list.pop());
// }
// test "fuzz example" {
// const Context = struct {
// fn testOne(context: @This(), input: []const u8) anyerror!void {
// _ = context;
// // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
// try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
// }
// };
// try std.testing.fuzz(Context{}, Context.testOne, .{});
// }