Пример #1
0
        /// <summary>
        /// Constructor that binds this object instance to an IPEndPoint.  If you need to change settings dynamically, Dispose and recreate a new object.
        /// </summary>
        /// <param name="endPoint">IPEndPoint where to send the multicast packets -- should be in range 224.0.0.0 to 239.255.255.255</param>
        /// <param name="timeToLive">ushort Time To Live of the packets -- how many routers will we cross -- set to 2 for local or testing</param>
        public UdpSender(System.Net.IPEndPoint endPoint, ushort timeToLive)
        {
            this.endPoint = endPoint;

            LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(endPoint);
            this.sock = sip.sock;
            this.externalInterface = sip.extInterface;

            if (Utility.IsMulticast(endPoint.Address))
            {
                SocketOptionLevel sOL = SocketOptionLevel.IP;
                if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    sOL = SocketOptionLevel.IPv6;
                }
                // Set the TTL
                sock.SetSocketOption(sOL, SocketOptionName.MulticastTimeToLive, timeToLive);
                // Enable Multicast Loopback
                sock.SetSocketOption(sOL, SocketOptionName.MulticastLoopback, 1);
            }
            else
            {
                // Enable Unicast Loopback
                echoEndPoint = new IPEndPoint(externalInterface, endPoint.Port);
            }
        }
Пример #2
0
        /// <summary>
        /// Constructor that binds this object instance to an IPEndPoint.  If you need to change settings dynamically, Dispose and recreate a new object.
        /// </summary>
        /// <param name="endPoint">IPEndPoint where to send the multicast packets -- should be in range 224.0.0.0 to 239.255.255.255</param>
        /// <param name="timeToLive">ushort Time To Live of the packets -- how many routers will we cross -- set to 2 for local or testing</param>
        public UdpSender(System.Net.IPEndPoint destEndPoint, ushort timeToLive)
        {
            this.endPoint = destEndPoint;

            LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(destEndPoint);
            this.sock = sip.sock;
            this.localRoutingInterface = sip.extInterface;

            if (Utility.IsMulticast(destEndPoint.Address))
            {
                SocketOptionLevel sOL = SocketOptionLevel.IP;
                if (destEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    sOL = SocketOptionLevel.IPv6;
                }
                // Set the TTL
                sock.SetSocketOption(sOL, SocketOptionName.MulticastTimeToLive, timeToLive);
                // Enable Multicast Loopback
                sock.SetSocketOption(sOL, SocketOptionName.MulticastLoopback, 1);
            }
            else
            {
                // Enable Unicast Loopback
                /// Note: If we didn't also instantiate a UdpListener, the socket won't have been bound
                /// to a local interface.  Specifically this comes up when we use the UdpSender to transmit
                /// reports to a diagnostic server.  In this case the loopback is not needed anyway.
                IPEndPoint localEndpoint = (IPEndPoint)sock.LocalEndPoint;
                if (localEndpoint != null)
                {
                    echoEndPoint = new IPEndPoint(localRoutingInterface, localEndpoint.Port);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Constructor that binds this object instance to an IPEndPoint.  If you need to change IPEndPoint dynamically, Dispose and recreate a new object.
        /// </summary>
        /// <param name="endPoint">IPEndPoint where we should be listening for IP Multicast packets.</param>
        /// <param name="TimeoutMilliseconds">Milliseconds before lack of a packet == a Network Timeout</param>
        /// <example>
        /// ...
        /// MulticastUdpListener mcListener = new MulticastUdpListener(endPoint1);
        /// mcListener.Receive(packetBuffer);
        /// mcListener.Displose();
        ///
        /// MulticastUdpListener mcListener = new MulticastUdpListener(endPoint2);
        /// mcListener.Receive(packetBuffer);
        /// mcListener.Displose();
        ///
        /// mcListener = null;
        /// ...
        /// </example>
        public UdpListener(System.Net.IPEndPoint endPoint, int timeoutMilliseconds)
        {
            LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(endPoint);
            this.sock = sip.sock;
            this.externalInterface = sip.extInterface;
            this.ep = endPoint;

            lock (sip)
            {
                if (!sip.Initialized)
                {
                    try
                    {
                        // Set the timeout on the socket
                        if (timeoutMilliseconds > 0)
                        {
                            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeoutMilliseconds);
                        }

                        // Set the socket to send & receive from this endpoint
                        sock.Bind(new IPEndPoint(externalInterface, endPoint.Port));

                        // Make room for 80 packets plus some overhead
                        sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1500 * 80);

                        if (Utility.IsMulticast(endPoint.Address))
                        {
                            if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                // Join the IPv6 Multicast group
                                sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                                                     new IPv6MulticastOption(endPoint.Address));
                            }
                            else
                            {
                                // Join the IPv4 Multicast group
                                sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                                     new MulticastOption(endPoint.Address));
                            }
                        }

                        sip.Initialized = true;
                    }
                    catch
                    {
                        this.Dispose();
                        throw;
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// This constructor is for use with a unicast reflector.  This constructor sends JOIN messages
        /// to the given reflector endpoint.
        ///
        /// </summary>
        /// <param name="reflectorEP"></param>
        /// <param name="multicastEP"></param>
        /// <param name="timeout"></param>
        public UdpListener(IPEndPoint multicastEP, IPEndPoint reflectorEP, int timeout)
        {
            LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(reflectorEP);
            this.sock = sip.sock;
            this.externalInterface = sip.extInterface;
            this.nextHopEP         = reflectorEP;
            this.multicastEP       = multicastEP;

            // check whether we must "force" the local port to equal the remote port -- this is done
            // for clients behind picky firewalls.  If not, use port number 0 to indicate a "don't care"

            int port = 0;

            try
            {
                string forcePort = ConfigurationManager.AppSettings["MSR.LST.Net.ForceLocalUnicastPorts"];
                bool   force     = Boolean.Parse(forcePort);
                if (force)
                {
                    port = reflectorEP.Port;
                }
            }
            catch (Exception) { }

            lock (sip)
            {
                if (!sip.Initialized)
                {
                    try
                    {
                        InitializeSocket(port, timeout);

                        // notify the reflector of our presence...
                        joiner = new UdpReflectorJoiner(nextHopEP, multicastEP);
                        joiner.Start();

                        sip.Initialized = true;
                    }
                    catch
                    {
                        this.Dispose();
                        throw;
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// This constructor is used for non-reflector sessions;
        /// if necessary, it joins the appropriate multicast group.
        /// </summary>
        /// <param name="multicastEP"></param>
        /// <param name="timeoutMilliseconds"></param>

        public UdpListener(System.Net.IPEndPoint nextHopEP, int timeoutMilliseconds)
        {
            LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(nextHopEP);
            this.sock = sip.sock;
            this.externalInterface = sip.extInterface;
            this.nextHopEP         = nextHopEP;
            this.multicastEP       = nextHopEP;

            lock (sip)
            {
                if (!sip.Initialized)
                {
                    try
                    {
                        InitializeSocket(nextHopEP.Port, timeoutMilliseconds);

                        if (Utility.IsMulticast(nextHopEP.Address))
                        {
                            if (nextHopEP.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                // Join the IPv6 Multicast group
                                sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                                                     new IPv6MulticastOption(nextHopEP.Address));
                            }
                            else
                            {
                                // Join the IPv4 Multicast group
                                sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                                     new MulticastOption(nextHopEP.Address));
                            }
                        }

                        sip.Initialized = true;
                    }
                    catch
                    {
                        this.Dispose();
                        throw;
                    }
                }
            }
        }