Skip to content

Commit

Permalink
ConnectorBase fix for chunked responses
Browse files Browse the repository at this point in the history
  • Loading branch information
briandunnington committed Oct 11, 2009
1 parent a809d35 commit 39e9c4d
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions Growl/Growl.Connector/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public abstract class ConnectorBase
/// </summary>
public const int TCP_PORT = 23053;

// End-of-message indicator
private const string EOM = "\r\n\r\n";

/// <summary>
/// Represents methods that handle the ResponseReceived events
/// </summary>
Expand Down Expand Up @@ -308,6 +311,7 @@ private void ReadCallback(IAsyncResult iar)
{
TcpState state = null;
bool waitForCallback = false;
bool moreData = false;
try
{
state = (TcpState)iar.AsyncState;
Expand All @@ -318,25 +322,30 @@ private void ReadCallback(IAsyncResult iar)
string response = System.Text.Encoding.UTF8.GetString(state.Buffer, 0, length);
state.Response += response;

// read additional data
while (state.Stream.DataAvailable)
// keep waiting for more data if this wasnt the end of the message
if (!state.Response.EndsWith(EOM))
{
moreData = true;
AsyncCallback callback = new AsyncCallback(ReadCallback);
state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, callback, state);
}
else
{

if (state.Delegate != null)
state.Delegate(response);
if (state.Delegate != null)
state.Delegate(state.Response);

// wait for more data
if (state.WaitForCallback)
{
waitForCallback = true;
state.WaitForCallback = false;
state.Buffer = new byte[4096];
AsyncCallback callback = new AsyncCallback(ReadCallback);
state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, callback, state);
return;
// wait for callback data
if (state.WaitForCallback)
{
waitForCallback = true;
state.WaitForCallback = false;
state.Buffer = new byte[4096];
state.Response = String.Empty;
AsyncCallback callback = new AsyncCallback(ReadCallback);
state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, callback, state);
return;
}
}
}
}
Expand All @@ -347,7 +356,7 @@ private void ReadCallback(IAsyncResult iar)
}
finally
{
if (state != null && !waitForCallback)
if (state != null && !waitForCallback && !moreData)
{
CleanUpSocket(state);
}
Expand Down

0 comments on commit 39e9c4d

Please sign in to comment.