public void FindDeviceAsync(string SearchTarget) { HTTPMessage packet = new HTTPMessage(); packet.Directive = "M-SEARCH"; packet.DirectiveObj = "*"; packet.AddTag("ST", SearchTarget); packet.AddTag("MX", MX.ToString()); packet.AddTag("MAN", "\"ssdp:discover\""); packet.AddTag("HOST", "239.255.255.250:1900"); IPAddress[] localAddresses = this.NetInfo.GetLocalAddresses(); IPEndPoint dest = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 0x76c); for (int i = 0; i < localAddresses.Length; i++) { try { Socket theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); theSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); theSocket.Bind(new IPEndPoint(localAddresses[i], 0)); SSDPSession session = new SSDPSession(theSocket, new SSDPSession.ReceiveHandler(this.HandleAsyncSearch)); this.SSDPSessions[session] = session; session.SendTo(packet, dest); session.SendTo(packet, dest); this.Lifetime.Add(session, 7); } catch (Exception exception) { EventLogger.Log(this, EventLogEntryType.Error, "CP Failure: " + localAddresses[i].ToString()); EventLogger.Log(exception); } } }
private void HandleAsyncSearch(SSDPSession sender, HTTPMessage msg) { DText text = new DText(); string tag = msg.GetTag("Location"); int maxAge = 0; string str2 = msg.GetTag("Cache-Control").Trim(); if (str2 != "") { text.ATTRMARK = ","; text.MULTMARK = "="; text[0] = str2; for (int i = 1; i <= text.DCOUNT(); i++) { if (text[i, 1].Trim().ToUpper() == "MAX-AGE") { maxAge = int.Parse(text[i, 2].Trim()); break; } } } str2 = msg.GetTag("USN"); string uSN = str2.Substring(str2.IndexOf(":") + 1); string searchTarget = msg.GetTag("ST"); if (uSN.IndexOf("::") != -1) { uSN = uSN.Substring(0, uSN.IndexOf("::")); } EventLogger.Log(this, EventLogEntryType.SuccessAudit, msg.RemoteEndPoint.ToString()); if (this.OnSearch != null) { this.OnSearch(msg.RemoteEndPoint, msg.LocalEndPoint, new Uri(tag), uSN, searchTarget, maxAge); } }