diff options
Diffstat (limited to 'OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs')
-rw-r--r-- | OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs | 183 |
1 files changed, 0 insertions, 183 deletions
diff --git a/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs b/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs deleted file mode 100644 index 728e25b..0000000 --- a/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs +++ /dev/null | |||
@@ -1,183 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Threading; | ||
30 | |||
31 | namespace OpenSim.Framework.Communications | ||
32 | { | ||
33 | internal class SimpleAsyncResult : IAsyncResult | ||
34 | { | ||
35 | private readonly AsyncCallback m_callback; | ||
36 | |||
37 | /// <summary> | ||
38 | /// Is process completed? | ||
39 | /// </summary> | ||
40 | /// <remarks>Should really be boolean, but VolatileRead has no boolean method</remarks> | ||
41 | private byte m_completed; | ||
42 | |||
43 | /// <summary> | ||
44 | /// Did process complete synchroneously? | ||
45 | /// </summary> | ||
46 | /// <remarks>I have a hard time imagining a scenario where this is the case, again, same issue about | ||
47 | /// booleans and VolatileRead as m_completed | ||
48 | /// </remarks> | ||
49 | private byte m_completedSynchronously; | ||
50 | |||
51 | private readonly object m_asyncState; | ||
52 | private ManualResetEvent m_waitHandle; | ||
53 | private Exception m_exception; | ||
54 | |||
55 | internal SimpleAsyncResult(AsyncCallback cb, object state) | ||
56 | { | ||
57 | m_callback = cb; | ||
58 | m_asyncState = state; | ||
59 | m_completed = 0; | ||
60 | m_completedSynchronously = 1; | ||
61 | } | ||
62 | |||
63 | #region IAsyncResult Members | ||
64 | |||
65 | public object AsyncState | ||
66 | { | ||
67 | get { return m_asyncState; } | ||
68 | } | ||
69 | |||
70 | public WaitHandle AsyncWaitHandle | ||
71 | { | ||
72 | get | ||
73 | { | ||
74 | if (m_waitHandle == null) | ||
75 | { | ||
76 | bool done = IsCompleted; | ||
77 | ManualResetEvent mre = new ManualResetEvent(done); | ||
78 | if (Interlocked.CompareExchange(ref m_waitHandle, mre, null) != null) | ||
79 | { | ||
80 | mre.Close(); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | if (!done && IsCompleted) | ||
85 | { | ||
86 | m_waitHandle.Set(); | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | return m_waitHandle; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | |||
95 | public bool CompletedSynchronously | ||
96 | { | ||
97 | get { return Thread.VolatileRead(ref m_completedSynchronously) == 1; } | ||
98 | } | ||
99 | |||
100 | |||
101 | public bool IsCompleted | ||
102 | { | ||
103 | get { return Thread.VolatileRead(ref m_completed) == 1; } | ||
104 | } | ||
105 | |||
106 | #endregion | ||
107 | |||
108 | #region class Methods | ||
109 | |||
110 | internal void SetAsCompleted(bool completedSynchronously) | ||
111 | { | ||
112 | m_completed = 1; | ||
113 | if (completedSynchronously) | ||
114 | m_completedSynchronously = 1; | ||
115 | else | ||
116 | m_completedSynchronously = 0; | ||
117 | |||
118 | SignalCompletion(); | ||
119 | } | ||
120 | |||
121 | internal void HandleException(Exception e, bool completedSynchronously) | ||
122 | { | ||
123 | m_completed = 1; | ||
124 | if (completedSynchronously) | ||
125 | m_completedSynchronously = 1; | ||
126 | else | ||
127 | m_completedSynchronously = 0; | ||
128 | m_exception = e; | ||
129 | |||
130 | SignalCompletion(); | ||
131 | } | ||
132 | |||
133 | private void SignalCompletion() | ||
134 | { | ||
135 | if (m_waitHandle != null) m_waitHandle.Set(); | ||
136 | |||
137 | if (m_callback != null) m_callback(this); | ||
138 | } | ||
139 | |||
140 | public void EndInvoke() | ||
141 | { | ||
142 | // This method assumes that only 1 thread calls EndInvoke | ||
143 | if (!IsCompleted) | ||
144 | { | ||
145 | // If the operation isn't done, wait for it | ||
146 | AsyncWaitHandle.WaitOne(); | ||
147 | AsyncWaitHandle.Close(); | ||
148 | m_waitHandle = null; // Allow early GC | ||
149 | } | ||
150 | |||
151 | // Operation is done: if an exception occured, throw it | ||
152 | if (m_exception != null) throw m_exception; | ||
153 | } | ||
154 | |||
155 | #endregion | ||
156 | } | ||
157 | |||
158 | internal class AsyncResult<T> : SimpleAsyncResult | ||
159 | { | ||
160 | private T m_result = default(T); | ||
161 | |||
162 | public AsyncResult(AsyncCallback asyncCallback, Object state) : | ||
163 | base(asyncCallback, state) | ||
164 | { | ||
165 | } | ||
166 | |||
167 | public void SetAsCompleted(T result, bool completedSynchronously) | ||
168 | { | ||
169 | // Save the asynchronous operation's result | ||
170 | m_result = result; | ||
171 | |||
172 | // Tell the base class that the operation completed | ||
173 | // sucessfully (no exception) | ||
174 | base.SetAsCompleted(completedSynchronously); | ||
175 | } | ||
176 | |||
177 | public new T EndInvoke() | ||
178 | { | ||
179 | base.EndInvoke(); | ||
180 | return m_result; | ||
181 | } | ||
182 | } | ||
183 | } | ||