Beispiel #1
0
        private void SubscribeConnect(IAsyncResult aAsync)
        {
            Trace.WriteLine(Trace.kUpnp, "Subscribe        End Connect");

            try
            {
                iRequest.EndConnect(aAsync);

                // The event URI might also be required to include the URI fragment
                Upnp.WriteMethodSubscribe(iWriter, iEventUri.PathAndQuery);
                Upnp.WriteHost(iWriter, iEventUri.Host + ":" + iEventUri.Port.ToString());
                Upnp.WriteCallback(iWriter, Server.Uri);
                Upnp.WriteNotificationType(iWriter);
                Upnp.WriteTimeout(iWriter, kSubscriptionTimeout);

                iWriter.WriteFlush();

                Trace.WriteLine(Trace.kUpnp, "Subscribe        Begin Wait For Data");

                iRequest.BeginWaitForData(SubscribeWaitForData);
            }
            catch (Exception ex)
            {
                HandleSubscriptionException("SubscribeConnect", ex);
            }
        }
Beispiel #2
0
        private XmlNode DeviceXmlExplicit()
        {
            string xml = DeviceXml;

            if (xml != null)
            {
                return(Upnp.DeviceXmlExplicit(xml, Udn));
            }

            return(null);
        }
Beispiel #3
0
        public object WriteEnd(GetResponseStreamCallback aCallback)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] message = encoding.GetBytes(iWriteDocument.OuterXml);

            HttpWebRequest request   = null;
            Stream         reqStream = null;

            try
            {
                request           = WebRequest.Create(iService.ControlUri.AbsoluteUri) as HttpWebRequest;
                request.KeepAlive = false;
                request.Pipelined = false;
                request.Proxy     = iService.WebProxy;

                request.Method      = "POST";
                request.ContentType = "text/xml; charset=\"utf-8\"";
                request.Headers.Add("SOAPACTION", string.Format("\"{0}#{1}\"", Upnp.ServiceTypeToString(iService.Type), iAction.Name));
                request.ContentLength    = message.Length;
                request.Timeout          = kControlInvokeTimeout;
                request.ReadWriteTimeout = kControlInvokeTimeout;

                if (aCallback == null)
                {
                    reqStream = request.GetRequestStream();
                    reqStream.Write(message, 0, message.Length);
                }
                else
                {
                    WebRequestPool.QueueJob(new JobSendRequest(aCallback, request, message));
                }
            }
            finally
            {
                if (aCallback == null && reqStream != null)
                {
                    reqStream.Close();
                    reqStream.Dispose();
                }

                iWriteDocument = null;
                iActionNode    = null;
            }

            return(request);
        }
Beispiel #4
0
        public static uint HasService(XmlNode aDevice, ServiceType aType)
        {
            if (aDevice != null)
            {
                XmlNamespaceManager nsmanager = new XmlNamespaceManager(aDevice.OwnerDocument.NameTable);
                nsmanager.AddNamespace("u", "urn:schemas-upnp-org:device-1-0");

                foreach (XmlNode s in aDevice.SelectNodes("u:serviceList/u:service/u:serviceType", nsmanager))
                {
                    ServiceType type = Upnp.StringToServiceType(s.FirstChild.Value);

                    if (aType.IsSupportedBy(type))
                    {
                        return(type.Version);
                    }
                }
            }

            return(0);
        }
Beispiel #5
0
        private void RenewConnect(IAsyncResult aAsync)
        {
            try
            {
                iRequest.EndConnect(aAsync);

                iMutex.WaitOne();

                string sid = iSubscriptionId;

                iMutex.ReleaseMutex();

                if (sid != null)
                {
                    Upnp.WriteMethodSubscribe(iWriter, iEventUri.AbsolutePath);
                    Upnp.WriteHost(iWriter, iEventUri.Host + ":" + iEventUri.Port.ToString());
                    Upnp.WriteSid(iWriter, iSubscriptionId);
                    Upnp.WriteTimeout(iWriter, kSubscriptionTimeout);

                    iWriter.WriteFlush();

                    iRequest.BeginWaitForData(RenewWaitForData);
                }
                else
                {
                    iRequest.Close();
                    iSubscribing        = false;
                    iPendingSubscribe   = false;
                    iUnsubscribing      = false;
                    iPendingUnsubscribe = false;
                    iSubscriptionId     = null;
                }
            }
            catch (Exception ex)
            {
                HandleSubscriptionException("RenewConnect", ex);
            }
        }
