/// <summary> /// Adds a server to the <see cref="ServerWatcher" />. /// </summary> /// <param name="serverEndPoint">The <see cref="IPEndPoint" /> of the server that should be watched.</param> /// <returns> The state object of the new server.</returns> /// <exception cref="ArgumentException">Thrown if the serverEndPoint parameter is null.</exception> /// <exception cref="ArgumentNullException">Thrown if the given endpoint is no valid IPv4 endpoint.</exception> public IServerState AddServer(IPEndPoint serverEndPoint) { if (serverEndPoint == null) { throw new ArgumentNullException(nameof(serverEndPoint)); } if (serverEndPoint.AddressFamily != AddressFamily.InterNetwork) { throw new ArgumentException( "The argument does not contain a valid IPv4 address", nameof(serverEndPoint)); } lock (this.instanceLock) { var newServer = new ServerState( serverEndPoint.Address, (ushort)serverEndPoint.Port, serverEndPoint.ToString()); this.watchedServers.Add(newServer); return(newServer); } }
/// <summary> /// Invokes the ServerStatusChanged event using the <see cref="ThreadPool" />. /// </summary> /// <param name="state">The changed server state</param> private void InvokeOnServerStatusChanged(ServerState state) { // Invoke the event on the thread pool ThreadPool.QueueUserWorkItem( delegate { this.ServerStatusChanged?.Invoke(this, new ServerStatusChangedEventArgs(state)); }); }