/// <summary> /// Provides an async method to request to retrieve edge server and client IP info. /// </summary> /// <param name="edgeServerRules">The rules to help define the request and how the response is parsed.</param> /// <param name="baseUri">The base Uri optionally used to create the request Uri. Only used if a pattern is specified in EdgeServerRules.</param> public static IAsyncResult BeginGetEdgeServer(EdgeServerRules edgeServerRules, Uri baseUri, AsyncCallback callback, object state) { if (edgeServerRules == null) { throw new ArgumentNullException("edgeServerRules"); } Uri ipRequestUri; if (baseUri != null) { ipRequestUri = new Uri(string.Format(CultureInfo.InvariantCulture, edgeServerRules.EdgeResolverUrlPattern, baseUri.Host, baseUri.Port), UriKind.Absolute); } else { ipRequestUri = new Uri(edgeServerRules.EdgeResolverUrlPattern, UriKind.Absolute); } // use the client networking stack so we can read headers HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(ipRequestUri); for (int i = 0; i < edgeServerRules.EdgeResolverHeaders.Count; i = i + 2) { string key = edgeServerRules.EdgeResolverHeaders[i]; string value = edgeServerRules.EdgeResolverHeaders[i + 1]; request.Headers[key] = value; } request.Method = "GET"; return(request.BeginGetResponse(callback, new object[] { request, edgeServerRules, state })); }
void SmoothStreamingSourceChanged(string url) { Uri currentStreamUri = new Uri(url, UriKind.Absolute); IsEdgeServerComplete = false; EdgeServer = EdgeServerDataClient.IpNA; // clear the current edge server ClientIP = EdgeServerDataClient.IpNA; if (Configuration.EdgeServerRuleCollection != null) { EdgeServerRules addressRules = Configuration.EdgeServerRuleCollection.FirstOrDefault(ai => ai.Domain != null && currentStreamUri.Host.EndsWith(ai.Domain, StringComparison.OrdinalIgnoreCase)); // fallback on the address rules without a domain if (addressRules == null) { addressRules = Configuration.EdgeServerRuleCollection.FirstOrDefault(ai => ai.Domain == null); } try { if (addressRules != null) { EdgeServerDataClient edgeServerDataClient = new EdgeServerDataClient(); edgeServerDataClient.GetEdgeServerCompleted += (s, e) => { // warning: this can come back after we've shut down, checking for the mediaelement is a good way to ensure we're still supposed to be tracking if (mediaElement != null) { if (e.Result != null) { EdgeServer = e.Result.EdgeServer; ClientIP = e.Result.ClientIP; } FinishSettingEdgeServer(); SmoothStreamingEvent entry = new SmoothStreamingEvent(); entry.EventType = EventType.AddressInfo; entry.Data1 = ClientIP.ToString(); entry.Data2 = EdgeServer; Enqueue(entry); } }; edgeServerDataClient.GetEdgeServerAsync(addressRules, currentStreamUri); } else { FinishSettingEdgeServer(); } } catch { FinishSettingEdgeServer(); } } else { FinishSettingEdgeServer(); } }
/// <summary> /// Loads a new instance of the EdgeServerRules class from Xml. /// </summary> /// <param name="reader"></param> /// <returns></returns> public static EdgeServerRules Load(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } var result = new EdgeServerRules(); reader.GoToElement(); if (!reader.IsEmptyElement) { result.Domain = reader.GetAttribute("Domain"); reader.ReadStartElement(); while (reader.GoToSibling()) { switch (reader.LocalName) { case "EdgeResolverUrlPattern": result.EdgeResolverUrlPattern = reader.ReadElementContentAsString(); break; case "EdgeRegEx": result.EdgeRegex = reader.ReadElementContentAsString(); break; case "ClientIpRegEx": result.ClientIPRegex = reader.ReadElementContentAsString(); break; case "ClientIpHeader": result.ClientIPHeader = reader.ReadElementContentAsString(); break; case "EdgeResolverHeaders": result.EdgeResolverHeaders = reader.ReadElementContentAsString().Split(','); break; case "EdgeHeader": result.EdgeHeader = reader.ReadElementContentAsString(); break; default: reader.Skip(); break; } } reader.ReadEndElement(); } else { reader.Skip(); } return(result); }
/// <summary> /// Loads a new instance of the EdgeServerRules class from Xml. /// </summary> /// <param name="reader"></param> /// <returns></returns> public static EdgeServerRules Load(XmlReader reader) { if (reader == null) throw new ArgumentNullException("reader"); var result = new EdgeServerRules(); reader.GoToElement(); if (!reader.IsEmptyElement) { result.Domain = reader.GetAttribute("Domain"); reader.ReadStartElement(); while (reader.GoToSibling()) { switch (reader.LocalName) { case "EdgeResolverUrlPattern": result.EdgeResolverUrlPattern = reader.ReadElementContentAsString(); break; case "EdgeRegEx": result.EdgeRegex = reader.ReadElementContentAsString(); break; case "ClientIpRegEx": result.ClientIPRegex = reader.ReadElementContentAsString(); break; case "ClientIpHeader": result.ClientIPHeader = reader.ReadElementContentAsString(); break; case "EdgeResolverHeaders": result.EdgeResolverHeaders = reader.ReadElementContentAsString().Split(','); break; case "EdgeHeader": result.EdgeHeader = reader.ReadElementContentAsString(); break; default: reader.Skip(); break; } } reader.ReadEndElement(); } else reader.Skip(); return result; }
/// <summary> /// Provides an async method to request to retrieve edge server and client IP info. /// </summary> /// <param name="edgeServerRules">The rules to help define the request and how the response is parsed.</param> /// <param name="baseUri">The base Uri optionally used to create the request Uri. Only used if a pattern is specified in EdgeServerRules.</param> public static IAsyncResult BeginGetEdgeServer(EdgeServerRules edgeServerRules, Uri baseUri, AsyncCallback callback, object state) { if (edgeServerRules == null) throw new ArgumentNullException("edgeServerRules"); Uri ipRequestUri; if (baseUri != null) ipRequestUri = new Uri(string.Format(CultureInfo.InvariantCulture, edgeServerRules.EdgeResolverUrlPattern, baseUri.Host, baseUri.Port), UriKind.Absolute); else ipRequestUri = new Uri(edgeServerRules.EdgeResolverUrlPattern, UriKind.Absolute); // use the client networking stack so we can read headers HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(ipRequestUri); for (int i = 0; i < edgeServerRules.EdgeResolverHeaders.Count; i = i + 2) { string key = edgeServerRules.EdgeResolverHeaders[i]; string value = edgeServerRules.EdgeResolverHeaders[i + 1]; request.Headers[key] = value; } request.Method = "GET"; return request.BeginGetResponse(callback, new object[] { request, edgeServerRules, state }); }
/// <summary> /// Initiates the async request to retrieve edge server and client IP info. /// </summary> /// <param name="edgeServerRules">The rules to help define the request and how the response is parsed.</param> /// <param name="baseUri">The base Uri optionally used to create the request Uri. Only used if a pattern is specified in EdgeServerRules.</param> public void GetEdgeServerAsync(EdgeServerRules edgeServerRules, Uri baseUri) { BeginGetEdgeServer(edgeServerRules, baseUri, new AsyncCallback(ReadCallback), null); }
/// <summary> /// Creates an instance of the main diagnostic config object from an XmlReader /// </summary> public static DiagnosticsConfig Load(XmlReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } var result = new DiagnosticsConfig(); List <EdgeServerRules> edgeServerRuleCollection = null; reader.GoToElement(); reader.ReadStartElement(); if (!reader.IsEmptyElement) { while (reader.GoToSibling()) { switch (reader.LocalName) { case "TraceMonitor": result.TracingConfig = TracingConfig.Load(reader); break; case "EdgeServerRules": if (!reader.IsEmptyElement) { if (edgeServerRuleCollection == null) { edgeServerRuleCollection = new List <EdgeServerRules>(); result.EdgeServerRuleCollection = edgeServerRuleCollection; } edgeServerRuleCollection.Add(EdgeServerRules.Load(reader)); } else { reader.Skip(); } break; case "Diagnostics": if (!reader.IsEmptyElement) { reader.ReadStartElement(); while (reader.GoToSibling()) { switch (reader.LocalName) { case "TrackQuality": result.TrackQuality = Convert.ToBoolean(reader.ReadElementContentAsInt()); break; case "TrackDownloadErrors": result.TrackDownloadErrors = Convert.ToBoolean(reader.ReadElementContentAsInt()); break; case "AggregationIntervalMilliseconds": result.AggregationInterval = TimeSpan.FromMilliseconds(reader.ReadElementContentAsInt()); break; case "TrackQualitySnapshot": result.TrackQualitySnapshot = Convert.ToBoolean(reader.ReadElementContentAsInt()); break; case "SnapshotIntervalMilliseconds": result.SnapshotInterval = TimeSpan.FromMilliseconds(reader.ReadElementContentAsInt()); break; case "LatencyAlertThreshold": result.LatencyAlertThreshold = reader.ReadElementContentAsFloat(); break; case "RecordTraceLogs": result.RecordTraceLogs = Convert.ToBoolean(reader.ReadElementContentAsInt()); break; case "QualityTracking": result.QualityConfig = QualityConfig.Load(reader); break; default: reader.Skip(); break; } } reader.ReadEndElement(); } else { reader.Skip(); } break; default: reader.Skip(); break; } } reader.ReadEndElement(); } else { reader.Skip(); } return(result); }
/// <summary> /// The call back used in combination with BeginGetEdgeServer /// </summary> /// <returns>The result of the edge server detection request.</returns> public static EdgeServerResult EndGetEdgeServer(IAsyncResult asynchronousResult) { if (asynchronousResult == null) { throw new ArgumentNullException("asynchronousResult"); } object[] args = (object[])asynchronousResult.AsyncState; HttpWebRequest request = (HttpWebRequest)args[0]; EdgeServerRules edgeServerRules = (EdgeServerRules)args[1]; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); string result; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { result = reader.ReadToEnd(); } string thingToParse; // get the edge server string edgeServer = ""; thingToParse = result; if (edgeServerRules.EdgeHeader != null) { if (response.SupportsHeaders) { thingToParse = response.Headers[edgeServerRules.EdgeHeader].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); } } if (edgeServerRules.EdgeRegex != null && thingToParse != null) { Regex regex = new Regex(edgeServerRules.EdgeRegex); if (regex.IsMatch(thingToParse)) { var matches = regex.Matches(thingToParse); edgeServer = matches[0].Value; } } // get the client IP string clientIP = IpNA; thingToParse = result; if (edgeServerRules.ClientIPHeader != null) { if (response.SupportsHeaders) { thingToParse = response.Headers[edgeServerRules.ClientIPHeader]; } } if (edgeServerRules.ClientIPRegex != null && thingToParse != null) { Regex regex = new Regex(edgeServerRules.ClientIPRegex); if (regex.IsMatch(thingToParse)) { var matches = regex.Matches(thingToParse); clientIP = matches[0].Value; } } return(new EdgeServerResult(edgeServer, clientIP)); }