/// <summary> /// Initializes a new instance of the <see cref="MachineId"/> class. /// </summary> internal MachineId(Type type, string machineName, MachineRuntime runtime, bool useNameForHashing = false) { this.Runtime = runtime; this.Endpoint = string.Empty; if (useNameForHashing) { this.Value = 0; this.NameValue = machineName; this.Runtime.Assert(!string.IsNullOrEmpty(this.NameValue), "Input machine name cannot be null when used as id."); } else { // Atomically increments and safely wraps into an unsigned long. this.Value = (ulong)Interlocked.Increment(ref runtime.MachineIdCounter) - 1; this.NameValue = string.Empty; // Checks for overflow. this.Runtime.Assert(this.Value != ulong.MaxValue, "Detected machine id overflow."); } this.Generation = runtime.Configuration.RuntimeGeneration; this.Type = type.FullName; if (this.IsNameUsedForHashing) { this.Name = this.NameValue; } else { this.Name = string.Format(CultureInfo.InvariantCulture, "{0}({1})", string.IsNullOrEmpty(machineName) ? this.Type : machineName, this.Value.ToString()); } }
/// <summary> /// Bind the machine id. /// </summary> internal void Bind(MachineRuntime runtime) { this.Runtime = runtime; }