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

+77
View File
@@ -0,0 +1,77 @@
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 Client!", .{});
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 connection = try std.net.tcpConnectToAddress(address);
std.log.info("Connected to server", .{});
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);
var send_buf: [1024]u8 = undefined;
var writer = connection.writer(&send_buf);
try shared.protocol.sendHello(&writer.interface, .{ .msg = "Hello from client" });
var recv_buf: [1024]u8 = undefined;
var reader = connection.reader(&recv_buf);
const line = try shared.protocol.recvLine(reader.interface());
std.log.info("{s}", .{line});
}
//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, .{});
// }
+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, .{});
// }
+99
View File
@@ -0,0 +1,99 @@
const std = @import("std");
const entity = @import("entity.zig");
const misc = @import("misc.zig");
fn Storage(comptime T: type) type {
return struct {
items: std.ArrayList(T),
pub fn init(allocator: std.mem.Allocator) !@This() {
return .{
.items = try std.ArrayList(T).initCapacity(allocator, 32),
};
}
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
self.items.deinit(allocator);
}
pub fn update(self: *@This()) void {
for (self.items.items, 0..) |*item, i| {
_ = i;
if (@hasDecl(T, "update"))
item.update();
}
}
};
}
pub fn initChunk(allocator: std.mem.Allocator) !Chunk {
var chunk: Chunk = undefined;
switch (@typeInfo(Chunk)) {
.@"struct" => |s| {
inline for (s.fields) |field| {
const StorageT = field.type;
@field(chunk, field.name) = try StorageT.init(allocator);
}
},
else => unreachable,
}
return chunk;
}
pub fn deinitChunk(chunk: *Chunk, allocator: std.mem.Allocator) void {
switch (@typeInfo(Chunk)) {
.@"struct" => |s| {
inline for (s.fields) |field| {
@field(chunk, field.name).deinit(allocator);
}
},
else => unreachable,
}
}
fn _Chunk(comptime Types: anytype) type {
const FieldCount = Types.len;
var fields: [FieldCount]std.builtin.Type.StructField = undefined;
inline for (Types, 0..) |T, i| {
fields[i] = .{
.name = misc.short_type_name(T),
.type = Storage(T),
.default_value_ptr = null,
.is_comptime = false,
.alignment = @alignOf(Storage(T)),
};
}
return @Type(.{
.@"struct" = .{
.layout = .auto,
.fields = &fields,
.decls = &.{},
.is_tuple = false,
},
});
}
pub const Chunk = _Chunk(entity.EntityKinds);
pub fn updateChunk(chunk: *Chunk) void {
const info = @typeInfo(Chunk);
switch (info) {
.@"struct" => |s| {
inline for (s.fields) |field| {
@field(chunk, field.name).update();
}
},
else => unreachable,
}
}
pub fn spawn(chunk: anytype, comptime T: type, allocator: std.mem.Allocator, value: T) void {
std.log.info("hello!? {s}", .{@typeName(T)});
@field(chunk, misc.short_type_name(T)).items.append(allocator, value) catch unreachable;
}
+36
View File
@@ -0,0 +1,36 @@
const std = @import("std");
const zm = @import("zmath");
pub const EntityKinds = .{
Player,
Monster,
Projectile,
};
pub const Player = struct {
pos: zm.Vec,
hp: i32,
pub fn update(self: *Player) void {
std.log.info("pos=({})", .{self.pos});
}
};
pub const Monster = struct {
pos: zm.Vec,
hp: i32,
pub fn update(self: *Monster) void {
std.log.info("pos=({})", .{self.pos});
}
};
pub const Projectile = struct {
pos: zm.Vec,
vel: zm.Vec,
pub fn update(self: *Projectile) void {
self.pos = self.pos + self.vel;
std.log.info("pos=({})", .{self.pos});
}
};
+7
View File
@@ -0,0 +1,7 @@
const std = @import("std");
pub fn short_type_name(comptime T: type) [:0]const u8 {
const full = @typeName(T);
const base = full[std.mem.lastIndexOf(u8, full, ".").? + 1 ..];
return base ++ "\x00";
}
+15
View File
@@ -0,0 +1,15 @@
const std = @import("std");
pub const SERVER_PORT: u16 = 1337;
pub const Hello = struct {
msg: []const u8,
};
pub fn sendHello(writer: *std.io.Writer, hello: Hello) !void {
try writer.print("{s}\n", .{hello.msg});
}
pub fn recvLine(reader: *std.io.Reader) ![]u8 {
return try reader.takeDelimiterExclusive('\n');
}
+4
View File
@@ -0,0 +1,4 @@
pub const entity = @import("entity.zig");
pub const chunk = @import("chunk.zig");
pub const misc = @import("misc.zig");
pub const protocol = @import("protocol.zig");