2
0

ClientHandshake.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Text.RegularExpressions;
  3. namespace GodotTools.IdeMessaging
  4. {
  5. public class ClientHandshake : IHandshake
  6. {
  7. private static readonly string ClientHandshakeBase = $"{Peer.ClientHandshakeName},Version={Peer.ProtocolVersionMajor}.{Peer.ProtocolVersionMinor}.{Peer.ProtocolVersionRevision}";
  8. private static readonly string ServerHandshakePattern = $@"{Regex.Escape(Peer.ServerHandshakeName)},Version=([0-9]+)\.([0-9]+)\.([0-9]+),([_a-zA-Z][_a-zA-Z0-9]{{0,63}})";
  9. public string GetHandshakeLine(string identity) => $"{ClientHandshakeBase},{identity}";
  10. public bool IsValidPeerHandshake(string handshake, [NotNullWhen(true)] out string? identity, ILogger logger)
  11. {
  12. identity = null;
  13. var match = Regex.Match(handshake, ServerHandshakePattern);
  14. if (!match.Success)
  15. return false;
  16. if (!uint.TryParse(match.Groups[1].Value, out uint serverMajor) || Peer.ProtocolVersionMajor != serverMajor)
  17. {
  18. logger.LogDebug("Incompatible major version: " + match.Groups[1].Value);
  19. return false;
  20. }
  21. if (!uint.TryParse(match.Groups[2].Value, out uint serverMinor) || Peer.ProtocolVersionMinor < serverMinor)
  22. {
  23. logger.LogDebug("Incompatible minor version: " + match.Groups[2].Value);
  24. return false;
  25. }
  26. if (!uint.TryParse(match.Groups[3].Value, out uint _)) // Revision
  27. {
  28. logger.LogDebug("Incompatible revision build: " + match.Groups[3].Value);
  29. return false;
  30. }
  31. identity = match.Groups[4].Value;
  32. return true;
  33. }
  34. }
  35. }