Esempio n. 1
0
        /// <summary>
        /// Re initializes the socket
        /// </summary>
        /// <param name="socket">The socket which should be reinitialized</param>
        private void Renew(UdpClient socket)
        {
            UdpClient newsocket = null;

            if (socket is UdpClientMulticast mcastSocket)
            {
                newsocket = new UdpClientMulticast(mcastSocket.Address, mcastSocket.MulticastAddress, mcastSocket.Port);
            }
            else if (socket is UdpClientBroadcast bcastSocket)
            {
                newsocket = new UdpClientBroadcast(bcastSocket.Address, bcastSocket.Port, bcastSocket.PubSubContext);
            }
            else if (socket is UdpClientUnicast ucastSocket)
            {
                newsocket = new UdpClientUnicast(ucastSocket.Address, ucastSocket.Port);
            }
            m_subscriberUdpClients.Remove(socket);
            m_subscriberUdpClients.Add(newsocket);
            socket.Close();
            socket.Dispose();

            newsocket.BeginReceive(new AsyncCallback(OnUadpReceive), newsocket);
        }
Esempio n. 2
0
        /// <summary>
        /// Create specific <see cref="UdpClient"/> for specified <see cref="NetworkInterface"/> and <see cref="IPEndPoint"/>.
        /// </summary>
        /// <param name="pubSubContext">Is the method called in a publisher context or a subscriber context</param>
        /// <param name="networkInterface"></param>
        /// <param name="configuredEndpoint"></param>
        /// <returns></returns>
        private static UdpClient CreateUdpClientForNetworkInterface(UsedInContext pubSubContext, NetworkInterface networkInterface, IPEndPoint configuredEndpoint)
        {
            UdpClient             udpClient    = null;
            IPInterfaceProperties ipProps      = networkInterface.GetIPProperties();
            IPAddress             localAddress = IPAddress.Any;

            foreach (var address in ipProps.UnicastAddresses)
            {
                if (address.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    localAddress = address.Address;
                }
            }

            try
            {
                //detect the port used for binding
                int port = 0;
                if (pubSubContext == UsedInContext.Subscriber)
                {
                    port = configuredEndpoint.Port;
                }
                if (IsIPv4MulticastAddress(configuredEndpoint.Address))
                {
                    //instantiate multi-cast UdpClient
                    udpClient = new UdpClientMulticast(localAddress, configuredEndpoint.Address, port);
                }
                else if (IsIPv4BroadcastAddress(configuredEndpoint.Address, networkInterface))
                {
                    //instantiate broadcast UdpClient depending on publisher/subscriber usage context
                    udpClient = new UdpClientBroadcast(localAddress, port, pubSubContext);
                }
                else
                {
                    //instantiate unicast UdpClient depending on publisher/subscriber usage context
                    udpClient = new UdpClientUnicast(localAddress, port);
                }
                if (pubSubContext == UsedInContext.Publisher)
                {
                    //try to send 1 byte for target IP
                    udpClient.Send(new byte[] { 0 }, 1, configuredEndpoint);
                }

                // On Windows Only since Linux does not support this
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // Disable exceptions raised by ICMP Port Unreachable messages
                    udpClient.Client.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null);
                }
            }
            catch (Exception ex)
            {
                Utils.Trace(Utils.TraceMasks.Information, "Cannot use Network interface '{0}'. Exception: {1}",
                            networkInterface.Name, ex.Message);
                if (udpClient != null)
                {
                    //cleanup
                    udpClient.Dispose();
                    udpClient = null;
                }
            }

            return(udpClient);
        }