networking- can send a vector over reliable
This commit is contained in:
parent
76fd816c2c
commit
4d06d89135
|
|
@ -28,6 +28,8 @@ const ENetLinkData = struct {
|
|||
// also maybe move to the upper level net.Link instead of LinkData
|
||||
queuedMessages: core.RingQueueU(QueuedLinkData),
|
||||
|
||||
testLinkPosition: core.Vectorf = .{},
|
||||
|
||||
pub fn destroy(self: *@This(), allocator: std.mem.Allocator) void {
|
||||
self.queuedMessages.deinit();
|
||||
allocator.destroy(self);
|
||||
|
|
@ -41,9 +43,25 @@ const ENetLinkData = struct {
|
|||
}) catch unreachable;
|
||||
}
|
||||
|
||||
pub fn readDebugVector(self: *@This(), data: []const u8) void {
|
||||
self.testLinkPosition = @as(*const core.Vectorf, @ptrCast(@alignCast(data.ptr))).*;
|
||||
//core.engine_log("reading vector position: {d} {d} {d}", self.testLinkPosition);
|
||||
}
|
||||
|
||||
pub fn pushDebugLoc(self: *@This()) void {
|
||||
// const x = self.testLinkPosition;
|
||||
// core.engine_log("sending vector position: {d} {d} {d}", x);
|
||||
|
||||
const allocator = net.netAllocator();
|
||||
self.queuedMessages.push(.{
|
||||
.bytes = allocator.dupe(u8, &@as([@sizeOf(core.Vectorf)]u8, @bitCast(self.testLinkPosition))) catch unreachable,
|
||||
.reliable = true,
|
||||
}) catch unreachable;
|
||||
}
|
||||
|
||||
// raw access to sendPacket
|
||||
pub fn sendPacket(self: *@This(), bytes: []const u8, reliable: bool) void {
|
||||
if (enet.enet_packet_create(bytes.ptr, bytes.len, if (reliable) enet.ENET_PACKET_FLAG_RELIABLE else enet.ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT)) |packet| {
|
||||
if (enet.enet_packet_create(bytes.ptr, bytes.len, if (reliable) enet.ENET_PACKET_FLAG_RELIABLE else 0)) |packet| {
|
||||
_ = enet.enet_peer_send(self.peer, if (reliable) 0 else 1, packet);
|
||||
}
|
||||
}
|
||||
|
|
@ -57,6 +75,8 @@ const ENetSessionData = struct {
|
|||
session: *net.Session,
|
||||
messageCount: u32 = 0,
|
||||
|
||||
debugTick: f64 = 0.05,
|
||||
|
||||
pub fn findLinkByPeer(self: *@This(), peer: [*c]enet.ENetPeer) ?*net.Link {
|
||||
for (self.session.links.items) |link| {
|
||||
if (getLinkData(link).peer == peer) {
|
||||
|
|
@ -66,7 +86,7 @@ const ENetSessionData = struct {
|
|||
return null;
|
||||
}
|
||||
|
||||
pub fn tick(self: *@This()) void {
|
||||
pub fn tick(self: *@This(), dt: f64) void {
|
||||
var event: enet.ENetEvent = undefined;
|
||||
|
||||
// Service the host with a 1000ms timeout
|
||||
|
|
@ -75,21 +95,29 @@ const ENetSessionData = struct {
|
|||
if (result > 0) {
|
||||
switch (event.type) {
|
||||
enet.ENET_EVENT_TYPE_CONNECT => {
|
||||
core.engine_log("a client connected, creating link", .{});
|
||||
const welcome_msg = "Welcome to ENet test server!";
|
||||
// core.engine_log("a client connected, creating link", .{});
|
||||
const welcome_msg = "msg: Welcome to ENet test server!";
|
||||
const link = core.get(ENetTransport).createLink(self.session) catch unreachable;
|
||||
const linkData = getLinkData(link);
|
||||
linkData.peer = event.peer;
|
||||
linkData.sendPacket(welcome_msg, true);
|
||||
link.linkType = .server;
|
||||
},
|
||||
enet.ENET_EVENT_TYPE_RECEIVE => {
|
||||
self.messageCount += 1;
|
||||
const data = @as([*]u8, @ptrCast(event.packet.*.data))[0..event.packet.*.dataLength];
|
||||
core.engine_log("Received message #{}: '{s}' echoing it back", .{ self.messageCount, data });
|
||||
// core.engine_log("Received message #{}: '{s}' echoing it back", .{ self.messageCount, data });
|
||||
|
||||
if (self.findLinkByPeer(event.peer)) |link| {
|
||||
const linkData = getLinkData(link);
|
||||
linkData.queuePacketData(data, true);
|
||||
if (link.linkType == .server) {
|
||||
linkData.queuePacketData(data, true);
|
||||
}
|
||||
if (link.linkType == .client) {
|
||||
if (!std.mem.startsWith(u8, data, "msg:")) {
|
||||
linkData.readDebugVector(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Destroy the received packet
|
||||
|
|
@ -102,6 +130,14 @@ const ENetSessionData = struct {
|
|||
for (self.session.links.items) |link| {
|
||||
const linkData = getLinkData(link);
|
||||
|
||||
self.debugTick -= dt;
|
||||
if (self.debugTick < 0) {
|
||||
self.debugTick = 0.05;
|
||||
if (link.linkType == .server) {
|
||||
linkData.pushDebugLoc();
|
||||
}
|
||||
}
|
||||
|
||||
while (linkData.queuedMessages.pop()) |queuedData| {
|
||||
linkData.sendPacket(queuedData.bytes, queuedData.reliable);
|
||||
net.netAllocator().free(queuedData.bytes);
|
||||
|
|
@ -250,7 +286,7 @@ pub fn getLinkData(link: *net.Link) *ENetLinkData {
|
|||
return core.cast(*ENetLinkData, link.transportData.?);
|
||||
}
|
||||
|
||||
// creates a session and a link with that session
|
||||
// creates a session and a link with that session, to the host
|
||||
pub fn connect(self: *@This(), target: []const u8) !*net.Link {
|
||||
const rv = try parseConnectTarget(self.allocator, target);
|
||||
defer self.allocator.free(rv.address);
|
||||
|
|
@ -263,6 +299,7 @@ pub fn connect(self: *@This(), target: []const u8) !*net.Link {
|
|||
}
|
||||
|
||||
const newLink = try self.createLink(session);
|
||||
newLink.linkType = .client;
|
||||
const linkData = core.cast(*ENetLinkData, newLink.transportData.?);
|
||||
|
||||
// Set up server address
|
||||
|
|
@ -288,7 +325,6 @@ pub fn connect(self: *@This(), target: []const u8) !*net.Link {
|
|||
if (enet.enet_host_service(sessionInfo.host.?, &event, ConnectionTimeout) > 0 and event.type == enet.ENET_EVENT_TYPE_CONNECT) {
|
||||
std.debug.print("Connected to server successfully!\n", .{});
|
||||
newLink.state = .connecting;
|
||||
newLink.linkType = .client;
|
||||
newLink.session = session;
|
||||
} else {
|
||||
std.debug.print("Failed to connect to server within timeout event.type {d} \n", .{event.type});
|
||||
|
|
@ -325,11 +361,10 @@ pub fn preTick(self: *@This(), dt: f64) !void {
|
|||
// },
|
||||
//else => {},
|
||||
// }
|
||||
_ = dt;
|
||||
|
||||
for (self.sessions.items) |session| {
|
||||
const sessionInfo = getSessionData(session);
|
||||
sessionInfo.tick();
|
||||
sessionInfo.tick(dt);
|
||||
|
||||
// tick messages in here
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ const std = @import("std");
|
|||
|
||||
const net = @import("net.zig");
|
||||
const netEngine = @import("netEngine.zig");
|
||||
|
||||
pub const EnetTransport = netEngine.EnetTransport;
|
||||
pub const NetEngine = netEngine.NetEngine;
|
||||
pub const err = netEngine.net_err;
|
||||
pub const errs = netEngine.net_errs;
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@ pub const ExternGameObject = struct {
|
|||
defer settings.release();
|
||||
try physics.addShape("ph_small_box", settings);
|
||||
|
||||
if (true) {
|
||||
if (false) {
|
||||
const ctx = ui.getScreen();
|
||||
const panel2 = try ctx.addPanel(.{});
|
||||
ctx.drawDebug = true;
|
||||
|
|
@ -419,6 +419,21 @@ pub const ExternGameObject = struct {
|
|||
}
|
||||
self.particleDebugger.particleDebug();
|
||||
}
|
||||
if (self.session) |session| {
|
||||
if (session.links.items.len > 0) {
|
||||
const linkData = net.EnetTransport.getLinkData(session.links.items[0]);
|
||||
if (rend.context().activeCamera) |camera| {
|
||||
linkData.testLinkPosition = camera.entity.fetch(core.Scene).?.getPosition();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (self.clientLink) |link| {
|
||||
const linkData = net.EnetTransport.getLinkData(link);
|
||||
if (self.spawnedEntities.items.len > 0) {
|
||||
self.spawnedEntities.items[0].fetch(core.Scene).?.setPosition(linkData.testLinkPosition);
|
||||
}
|
||||
core.debugSphere(linkData.testLinkPosition, 1.0, .{});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn destroy(self: *@This()) void {
|
||||
|
|
|
|||
Loading…
Reference in New Issue