SanctumTerra/ZigNet
Raknet implementation for ZigLang.
dd792a5bc4d68787a7e0e09f5b83a397c0780974
A high-performance RakNet implementation written in Zig, providing reliable UDP networking with automatic packet ordering, fragmentation, and connection management.
Add ZigNet to your project:
zig fetch --save git+https://github.com/SanctumTerra/ZigNet
Then add it to your build.zig
:
const zignet_dep = b.dependency("ZigNet", .{});
exe.root_module.addImport("ZigNet", zignet_dep.module("Raknet"));
const std = @import("std");
const ZigNet = @import("ZigNet");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Create and configure server
var server = try ZigNet.Server.init(.{
.allocator = allocator,
});
defer server.deinit();
// Set up callbacks
server.setConnectCallback(onConnect, null);
server.setDisconnectCallback(onDisconnect, null);
// Start the server
try server.start();
ZigNet.Logger.INFO("Server started! Listening for connections...", .{});
// Keep server running
std.time.sleep(std.time.ns_per_s * 30);
}
fn onConnect(connection: *ZigNet.Connection, context: ?*anyopaque) void {
_ = context;
ZigNet.Logger.INFO("Client connected!", .{});
// Set up packet handler for this connection
connection.setGamePacketCallback(onGamePacket, null);
}
fn onDisconnect(connection: *ZigNet.Connection, context: ?*anyopaque) void {
_ = context;
ZigNet.Logger.INFO("Client disconnected!", .{});
}
fn onGamePacket(connection: *ZigNet.Connection, payload: []const u8, context: ?*anyopaque) void {
_ = connection;
_ = context;
ZigNet.Logger.INFO("Received packet: {any}", .{payload});
// Echo the packet back to the client
// connection.send(payload);
}
The main server class that handles incoming connections and manages the networking.
const server = try ZigNet.Server.init(.{
.allocator = allocator,
// Add more options as needed
});
init(options: ServerOptions) !Server
- Create a new server instancestart() !void
- Start listening for connectionsdeinit() void
- Clean up server resourcessetConnectCallback(callback: fn(*Connection, ?*anyopaque) void, context: ?*anyopaque) void
- Set connection callbacksetDisconnectCallback(callback: fn(*Connection, ?*anyopaque) void, context: ?*anyopaque) void
- Set disconnection callbackRepresents a client connection to the server.
setGamePacketCallback(callback: fn(*Connection, []const u8, ?*anyopaque) void, context: ?*anyopaque) void
- Set packet handlersend(data: []const u8) !void
- Send data to the clientBuilt-in logging system with different log levels.
ZigNet.Logger.INFO("Information message", .{});
ZigNet.Logger.ERROR("Error message", .{});
ZigNet.Logger.DEBUG("Debug message", .{});
zig build
zig build test
ZigNet implements the complete RakNet protocol including:
Offline Protocol: Connection discovery and handshake
Online Protocol: Reliable data transmission
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Based on the RakNet networking library protocol.