private void ReadMessages(Uri uri)
{
if (this.stopCounter > 0) return;
#if DEBUG_RETRY
ThreadPool.QueueUserWorkItem(o =>
{
Thread.Sleep(4000);
CleanupRequests();
});
if (this.currentRetryCount > 0) throw new IOException();
#endif
this.CleanupRequests();
this.request = this.client.GetWebRequest(uri, uri.GetHashCode().ToString());
this.response = this.request.GetResponse();
// the url is supposed to send data indefinitely
// the only way out of here is either calling Stop() or failing by an exception
// (somehow stream.Dispose() hangs, so we'll not use the 'using' construct)
var stream = this.response.GetResponseStream();
var reader = new StreamReader(stream, Encoding.UTF8, false);
string line;
var emptyCounter = 0;
var messageBuilder = new StringBuilder();
while ((line = reader.ReadLine()) != null)
{
if (this.stopCounter > 0) return;
// we're successfully reading the stream, so reset the retry counter
this.currentRetryCount = 0;
if (line.Length == 0)
{
emptyCounter++;
// messages are separated by 3 empty lines
if (emptyCounter == 3)
{
this.Trigger(messageBuilder.ToString());
messageBuilder.Length = 0;
emptyCounter = 0;
}
}
else
{
emptyCounter = 0;
messageBuilder.Append(line);
}
}
if (log.IsErrorEnabled)
log.Error("The infinite loop just finished, probably the server closed the connection without errors. (?)");
throw new IOException("Remote host closed the streaming connection");
}