From 39e9c4df3be1c10932ae31e2c95c0970e61f448e Mon Sep 17 00:00:00 2001 From: briandunnington Date: Sun, 11 Oct 2009 01:28:14 +0000 Subject: [PATCH] ConnectorBase fix for chunked responses --- Growl/Growl.Connector/ConnectorBase.cs | 37 ++++++++++++++++---------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Growl/Growl.Connector/ConnectorBase.cs b/Growl/Growl.Connector/ConnectorBase.cs index 8887fd3..954e5b8 100644 --- a/Growl/Growl.Connector/ConnectorBase.cs +++ b/Growl/Growl.Connector/ConnectorBase.cs @@ -22,6 +22,9 @@ public abstract class ConnectorBase /// public const int TCP_PORT = 23053; + // End-of-message indicator + private const string EOM = "\r\n\r\n"; + /// /// Represents methods that handle the ResponseReceived events /// @@ -308,6 +311,7 @@ private void ReadCallback(IAsyncResult iar) { TcpState state = null; bool waitForCallback = false; + bool moreData = false; try { state = (TcpState)iar.AsyncState; @@ -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; + } } } } @@ -347,7 +356,7 @@ private void ReadCallback(IAsyncResult iar) } finally { - if (state != null && !waitForCallback) + if (state != null && !waitForCallback && !moreData) { CleanUpSocket(state); }