/// <summary> /// Creates a UDP socket, listens for new radios on the network, and adds them to the RadioList /// </summary> public static void Init() { lock (init_obj) // ensure that the initialized variable is atomically set here (i.e. only let one instance through here) if (!initialized) { initialized = true; string log_enable_file = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\FlexRadio Systems\\log_discovery.txt"; _logDiscovery = File.Exists(log_enable_file); log_enable_file = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\FlexRadio Systems\\log_disconnect.txt"; _logDisconnect = File.Exists(log_enable_file); LogDiscovery("API::Init()"); radio_list = new List <Radio>(); _radio_list_timed = new ConcurrentDictionary <string, double>(); _radioDictionaryByIP = new ConcurrentDictionary <IPAddress, Radio>(); filter_serial = new List <string>(); ProcessFilterFile(); ProcessUDPPackets(); VitaDataReceivedCallback socket_callback = new VitaDataReceivedCallback(UDPDataReceivedCallback); vita_socket = new VitaSocket(4991, socket_callback); Discovery.RadioDiscovered += new RadioDiscoveredEventHandler(Discovery_RadioDiscovered); Discovery.Start(); #if (!DEBUG) CleanupRadioList(); #endif } }
public VitaSocket(int _port, VitaDataReceivedCallback _callback) { bool done = false; port = _port; callback = _callback; while (!done) { try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); socket.ReceiveBufferSize = 150000 * 5; socket.Bind(new IPEndPoint(IPAddress.Any, port)); done = true; } catch (SocketException ex) // if we get here, it is likely because the port is already open { port++; // lets increment the port and try again if (port > 5010) { throw new SocketException(ex.ErrorCode); } } } // beging looking for UDP packets immediately StartReceive(); }