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 async void BeginReceiveOutgoing(UdpClient Client) { try { while (!this.disposed) { UdpReceiveResult Data = await Client.ReceiveAsync(); if (this.disposed) { return; } byte[] Packet = Data.Buffer; this.ReceiveBinary(Packet); try { string Header = Encoding.ASCII.GetString(Packet); UPnPHeaders Headers = new UPnPHeaders(Header); this.ReceiveText(Header); if (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)Client.Client.LocalEndPoint, Data.RemoteEndPoint); try { h(this, e); } catch (Exception ex) { this.RaiseOnError(ex); } } } } else if (Headers.Direction == HttpDirection.Request && Headers.HttpVersion >= 1.0) { this.HandleIncoming(Client, Data.RemoteEndPoint, Headers); } } catch (Exception ex) { this.RaiseOnError(ex); } } } catch (ObjectDisposedException) { // Closed. } catch (Exception ex) { this.Exception(ex); } }