Ejemplo n.º 1
0
        public ServiceDiscoverySocket(NetworkInterface iface,
                                      IPAddress localAddress,
                                      IPAddress multicastAddress,
                                      int port,
                                      int ttl,
                                      INetworkServiceRegisty services,
                                      bool sendLegacyResponse)
        {
            _iface = iface;
            _name  = iface.Name;
            _networkInterfaceId = iface.Id;
            _services           = services;
            _localAddress       = localAddress;
            _localEndPoint      = new IPEndPoint(_localAddress, port);
            _multicastAddress   = multicastAddress;
            _multicastEndPoint  = new IPEndPoint(multicastAddress, port);
            _sendLegacyResponse = sendLegacyResponse;

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                MulticastLoopback = true
            };
            _socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl);
            _socket.SetSocketOption(SocketOptionLevel.Socket,
                                    SocketOptionName.ReuseAddress, optionValue: true);
            _socket.Bind(new IPEndPoint(localAddress, port));

            var addressFamily = _localAddress.AddressFamily;

            if (addressFamily == AddressFamily.InterNetwork)
            {
                try
                {
                    _socket.SetSocketOption(SocketOptionLevel.IP,
                                            SocketOptionName.AddMembership,
                                            new MulticastOption(_multicastAddress,
                                                                _localAddress));

                    Log.DebugFormat("Joined multicast group {0} for {1}@{2}",
                                    _multicastAddress,
                                    _localAddress,
                                    iface.Name);
                }
                catch (SocketException e)
                {
                    Log.DebugFormat("Unable to join multicast group {0} for {1}@{2}: {3}",
                                    _multicastAddress,
                                    _localAddress,
                                    iface.Name,
                                    e);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException(string.Format("Service discovery is not implemented for '{0}'",
                                                                    addressFamily));
            }

            BeginReceive();
        }
        public ServiceDiscoveryAnySocket(INetworkServiceRegisty services,
                                         IPAddress multicastAddress,
                                         int port,
                                         int ttl,
                                         bool sendLegacyResponse)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (multicastAddress == null)
            {
                throw new ArgumentNullException(nameof(multicastAddress));
            }
            if (port <= 0 || port >= ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (ttl <= 0 || ttl >= byte.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(ttl));
            }

            _services         = services;
            _multicastAddress = multicastAddress;
            _port             = port;
            _ttl = ttl;
            _sendLegacyResponse = sendLegacyResponse;
            _sockets            = new Dictionary <IPAddress, ServiceDiscoverySocket>();
            _syncRoot           = new object();

            NetworkChange.NetworkAddressChanged += NetworkChangeOnNetworkAddressChanged;
            BindToAllAddresses();
        }