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

value ulong

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

string

Properties

IsNone

True for None.

public bool IsNone { get; }

Property Value

bool

None

"No protocol generated": the value of default(ProtocolHash).

public static ProtocolHash None { get; }

Property Value

ProtocolHash

Value

The raw 64-bit value.

public ulong Value { get; }

Property Value

ulong

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

parts IEnumerable<ProtocolHash>

Returns

ProtocolHash

Exceptions

ArgumentNullException

parts is 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

signatures IEnumerable<string>

Returns

ProtocolHash

Exceptions

ArgumentNullException

signatures is null.

Compute(params string[])

Hashes the given signatures; see Compute(IEnumerable<string>).

public static ProtocolHash Compute(params string[] signatures)

Parameters

signatures string[]

Returns

ProtocolHash

Equals(object)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and 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

other ProtocolHash

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

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

assemblies IEnumerable<Assembly>

Returns

ProtocolHash

Exceptions

ArgumentNullException

assemblies is 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

assemblies Assembly[]

Returns

ProtocolHash

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

text string

Returns

ProtocolHash

Exceptions

ArgumentNullException

text is null.

FormatException

The text is not 16 hex digits.

ToString()

16 lower-case hex digits.

public override string ToString()

Returns

string

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

text string
hash ProtocolHash

Returns

bool

Operators

operator ==(ProtocolHash, ProtocolHash)

Equality.

public static bool operator ==(ProtocolHash left, ProtocolHash right)

Parameters

left ProtocolHash
right ProtocolHash

Returns

bool

operator !=(ProtocolHash, ProtocolHash)

Inequality.

public static bool operator !=(ProtocolHash left, ProtocolHash right)

Parameters

left ProtocolHash
right ProtocolHash

Returns

bool