feat(battle-node): MsgPayloadCodec encodes/decodes msgpack↔envelope chain

This commit is contained in:
gamer147
2026-05-31 21:58:06 -04:00
parent 4cc8b3c01c
commit c0c2bb5772
2 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
using MessagePack;
using SVSim.BattleNode.Wire;
namespace SVSim.BattleNode.Protocol;
/// <summary>
/// Full chain between an envelope and the bytes that ride as a SocketIO binary attachment.
/// Inbound: bytes → msgpack-string → NodeCrypto.Decrypt → JSON → MsgEnvelope
/// Outbound: MsgEnvelope → JSON → NodeCrypto.Encrypt → msgpack-bytes
/// </summary>
public static class MsgPayloadCodec
{
public static MsgEnvelope Decode(byte[] msgpackBytes)
{
var encryptedString = MessagePackSerializer.Deserialize<string>(msgpackBytes);
var json = NodeCrypto.DecryptForNode(encryptedString);
return MsgEnvelope.FromJson(json);
}
public static byte[] Encode(MsgEnvelope envelope, string key)
{
var json = MsgEnvelope.ToJson(envelope);
var encryptedString = NodeCrypto.EncryptForNode(json, key);
return MessagePackSerializer.Serialize(encryptedString);
}
}