Exemple #1
0
 /// <summary>
 /// Contains information about the location of a device on the network.
 /// </summary>
 /// <param name="Client">UPnP Client</param>
 /// <param name="Headers">All headers in notification.</param>
 /// <param name="LocalEndPoint">Local End Point.</param>
 /// <param name="RemoteEndPoint">Remote End Point.</param>
 internal NotificationEventArgs(UPnPClient Client, UPnPHeaders Headers, IPEndPoint LocalEndPoint, IPEndPoint RemoteEndPoint)
 {
     this.client         = Client;
     this.headers        = Headers;
     this.localEndPoint  = LocalEndPoint;
     this.remoteEndPoint = RemoteEndPoint;
 }
Exemple #2
0
        private void HandleIncoming(UdpClient UdpClient, IPEndPoint RemoteIP, UPnPHeaders Headers)
        {
            switch (Headers.Verb)
            {
            case "M-SEARCH":
                NotificationEventHandler h = this.OnSearch;
                if (!(h is null))
                {
                    try
                    {
                        h(this, new NotificationEventArgs(this, Headers, (IPEndPoint)UdpClient.Client.LocalEndPoint, RemoteIP));
                    }
                    catch (Exception ex)
                    {
                        this.RaiseOnError(ex);
                    }
                }
                break;

            case "NOTIFY":
                h = this.OnNotification;
                if (!(h is null))
                {
                    try
                    {
                        h(this, new NotificationEventArgs(this, Headers, (IPEndPoint)UdpClient.Client.LocalEndPoint, RemoteIP));
                    }
                    catch (Exception ex)
                    {
                        this.RaiseOnError(ex);
                    }
                }
                break;
            }
        }
 /// <summary>
 /// Contains information about the location of a device on the network.
 /// </summary>
 /// <param name="Client">UPnP Client.</param>
 /// <param name="SearchTarget">SSDP Search Target</param>
 /// <param name="Server">Server</param>
 /// <param name="Location">Location of device information</param>
 /// <param name="UniqueServiceName">Unique Service Name (USN)</param>
 /// <param name="Headers">All headers in response.</param>
 internal DeviceLocation(UPnPClient Client, string SearchTarget, string Server, string Location, string UniqueServiceName, UPnPHeaders Headers)
 {
     this.client            = Client;
     this.searchTarget      = SearchTarget;
     this.server            = Server;
     this.location          = Location;
     this.uniqueServiceName = UniqueServiceName;
     this.headers           = Headers;
 }
Exemple #4
0
        private async void BeginReceiveIncoming(UdpClient Client)
        {
            try
            {
                while (!this.disposed)
                {
                    UdpReceiveResult Data = await Client.ReceiveAsync();

                    if (this.disposed)
                    {
                        return;
                    }

                    byte[] Packet = Data.Buffer;
                    this.ReceiveBinary(Packet);

                    if (this.disposed)
                    {
                        return;
                    }

                    try
                    {
                        string      Header  = Encoding.ASCII.GetString(Packet);
                        UPnPHeaders Headers = new UPnPHeaders(Header);

                        this.ReceiveText(Header);

                        if (Data.RemoteEndPoint != null &&
                            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);
            }
        }
Exemple #5
0
        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);
            }
        }