internal void OnActionCalled(CpAction action, IList <object> inParams, object clientState) { if (!action.IsConnected) { throw new UPnPDisconnectedException("Action '{0}' is not connected to a UPnP network action", action.FullQualifiedName); } CpService service = action.ParentService; ServiceDescriptor sd = GetServiceDescriptor(service); string message = SOAPHandler.EncodeCall(action, inParams, _rootDescriptor.SSDPRootEntry.UPnPVersion); HttpWebRequest request = CreateActionCallRequest(sd, action); ActionCallState state = new ActionCallState(action, clientState, request); state.SetRequestMessage(message); lock (_cpData.SyncObj) _pendingCalls.Add(state); IAsyncResult result = state.Request.BeginGetResponse(OnCallResponseReceived, state); NetworkHelper.AddTimeout(request, result, PENDING_ACTION_CALL_TIMEOUT * 1000); }
private void OnCallResponseReceived(IAsyncResult ar) { ActionCallState state = (ActionCallState)ar.AsyncState; lock (_cpData.SyncObj) _pendingCalls.Remove(state); HttpWebResponse response = null; Stream body = null; try { Encoding contentEncoding; try { response = (HttpWebResponse)state.Request.EndGetResponse(ar); body = CompressionHelper.Decompress(response); string mediaType; if (!EncodingUtils.TryParseContentTypeEncoding(response.ContentType, Encoding.UTF8, out mediaType, out contentEncoding) || mediaType != "text/xml") { SOAPHandler.ActionFailed(state.Action, state.ClientState, "Invalid content type"); return; } } catch (WebException e) { response = (HttpWebResponse)e.Response; if (response == null) { SOAPHandler.ActionFailed(state.Action, state.ClientState, string.Format("Network error when invoking action '{0}': {1}", state.Action.Name, e.Message)); } else if (response.StatusCode == HttpStatusCode.InternalServerError) { string mediaType; if (!EncodingUtils.TryParseContentTypeEncoding(response.ContentType, Encoding.UTF8, out mediaType, out contentEncoding) || mediaType != "text/xml") { SOAPHandler.ActionFailed(state.Action, state.ClientState, "Invalid content type"); return; } using (TextReader reader = new StreamReader(response.GetResponseStream(), contentEncoding)) SOAPHandler.HandleErrorResult(reader, state.Action, state.ClientState); } else { SOAPHandler.ActionFailed(state.Action, state.ClientState, string.Format("Network error {0} when invoking action '{1}'", response.StatusCode, state.Action.Name)); } return; } UPnPVersion uPnPVersion; lock (_cpData.SyncObj) uPnPVersion = _rootDescriptor.SSDPRootEntry.UPnPVersion; SOAPHandler.HandleResult(body, contentEncoding, state.Action, state.ClientState, uPnPVersion); } finally { if (body != null) { body.Dispose(); } if (response != null) { response.Close(); } } }
internal void OnActionCalled(CpAction action, IList<object> inParams, object clientState) { if (!action.IsConnected) throw new UPnPDisconnectedException("Action '{0}' is not connected to a UPnP network action", action.FullQualifiedName); CpService service = action.ParentService; ServiceDescriptor sd = GetServiceDescriptor(service); string message = SOAPHandler.EncodeCall(action, inParams, _rootDescriptor.SSDPRootEntry.UPnPVersion); HttpWebRequest request = CreateActionCallRequest(sd, action); ActionCallState state = new ActionCallState(action, clientState, request); state.SetRequestMessage(message); lock (_cpData.SyncObj) _pendingCalls.Add(state); IAsyncResult result = state.Request.BeginGetResponse(OnCallResponseReceived, state); NetworkHelper.AddTimeout(request, result, PENDING_ACTION_CALL_TIMEOUT * 1000); }