private void EndReceiveOutgoing(IAsyncResult ar) { try { UdpClient UdpClient = (UdpClient)ar.AsyncState; IPEndPoint RemoteIP = null; byte[] Packet = UdpClient.EndReceive(ar, ref RemoteIP); string Header = Encoding.ASCII.GetString(Packet); UPnPHeaders Headers = new UPnPHeaders(Header); if (!(RemoteIP is null) && Headers.Direction == HttpDirection.Response && Headers.HttpVersion >= 1.0 && Headers.ResponseCode == 200) { if (!string.IsNullOrEmpty(Headers.Location)) { UPnPDeviceLocationEventHandler h = this.OnDeviceFound; if (!(h is null)) { DeviceLocation DeviceLocation = new DeviceLocation(this, Headers.SearchTarget, Headers.Server, Headers.Location, Headers.UniqueServiceName, Headers); DeviceLocationEventArgs e = new DeviceLocationEventArgs(DeviceLocation, (IPEndPoint)UdpClient.Client.LocalEndPoint, RemoteIP); try { h(this, e); } catch (Exception ex) { this.RaiseOnError(ex); } } } } else if (Headers.Direction == HttpDirection.Request && Headers.HttpVersion >= 1.0) { this.HandleIncoming(UdpClient, RemoteIP, Headers); } UdpClient.BeginReceive(this.EndReceiveOutgoing, UdpClient); }
private void EndReceiveOutgoing(IAsyncResult ar) { try { UdpClient UdpClient = (UdpClient)ar.AsyncState; IPEndPoint RemoteIP = null; byte[] Packet = UdpClient.EndReceive(ar, ref RemoteIP); string Header = Encoding.ASCII.GetString(Packet); UPnPHeaders Headers = new UPnPHeaders(Header); if (RemoteIP != null && Headers.Direction == HttpDirection.Response && Headers.HttpVersion >= 1.0 && Headers.ResponseCode == 200) { if (!string.IsNullOrEmpty(Headers.Location)) { UPnPDeviceLocationEventHandler h = this.OnDeviceFound; if (h != null) { DeviceLocation DeviceLocation = new DeviceLocation(this, Headers.SearchTarget, Headers.Server, Headers.Location, Headers.UniqueServiceName, Headers); DeviceLocationEventArgs e = new DeviceLocationEventArgs(DeviceLocation, (IPEndPoint)UdpClient.Client.LocalEndPoint, RemoteIP); try { h(this, e); } catch (Exception ex) { this.RaiseOnError(ex); } } } } else if (Headers.Direction == HttpDirection.Request && Headers.HttpVersion >= 1.0) this.HandleIncoming(UdpClient, RemoteIP, Headers); UdpClient.BeginReceive(this.EndReceiveOutgoing, UdpClient); } catch (Exception ex) { this.RaiseOnError(ex); } }
private void upnpClient_OnDeviceFound(UPnPClient Sender, DeviceLocationEventArgs e) { try { lock (this.ipAddressesFound) { if (this.ipAddressesFound.ContainsKey(e.RemoteEndPoint.Address)) return; this.ipAddressesFound[e.RemoteEndPoint.Address] = true; } e.Location.StartGetDevice(this.DeviceRetrieved, e); } catch (Exception ex) { this.exception = ex; this.State = PeerToPeerNetworkState.Error; } }