#region Receive Record Async Result
private class ReceiveRecordAsyncResult : IAsyncResult
{
+ private object locker = new object ();
private AsyncCallback _userCallback;
private object _userState;
private Exception _asyncException;
- private ManualResetEvent _complete;
+ private ManualResetEvent handle;
private byte[] _resultingBuffer;
private Stream _record;
+ private bool completed;
private byte[] _initialBuffer;
{
_userCallback = userCallback;
_userState = userState;
- _complete = new ManualResetEvent(false);
_initialBuffer = initialBuffer;
_record = record;
}
public bool CompletedWithError
{
- get { return null != _asyncException; }
+ get {
+ if (!IsCompleted)
+ return false; // Perhaps throw InvalidOperationExcetion?
+
+ return null != _asyncException;
+ }
}
public WaitHandle AsyncWaitHandle
{
- get { return _complete; }
+ get {
+ lock (locker) {
+ if (handle == null)
+ handle = new ManualResetEvent (completed);
+ }
+ return handle;
+ }
+
}
public bool CompletedSynchronously
public bool IsCompleted
{
- get { return _complete.WaitOne(0, false); }
+ get {
+ lock (locker) {
+ return completed;
+ }
+ }
}
private void SetComplete(Exception ex, byte[] resultingBuffer)
{
- if (this.IsCompleted)
- return;
-
- lock (this)
- {
- if (this.IsCompleted)
+ lock (locker) {
+ if (completed)
return;
+ completed = true;
+ if (handle != null)
+ handle.Set ();
+
+ if (_userCallback != null)
+ _userCallback.BeginInvoke (this, null, null);
+
_asyncException = ex;
_resultingBuffer = resultingBuffer;
- _complete.Set();
}
-
- if (null != _userCallback)
- _userCallback (this);
-
}
public void SetComplete(Exception ex)
#region Receive Record Async Result
private class SendRecordAsyncResult : IAsyncResult
{
+ private object locker = new object ();
private AsyncCallback _userCallback;
private object _userState;
private Exception _asyncException;
- private ManualResetEvent _complete;
+ private ManualResetEvent handle;
private HandshakeMessage _message;
+ private bool completed;
public SendRecordAsyncResult(AsyncCallback userCallback, object userState, HandshakeMessage message)
{
_userCallback = userCallback;
_userState = userState;
- _complete = new ManualResetEvent(false);
_message = message;
}
public bool CompletedWithError
{
- get { return null != _asyncException; }
+ get {
+ if (!IsCompleted)
+ return false; // Perhaps throw InvalidOperationExcetion?
+
+ return null != _asyncException;
+ }
}
public WaitHandle AsyncWaitHandle
{
- get { return _complete; }
+ get {
+ lock (locker) {
+ if (handle == null)
+ handle = new ManualResetEvent (completed);
+ }
+ return handle;
+ }
+
}
public bool CompletedSynchronously
public bool IsCompleted
{
- get { return _complete.WaitOne(0, false); }
+ get {
+ lock (locker) {
+ return completed;
+ }
+ }
}
public void SetComplete(Exception ex)
{
- if (this.IsCompleted)
- return;
-
- lock (this)
- {
- if (this.IsCompleted)
+ lock (locker) {
+ if (completed)
return;
- _asyncException = ex;
- _complete.Set();
- }
+ completed = true;
+ if (handle != null)
+ handle.Set ();
- if (null != _userCallback)
- _userCallback (this);
+ if (_userCallback != null)
+ _userCallback.BeginInvoke (this, null, null);
+ _asyncException = ex;
+ }
}
public void SetComplete()
if (null == internalResult)
throw new ArgumentException("Either the provided async result is null or was not created by this RecordProtocol.");
- internalResult.AsyncWaitHandle.WaitOne();
+ if (!internalResult.IsCompleted)
+ internalResult.AsyncWaitHandle.WaitOne();
if (internalResult.CompletedWithError)
throw internalResult.AsyncException;
if (asyncResult is SendRecordAsyncResult)
{
SendRecordAsyncResult internalResult = asyncResult as SendRecordAsyncResult;
- internalResult.AsyncWaitHandle.WaitOne();
+ if (!internalResult.IsCompleted)
+ internalResult.AsyncWaitHandle.WaitOne();
if (internalResult.CompletedWithError)
throw internalResult.AsyncException;
}