CommunicationObject.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // CommunicationObject.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ServiceModel;
  30. using System.Threading;
  31. namespace System.ServiceModel.Channels
  32. {
  33. public abstract class CommunicationObject : ICommunicationObject
  34. {
  35. object mutex;
  36. CommunicationState state = CommunicationState.Created;
  37. TimeSpan open_timeout, close_timeout;
  38. bool aborted;
  39. protected CommunicationObject ()
  40. : this (new object ())
  41. {
  42. }
  43. protected CommunicationObject (object mutex)
  44. {
  45. this.mutex = mutex;
  46. }
  47. #region Events
  48. public event EventHandler Closed;
  49. public event EventHandler Closing;
  50. public event EventHandler Faulted;
  51. public event EventHandler Opened;
  52. public event EventHandler Opening;
  53. #endregion
  54. #region Properties
  55. public CommunicationState State {
  56. get { return state; }
  57. }
  58. protected bool IsDisposed {
  59. get { return state == CommunicationState.Closed; }
  60. }
  61. protected object ThisLock {
  62. get { return mutex; }
  63. }
  64. protected internal abstract TimeSpan DefaultCloseTimeout { get; }
  65. protected internal abstract TimeSpan DefaultOpenTimeout { get; }
  66. #endregion
  67. #region Methods
  68. [MonoTODO]
  69. public void Abort ()
  70. {
  71. OnAbort ();
  72. }
  73. [MonoTODO]
  74. protected void Fault ()
  75. {
  76. state = CommunicationState.Faulted;
  77. OnFaulted ();
  78. }
  79. public IAsyncResult BeginClose (AsyncCallback callback,
  80. object state)
  81. {
  82. return BeginClose (close_timeout, callback, state);
  83. }
  84. public IAsyncResult BeginClose (TimeSpan timeout,
  85. AsyncCallback callback, object state)
  86. {
  87. ProcessClosing ();
  88. return OnBeginClose (timeout, callback, state);
  89. }
  90. public IAsyncResult BeginOpen (AsyncCallback callback,
  91. object state)
  92. {
  93. return BeginOpen (open_timeout, callback, state);
  94. }
  95. public IAsyncResult BeginOpen (TimeSpan timeout,
  96. AsyncCallback callback, object state)
  97. {
  98. ProcessOpening ();
  99. return OnBeginOpen (timeout, callback, state);
  100. }
  101. public void Close ()
  102. {
  103. Close (close_timeout);
  104. }
  105. public void Close (TimeSpan timeout)
  106. {
  107. ProcessClosing ();
  108. OnClose (timeout);
  109. ProcessClosed ();
  110. }
  111. public void EndClose (IAsyncResult result)
  112. {
  113. OnEndClose (result);
  114. ProcessClosed ();
  115. }
  116. public void EndOpen (IAsyncResult result)
  117. {
  118. OnEndOpen (result);
  119. ProcessOpened ();
  120. }
  121. public void Open ()
  122. {
  123. Open (open_timeout);
  124. }
  125. public void Open (TimeSpan timeout)
  126. {
  127. ProcessOpening ();
  128. OnOpen (timeout);
  129. ProcessOpened ();
  130. }
  131. protected abstract void OnAbort ();
  132. protected abstract IAsyncResult OnBeginClose (TimeSpan timeout,
  133. AsyncCallback callback, object state);
  134. protected abstract IAsyncResult OnBeginOpen (TimeSpan timeout,
  135. AsyncCallback callback, object state);
  136. protected abstract void OnClose (TimeSpan timeout);
  137. void ProcessClosing ()
  138. {
  139. if (State == CommunicationState.Faulted)
  140. throw new CommunicationObjectFaultedException ();
  141. state = CommunicationState.Closing;
  142. OnClosing ();
  143. }
  144. protected virtual void OnClosing ()
  145. {
  146. // This means, if this method is overriden, then
  147. // Opening event is surpressed.
  148. if (Closing != null)
  149. Closing (this, new EventArgs ());
  150. }
  151. void ProcessClosed ()
  152. {
  153. state = CommunicationState.Closed;
  154. OnClosed ();
  155. }
  156. protected virtual void OnClosed ()
  157. {
  158. // This means, if this method is overriden, then
  159. // Closed event is surpressed.
  160. if (Closed != null)
  161. Closed (this, new EventArgs ());
  162. }
  163. protected abstract void OnEndClose (IAsyncResult result);
  164. protected abstract void OnEndOpen (IAsyncResult result);
  165. [MonoTODO]
  166. protected virtual void OnFaulted ()
  167. {
  168. // This means, if this method is overriden, then
  169. // Opened event is surpressed.
  170. if (Faulted != null)
  171. Faulted (this, new EventArgs ());
  172. }
  173. protected abstract void OnOpen (TimeSpan timeout);
  174. void ProcessOpened ()
  175. {
  176. state = CommunicationState.Opened;
  177. OnOpened ();
  178. }
  179. protected virtual void OnOpened ()
  180. {
  181. // This means, if this method is overriden, then
  182. // Opened event is surpressed.
  183. if (Opened != null)
  184. Opened (this, new EventArgs ());
  185. }
  186. void ProcessOpening ()
  187. {
  188. ThrowIfDisposedOrImmutable ();
  189. state = CommunicationState.Opening;
  190. OnOpening ();
  191. }
  192. protected virtual void OnOpening ()
  193. {
  194. // This means, if this method is overriden, then
  195. // Opening event is surpressed.
  196. if (Opening != null)
  197. Opening (this, new EventArgs ());
  198. }
  199. protected void ThrowIfDisposed ()
  200. {
  201. if (IsDisposed)
  202. throw new ObjectDisposedException (String.Format ("This communication object {0} is already disposed.", GetCommunicationObjectType ()));
  203. }
  204. protected void ThrowIfDisposedOrNotOpen ()
  205. {
  206. ThrowIfDisposed ();
  207. if (State == CommunicationState.Faulted)
  208. throw new CommunicationObjectFaultedException ();
  209. if (State != CommunicationState.Opened)
  210. throw new InvalidOperationException (String.Format ("The communication object {0} must be at opened state.", GetCommunicationObjectType ()));
  211. }
  212. protected void ThrowIfDisposedOrImmutable ()
  213. {
  214. ThrowIfDisposed ();
  215. // hmm, according to msdn, Closing is OK here.
  216. switch (State) {
  217. case CommunicationState.Faulted:
  218. throw new CommunicationObjectFaultedException ();
  219. case CommunicationState.Opening:
  220. case CommunicationState.Opened:
  221. throw new InvalidOperationException (String.Format ("The communication object {0} is not at created state.", GetType ()));
  222. }
  223. }
  224. protected virtual Type GetCommunicationObjectType ()
  225. {
  226. return GetType ();
  227. }
  228. #endregion
  229. class SimpleAsyncResult : IAsyncResult
  230. {
  231. CommunicationState comm_state;
  232. object async_state;
  233. public SimpleAsyncResult (
  234. CommunicationState communicationState,
  235. TimeSpan timeout, AsyncCallback callback,
  236. object asyncState)
  237. {
  238. comm_state = communicationState;
  239. async_state = asyncState;
  240. }
  241. public object AsyncState {
  242. get { return async_state; }
  243. }
  244. // FIXME: implement
  245. public WaitHandle AsyncWaitHandle {
  246. get { throw new NotImplementedException (); }
  247. }
  248. // FIXME: implement
  249. public bool CompletedSynchronously {
  250. get { throw new NotImplementedException (); }
  251. }
  252. // FIXME: implement
  253. public bool IsCompleted {
  254. get { throw new NotImplementedException (); }
  255. }
  256. }
  257. }
  258. }