Skip to content

Commit

Permalink
We've received contact from Ceton to say that they don't think their …
Browse files Browse the repository at this point in the history
…HTTP stack supports keep-alive... so I've parametised that setting. Also some tweaks to double-triple check that HttpWebRequest instance handling is correct.

(MP2-368)
  • Loading branch information
mm1352000 authored and morpheusxx committed Aug 4, 2013
1 parent 5ca9cbd commit 871551a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
25 changes: 18 additions & 7 deletions MediaPortal/Source/Core/UPnP/Infrastructure/CP/DeviceConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Text;
using MediaPortal.Utilities.Exceptions;
using MediaPortal.Utilities.Network;
Expand Down Expand Up @@ -97,12 +98,16 @@ public void SetRequestMessage(string message)
{
try
{
StreamWriter sw = new StreamWriter(_httpWebRequest.GetRequestStream(), UPnPConsts.UTF8_NO_BOM);
sw.Write(message);
sw.Close();
using (Stream s = _httpWebRequest.GetRequestStream())
using (StreamWriter sw = new StreamWriter(s, UPnPConsts.UTF8_NO_BOM))
{
sw.Write(message);
sw.Close();
}
}
catch (Exception e)
{
_httpWebRequest.Abort();
throw new UPnPRemoteException(new UPnPError(501, "Error writing action call document: " + e.Message));
}
}
Expand All @@ -115,6 +120,7 @@ public void SetRequestMessage(string message)
protected CpDevice _device;
protected GENAClientController _genaClientController;
protected ICollection<AsyncWebRequestState> _pendingCalls = new List<AsyncWebRequestState>();
protected bool _useHttpKeepAlive;

/// <summary>
/// Creates a new <see cref="DeviceConnection"/> to the UPnP device contained in the given
Expand All @@ -125,13 +131,15 @@ public void SetRequestMessage(string message)
/// <param name="deviceUuid">UUID of the UPnP device to connect.</param>
/// <param name="cpData">Shared control point data structure.</param>
/// <param name="dataTypeResolver">Delegate method to resolve extended datatypes.</param>
/// <param name="useHttpKeepAlive"><c>True</c> to set the HTTP keep-alive header in action requests sent over the connection, otherwise <c>false</c>.</param>
public DeviceConnection(UPnPControlPoint controlPoint, RootDescriptor rootDescriptor, string deviceUuid,
CPData cpData, DataTypeResolverDlgt dataTypeResolver)
CPData cpData, DataTypeResolverDlgt dataTypeResolver, bool useHttpKeepAlive)
{
_controlPoint = controlPoint;
_cpData = cpData;
_rootDescriptor = rootDescriptor;
_deviceUUID = deviceUuid;
_useHttpKeepAlive = useHttpKeepAlive;
_genaClientController = new GENAClientController(_cpData, this, rootDescriptor.SSDPRootEntry.PreferredLink.Endpoint, rootDescriptor.SSDPRootEntry.UPnPVersion);
BuildDeviceProxy(rootDescriptor, deviceUuid, dataTypeResolver);
_genaClientController.Start();
Expand Down Expand Up @@ -241,7 +249,8 @@ private void OnCallResponseReceived(IAsyncResult ar)
SOAPHandler.ActionFailed(state.Action, state.ClientState, "Invalid content type");
return;
}
using (TextReader reader = new StreamReader(response.GetResponseStream(), contentEncoding))
using (Stream s = response.GetResponseStream())
using (TextReader reader = new StreamReader(s, contentEncoding))
SOAPHandler.HandleErrorResult(reader, state.Action, state.ClientState);
}
else
Expand Down Expand Up @@ -284,14 +293,16 @@ internal void OnUnsubscribeEvents(CpService service)
_genaClientController.UnsubscribeEvents(subscription);
}

protected static HttpWebRequest CreateActionCallRequest(ServiceDescriptor sd, CpAction action)
protected HttpWebRequest CreateActionCallRequest(ServiceDescriptor sd, CpAction action)
{
LinkData preferredLink = sd.RootDescriptor.SSDPRootEntry.PreferredLink;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(new Uri(
new Uri(preferredLink.DescriptionLocation), sd.ControlURL));
NetworkUtils.SetLocalEndpoint(request, preferredLink.Endpoint.EndPointIPAddress);
request.Method = "POST";
request.KeepAlive = false;
request.KeepAlive = _useHttpKeepAlive;
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
request.ServicePoint.Expect100Continue = false;
request.AllowAutoRedirect = true;
request.UserAgent = UPnPConfiguration.UPnPMachineInfoHeader;
request.ContentType = "text/xml; charset=\"utf-8\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public void Close()
/// <param name="rootDescriptor">UPnP root descriptor to connect.</param>
/// <param name="deviceUuid">UUID of the device in the root descriptor which is the node to connect.</param>
/// <param name="dataTypeResolver">Delegate method to resolve extended datatypes.</param>
/// <param name="useHttpKeepAlive"><c>True</c> to set the HTTP keep-alive header in action requests sent over the connection, otherwise <c>false</c>.</param>
/// <exception cref="ArgumentException">
/// <list type="bullet">
/// <item>If the device with the specified <paramref name="deviceUuid"/> isn't present
Expand All @@ -277,9 +278,9 @@ public void Close()
/// service descriptions</item>
/// </list>
/// </exception>
public DeviceConnection Connect(RootDescriptor rootDescriptor, string deviceUuid, DataTypeResolverDlgt dataTypeResolver)
public DeviceConnection Connect(RootDescriptor rootDescriptor, string deviceUuid, DataTypeResolverDlgt dataTypeResolver, bool useHttpKeepAlive)
{
return DoConnect(rootDescriptor, deviceUuid, dataTypeResolver);
return DoConnect(rootDescriptor, deviceUuid, dataTypeResolver, useHttpKeepAlive);
}

/// <summary>
Expand Down Expand Up @@ -315,11 +316,11 @@ public void DisconnectAll()

#region Private/protected methods

protected DeviceConnection DoConnect(RootDescriptor descriptor, string deviceUuid, DataTypeResolverDlgt dataTypeResolver)
protected DeviceConnection DoConnect(RootDescriptor descriptor, string deviceUuid, DataTypeResolverDlgt dataTypeResolver, bool useHttpKeepAlive)
{
lock (_cpData.SyncObj)
{
DeviceConnection connection = new DeviceConnection(this, descriptor, deviceUuid, _cpData, dataTypeResolver);
DeviceConnection connection = new DeviceConnection(this, descriptor, deviceUuid, _cpData, dataTypeResolver, useHttpKeepAlive);
_connectedDevices.Add(deviceUuid, connection);
return connection;
}
Expand Down

0 comments on commit 871551a

Please sign in to comment.