Skip to content

Commit

Permalink
Modified existing classes to utilize the new IResponse object.
Browse files Browse the repository at this point in the history
- Also instead of Close we now close out the response on dispose, hence the IDisposable.
#1536
  • Loading branch information
NTaylorMullen committed Mar 19, 2013
1 parent 200f766 commit 0f9688a
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 42 deletions.
Expand Up @@ -20,14 +20,22 @@ public string ReadAsString()
return _httpResponseMessage.Content.ReadAsStringAsync().Result;
}

public Stream GetResponseStream()
public Stream GetStream()
{
return _httpResponseMessage.Content.ReadAsStreamAsync().Result;
}

public void Close()
protected virtual void Dispose(bool disposing)
{
_httpResponseMessage.Dispose();
if (disposing)
{
_httpResponseMessage.Dispose();
}
}

public void Dispose()
{
Dispose(true);
}
}
}
16 changes: 10 additions & 6 deletions src/Microsoft.AspNet.SignalR.Client/Http/HttpWebResponseWrapper.cs
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace Microsoft.AspNet.SignalR.Client.Http
{
Expand All @@ -15,19 +16,22 @@ public HttpWebResponseWrapper(HttpWebResponse response)
_response = response;
}

public string ReadAsString()
public Stream GetStream()
{
return _response.ReadAsString();
return _response.GetResponseStream();
}

public Stream GetResponseStream()
protected virtual void Dispose(bool disposing)
{
return _response.GetResponseStream();
if (disposing)
{
((IDisposable)_response).Dispose();
}
}

public void Close()
public void Dispose()
{
((IDisposable)_response).Dispose();
Dispose(true);
}
}
}
Expand Up @@ -89,10 +89,9 @@ public Task Send(IConnection connection, string data)
};

return _httpClient.Post(url, connection.PrepareRequest, postData)
.Then(response =>
.Then(response => response.ReadAsString())
.Then(raw =>
{
string raw = response.ReadAsString();
connection.Trace.WriteLine("OnMessage({0}, {1})", connection.ConnectionId, raw);
if (!String.IsNullOrEmpty(raw))
Expand Down
Expand Up @@ -99,7 +99,7 @@ public override bool SupportsKeepAlive

connection.Trace.WriteLine("LP: {0}", url);

HttpClient.Post(url, req =>
HttpClient.Post(url, req =>
{
request = req;
connection.PrepareRequest(request);
Expand Down Expand Up @@ -127,14 +127,16 @@ public override bool SupportsKeepAlive
}
// Get the response
var raw = task.Result.ReadAsString();
task.Result.ReadAsString().Then(raw =>
{
connection.Trace.WriteLine("LP: OnMessage({0}, {1})", connection.ConnectionId, raw);
connection.Trace.WriteLine("LP: OnMessage({0}, {1})", connection.ConnectionId, raw);
TransportHelper.ProcessResponse(connection,
raw,
out shouldRaiseReconnect,
out disconnectedReceived);
TransportHelper.ProcessResponse(connection,
raw,
out shouldRaiseReconnect,
out disconnectedReceived);
});
}
}
finally
Expand Down Expand Up @@ -219,7 +221,7 @@ public override bool SupportsKeepAlive
if (state != null)
{
// This will no-op if the request is already finished.
((IRequest)state).Abort();
((IRequest)state).Abort();
}
// Prevent the connection state from switching to the reconnected state.
Expand Down Expand Up @@ -272,7 +274,7 @@ private static void FireReconnected(IConnection connection)

public override void LostConnection(IConnection connection)
{

}
}
}
Expand Up @@ -121,7 +121,7 @@ private void Reconnect(IConnection connection, string data, CancellationToken di
else
{
var response = task.Result;
Stream stream = response.GetResponseStream();
Stream stream = response.GetStream();
var eventSource = new EventSourceStreamReader(connection, stream);
Expand Down Expand Up @@ -185,7 +185,7 @@ private void Reconnect(IConnection connection, string data, CancellationToken di
}
requestDisposer.Dispose();
esCancellationRegistration.Dispose();
response.Close();
response.Dispose();
if (AbortResetEvent != null)
{
Expand Down
24 changes: 12 additions & 12 deletions src/Microsoft.AspNet.SignalR.Client/Transports/TransportHelper.cs
Expand Up @@ -35,17 +35,17 @@ public static Task<NegotiationResponse> GetNegotiationResponse(this IHttpClient
#endif
negotiateUrl += AppendCustomQueryString(connection, negotiateUrl);

return httpClient.Get(negotiateUrl, connection.PrepareRequest).Then(response =>
{
string raw = response.ReadAsString();
if (String.IsNullOrEmpty(raw))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_ServerNegotiationFailed));
}
return JsonConvert.DeserializeObject<NegotiationResponse>(raw);
});
return httpClient.Get(negotiateUrl, connection.PrepareRequest)
.Then(response => response.ReadAsString())
.Then(raw =>
{
if (String.IsNullOrEmpty(raw))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_ServerNegotiationFailed));
}
return JsonConvert.DeserializeObject<NegotiationResponse>(raw);
});
}

[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "This is called by internally")]
Expand Down Expand Up @@ -103,7 +103,7 @@ public static string AppendCustomQueryString(IConnection connection, string base

string appender = "",
customQuery = connection.QueryString,
qs = "";
qs = "";

if (!String.IsNullOrEmpty(customQuery))
{
Expand Down
15 changes: 9 additions & 6 deletions src/Microsoft.AspNet.SignalR.Hosting.Memory/Response.cs
Expand Up @@ -16,19 +16,22 @@ public Response(ClientStream stream)
_stream = stream;
}

public string ReadAsString()
public Stream GetStream()
{
return new StreamReader(_stream).ReadToEnd();
return _stream;
}

public Stream GetResponseStream()
protected virtual void Dispose(bool disposing)
{
return _stream;
if (disposing)
{
_stream.Close();
}
}

public void Close()
public void Dispose()
{
_stream.Close();
Dispose(true);
}
}
}
Expand Up @@ -118,7 +118,7 @@ public void ConnectionIdsCantBeUsedAsGroups()
{
string url = GetUrl(protectedData, connection);
var response = await host.Get(url);
reader = new EventSourceStreamReader(connection, response.GetResponseStream());
reader = new EventSourceStreamReader(connection, response.GetStream());
reader.Message = sseEvent =>
{
Expand Down

0 comments on commit 0f9688a

Please sign in to comment.