acdream/src/AcDream.Core.Net/Messages/ServerMessage.cs
Erik e928c5dd02 perf(net): borrow inbound packet storage
Decode headers, optional fields, fragments, and single-fragment messages directly over pooled datagrams. Copy only fragment state that crosses a datagram lifetime, preserve synchronous dispatch and ACK ordering, and lock the path to the owned decoder with differential and zero-allocation tests.
2026-07-25 05:58:55 +02:00

51 lines
1.6 KiB
C#

using System;
using System.Buffers.Binary;
namespace AcDream.Core.Net.Messages;
/// <summary>
/// Inbound <c>0xF7E0 ServerMessage</c> top-level GameMessage.
/// General-purpose server-broadcast text — admin announcements,
/// combat logs, and routine error messages routed by the server
/// instead of via WeenieError.
///
/// <para>
/// This is a standalone GameMessage — NOT wrapped in 0xF7B0
/// GameEvent envelope. Dispatched directly from
/// <see cref="WorldSession.ProcessDatagram"/>.
/// </para>
///
/// <para>
/// Wire layout (port from holtburger
/// <c>references/holtburger/.../messages/chat/types.rs::ServerMessageData</c>,
/// see also opcodes.rs:167):
/// <code>
/// u32 opcode // 0xF7E0
/// string16L message
/// u32 chatType // ChatMessageType (Broadcast / System / etc)
/// </code>
/// </para>
/// </summary>
public static class ServerMessage
{
public const uint Opcode = 0xF7E0u;
public readonly record struct Parsed(string Message, uint ChatType);
public static Parsed? TryParse(ReadOnlySpan<byte> body)
{
if (body.Length < 8) return null;
uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body);
if (opcode != Opcode) return null;
try
{
int pos = 4;
string message = StringReader.ReadString16L(body, ref pos);
if (body.Length - pos < 4) return null;
uint chatType = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(pos));
return new Parsed(message, chatType);
}
catch { return null; }
}
}