Beispiel #6
0
        private void UnsubscribeConnect(IAsyncResult aAsync)
        {
            Trace.WriteLine(Trace.kUpnp, "Unsubscribe      End Connect");

            try
            {
                iRequest.EndConnect(aAsync);

                iMutex.WaitOne();

                string sid = iSubscriptionId;

                iMutex.ReleaseMutex();

                if (sid != null)
                {
                    Upnp.WriteMethodUnsubscribe(iWriter, iEventUri.AbsolutePath);
                    Upnp.WriteHost(iWriter, iEventUri.Host + ":" + iEventUri.Port.ToString());
                    Upnp.WriteSid(iWriter, sid);
                    iWriter.WriteFlush();

                    Trace.WriteLine(Trace.kUpnp, "Unsubscribe      Begin Wait For Data");

                    iRequest.BeginWaitForData(UnsubscribeWaitForData);
                }
                else
                {
                    Trace.WriteLine(Trace.kUpnp, "Unsubscribe      Killed");

                    iRequest.Close();
                }
            }
            catch (Exception ex)
            {
                HandleSubscriptionException("UnsubscribeConnect", ex);
            }
        }
Beispiel #7
0
        public override ServiceLocation FindServiceLocation(ServiceType aType)
        {
            try
            {
                // remove hardcoded shortcut to linn devices for volkano 2 support
                //if (IsLinn)
                //{
                //    // this is a volkano device, so try to infer control and subscription uris
                //    Uri uri = new Uri(Find(kKeyUpnpLocation));

                //    if (uri.AbsolutePath != "/")
                //    {
                //        // this is a bute or post-bute volkano device from which the control and
                //        // subscription uris can be inferred

                //        Uri serviceuri = new Uri(uri, aType.Type + "/");
                //        Uri controluri = new Uri(serviceuri, kVolkanoRelativeControlUri);
                //        Uri subscriptionuri = new Uri(serviceuri, kVolkanoRelativeSubscriptionUri);

                //        return (new ServiceLocationUpnp(controluri, subscriptionuri));
                //    }
                //}

                // this is not a volkano device or it is a pre-bute volkano device, so
                // must inspect the device xml in order to find the control and subscription uris

                string servicetype = Upnp.ServiceTypeToString(aType);

                XmlNode device = DeviceXmlExplicit();

                if (device != null)
                {
                    XmlNamespaceManager nsmanager = new XmlNamespaceManager(device.OwnerDocument.NameTable);
                    nsmanager.AddNamespace("u", "urn:schemas-upnp-org:device-1-0");

                    // collect the URLBase if there is one

                    Uri rooturi;

                    XmlNode root = device.SelectSingleNode("/u:root/u:URLBase", nsmanager);

                    try
                    {
                        if (root == null)
                        {
                            rooturi = new Uri(Find(kKeyUpnpLocation));
                        }
                        else
                        {
                            rooturi = new Uri(root.FirstChild.Value);
                        }
                    }
                    catch (UriFormatException)
                    {
                        throw (new DeviceException(104, "URLBase malformed"));
                    }

                    // look in the root device for the service

                    XmlNodeList list;

                    list = device.SelectNodes("u:serviceList/u:service", nsmanager);

                    foreach (XmlNode n in list)
                    {
                        XmlNode type = n.SelectSingleNode("u:serviceType", nsmanager);

                        if (type != null && type.FirstChild != null)
                        {
                            ServiceType found = new ServiceType(type.FirstChild.Value);
                            ServiceType mine  = new ServiceType(servicetype);

                            if (mine.IsSupportedBy(found))
                            {
                                return(GetServiceLocation(n, nsmanager, rooturi));
                            }
                        }
                    }
                }

                throw (new DeviceException(101, servicetype + " not found in device xml"));
            }
            catch (Exception ex)
            {
                // logging for ticket #876
                UserLog.WriteLine("Unhandled exception in FindServiceLocation : " + ex);
                throw ex;
            }
        }
