public IAsyncResult BeginReceiveRecord(Stream record, AsyncCallback callback, object state) { if (this.context.ReceivedConnectionEnd) { throw new TlsException(AlertDescription.InternalError, "The session is finished and it's no longer valid."); } RecordProtocol.record_processing.Reset(); byte[] initialBuffer = new byte[1]; RecordProtocol.ReceiveRecordAsyncResult receiveRecordAsyncResult = new RecordProtocol.ReceiveRecordAsyncResult(callback, state, initialBuffer, record); record.BeginRead(receiveRecordAsyncResult.InitialBuffer, 0, receiveRecordAsyncResult.InitialBuffer.Length, new AsyncCallback(this.InternalReceiveRecordCallback), receiveRecordAsyncResult); return(receiveRecordAsyncResult); }
public byte[] EndReceiveRecord(IAsyncResult asyncResult) { RecordProtocol.ReceiveRecordAsyncResult receiveRecordAsyncResult = asyncResult as RecordProtocol.ReceiveRecordAsyncResult; if (receiveRecordAsyncResult == null) { throw new ArgumentException("Either the provided async result is null or was not created by this RecordProtocol."); } if (!receiveRecordAsyncResult.IsCompleted) { receiveRecordAsyncResult.AsyncWaitHandle.WaitOne(); } if (receiveRecordAsyncResult.CompletedWithError) { throw receiveRecordAsyncResult.AsyncException; } byte[] resultingBuffer = receiveRecordAsyncResult.ResultingBuffer; RecordProtocol.record_processing.Set(); return(resultingBuffer); }
private void InternalReceiveRecordCallback(IAsyncResult asyncResult) { RecordProtocol.ReceiveRecordAsyncResult receiveRecordAsyncResult = asyncResult.AsyncState as RecordProtocol.ReceiveRecordAsyncResult; Stream record = receiveRecordAsyncResult.Record; try { if (receiveRecordAsyncResult.Record.EndRead(asyncResult) == 0) { receiveRecordAsyncResult.SetComplete(null); } else { int num = (int)receiveRecordAsyncResult.InitialBuffer[0]; this.context.LastHandshakeMsg = HandshakeType.ClientHello; ContentType contentType = (ContentType)num; byte[] array = this.ReadRecordBuffer(num, record); if (array == null) { receiveRecordAsyncResult.SetComplete(null); } else { if (contentType != ContentType.Alert || array.Length != 2) { if (this.Context.Read != null && this.Context.Read.Cipher != null) { array = this.decryptRecordFragment(contentType, array); } } ContentType contentType2 = contentType; switch (contentType2) { case ContentType.ChangeCipherSpec: this.ProcessChangeCipherSpec(); break; case ContentType.Alert: this.ProcessAlert((AlertLevel)array[0], (AlertDescription)array[1]); if (record.CanSeek) { record.SetLength(0L); } array = null; break; case ContentType.Handshake: { TlsStream tlsStream = new TlsStream(array); while (!tlsStream.EOF) { this.ProcessHandshakeMessage(tlsStream); } break; } case ContentType.ApplicationData: break; default: if (contentType2 != (ContentType)128) { throw new TlsException(AlertDescription.UnexpectedMessage, "Unknown record received from server."); } this.context.HandshakeMessages.Write(array); break; } receiveRecordAsyncResult.SetComplete(array); } } } catch (Exception complete) { receiveRecordAsyncResult.SetComplete(complete); } }
private void InternalReceiveRecordCallback(IAsyncResult asyncResult) { RecordProtocol.ReceiveRecordAsyncResult asyncState = asyncResult.AsyncState as RecordProtocol.ReceiveRecordAsyncResult; Stream record = asyncState.Record; try { if (asyncState.Record.EndRead(asyncResult) == 0) { asyncState.SetComplete((byte[])null); } else { int contentType1 = (int)asyncState.InitialBuffer[0]; this.context.LastHandshakeMsg = HandshakeType.ClientHello; ContentType contentType2 = (ContentType)contentType1; byte[] numArray = this.ReadRecordBuffer(contentType1, record); if (numArray == null) { asyncState.SetComplete((byte[])null); } else { if ((contentType2 != ContentType.Alert || numArray.Length != 2) && (this.Context.Read != null && this.Context.Read.Cipher != null)) { numArray = this.decryptRecordFragment(contentType2, numArray); } switch (contentType2) { case ContentType.ChangeCipherSpec: this.ProcessChangeCipherSpec(); goto case ContentType.ApplicationData; case ContentType.Alert: this.ProcessAlert((AlertLevel)numArray[0], (AlertDescription)numArray[1]); if (record.CanSeek) { record.SetLength(0L); } numArray = (byte[])null; goto case ContentType.ApplicationData; case ContentType.Handshake: TlsStream handMsg = new TlsStream(numArray); while (!handMsg.EOF) { this.ProcessHandshakeMessage(handMsg); } goto case ContentType.ApplicationData; case ContentType.ApplicationData: asyncState.SetComplete(numArray); break; case (ContentType)128: this.context.HandshakeMessages.Write(numArray); goto case ContentType.ApplicationData; default: throw new TlsException(AlertDescription.UnexpectedMessage, "Unknown record received from server."); } } } } catch (Exception ex) { asyncState.SetComplete(ex); } }