Merge pull request #3442 from lateralusX/jlorenss/win-atexit-commands
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsObject.cs
1 //
2 // MonoBtlsObject.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26 #if SECURITY_DEP
27 using System;
28 using System.Threading;
29 using System.Runtime.InteropServices;
30 using System.Runtime.CompilerServices;
31
32 namespace Mono.Btls
33 {
34         abstract class MonoBtlsObject : IDisposable
35         {
36 #if DYNAMIC_BTLS
37                 internal const string BTLS_DYLIB = "libmono-btls-shared";
38 #else
39                 internal const string BTLS_DYLIB = "__Internal";
40 #endif
41
42                 internal MonoBtlsObject (MonoBtlsHandle handle)
43                 {
44                         this.handle = handle;
45                 }
46
47                 protected internal abstract class MonoBtlsHandle : SafeHandle
48                 {
49                         internal MonoBtlsHandle ()
50                                 : base (IntPtr.Zero, true)
51                         {
52                         }
53
54                         internal MonoBtlsHandle (IntPtr handle, bool ownsHandle)
55                                 : base (handle, ownsHandle)
56                         {
57                         }
58
59                         public override bool IsInvalid {
60                                 get { return handle == IntPtr.Zero; }
61                         }
62                 }
63
64                 internal MonoBtlsHandle Handle {
65                         get {
66                                 CheckThrow ();
67                                 return handle;
68                         }
69                 }
70
71                 public bool IsValid {
72                         get { return handle != null && !handle.IsInvalid; }
73                 }
74
75                 MonoBtlsHandle handle;
76                 Exception lastError;
77
78                 protected void CheckThrow ()
79                 {
80                         if (lastError != null)
81                                 throw lastError;
82                         if (handle == null || handle.IsInvalid)
83                                 throw new ObjectDisposedException ("MonoBtlsSsl");
84                 }
85
86                 protected Exception SetException (Exception ex)
87                 {
88                         if (lastError == null)
89                                 lastError = ex;
90                         return ex;
91                 }
92
93                 protected void CheckError (bool ok, [CallerMemberName] string callerName = null)
94                 {
95                         if (!ok) {
96                                 if (callerName != null)
97                                         throw new MonoBtlsException ("{0}.{1} failed.", GetType ().Name, callerName);
98                                 else
99                                         throw new MonoBtlsException ();
100                         }
101
102                 }
103
104                 protected void CheckError (int ret, [CallerMemberName] string callerName = null)
105                 {
106                         CheckError (ret == 1, callerName);
107                 }
108
109                 [DllImport (BTLS_DYLIB)]
110                 extern static void mono_btls_free (IntPtr data);
111
112                 protected void FreeDataPtr (IntPtr data)
113                 {
114                         mono_btls_free (data);
115                 }
116
117                 protected virtual void Close ()
118                 {
119                 }
120
121                 protected void Dispose (bool disposing)
122                 {
123                         if (disposing) {
124                                 try {
125                                         if (handle != null) {
126                                                 Close ();
127                                                 handle.Dispose ();
128                                                 handle = null;
129                                         }
130                                 } finally {
131                                         var disposedExc = new ObjectDisposedException (GetType ().Name);
132                                         Interlocked.CompareExchange (ref lastError, disposedExc, null);
133                                 }
134                         }
135                 }
136
137                 public void Dispose ()
138                 {
139                         Dispose (true);
140                         GC.SuppressFinalize (this);
141                 }
142
143                 ~MonoBtlsObject ()
144                 {
145                         Dispose (false);
146                 }
147         }
148 }
149 #endif