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); }
internal DeviceLocationEventArgs(DeviceLocation Location, IPEndPoint LocalEndPoint, IPEndPoint RemoteEndPoint) { this.location = Location; this.localEndPoint = LocalEndPoint; this.remoteEndPoint = RemoteEndPoint; }
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); } }