-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRpcVersion.cs
164 lines (141 loc) · 5.13 KB
/
RpcVersion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
namespace Coder.Desktop.Vpn.Proto;
/// <summary>
/// A version of the RPC API. Can be compared other versions to determine compatibility between two peers.
/// </summary>
public class RpcVersion
{
public static readonly RpcVersion Current = new(1, 1);
public ulong Major { get; }
public ulong Minor { get; }
/// <param name="major">The major version of the peer</param>
/// <param name="minor">The minor version of the peer</param>
public RpcVersion(ulong major, ulong minor)
{
Major = major;
Minor = minor;
}
/// <summary>
/// Parse a string in the format "major.minor" into an ApiVersion.
/// </summary>
/// <param name="versionString">Version string to parse</param>
/// <returns>Parsed ApiVersion</returns>
/// <exception cref="ArgumentException">The version string is invalid</exception>
public static RpcVersion Parse(string versionString)
{
var parts = versionString.Split('.');
if (parts.Length != 2) throw new ArgumentException($"Invalid version string '{versionString}'");
try
{
var major = ulong.Parse(parts[0]);
if (major == 0) throw new ArgumentException($"Invalid major version '{major}'");
var minor = ulong.Parse(parts[1]);
return new RpcVersion(major, minor);
}
catch (FormatException e)
{
throw new ArgumentException($"Invalid version string '{versionString}'", e);
}
}
public override string ToString()
{
return $"{Major}.{Minor}";
}
/// <summary>
/// Returns the lowest version that is compatible with both this version and the other version. If no compatible
/// version is found, null is returned.
/// </summary>
/// <param name="other">Version to compare against</param>
/// <returns>The highest compatible version</returns>
public RpcVersion? IsCompatibleWith(RpcVersion other)
{
if (Major != other.Major)
return null;
// The lowest minor version from the two versions should be returned.
return Minor < other.Minor ? this : other;
}
#region RpcVersion Equality
public static bool operator ==(RpcVersion a, RpcVersion b)
{
return a.Equals(b);
}
public static bool operator !=(RpcVersion a, RpcVersion b)
{
return !a.Equals(b);
}
private bool Equals(RpcVersion other)
{
return Major == other.Major && Minor == other.Minor;
}
public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((RpcVersion)obj);
}
public override int GetHashCode()
{
return HashCode.Combine(Major, Minor);
}
#endregion
}
public class RpcVersionList : List<RpcVersion>
{
public static readonly RpcVersionList Current = new(RpcVersion.Current);
public RpcVersionList(IEnumerable<RpcVersion> versions) : base(versions)
{
}
public RpcVersionList(params RpcVersion[] versions) : base(versions)
{
}
public static RpcVersionList Parse(string versions)
{
try
{
var l = new RpcVersionList(versions.Split(',').Select(RpcVersion.Parse));
l.Validate();
return l;
}
catch (Exception e)
{
throw new ArgumentException($"Invalid version list '{versions}'", e);
}
}
public override string ToString()
{
return string.Join(",", this);
}
/// <summary>
/// Validates that the version list doesn't contain any invalid versions, duplicate major versions, or is unsorted.
/// </summary>
/// <exception cref="ArgumentException">The version list is not valid</exception>
public void Validate()
{
if (Count == 0) throw new ArgumentException("Version list must contain at least one version");
for (var i = 0; i < Count; i++)
{
if (this[i].Major == 0) throw new ArgumentException($"Invalid major version '{this[i].Major}'");
if (i > 0 && this[i - 1].Major == this[i].Major)
throw new ArgumentException($"Duplicate major version '{this[i].Major}'");
if (i > 0 && this[i - 1].Major > this[i].Major) throw new ArgumentException("Versions are not sorted");
}
}
/// <summary>
/// Returns the lowest version that is compatible with both version lists. If there is no compatible version,
/// null is returned.
/// </summary>
/// <param name="other">Version list to compare against</param>
/// <returns>The highest compatible version</returns>
public RpcVersion? IsCompatibleWith(RpcVersionList other)
{
RpcVersion? bestVersion = null;
foreach (var v1 in this)
foreach (var v2 in other)
if (v1.Major == v2.Major && (bestVersion is null || v1.Major > bestVersion.Major))
{
var v = v1.IsCompatibleWith(v2);
if (v is not null) bestVersion = v;
}
return bestVersion;
}
}