Discovery.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // DiscoveryPacket.cs - Replaces your string header
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. namespace Microsoft.Xna.Framework.Net
  6. {
  7. /// <summary>
  8. /// Immutable discovery packet with length-prefixed binary format.
  9. /// Format: [Magic(4)][Version(1)][Type(1)][PayloadLength(2)][Payload][CRC32(4)]
  10. /// </summary>
  11. public class DiscoveryPacket
  12. {
  13. public const uint MAGIC = 0x4D6F6E6F; // "Mono" in hex
  14. public const byte PROTOCOL_VERSION = 1;
  15. public const byte TYPE_DISCOVERY = 1;
  16. public const byte TYPE_RESPONSE = 2;
  17. public uint Magic { get; } = MAGIC;
  18. public byte Version { get; } = PROTOCOL_VERSION;
  19. public byte Type { get; }
  20. public DiscoveryPayload Payload { get; }
  21. public DiscoveryPacket(byte type, DiscoveryPayload payload)
  22. {
  23. Type = type;
  24. Payload = payload ?? throw new ArgumentNullException(nameof(payload));
  25. }
  26. // Serialize to bytes using PacketWriter
  27. public byte[] ToByteArray()
  28. {
  29. var writer = new PacketWriter();
  30. // Header
  31. writer.Write(Magic);
  32. writer.Write(Version);
  33. writer.Write(Type);
  34. // Payload (write length first, then data)
  35. var payloadBytes = Payload.ToByteArray();
  36. writer.Write((ushort)payloadBytes.Length);
  37. writer.Write(payloadBytes);
  38. // CRC32 checksum (calculate on all data before CRC field)
  39. var dataWithoutCrc = writer.GetData();
  40. uint crc = CalculateCRC32(dataWithoutCrc, 0, dataWithoutCrc.Length);
  41. writer.Write(crc);
  42. return writer.GetData();
  43. }
  44. // Deserialize from bytes
  45. public static bool TryParse(byte[] data, out DiscoveryPacket packet)
  46. {
  47. packet = null;
  48. if (data == null || data.Length < 12) return false; // Minimum size
  49. try
  50. {
  51. var reader = new PacketReader(data);
  52. var magic = reader.ReadUInt32();
  53. if (magic != MAGIC) return false;
  54. var version = reader.ReadByte();
  55. if (version != PROTOCOL_VERSION) return false;
  56. var type = reader.ReadByte();
  57. var payloadLength = reader.ReadUInt16();
  58. // Validate payload length
  59. if (payloadLength > reader.BytesRemaining - 4) return false;
  60. var payloadBytes = reader.ReadBytes(payloadLength);
  61. var checksum = reader.ReadUInt32();
  62. // Verify CRC (basic check)
  63. var calculatedCrc = CalculateCRC32(data, 0, data.Length - 4);
  64. if (checksum != calculatedCrc) return false;
  65. if (!DiscoveryPayload.TryParse(payloadBytes, out var payload)) return false;
  66. packet = new DiscoveryPacket(type, payload);
  67. return true;
  68. }
  69. catch
  70. {
  71. return false; // Any parse error = invalid packet
  72. }
  73. }
  74. private static uint CalculateCRC32(byte[] data, int offset, int length)
  75. {
  76. uint crc = 0xFFFFFFFF;
  77. for (int i = offset; i < offset + length; i++)
  78. {
  79. crc ^= data[i];
  80. for (uint j = 0; j < 8; j++)
  81. {
  82. bool isOdd = (crc & 1) != 0;
  83. crc = (crc >> 1) ^ (isOdd ? 0xEDB88320 : 0);
  84. }
  85. }
  86. return ~crc;
  87. }
  88. }
  89. /// <summary>
  90. /// Payload containing session information.
  91. /// </summary>
  92. public class DiscoveryPayload
  93. {
  94. public string SessionId { get; }
  95. public int MaxGamers { get; }
  96. public int PrivateGamerSlots { get; }
  97. public string HostGamertag { get; }
  98. public int GamePort { get; }
  99. public byte[] SessionProperties { get; }
  100. public DiscoveryPayload(string sessionId, int maxGamers, int privateSlots,
  101. string hostGamertag, int gamePort, byte[] properties)
  102. {
  103. SessionId = sessionId ?? throw new ArgumentNullException(nameof(sessionId));
  104. MaxGamers = maxGamers;
  105. PrivateGamerSlots = privateSlots;
  106. HostGamertag = hostGamertag ?? throw new ArgumentNullException(nameof(hostGamertag));
  107. GamePort = gamePort;
  108. SessionProperties = properties ?? Array.Empty<byte>();
  109. Validate();
  110. }
  111. private void Validate()
  112. {
  113. if (SessionId.Length != 36) throw new ArgumentException("Invalid GUID length", nameof(SessionId));
  114. if (MaxGamers < 1 || MaxGamers > 32) throw new ArgumentOutOfRangeException(nameof(MaxGamers));
  115. if (PrivateGamerSlots < 0 || PrivateGamerSlots > MaxGamers)
  116. throw new ArgumentOutOfRangeException(nameof(PrivateGamerSlots));
  117. if (GamePort < 1024 || GamePort > 65535) throw new ArgumentOutOfRangeException(nameof(GamePort));
  118. if (HostGamertag.Length > 32) throw new ArgumentException("Gamertag too long", nameof(HostGamertag));
  119. }
  120. public byte[] ToByteArray()
  121. {
  122. var writer = new PacketWriter();
  123. writer.Write(SessionId);
  124. writer.Write(MaxGamers);
  125. writer.Write(PrivateGamerSlots);
  126. writer.Write(HostGamertag);
  127. writer.Write(GamePort);
  128. writer.Write(SessionProperties);
  129. return writer.GetData();
  130. }
  131. public static bool TryParse(byte[] data, out DiscoveryPayload payload)
  132. {
  133. payload = null;
  134. if (data == null || data.Length < 50) return false; // Minimum GUID + metadata
  135. try
  136. {
  137. var reader = new PacketReader(data);
  138. var sessionId = reader.ReadString();
  139. var maxGamers = reader.ReadInt32();
  140. var privateSlots = reader.ReadInt32();
  141. var gamertag = reader.ReadString();
  142. var gamePort = reader.ReadInt32();
  143. var properties = reader.ReadBytes();
  144. payload = new DiscoveryPayload(sessionId, maxGamers, privateSlots, gamertag, gamePort, properties);
  145. return true;
  146. }
  147. catch
  148. {
  149. return false;
  150. }
  151. }
  152. }
  153. }