private void Poll(IConnection connection, string connectionData)
{
// This is to ensure that we do not accidently fire off another poll after being told to stop
lock (_stopLock)
{
// Only poll if we're running
if (_running == 0)
{
return;
}
// A url is required
var url = ResolveUrl(connection, connectionData);
HttpClient.Post(url, request =>
{
connection.PrepareRequest(request);
_currentRequest = request;
// This is called just prior to posting the request to ensure that any in-flight polling request
// is always executed before an OnAfterPoll
TryDelayedReconnect(connection, _reconnectInvoker);
}, isLongRunning: true)
.ContinueWith(task =>
{
var next = TaskAsyncHelper.Empty;
Exception exception = null;
if (task.IsFaulted || task.IsCanceled)
{
exception = task.IsCanceled
? new OperationCanceledException(Resources.Error_TaskCancelledException)
: task.Exception.Unwrap();
OnError(connection, exception);
}
else
{
try
{
next = task.Result.ReadAsString(readBuffer => OnChunk(connection, readBuffer))
.Then(raw => OnMessage(connection, raw));
}
catch (Exception ex)
{
exception = ex;
OnError(connection, exception);
}
}
next.Finally(
state => OnAfterPoll((Exception) state).Then(() => Poll(connection, connectionData)),
exception);
});
}
}