5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 using System.Threading;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
34 abstract class MonoBtlsObject : IDisposable
36 internal MonoBtlsObject (MonoBtlsHandle handle)
41 protected internal abstract class MonoBtlsHandle : SafeHandle
43 internal MonoBtlsHandle ()
44 : base (IntPtr.Zero, true)
48 internal MonoBtlsHandle (IntPtr handle, bool ownsHandle)
49 : base (handle, ownsHandle)
53 public override bool IsInvalid {
54 get { return handle == IntPtr.Zero; }
58 internal MonoBtlsHandle Handle {
66 get { return handle != null && !handle.IsInvalid; }
69 MonoBtlsHandle handle;
72 protected void CheckThrow ()
74 if (lastError != null)
76 if (handle == null || handle.IsInvalid)
77 throw new ObjectDisposedException ("MonoBtlsSsl");
80 protected Exception SetException (Exception ex)
82 if (lastError == null)
87 protected void CheckError (bool ok, [CallerMemberName] string callerName = null)
90 if (callerName != null)
91 throw new MonoBtlsException ("{0}.{1} failed.", GetType ().Name, callerName);
93 throw new MonoBtlsException ();
98 protected void CheckError (int ret, [CallerMemberName] string callerName = null)
100 CheckError (ret == 1, callerName);
103 [MethodImpl (MethodImplOptions.InternalCall)]
104 extern static void mono_btls_free (IntPtr data);
106 protected void FreeDataPtr (IntPtr data)
108 mono_btls_free (data);
111 protected virtual void Close ()
115 protected void Dispose (bool disposing)
119 if (handle != null) {
125 var disposedExc = new ObjectDisposedException (GetType ().Name);
126 Interlocked.CompareExchange (ref lastError, disposedExc, null);
131 public void Dispose ()
134 GC.SuppressFinalize (this);