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); } }
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).ConfigureAwait(false); // Notify that we successfully paired PairingComplete.RaiseOnUIThread(null, new PairingCompleteEventArgs(servicename, pairingCode.ToString("X16"))); } else { await e.Request.SendResponse(HttpStatusCode.NotFound, string.Empty).ConfigureAwait(false); } }