Example #1
0
            public List<MFDevice> EnumPorts(TransportType type)
            {
                /*
                We need to create two socket for discovery Udp multicast
                socket  sends discovery message
                socket2 receive messages
                */
                if (type != TransportType.Udp) //sanity check
                    type = TransportType.Udp;

                this.m_deviceList.Clear();          //start anew

                try
                {

                    foreach (IPAddress address in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
                    {
                        if (address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            int num = 0;          // num of bytes return with receive socket
                            int index = 0;        // denotes the beginning of each stream from MF modules
                            int iy = 0;           // counts macaddress bytes
                            byte[] buffer = new byte[0x400]; //data buffer
                            byte[] macarr = new byte[0x06];  //mac address from data buffer
                            Socket socket = null; //tx socket on host
                            Socket socket2 = null;//rx socket on host
                            IPEndPoint localEP = new IPEndPoint(address, 0);
                            EndPoint remoteEP = new IPEndPoint(IPAddress.Any, this.m_DiscoveryMulticastPort);
                            IPEndPoint point3 = new IPEndPoint(address, this.m_DiscoveryMulticastPort);
                            IPEndPoint point4 = new IPEndPoint(this.m_DiscoveryMulticastAddress, this.m_DiscoveryMulticastPort);
                            try
                            {
                                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                                socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                                socket2.Bind(point3);
                                socket2.ReceiveTimeout = 0x3e8;

                                socket2.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(this.m_DiscoveryMulticastAddressRecv, address));
                                socket.Bind(localEP);
                                socket.MulticastLoopback = false;
                                socket.Ttl = 1;
                                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 0x40);
                                socket.SendTo(Encoding.ASCII.GetBytes(this.m_DiscoveryMulticastToken), SocketFlags.None, point4);

                                while (0 < (num = socket2.ReceiveFrom(buffer, index, buffer.Length - index, SocketFlags.None, ref remoteEP)))
                                {
                                    MFDevice mfDev = new MFDevice();
                                    mfDev.setPointRemoteIP(((IPEndPoint)remoteEP).Address.ToString()); //add Remote IP
                                    mfDev.setPointRemotePort(((IPEndPoint)remoteEP).Port);             //add Remote Port
                                    mfDev.setPointRemoteAdrFamily(((IPEndPoint)remoteEP).AddressFamily.ToString());  //add Remote Type
                                    //get/add mac address
                                    iy = 0;
                                    while (iy < 6)
                                    {
                                        macarr[iy] = buffer[index + 8 + iy];
                                        iy++;
                                    }
                                    mfDev.GetMacAddr(macarr);                                           //add Remote Mac Address
                                    m_deviceList.Add(mfDev);     //add object to Factory
                                    index += num;                // new ofset- remote dev answer simultaniously to multicast
                                    socket2.ReceiveTimeout = 200;//timeout of 200ms
                                }
                                socket2.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(this.m_DiscoveryMulticastAddressRecv));
                            }
                            catch (SocketException)
                            {
                            }
                            finally
                            {
                                if (socket2 != null)
                                {
                                    socket2.Close();
                                    socket2 = null;
                                }
                                if (socket != null)
                                {
                                    socket.Close();
                                    socket = null;
                                }
                            }

                        }
                    }
                }
                catch (Exception)
                {
                }
                return m_deviceList;
            }