mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 03:34:48 +00:00
it works well!
This commit is contained in:
@@ -71,7 +71,7 @@ pub const Message = packed struct {
|
||||
};
|
||||
|
||||
const Self = @This();
|
||||
const SelfBytes = []align(@alignOf(Self)) u8;
|
||||
const SelfBytes = []align(1) u8;
|
||||
|
||||
type: PacketType,
|
||||
length: u16,
|
||||
@@ -81,9 +81,9 @@ pub const Message = packed struct {
|
||||
/// This properly initializes the top level headers within the slice.
|
||||
/// This is used for creating new messages. For reading messages from the network,
|
||||
/// see: networkBytesAsValue.
|
||||
pub fn init(@"type": PacketType, bytes: []align(@alignOf(Self)) u8) *Self {
|
||||
pub fn init(@"type": PacketType, bytes: []u8) *align(1) Self {
|
||||
std.debug.assert(bytes.len >= @sizeOf(Self));
|
||||
const res: *Self = @ptrCast(bytes.ptr);
|
||||
const res: *align(1) Self = @ptrCast(bytes.ptr);
|
||||
res.type = @"type";
|
||||
res.length = @intCast(bytes.len - @sizeOf(Self));
|
||||
return res;
|
||||
@@ -100,15 +100,15 @@ pub const Message = packed struct {
|
||||
return @intCast(payload_len + @sizeOf(Self) + header_size);
|
||||
}
|
||||
|
||||
fn getRelay(self: *Self) *align(1) Relay {
|
||||
fn getRelay(self: *align(1) Self) *align(1) Relay {
|
||||
return std.mem.bytesAsValue(Relay, &self.bytes);
|
||||
}
|
||||
fn getConnection(self: *Self) *align(1) Connection {
|
||||
fn getConnection(self: *align(1) Self) *align(1) Connection {
|
||||
return std.mem.bytesAsValue(Connection, &self.bytes);
|
||||
}
|
||||
|
||||
/// Access the message Saprus payload.
|
||||
pub fn getSaprusTypePayload(self: *Self) MessageTypeError!(union(PacketType) {
|
||||
pub fn getSaprusTypePayload(self: *align(1) Self) MessageTypeError!(union(PacketType) {
|
||||
relay: *align(1) Relay,
|
||||
file_transfer: void,
|
||||
connection: *align(1) Connection,
|
||||
@@ -122,7 +122,7 @@ pub const Message = packed struct {
|
||||
}
|
||||
|
||||
/// Convert the message to native endianness from network endianness in-place.
|
||||
pub fn nativeFromNetworkEndian(self: *Self) MessageTypeError!void {
|
||||
pub fn nativeFromNetworkEndian(self: *align(1) Self) MessageTypeError!void {
|
||||
self.type = @enumFromInt(bigToNative(
|
||||
@typeInfo(@TypeOf(self.type)).@"enum".tag_type,
|
||||
@intFromEnum(self.type),
|
||||
@@ -146,7 +146,7 @@ pub const Message = packed struct {
|
||||
}
|
||||
|
||||
/// Convert the message to network endianness from native endianness in-place.
|
||||
pub fn networkFromNativeEndian(self: *Self) MessageTypeError!void {
|
||||
pub fn networkFromNativeEndian(self: *align(1) Self) MessageTypeError!void {
|
||||
try switch (try self.getSaprusTypePayload()) {
|
||||
.relay => {},
|
||||
.connection => |*con| con.*.networkFromNativeEndian(),
|
||||
@@ -161,7 +161,7 @@ pub const Message = packed struct {
|
||||
}
|
||||
|
||||
/// Convert network endian bytes to a native endian value in-place.
|
||||
pub fn networkBytesAsValue(bytes: SelfBytes) MessageParseError!*Self {
|
||||
pub fn networkBytesAsValue(bytes: SelfBytes) MessageParseError!*align(1) Self {
|
||||
const res = std.mem.bytesAsValue(Self, bytes);
|
||||
try res.nativeFromNetworkEndian();
|
||||
return .bytesAsValue(bytes);
|
||||
@@ -169,7 +169,7 @@ pub const Message = packed struct {
|
||||
|
||||
/// Create a structured view of the bytes without initializing the length or type,
|
||||
/// and without converting the endianness.
|
||||
pub fn bytesAsValue(bytes: SelfBytes) MessageParseError!*Self {
|
||||
pub fn bytesAsValue(bytes: SelfBytes) MessageParseError!*align(1) Self {
|
||||
const res = std.mem.bytesAsValue(Self, bytes);
|
||||
return switch (res.type) {
|
||||
.relay, .connection => if (bytes.len == res.length + @sizeOf(Self))
|
||||
@@ -183,9 +183,9 @@ pub const Message = packed struct {
|
||||
|
||||
/// Deprecated.
|
||||
/// If I need the bytes, I should just pass around the slice that is backing this to begin with.
|
||||
pub fn asBytes(self: *Self) SelfBytes {
|
||||
pub fn asBytes(self: *align(1) Self) SelfBytes {
|
||||
const size = @sizeOf(Self) + self.length;
|
||||
return @as([*]align(@alignOf(Self)) u8, @ptrCast(self))[0..size];
|
||||
return @as([*]align(1) u8, @ptrCast(self))[0..size];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user