Beispiel #8
0
 public override uint HasService(ServiceType aType)
 {
     return(Upnp.HasService(DeviceXmlExplicit(), aType));
 }
Beispiel #9
0
        public void WriteBegin()
        {
            iWriteDocument = new XmlDocument();

            XmlElement envelope = iWriteDocument.CreateElement("s:Envelope", "http://schemas.xmlsoap.org/soap/envelope/");

            envelope.SetAttribute("encodingStyle", "http://schemas.xmlsoap.org/soap/envelope/", "http://schemas.xmlsoap.org/soap/encoding/");
            XmlElement body = iWriteDocument.CreateElement("s:Body", "http://schemas.xmlsoap.org/soap/envelope/");

            iActionNode = iWriteDocument.CreateElement(string.Format("u:{0}", iAction.Name), Upnp.ServiceTypeToString(iService.Type));

            body.AppendChild(iActionNode);
            envelope.AppendChild(body);
            iWriteDocument.AppendChild(envelope);
        }
Beispiel #10
0
        public void ReadBegin(object aResult)
        {
            HttpWebRequest request = aResult as HttpWebRequest;

            HttpWebResponse response  = null;
            Stream          resStream = null;

            try
            {
                response  = request.GetResponse() as HttpWebResponse;
                resStream = response.GetResponseStream();

                iReadDocument = new XmlDocument();
                iReadDocument.Load(resStream);

                XmlNamespaceManager xmlNsMan = new XmlNamespaceManager(iReadDocument.NameTable);
                xmlNsMan.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    xmlNsMan.AddNamespace("u", Upnp.ServiceTypeToString(iService.Type));
                    XmlNodeList nodeList = iReadDocument.SelectNodes(string.Format("/s:Envelope/s:Body/u:{0}Response", iAction.Name), xmlNsMan);
                    if (nodeList.Count == 1)
                    {
                        iResponse = nodeList[0].ChildNodes;
                        iIndex    = 0;
                    }
                    else
                    {
                        throw new ServiceException(600, "Invalid response: " + iReadDocument.OuterXml);
                    }
                }
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    response  = e.Response as HttpWebResponse;
                    resStream = response.GetResponseStream();

                    if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        XmlDocument document = new XmlDocument();
                        document.Load(resStream);

                        XmlNamespaceManager xmlNsMan = new XmlNamespaceManager(document.NameTable);
                        xmlNsMan.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
                        xmlNsMan.AddNamespace("c", "urn:schemas-upnp-org:control-1-0");

                        string  faultCode     = string.Empty;
                        XmlNode faultCodeNode = document.SelectSingleNode("/s:Envelope/s:Body/s:Fault/faultcode", xmlNsMan);
                        if (faultCodeNode != null)
                        {
                            faultCode = faultCodeNode.InnerText;
                        }
                        string  faultString     = string.Empty;
                        XmlNode faultStringNode = document.SelectSingleNode("/s:Envelope/s:Body/s:Fault/faultstring", xmlNsMan);
                        if (faultStringNode != null)
                        {
                            faultString = faultStringNode.InnerText;
                        }
                        XmlNode detail = document.SelectSingleNode("/s:Envelope/s:Body/s:Fault/detail", xmlNsMan);
                        throw new SoapException(faultString, new XmlQualifiedName(faultCode), string.Empty, detail);
                    }

                    throw new ServiceException(600, "Invalid response: " + response.StatusDescription);
                }

                throw new Exception(e.Status.ToString());
            }
            finally
            {
                if (resStream != null)
                {
                    resStream.Close();
                    resStream.Dispose();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }