/// <summary> /// Stop listening for new incoming connections on specified <see cref="ConnectionListenerBase"/> and remove it from the local listeners dictionary. /// </summary> /// <param name="listener">The listener which should stop listening.</param> public static void StopListening(ConnectionListenerBase listener) { if (listener == null) { throw new ArgumentNullException("listener", "Provided listener cannot be null."); } StopListening(listener.ConnectionType, listener.LocalListenEndPoint); }
/// <summary> /// Start listening for new incoming connections on specified <see cref="IPEndPoint"/>. Inspect listener.LocalListenIPEndPoint /// when method returns to determine the <see cref="IPEndPoint"/> used. /// </summary> /// <param name="listener">The listener to use.</param> /// <param name="desiredLocalEndPoint">The desired local <see cref="IPEndPoint"/> to use for listening. IPAddress.Any corresponds with listening on /// 0.0.0.0. Use a port number of 0 to dynamically select a port.</param> /// <param name="useRandomPortFailOver">If true and the requested local port is not available will select one at random. /// If false and provided port is unavailable will throw <see cref="CommsSetupShutdownException"/></param> public static void StartListening <T>(ConnectionListenerBase listener, T desiredLocalEndPoint, bool useRandomPortFailOver = false) where T : EndPoint { #region Input Validation if (listener == null) { throw new ArgumentNullException("listener", "Provided listener cannot be null."); } if (desiredLocalEndPoint == null) { throw new ArgumentNullException("desiredLocalEndPoint", "Provided desiredLocalEndPoint cannot be null."); } //Commented out as listening on IPAddress.Any does have specific usage cases, such as receiving UDP broadcast on iOS //if (desiredLocalEndPoint.GetType() == typeof(IPEndPoint) && ((desiredLocalEndPoint as IPEndPoint).Address == IPAddress.Any || (desiredLocalEndPoint as IPEndPoint).Address == IPAddress.IPv6Any)) throw new ArgumentException("desiredLocalEndPoint must specify a valid local IPAddress.", "desiredLocalEndPoint"); #if NET35 || NET4 if (desiredLocalEndPoint is BluetoothEndPoint && (desiredLocalEndPoint as BluetoothEndPoint).Address == BluetoothAddress.None) { throw new ArgumentException("desiredLocalEndPoint must specify a valid local Bluetooth Address.", "desiredLocalEndPoint"); } #endif #endregion Input Validation lock (staticConnectionLocker) { if (!useRandomPortFailOver && listenersDict.ContainsKey(listener.ConnectionType) && listenersDict[listener.ConnectionType].ContainsKey(desiredLocalEndPoint)) { throw new CommsSetupShutdownException("A listener already exists for " + listener.ConnectionType.ToString() + " connections with a local IPEndPoint " + desiredLocalEndPoint.ToString() + ". Please try a different local IPEndPoint" + " or all random port fail over."); } //Start listening listener.StartListening(desiredLocalEndPoint, useRandomPortFailOver); //Idiot check if (!listener.IsListening) { throw new CommsSetupShutdownException("Listener is not listening after calling StartListening."); } if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Info("Listener started (" + listener.ConnectionType.ToString() + "-" + (listener.ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Enabled ? "E" : "D") + " - " + listener.LocalListenEndPoint.ToString() + ")."); } //Idiot check if (listenersDict.ContainsKey(listener.ConnectionType) && listenersDict[listener.ConnectionType].ContainsKey(listener.LocalListenEndPoint)) { listener.StopListening(); throw new InvalidOperationException("According to the listener dictionary there is already a listener for " + listener.ConnectionType.ToString() + " connections with a local IPEndPoint " + listener.LocalListenEndPoint.ToString() + ". This should not be possible."); } //Add the listener to the global dict if (listenersDict.ContainsKey(listener.ConnectionType)) { if (listenersDict[listener.ConnectionType].ContainsKey(listener.LocalListenEndPoint)) { throw new CommsSetupShutdownException("A listener already exists with an IPEndPoint that matches the new listener. This should not be possible."); } else { listenersDict[listener.ConnectionType].Add(listener.LocalListenEndPoint, listener); } } else { listenersDict.Add(listener.ConnectionType, new Dictionary <EndPoint, ConnectionListenerBase>() { { listener.LocalListenEndPoint, listener } }); } } }