Exemplo n.º 1
0
        /// <summary>
        /// Static method to create a DACPPairingDatabase and try and load XML
        /// file from disk located at EXENAME.xml in the same directory.
        /// </summary>
        /// <returns>a DACPPairingDatabase object</returns>
        public static DACPPairingDatabase Initialize(DACPServer dacpServer)
        {
            Console.WriteLine("Initializing DACPPairingDatabase");
            DACPPairingDatabase database = new DACPPairingDatabase();

            database.Server = dacpServer;

            string fileName = database.GetFileName();

            if (File.Exists(fileName))
            {
                Console.WriteLine("Deserializing XML file: {0}", fileName);

                try {
                    DACPPairingDatabaseSerializer serializer = new DACPPairingDatabaseSerializer();
                    //XmlSerializer serializer = new XmlSerializer(CLAZZ_TYPE);
                    using (FileStream stream = new FileStream(fileName, FileMode.Open)) {
                        database        = (DACPPairingDatabase)serializer.Deserialize(stream);
                        database.Server = dacpServer;
                    }
                } catch (Exception ex) {
                    Console.WriteLine("XML Exception", ex);
                    database        = new DACPPairingDatabase();
                    database.Server = dacpServer;
                }
            }

            return(database);
        }
        /// <summary>
        /// Constructor creates the mDNS service listener.
        /// </summary>
        public DACPPairingServer(DACPServer server)
        {
            Console.WriteLine("Initializing DACPPairingServer...");
            this.dacpServer = server;
            nsBrowser.AllowMultithreadedCallbacks = true;
            nsBrowser.DidFindService   += new NetServiceBrowser.ServiceFound(NetServiceBrowser_DidFindService);
            nsBrowser.DidRemoveService += new NetServiceBrowser.ServiceRemoved(NetServiceBrowser_DidRemoveService);

            try {
                Console.WriteLine("mDNS Version: {0}", NetService.DaemonVersion);
                nsBrowser.SearchForService(SERVICE_TYPE, "");
            } catch (Exception ex) {
                Console.WriteLine("Apple Bonjour is not installed!", ex);
                throw new DACPBonjourException("Bonjour is required by this application and it was not found.  Please install Bonjour For Windowsfrom Apple.com");
            }
        }
        /// <summary>
        /// Performs an HTTP GET on 1024 of the client to give it back the
        /// proper pairing code.
        /// </summary>
        /// <param name="service">the NetService to call back</param>
        /// <param name="passCode">the passcode to pair with</param>
        public void PairService(NetService service, string passCode)
        {
            Console.WriteLine("Attempting to Pair Service: {0}", service.Name);
            try {
                foreach (System.Net.IPEndPoint ep in service.Addresses)
                {
                    Console.WriteLine("Pairing Service: {0}", service.ToString());
                    string pairingClient = ep.ToString();
                    if (pairingClient.StartsWith("0.0.0.0"))
                    {
                        pairingClient = pairingClient.Replace("0.0.0.0", "localhost");
                    }
                    Console.WriteLine("IP: {0}", pairingClient);

                    byte[]      txt  = service.TXTRecordData;
                    IDictionary dict = NetService.DictionaryFromTXTRecordData(txt);
                    string      pair = String.Empty;
                    if (dict.Contains("Pair"))
                    {
                        pair = new UTF8Encoding(false).GetString((byte[])dict["Pair"]);
                    }
                    else
                    {
                        throw new InvalidOperationException("mDNS Service did not contain record for 'Pair'.");
                    }
                    string pairingCode = Pair(pair, passCode);
                    Console.WriteLine("Pairing Code = {0}", pairingCode);

                    string requestUrl = String.Format("http://{0}/pair?pairingcode={1}&servicename={2}", pairingClient, pairingCode, this.dacpServer.ServiceName);

                    byte[] data = new byte[24];
                    using (WebClient client = new WebClient()) {
                        try {
                            Console.WriteLine("Pairing Client Request = {0}", requestUrl);
                            data = client.DownloadData(requestUrl);
                            Console.WriteLine("Pairing Client Responded");
                        } catch (WebException ex) {
                            Console.WriteLine("No Response from Pairing!!!");
                            if (ex.Status == WebExceptionStatus.ProtocolError)
                            {
                                if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
                                {
                                    throw new DACPPairingException("Invalid PIN code specified");
                                }
                            }

                            throw new DACPPairingException("Unable to complete pairing", ex);
                        }
                    }

                    PairingReply reply;
                    try {
                        reply = new PairingReply(data);

                        ulong guid = reply.PairingGuid;
                        Console.WriteLine("Remote Device GUID After = {0}", guid);

                        if (DACPServer.PairingDatabase.DACPClients.ContainsKey(guid))
                        {
                            DACPServer.PairingDatabase.DACPClients.Remove(guid);
                        }
                        DACPServer.PairingDatabase.DACPClients.Add(guid, DEFAULT_SESSION_ID);

                        // now we must release the latch and allow /login to respond
                        DACPServer.ReleaseAllLatches();
                    } catch (Exception ex) {
                        Console.WriteLine("Error parsing Pairing Reply!");
                        throw new DACPPairingException("Unexpected reply from device", ex);
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("HTTP GET Error", ex);
            }
        }