/// <summary> /// Gets the uri prefix in format "uri://" for the specified protocol /// </summary> /// <param name="protocol">protocol to get prefix for</param> /// <returns>Returns the appropriate prefix string, or ltsf:// if the protocol is unrecognized</returns> public static string GetPrefix(SinkProtocolType protocol) { if (!Mappings.ContainsKey(protocol)) { return("ltsf://"); } return(Mappings[protocol]); }
/// <summary> /// Determines if a given port is available /// </summary> /// <param name="sinkProtocolType">sink protocol type</param> /// <param name="interfaceAddress">interface address to test with</param> /// <param name="port">port number to check</param> /// <returns>True if available, false if could not bind.</returns> private bool IsPortAvailable(SinkProtocolType sinkProtocolType, IPAddress interfaceAddress, int port) { bool available = false; Socket testSocket = null; try { if ((sinkProtocolType == SinkProtocolType.LTSF_DGRAM) || (sinkProtocolType == SinkProtocolType.RTP)) { testSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } else if ((sinkProtocolType == SinkProtocolType.LTSF_TCP) || (sinkProtocolType == SinkProtocolType.HTTP) || (sinkProtocolType == SinkProtocolType.Default)) { testSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } EndPoint myEndPoint = new IPEndPoint(interfaceAddress, port); testSocket.Bind(myEndPoint); available = true; } catch (Exception exc) { AppLogger.Message(exc.Message); } finally { if (testSocket != null) { testSocket.Close(); testSocket = null; } } return(available); }
/// <summary> /// Populates the SinkURL and BaseSinkURL properties. /// If the $PortRange macro is defined, a port is selected, and the macro is expanded. /// </summary> /// <remarks> /// does not expand $InterfaceAddress for the BaseSinkURL property /// </remarks> /// <param name="configSinkAddress">the sink address for this source, from the configuration file</param> /// <param name="interfaceAddress">The interface address that the graph is/will be opened on</param> /// <param name="protocol">protocol that will be used</param> protected void BuildClientURL(SinkProtocolType protocol) { this.SinkProtocolType = protocol; if (SourceConfig.SinkAddress == null) { if (SourceConfig.SinkAddress != null) { ClientURL = SourceConfig.SinkAddress; } else { throw new Exception("No ClientURL or SinkAddress defined for Source " + SourceConfig.SourceName); } } else { ClientURL = SourceConfig.SinkAddress; } BaseClientURL = ClientURL; if (ClientURL.Contains(InterfaceAddressMacro)) { ClientURL = ExpandInterfaceAddressMacro(ClientURL, OpenGraphRequest.InterfaceAddress); } if (ClientURL.Contains(PortRangeMacro)) { List <int> allocedPorts = GetAllocatedPorts(); int pos = ClientURL.LastIndexOf(PortRangeMacro); string prefix = ClientURL.Substring(0, pos); string suffix = ClientURL.Substring(pos); int rangeNumbersPos = suffix.LastIndexOf(@"(") + 1; if (rangeNumbersPos > 0) { int rangeNumbersLength = suffix.Length - rangeNumbersPos - 1; string rangeNumbers = suffix.Substring(rangeNumbersPos, rangeNumbersLength); string[] parts = rangeNumbers.Split(new char[] { '-' }); int portLow = Convert.ToInt32(parts[0]); int portHigh = Convert.ToInt32(parts[1]); int testPort = 0; for (testPort = portLow; testPort <= portHigh; testPort++) { if ((!allocedPorts.Contains(testPort)) && IsPortAvailable(protocol, OpenGraphRequest.InterfaceAddress, testPort)) { break; } } if (testPort <= portHigh) { AppLogger.Message(String.Format("PortRange specified ({0},{1}) -- first available port was {2}", portLow, portHigh, testPort)); ClientURL = prefix + testPort.ToString(); BaseClientURL = BaseClientURL.Substring(0, BaseClientURL.LastIndexOf(@"$PortRange")) + testPort.ToString(); } else { throw new Exception(String.Format("No ports for protocol \"{0}\" available between {1} and {2}!", protocol, portLow, portHigh)); } } } AppLogger.Message(String.Format(" BaseClientURL = {0} ClientURL = {1}", BaseClientURL, ClientURL)); }