Struct ProtocolHash
- Namespace
- Unity.Multiplayer.Core
A 64-bit fingerprint of a game's network protocol: every [ToServer] / [ToClients] /
[ToOwner] / [ToClient] call and every [Synced] field signature. Connection approval
rejects a client whose hash differs from the server's (RFC 0001, "Protocol hash").
public readonly struct ProtocolHash : IEquatable<ProtocolHash>
- Implements
- Inherited Members
Remarks
The contract the source generator fills: it collects one signature string per RPC and synced field,
computes Compute(IEnumerable<string>) over them at compile time, and emits
[assembly: NetProtocol("<16 hex digits>", <count>)] (NetProtocolAttribute)
into the game assembly. At runtime FromAssemblies(IEnumerable<Assembly>) reads it back.
The algorithm is fixed so the generator (netstandard2.0, no reference to this assembly) can reproduce it:
trim each signature, drop empty ones, sort the rest ordinally and remove duplicates, join them with
'\n' after the line Domain, and take 64-bit FNV-1a over the UTF-8 bytes. A result of
zero is replaced by one, so None (zero) always means "no protocol was generated".
Constructors
ProtocolHash(ulong)
Wraps a raw hash value. Zero is None.
public ProtocolHash(ulong value)
Parameters
valueulong
Fields
Domain
The first line hashed, so the format can change later without colliding with this one.
public const string Domain = "Unity.Multiplayer.Protocol/1"
Field Value
Properties
IsNone
True for None.
public bool IsNone { get; }
Property Value
None
"No protocol generated": the value of default(ProtocolHash).
public static ProtocolHash None { get; }
Property Value
Value
The raw 64-bit value.
public ulong Value { get; }
Property Value
Methods
Combine(IEnumerable<ProtocolHash>)
Combines the hashes of several assemblies into one, independent of order. None parts are skipped; no parts gives None and a single part is returned unchanged.
public static ProtocolHash Combine(IEnumerable<ProtocolHash> parts)
Parameters
partsIEnumerable<ProtocolHash>
Returns
Exceptions
- ArgumentNullException
partsis null.
Compute(IEnumerable<string>)
Hashes a set of RPC / synced-field signatures. Order and duplicates do not matter; leading and trailing whitespace is ignored. An empty set still yields a non-zero hash (a game with no RPCs).
public static ProtocolHash Compute(IEnumerable<string> signatures)
Parameters
signaturesIEnumerable<string>
Returns
Exceptions
- ArgumentNullException
signaturesis null.
Compute(params string[])
Hashes the given signatures; see Compute(IEnumerable<string>).
public static ProtocolHash Compute(params string[] signatures)
Parameters
signaturesstring[]
Returns
Equals(object)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
Equals(ProtocolHash)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(ProtocolHash other)
Parameters
otherProtocolHashAn object to compare with this object.
Returns
FromAssemblies(IEnumerable<Assembly>)
Reads the generator's NetProtocolAttribute from each assembly and combines what it finds (Combine(IEnumerable<ProtocolHash>)). Assemblies without the attribute are skipped; if none has it the result is None.
public static ProtocolHash FromAssemblies(IEnumerable<Assembly> assemblies)
Parameters
assembliesIEnumerable<Assembly>
Returns
Exceptions
- ArgumentNullException
assembliesis null.- FormatException
An attribute holds a value that is not 16 hex digits.
FromAssemblies(params Assembly[])
Reads and combines the generator's hash from the given assemblies; see FromAssemblies(IEnumerable<Assembly>).
public static ProtocolHash FromAssemblies(params Assembly[] assemblies)
Parameters
assembliesAssembly[]
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Parse(string)
Parses 16 hex digits (case-insensitive, optional 0x prefix).
public static ProtocolHash Parse(string text)
Parameters
textstring
Returns
Exceptions
- ArgumentNullException
textis null.- FormatException
The text is not 16 hex digits.
ToString()
16 lower-case hex digits.
public override string ToString()
Returns
TryParse(string, out ProtocolHash)
Parses 16 hex digits (case-insensitive, optional 0x prefix); false if the text is anything else.
public static bool TryParse(string text, out ProtocolHash hash)
Parameters
textstringhashProtocolHash
Returns
Operators
operator ==(ProtocolHash, ProtocolHash)
Equality.
public static bool operator ==(ProtocolHash left, ProtocolHash right)
Parameters
leftProtocolHashrightProtocolHash
Returns
operator !=(ProtocolHash, ProtocolHash)
Inequality.
public static bool operator !=(ProtocolHash left, ProtocolHash right)
Parameters
leftProtocolHashrightProtocolHash