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
37 internal const string BTLS_DYLIB = "libmono-btls-shared";
39 internal const string BTLS_DYLIB = "__Internal";
42 internal MonoBtlsObject (MonoBtlsHandle handle)
47 protected internal abstract class MonoBtlsHandle : SafeHandle
49 internal MonoBtlsHandle ()
50 : base (IntPtr.Zero, true)
54 internal MonoBtlsHandle (IntPtr handle, bool ownsHandle)
55 : base (handle, ownsHandle)
59 public override bool IsInvalid {
60 get { return handle == IntPtr.Zero; }
64 internal MonoBtlsHandle Handle {
72 get { return handle != null && !handle.IsInvalid; }
75 MonoBtlsHandle handle;
78 protected void CheckThrow ()
80 if (lastError != null)
82 if (handle == null || handle.IsInvalid)
83 throw new ObjectDisposedException ("MonoBtlsSsl");
86 protected Exception SetException (Exception ex)
88 if (lastError == null)
93 protected void CheckError (bool ok, [CallerMemberName] string callerName = null)
96 if (callerName != null)
97 throw new MonoBtlsException ("{0}.{1} failed.", GetType ().Name, callerName);
99 throw new MonoBtlsException ();
104 protected void CheckError (int ret, [CallerMemberName] string callerName = null)
106 CheckError (ret == 1, callerName);
109 [DllImport (BTLS_DYLIB)]
110 extern static void mono_btls_free (IntPtr data);
112 protected void FreeDataPtr (IntPtr data)
114 mono_btls_free (data);
117 protected virtual void Close ()
121 protected void Dispose (bool disposing)
125 if (handle != null) {
131 var disposedExc = new ObjectDisposedException (GetType ().Name);
132 Interlocked.CompareExchange (ref lastError, disposedExc, null);
137 public void Dispose ()
140 GC.SuppressFinalize (this);