private static async void HttpServer_RequestReceived(object sender, HttpRequestEventArgs e) { if (sender != _httpServer) { return; } if (IsValidPairingRequest(e.Request)) { string servicename = e.Request.QueryString["servicename"]; _log.Info("Paired with service ID: " + servicename); // Stop HTTP server and Bonjour publisher Stop(); // Generate new pairing code ulong pairingCode = GetRandomUInt64(); // Build response List <byte> bodyBytes = new List <byte>(); bodyBytes.AddRange(GetDACPFormattedBytes("cmpg", pairingCode)); bodyBytes.AddRange(GetDACPFormattedBytes("cmnm", DeviceName)); bodyBytes.AddRange(GetDACPFormattedBytes("cmty", "iPhone")); byte[] responseBytes = GetDACPFormattedBytes("cmpa", bodyBytes.ToArray()); // Send response HttpResponse response = new HttpResponse(); response.Body.Write(responseBytes, 0, responseBytes.Length); await e.Request.SendResponse(response); // Save server connection info ServerConnectionInfo info = new ServerConnectionInfo(); info.ServiceID = servicename; info.PairingCode = pairingCode.ToString("X16"); ServerManager.AddServerInfo(info); PairingComplete.Raise(null, new ServerConnectionInfoEventArgs(info)); } else { await e.Request.SendResponse(HttpStatusCode.NotFound, string.Empty); } }
protected async void ConnectToServer() { if (_server != null) { return; } if (!HasValidData()) { return; } string host; int port; ParseHostname(out host, out port); string pairingCode = string.Format("{0:0000}{0:0000}{0:0000}{0:0000}", pinTextBox.IntValue.Value); try { _server = new DACPServer(host, port, pairingCode); } catch { MessageBox.Show(LocalizedStrings.LibraryConnectionErrorBody, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK); _server = null; UpdateWizardItem(true); return; } UpdateWizardItem(true); var result = await _server.ConnectAsync(); switch (result) { case ConnectionResult.Success: // Save the server connection info ServerConnectionInfo info = new ServerConnectionInfo(); info.Name = _server.LibraryName; info.PairingCode = _server.PairingCode; info.LastHostname = _server.Hostname; info.LastIPAddress = _server.Hostname; info.LastPort = _server.Port; // Get the service ID for Bonjour // In iTunes 10.1 and later, the service name comes from ServiceID (parameter aeIM). // In foo_touchremote the service name is the same as the database ID (parameter mper). // In MonkeyTunes, the service ID is not available from the database query. TODO. if (_server.MainDatabase.ServiceID > 0) { info.ServiceID = _server.MainDatabase.ServiceID.ToString("x16").ToUpper(); } else { info.ServiceID = _server.MainDatabase.PersistentID.ToString("x16").ToUpper(); } ServerManager.AddServerInfo(info); ServerManager.ChooseServer(info); Hide(); NavigationManager.GoToFirstPage(); break; case ConnectionResult.InvalidPIN: MessageBox.Show(LocalizedStrings.LibraryPINErrorBody, LocalizedStrings.LibraryPINErrorTitle, MessageBoxButton.OK); _server = null; UpdateWizardItem(true); break; case ConnectionResult.ConnectionError: MessageBox.Show(LocalizedStrings.LibraryConnectionErrorBody, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK); _server = null; UpdateWizardItem(true); break; } }
private async void HandleServerConnection(Task <ConnectionResult> task) { var result = await task; switch (result) { case ConnectionResult.Success: _log.Info("Successfully connected to server at {0}:{1}", _server.Hostname, _server.Port); // Notify the pairing utility so it can close if (_currentUtility != null) { _currentUtility.SendPairedNotification(_server.PairingCode); } // Save the server connection info ServerConnectionInfo info = new ServerConnectionInfo(); info.Name = _server.LibraryName; info.ServiceID = _libraryService.Name; info.PairingCode = _server.PairingCode; info.LastHostname = _libraryService.Hostname; info.LastIPAddress = _server.Hostname; info.LastPort = _server.Port; _server = null; ServerManager.AddServerInfo(info); ServerManager.ChooseServer(info); Hide(); NavigationManager.GoToFirstPage(); break; case ConnectionResult.InvalidPIN: MessageBox.Show(LocalizedStrings.LibraryPINErrorBody, LocalizedStrings.LibraryPINErrorTitle, MessageBoxButton.OK); _server = null; UpdateWizardItem(true); break; case ConnectionResult.ConnectionError: // Check whether there are any other IP addresses we could try var ipStrings = _libraryService.IPAddresses.Select(ip => ip.ToString()).ToList(); var ipIndex = ipStrings.IndexOf(_server.Hostname); if (ipIndex >= 0 && ipIndex < (ipStrings.Count - 1)) { ipIndex++; string nextIP = ipStrings[ipIndex]; _server.Hostname = nextIP; _server.Port = _libraryService.Port; _log.Info("Retrying connection on new IP: {0}:{1}", _server.Hostname, _server.Port); HandleServerConnection(_server.ConnectAsync()); return; } // No other IPs, so we can't do anything else // TODO: Display error _server = null; UpdateWizardItem(true); break; } }