private void ReadCallback(IAsyncResult asyncResult) { try { ReadContext readContext = (ReadContext)asyncResult.AsyncState; byte[] rawData = readContext.Data; int bytesReceived = 0; try { bytesReceived = ReadStream.EndRead(asyncResult); } catch (Exception) { bytesReceived = 0; } if (bytesReceived == 0) { // Read as failed (bytes received == 0 or exception in EndRead) ReadFailed(readContext); return; } MessageAccumulator msgAcc = readContext.Accumulator; ProcessReceivedData(ref msgAcc, rawData, bytesReceived); readContext.Accumulator = msgAcc; //continue to read if it's not closed. lock (this) { if (closed) { return; } } ReadStream.BeginRead(readContext.Data, 0, readContext.Data.Length, new AsyncCallback(ReadCallback), readContext); } catch (Exception e) { log.Error(e); } }
public override int EndRead(IAsyncResult asyncResult) { return(ReadStream.EndRead(asyncResult)); }