private void OnRecieved(IAsyncResult ar)
{
lock (readLock)
{
if (isdisconnecting)
{
return;
}
var buf = (ByteBuffer)ar.AsyncState;
int n = 0;
try
{
n = sock.EndReceive(ar);
totalRcvdBytes += n;
}
catch (ObjectDisposedException)
{
// socket has been closed
return;
}
catch
{
DisconnectNow(true);
return;
}
if (n > 0)
{
if (DownloadThrottled)
{
currentRcvdBytes += n;
throttle.UpdateDownload(n);
}
else
{
// TODO: find a way for consistent speed calculation for non-polled connections
currentRcvdBytes = n;
lastReceiveTime = DateTime.Now;
}
buf.offset = 0;
buf.length = n;
ReceivedNotify(buf);
if (throttle == null || !throttle.DownloadSuspended)
{
ContinueReadUnlocked();
}
else
{
reading = false;
}
}
else
{
//throw new Exception("FATAL: Received non-positive amount of buf: " + n);
DisconnectNow(true);
}
}
}