diff options
author | Gonzalo Paniagua Javier <gonzalo.mono@gmail.com> | 2006-03-07 23:09:57 +0300 |
---|---|---|
committer | Gonzalo Paniagua Javier <gonzalo.mono@gmail.com> | 2006-03-07 23:09:57 +0300 |
commit | f24df964dd9bac0a1b27ba6ac710b471c2951048 (patch) | |
tree | 5486fdd334305392e32c63498248a1aad4044ea6 /mcs | |
parent | 982b07d69fb61c72f4dc07ba73cc1964501b5d6a (diff) |
2006-03-07 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* SslStreamBase.cs: avoid creating the ManualResetEvent whenever
possible. Don't lock on an instance of an object that the user can get.
svn path=/branches/mono-1-1-13/mcs/; revision=57664
Diffstat (limited to 'mcs')
-rw-r--r-- | mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog | 5 | ||||
-rwxr-xr-x | mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs | 69 |
2 files changed, 45 insertions, 29 deletions
diff --git a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog index 5ec11bc6b1f..2adb1ed5e8d 100644 --- a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog +++ b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/ChangeLog @@ -1,3 +1,8 @@ +2006-03-07 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * SslStreamBase.cs: avoid creating the ManualResetEvent whenever + possible. Don't lock on an instance of an object that the user can get. + 2006-03-06 Gonzalo Paniagua Javier <gonzalo@ximian.com> * RecordProtocol.cs: avoid creating the ManualResetEvent whenever diff --git a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs index 9b6df730268..952623fa050 100755 --- a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs +++ b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs @@ -104,8 +104,6 @@ namespace Mono.Security.Protocol.Tls throw new IOException("The authentication or decryption has failed.", ex); } - negotiationComplete.Set(); - if (internalResult.ProceedAfterHandshake) { //kick off the read or write process (whichever called us) after the handshake is complete @@ -117,6 +115,7 @@ namespace Mono.Security.Protocol.Tls { InternalBeginRead(internalResult); } + negotiationComplete.Set(); } else { @@ -338,10 +337,12 @@ namespace Mono.Security.Protocol.Tls private class InternalAsyncResult : IAsyncResult { + private object locker = new object (); private AsyncCallback _userCallback; private object _userState; private Exception _asyncException; - private ManualResetEvent _complete; + private ManualResetEvent handle; + private bool completed; private int _bytesRead; private bool _fromWrite; private bool _proceedAfterHandshake; @@ -354,7 +355,6 @@ namespace Mono.Security.Protocol.Tls { _userCallback = userCallback; _userState = userState; - _complete = new ManualResetEvent(false); _buffer = buffer; _offset = offset; _count = count; @@ -404,12 +404,22 @@ namespace Mono.Security.Protocol.Tls public bool CompletedWithError { - get { return null != _asyncException; } + get { + if (IsCompleted == false) + return false; + return null != _asyncException; + } } public WaitHandle AsyncWaitHandle { - get { return _complete; } + get { + lock (locker) { + if (handle == null) + handle = new ManualResetEvent (completed); + } + return handle; + } } public bool CompletedSynchronously @@ -419,27 +429,26 @@ namespace Mono.Security.Protocol.Tls public bool IsCompleted { - get { return _complete.WaitOne(0, false); } + get { + lock (locker) + return completed; + } } private void SetComplete(Exception ex, int bytesRead) { - if (this.IsCompleted) - return; - - lock (this) - { - if (this.IsCompleted) + lock (locker) { + if (completed) return; + completed = true; + if (handle != null) + handle.Set (); _asyncException = ex; _bytesRead = bytesRead; - _complete.Set(); } - - if (null != _userCallback) - _userCallback (this); - + if (_userCallback != null) + _userCallback.BeginInvoke (this, null, null); } public void SetComplete(Exception ex) @@ -497,7 +506,8 @@ namespace Mono.Security.Protocol.Tls private void EndNegotiateHandshake(InternalAsyncResult asyncResult) { - asyncResult.AsyncWaitHandle.WaitOne(); + if (asyncResult.IsCompleted == false) + asyncResult.AsyncWaitHandle.WaitOne(); if (asyncResult.CompletedWithError) { @@ -770,13 +780,13 @@ namespace Mono.Security.Protocol.Tls { if (this.disposed) return; - + InternalAsyncResult internalResult = (InternalAsyncResult)ar.AsyncState; try { this.innerStream.EndWrite(ar); - internalResult.SetComplete(0); + internalResult.SetComplete(); } catch (Exception ex) { @@ -842,15 +852,15 @@ namespace Mono.Security.Protocol.Tls this.checkDisposed(); InternalAsyncResult internalResult = asyncResult as InternalAsyncResult; - - // Always wait until the read is complete - internalResult.AsyncWaitHandle.WaitOne(); - if (internalResult == null) { throw new ArgumentNullException("asyncResult is null or was not obtained by calling BeginRead."); } + // Always wait until the read is complete + if (asyncResult.IsCompleted == false) + asyncResult.AsyncWaitHandle.WaitOne(); + if (internalResult.CompletedWithError) { throw internalResult.AsyncException; @@ -864,14 +874,15 @@ namespace Mono.Security.Protocol.Tls this.checkDisposed(); InternalAsyncResult internalResult = asyncResult as InternalAsyncResult; - - internalResult.AsyncWaitHandle.WaitOne(); - - if (asyncResult == null) + if (internalResult == null) { throw new ArgumentNullException("asyncResult is null or was not obtained by calling BeginWrite."); } + + if (asyncResult.IsCompleted == false) + internalResult.AsyncWaitHandle.WaitOne(); + if (internalResult.CompletedWithError) { throw internalResult.AsyncException; |