private void ReceiveDataCallback(IAsyncResult asyncResult)
{
AsyncReceiveDataResult asyncReceiveDataResult = (AsyncReceiveDataResult)asyncResult.AsyncState;
try {
Record[] records = _recordStream.EndReceive(asyncResult);
MemoryStream memStream = new MemoryStream();
foreach (Record record in records) {
_recordHandler.ProcessInputRecord(record);
switch (record.Type) {
case RecordType.ChangeCipherSpec:
// FIXME: get the handshake result from somewhere
//ProcessChangeCipherSpecRecord(record, asyncHandshakeResult);
break;
case RecordType.Alert:
ProcessAlertRecord(record, asyncReceiveDataResult);
break;
case RecordType.Handshake:
// FIXME: get the handshake result from somewhere
//ProcessHandshakeRecord(record, asyncHandshakeResult);
break;
case RecordType.Data:
memStream.Write(record.Fragment, 0, record.Fragment.Length);
break;
default:
ProcessUnknownRecord(record, asyncReceiveDataResult);
break;
}
}
asyncReceiveDataResult.SetComplete(memStream.ToArray());
} catch (AlertException ae) {
ProcessSendFatalAlert(new Alert(ae.AlertDescription, _handshakeSession.NegotiatedVersion));
asyncReceiveDataResult.SetComplete(new Exception("Connection closed because of local alert", ae));
} catch (IOException) {
asyncReceiveDataResult.SetComplete(new EndOfStreamException("Connection closed unexpectedly"));
} catch (Exception e) {
ProcessSendFatalAlert(new Alert(AlertDescription.InternalError, _handshakeSession.NegotiatedVersion));
asyncReceiveDataResult.SetComplete(new Exception("Connection closed because of local error", e));
}
}