CommunicationObject.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Diagnostics;
  9. using System.Runtime;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Diagnostics;
  12. using System.ServiceModel.Diagnostics.Application;
  13. public abstract class CommunicationObject : ICommunicationObject
  14. {
  15. bool aborted;
  16. bool closeCalled;
  17. #if DEBUG
  18. StackTrace closeStack;
  19. StackTrace faultedStack;
  20. #endif
  21. ExceptionQueue exceptionQueue;
  22. object mutex;
  23. bool onClosingCalled;
  24. bool onClosedCalled;
  25. bool onOpeningCalled;
  26. bool onOpenedCalled;
  27. bool raisedClosed;
  28. bool raisedClosing;
  29. bool raisedFaulted;
  30. bool traceOpenAndClose;
  31. object eventSender;
  32. CommunicationState state;
  33. protected CommunicationObject()
  34. : this(new object())
  35. {
  36. }
  37. protected CommunicationObject(object mutex)
  38. {
  39. this.mutex = mutex;
  40. this.eventSender = this;
  41. this.state = CommunicationState.Created;
  42. }
  43. internal CommunicationObject(object mutex, object eventSender)
  44. {
  45. this.mutex = mutex;
  46. this.eventSender = eventSender;
  47. this.state = CommunicationState.Created;
  48. }
  49. internal bool Aborted
  50. {
  51. get { return this.aborted; }
  52. }
  53. internal object EventSender
  54. {
  55. get { return this.eventSender; }
  56. set { eventSender = value; }
  57. }
  58. protected bool IsDisposed
  59. {
  60. get { return this.state == CommunicationState.Closed; }
  61. }
  62. public CommunicationState State
  63. {
  64. get { return this.state; }
  65. }
  66. protected object ThisLock
  67. {
  68. get { return this.mutex; }
  69. }
  70. protected abstract TimeSpan DefaultCloseTimeout { get; }
  71. protected abstract TimeSpan DefaultOpenTimeout { get; }
  72. internal TimeSpan InternalCloseTimeout
  73. {
  74. get { return this.DefaultCloseTimeout; }
  75. }
  76. internal TimeSpan InternalOpenTimeout
  77. {
  78. get { return this.DefaultOpenTimeout; }
  79. }
  80. public event EventHandler Closed;
  81. public event EventHandler Closing;
  82. public event EventHandler Faulted;
  83. public event EventHandler Opened;
  84. public event EventHandler Opening;
  85. public void Abort()
  86. {
  87. lock (ThisLock)
  88. {
  89. if (this.aborted || this.state == CommunicationState.Closed)
  90. return;
  91. this.aborted = true;
  92. #if DEBUG
  93. if (closeStack == null)
  94. closeStack = new StackTrace();
  95. #endif
  96. this.state = CommunicationState.Closing;
  97. }
  98. if (DiagnosticUtility.ShouldTraceInformation)
  99. {
  100. TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectAborted, SR.GetString(SR.TraceCodeCommunicationObjectAborted, TraceUtility.CreateSourceString(this)), this);
  101. }
  102. bool throwing = true;
  103. try
  104. {
  105. OnClosing();
  106. if (!this.onClosingCalled)
  107. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
  108. OnAbort();
  109. OnClosed();
  110. if (!this.onClosedCalled)
  111. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, this);
  112. throwing = false;
  113. }
  114. finally
  115. {
  116. if (throwing)
  117. {
  118. if (DiagnosticUtility.ShouldTraceWarning)
  119. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectAbortFailed, SR.GetString(SR.TraceCodeCommunicationObjectAbortFailed, this.GetCommunicationObjectType().ToString()), this);
  120. }
  121. }
  122. }
  123. public IAsyncResult BeginClose(AsyncCallback callback, object state)
  124. {
  125. return this.BeginClose(this.DefaultCloseTimeout, callback, state);
  126. }
  127. public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  128. {
  129. if (timeout < TimeSpan.Zero)
  130. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  131. new ArgumentOutOfRangeException("timeout", SR.GetString(SR.SFxTimeoutOutOfRange0)));
  132. using (DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose ? this.CreateCloseActivity() : null)
  133. {
  134. CommunicationState originalState;
  135. lock (ThisLock)
  136. {
  137. originalState = this.state;
  138. #if DEBUG
  139. if (closeStack == null)
  140. closeStack = new StackTrace();
  141. #endif
  142. if (originalState != CommunicationState.Closed)
  143. this.state = CommunicationState.Closing;
  144. this.closeCalled = true;
  145. }
  146. switch (originalState)
  147. {
  148. case CommunicationState.Created:
  149. case CommunicationState.Opening:
  150. case CommunicationState.Faulted:
  151. this.Abort();
  152. if (originalState == CommunicationState.Faulted)
  153. {
  154. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  155. }
  156. return new AlreadyClosedAsyncResult(callback, state);
  157. case CommunicationState.Opened:
  158. {
  159. bool throwing = true;
  160. try
  161. {
  162. OnClosing();
  163. if (!this.onClosingCalled)
  164. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
  165. IAsyncResult result = new CloseAsyncResult(this, timeout, callback, state);
  166. throwing = false;
  167. return result;
  168. }
  169. finally
  170. {
  171. if (throwing)
  172. {
  173. if (DiagnosticUtility.ShouldTraceWarning)
  174. {
  175. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectCloseFailed, SR.GetString(SR.TraceCodeCommunicationObjectCloseFailed, this.GetCommunicationObjectType().ToString()), this);
  176. }
  177. Abort();
  178. }
  179. }
  180. }
  181. case CommunicationState.Closing:
  182. case CommunicationState.Closed:
  183. return new AlreadyClosedAsyncResult(callback, state);
  184. default:
  185. throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState");
  186. }
  187. }
  188. }
  189. public IAsyncResult BeginOpen(AsyncCallback callback, object state)
  190. {
  191. return this.BeginOpen(this.DefaultOpenTimeout, callback, state);
  192. }
  193. public IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  194. {
  195. if (timeout < TimeSpan.Zero)
  196. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  197. new ArgumentOutOfRangeException("timeout", SR.GetString(SR.SFxTimeoutOutOfRange0)));
  198. lock (ThisLock)
  199. {
  200. ThrowIfDisposedOrImmutable();
  201. this.state = CommunicationState.Opening;
  202. }
  203. bool throwing = true;
  204. try
  205. {
  206. OnOpening();
  207. if (!this.onOpeningCalled)
  208. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnOpening"), Guid.Empty, this);
  209. IAsyncResult result = new OpenAsyncResult(this, timeout, callback, state);
  210. throwing = false;
  211. return result;
  212. }
  213. finally
  214. {
  215. if (throwing)
  216. {
  217. if (DiagnosticUtility.ShouldTraceWarning)
  218. {
  219. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectOpenFailed, SR.GetString(SR.TraceCodeCommunicationObjectOpenFailed, this.GetCommunicationObjectType().ToString()), this);
  220. }
  221. Fault();
  222. }
  223. }
  224. }
  225. public void Close()
  226. {
  227. this.Close(this.DefaultCloseTimeout);
  228. }
  229. public void Close(TimeSpan timeout)
  230. {
  231. if (timeout < TimeSpan.Zero)
  232. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  233. new ArgumentOutOfRangeException("timeout", SR.GetString(SR.SFxTimeoutOutOfRange0)));
  234. using (DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose ? this.CreateCloseActivity() : null)
  235. {
  236. CommunicationState originalState;
  237. lock (ThisLock)
  238. {
  239. originalState = this.state;
  240. #if DEBUG
  241. if (closeStack == null)
  242. closeStack = new StackTrace();
  243. #endif
  244. if (originalState != CommunicationState.Closed)
  245. this.state = CommunicationState.Closing;
  246. this.closeCalled = true;
  247. }
  248. switch (originalState)
  249. {
  250. case CommunicationState.Created:
  251. case CommunicationState.Opening:
  252. case CommunicationState.Faulted:
  253. this.Abort();
  254. if (originalState == CommunicationState.Faulted)
  255. {
  256. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  257. }
  258. break;
  259. case CommunicationState.Opened:
  260. {
  261. bool throwing = true;
  262. try
  263. {
  264. TimeoutHelper actualTimeout = new TimeoutHelper(timeout);
  265. OnClosing();
  266. if (!this.onClosingCalled)
  267. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
  268. OnClose(actualTimeout.RemainingTime());
  269. OnClosed();
  270. if (!this.onClosedCalled)
  271. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, this);
  272. throwing = false;
  273. }
  274. finally
  275. {
  276. if (throwing)
  277. {
  278. if (DiagnosticUtility.ShouldTraceWarning)
  279. {
  280. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectCloseFailed, SR.GetString(SR.TraceCodeCommunicationObjectCloseFailed, this.GetCommunicationObjectType().ToString()), this);
  281. }
  282. Abort();
  283. }
  284. }
  285. break;
  286. }
  287. case CommunicationState.Closing:
  288. case CommunicationState.Closed:
  289. break;
  290. default:
  291. throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState");
  292. }
  293. }
  294. }
  295. Exception CreateNotOpenException()
  296. {
  297. return new InvalidOperationException(SR.GetString(SR.CommunicationObjectCannotBeUsed, this.GetCommunicationObjectType().ToString(), this.state.ToString()));
  298. }
  299. Exception CreateImmutableException()
  300. {
  301. return new InvalidOperationException(SR.GetString(SR.CommunicationObjectCannotBeModifiedInState, this.GetCommunicationObjectType().ToString(), this.state.ToString()));
  302. }
  303. Exception CreateBaseClassMethodNotCalledException(string method)
  304. {
  305. return new InvalidOperationException(SR.GetString(SR.CommunicationObjectBaseClassMethodNotCalled, this.GetCommunicationObjectType().ToString(), method));
  306. }
  307. internal Exception CreateClosedException()
  308. {
  309. if (!this.closeCalled)
  310. {
  311. return CreateAbortedException();
  312. }
  313. else
  314. {
  315. #if DEBUG
  316. string originalStack = closeStack.ToString().Replace("\r\n", "\r\n ");
  317. return new ObjectDisposedException(this.GetCommunicationObjectType().ToString() + ", Object already closed:\r\n " + originalStack);
  318. #else
  319. return new ObjectDisposedException(this.GetCommunicationObjectType().ToString());
  320. #endif
  321. }
  322. }
  323. internal Exception CreateFaultedException()
  324. {
  325. #if DEBUG
  326. string originalStack = faultedStack.ToString().Replace("\r\n", "\r\n ");
  327. string message = SR.GetString(SR.CommunicationObjectFaultedStack2, this.GetCommunicationObjectType().ToString(), originalStack);
  328. #else
  329. string message = SR.GetString(SR.CommunicationObjectFaulted1, this.GetCommunicationObjectType().ToString());
  330. #endif
  331. return new CommunicationObjectFaultedException(message);
  332. }
  333. internal Exception CreateAbortedException()
  334. {
  335. #if DEBUG
  336. string originalStack = closeStack.ToString().Replace("\r\n", "\r\n ");
  337. return new CommunicationObjectAbortedException(SR.GetString(SR.CommunicationObjectAbortedStack2, this.GetCommunicationObjectType().ToString(), originalStack));
  338. #else
  339. return new CommunicationObjectAbortedException(SR.GetString(SR.CommunicationObjectAborted1, this.GetCommunicationObjectType().ToString()));
  340. #endif
  341. }
  342. internal virtual string CloseActivityName
  343. {
  344. get { return SR.GetString(SR.ActivityClose, this.GetType().FullName); }
  345. }
  346. internal virtual string OpenActivityName
  347. {
  348. get { return SR.GetString(SR.ActivityOpen, this.GetType().FullName); }
  349. }
  350. internal virtual ActivityType OpenActivityType
  351. {
  352. get { return ActivityType.Open; }
  353. }
  354. ServiceModelActivity CreateCloseActivity()
  355. {
  356. ServiceModelActivity retval = null;
  357. retval = ServiceModelActivity.CreateBoundedActivity();
  358. if (DiagnosticUtility.ShouldUseActivity)
  359. {
  360. ServiceModelActivity.Start(retval, this.CloseActivityName, ActivityType.Close);
  361. }
  362. return retval;
  363. }
  364. internal bool DoneReceivingInCurrentState()
  365. {
  366. this.ThrowPending();
  367. switch (this.state)
  368. {
  369. case CommunicationState.Created:
  370. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  371. case CommunicationState.Opening:
  372. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  373. case CommunicationState.Opened:
  374. return false;
  375. case CommunicationState.Closing:
  376. return true;
  377. case CommunicationState.Closed:
  378. return true;
  379. case CommunicationState.Faulted:
  380. return true;
  381. default:
  382. throw Fx.AssertAndThrow("DoneReceivingInCurrentState: Unknown CommunicationObject.state");
  383. }
  384. }
  385. public void EndClose(IAsyncResult result)
  386. {
  387. if (result is AlreadyClosedAsyncResult)
  388. AlreadyClosedAsyncResult.End(result);
  389. else
  390. CloseAsyncResult.End(result);
  391. }
  392. public void EndOpen(IAsyncResult result)
  393. {
  394. OpenAsyncResult.End(result);
  395. }
  396. protected void Fault()
  397. {
  398. lock (ThisLock)
  399. {
  400. if (this.state == CommunicationState.Closed || this.state == CommunicationState.Closing)
  401. return;
  402. if (this.state == CommunicationState.Faulted)
  403. return;
  404. #if DEBUG
  405. if (faultedStack == null)
  406. faultedStack = new StackTrace();
  407. #endif
  408. this.state = CommunicationState.Faulted;
  409. }
  410. OnFaulted();
  411. }
  412. internal void Fault(Exception exception)
  413. {
  414. lock (this.ThisLock)
  415. {
  416. if (this.exceptionQueue == null)
  417. this.exceptionQueue = new ExceptionQueue(this.ThisLock);
  418. }
  419. if (exception != null && DiagnosticUtility.ShouldTraceInformation)
  420. {
  421. TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectFaultReason,
  422. SR.GetString(SR.TraceCodeCommunicationObjectFaultReason), exception, null);
  423. }
  424. this.exceptionQueue.AddException(exception);
  425. this.Fault();
  426. }
  427. internal void AddPendingException(Exception exception)
  428. {
  429. lock (this.ThisLock)
  430. {
  431. if (this.exceptionQueue == null)
  432. this.exceptionQueue = new ExceptionQueue(this.ThisLock);
  433. }
  434. this.exceptionQueue.AddException(exception);
  435. }
  436. internal Exception GetPendingException()
  437. {
  438. CommunicationState currentState = this.state;
  439. Fx.Assert(currentState == CommunicationState.Closing || currentState == CommunicationState.Closed || currentState == CommunicationState.Faulted,
  440. "CommunicationObject.GetPendingException(currentState == CommunicationState.Closing || currentState == CommunicationState.Closed || currentState == CommunicationState.Faulted)");
  441. ExceptionQueue queue = this.exceptionQueue;
  442. if (queue != null)
  443. {
  444. return queue.GetException();
  445. }
  446. else
  447. {
  448. return null;
  449. }
  450. }
  451. // Terminal is loosely defined as an interruption to close or a fault.
  452. internal Exception GetTerminalException()
  453. {
  454. Exception exception = this.GetPendingException();
  455. if (exception != null)
  456. {
  457. return exception;
  458. }
  459. switch (this.state)
  460. {
  461. case CommunicationState.Closing:
  462. case CommunicationState.Closed:
  463. return new CommunicationException(SR.GetString(SR.CommunicationObjectCloseInterrupted1, this.GetCommunicationObjectType().ToString()));
  464. case CommunicationState.Faulted:
  465. return this.CreateFaultedException();
  466. default:
  467. throw Fx.AssertAndThrow("GetTerminalException: Invalid CommunicationObject.state");
  468. }
  469. }
  470. public void Open()
  471. {
  472. this.Open(this.DefaultOpenTimeout);
  473. }
  474. public void Open(TimeSpan timeout)
  475. {
  476. if (timeout < TimeSpan.Zero)
  477. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  478. new ArgumentOutOfRangeException("timeout", SR.GetString(SR.SFxTimeoutOutOfRange0)));
  479. using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity && this.TraceOpenAndClose ? ServiceModelActivity.CreateBoundedActivity() : null)
  480. {
  481. if (DiagnosticUtility.ShouldUseActivity)
  482. {
  483. ServiceModelActivity.Start(activity, this.OpenActivityName, this.OpenActivityType);
  484. }
  485. lock (ThisLock)
  486. {
  487. ThrowIfDisposedOrImmutable();
  488. this.state = CommunicationState.Opening;
  489. }
  490. bool throwing = true;
  491. try
  492. {
  493. TimeoutHelper actualTimeout = new TimeoutHelper(timeout);
  494. OnOpening();
  495. if (!this.onOpeningCalled)
  496. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnOpening"), Guid.Empty, this);
  497. OnOpen(actualTimeout.RemainingTime());
  498. OnOpened();
  499. if (!this.onOpenedCalled)
  500. throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnOpened"), Guid.Empty, this);
  501. throwing = false;
  502. }
  503. finally
  504. {
  505. if (throwing)
  506. {
  507. if (DiagnosticUtility.ShouldTraceWarning)
  508. {
  509. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectOpenFailed, SR.GetString(SR.TraceCodeCommunicationObjectOpenFailed, this.GetCommunicationObjectType().ToString()), this);
  510. }
  511. Fault();
  512. }
  513. }
  514. }
  515. }
  516. protected virtual void OnClosed()
  517. {
  518. this.onClosedCalled = true;
  519. lock (ThisLock)
  520. {
  521. if (this.raisedClosed)
  522. return;
  523. this.raisedClosed = true;
  524. this.state = CommunicationState.Closed;
  525. }
  526. if (DiagnosticUtility.ShouldTraceVerbose)
  527. {
  528. TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectClosed, SR.GetString(SR.TraceCodeCommunicationObjectClosed, TraceUtility.CreateSourceString(this)), this);
  529. }
  530. EventHandler handler = Closed;
  531. if (handler != null)
  532. {
  533. try
  534. {
  535. handler(eventSender, EventArgs.Empty);
  536. }
  537. catch (Exception exception)
  538. {
  539. if (Fx.IsFatal(exception))
  540. throw;
  541. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
  542. }
  543. }
  544. }
  545. protected virtual void OnClosing()
  546. {
  547. this.onClosingCalled = true;
  548. lock (ThisLock)
  549. {
  550. if (this.raisedClosing)
  551. return;
  552. this.raisedClosing = true;
  553. }
  554. if (DiagnosticUtility.ShouldTraceVerbose)
  555. {
  556. TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectClosing, SR.GetString(SR.TraceCodeCommunicationObjectClosing, TraceUtility.CreateSourceString(this)), this);
  557. }
  558. EventHandler handler = Closing;
  559. if (handler != null)
  560. {
  561. try
  562. {
  563. handler(eventSender, EventArgs.Empty);
  564. }
  565. catch (Exception exception)
  566. {
  567. if (Fx.IsFatal(exception))
  568. throw;
  569. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
  570. }
  571. }
  572. }
  573. protected virtual void OnFaulted()
  574. {
  575. lock (ThisLock)
  576. {
  577. if (this.raisedFaulted)
  578. return;
  579. this.raisedFaulted = true;
  580. }
  581. if (DiagnosticUtility.ShouldTraceWarning)
  582. {
  583. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectFaulted, SR.GetString(SR.TraceCodeCommunicationObjectFaulted, this.GetCommunicationObjectType().ToString()), this);
  584. }
  585. EventHandler handler = Faulted;
  586. if (handler != null)
  587. {
  588. try
  589. {
  590. handler(eventSender, EventArgs.Empty);
  591. }
  592. catch (Exception exception)
  593. {
  594. if (Fx.IsFatal(exception))
  595. throw;
  596. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
  597. }
  598. }
  599. }
  600. protected virtual void OnOpened()
  601. {
  602. this.onOpenedCalled = true;
  603. lock (ThisLock)
  604. {
  605. if (this.aborted || this.state != CommunicationState.Opening)
  606. return;
  607. this.state = CommunicationState.Opened;
  608. }
  609. if (DiagnosticUtility.ShouldTraceVerbose)
  610. TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectOpened, SR.GetString(SR.TraceCodeCommunicationObjectOpened, TraceUtility.CreateSourceString(this)), this);
  611. EventHandler handler = Opened;
  612. if (handler != null)
  613. {
  614. try
  615. {
  616. handler(eventSender, EventArgs.Empty);
  617. }
  618. catch (Exception exception)
  619. {
  620. if (Fx.IsFatal(exception))
  621. throw;
  622. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
  623. }
  624. }
  625. }
  626. protected virtual void OnOpening()
  627. {
  628. this.onOpeningCalled = true;
  629. if (DiagnosticUtility.ShouldTraceVerbose)
  630. {
  631. TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.CommunicationObjectOpening, SR.GetString(SR.TraceCodeCommunicationObjectOpening, TraceUtility.CreateSourceString(this)), this);
  632. }
  633. EventHandler handler = Opening;
  634. if (handler != null)
  635. {
  636. try
  637. {
  638. handler(eventSender, EventArgs.Empty);
  639. }
  640. catch (Exception exception)
  641. {
  642. if (Fx.IsFatal(exception))
  643. throw;
  644. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
  645. }
  646. }
  647. }
  648. internal void ThrowIfFaulted()
  649. {
  650. this.ThrowPending();
  651. switch (this.state)
  652. {
  653. case CommunicationState.Created:
  654. break;
  655. case CommunicationState.Opening:
  656. break;
  657. case CommunicationState.Opened:
  658. break;
  659. case CommunicationState.Closing:
  660. break;
  661. case CommunicationState.Closed:
  662. break;
  663. case CommunicationState.Faulted:
  664. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  665. default:
  666. throw Fx.AssertAndThrow("ThrowIfFaulted: Unknown CommunicationObject.state");
  667. }
  668. }
  669. internal void ThrowIfAborted()
  670. {
  671. if (this.aborted && !this.closeCalled)
  672. {
  673. throw TraceUtility.ThrowHelperError(CreateAbortedException(), Guid.Empty, this);
  674. }
  675. }
  676. internal bool TraceOpenAndClose
  677. {
  678. get
  679. {
  680. return this.traceOpenAndClose;
  681. }
  682. set
  683. {
  684. this.traceOpenAndClose = value && DiagnosticUtility.ShouldUseActivity;
  685. }
  686. }
  687. internal void ThrowIfClosed()
  688. {
  689. ThrowPending();
  690. switch (this.state)
  691. {
  692. case CommunicationState.Created:
  693. break;
  694. case CommunicationState.Opening:
  695. break;
  696. case CommunicationState.Opened:
  697. break;
  698. case CommunicationState.Closing:
  699. break;
  700. case CommunicationState.Closed:
  701. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  702. case CommunicationState.Faulted:
  703. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  704. default:
  705. throw Fx.AssertAndThrow("ThrowIfClosed: Unknown CommunicationObject.state");
  706. }
  707. }
  708. protected virtual Type GetCommunicationObjectType()
  709. {
  710. return this.GetType();
  711. }
  712. protected internal void ThrowIfDisposed()
  713. {
  714. ThrowPending();
  715. switch (this.state)
  716. {
  717. case CommunicationState.Created:
  718. break;
  719. case CommunicationState.Opening:
  720. break;
  721. case CommunicationState.Opened:
  722. break;
  723. case CommunicationState.Closing:
  724. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  725. case CommunicationState.Closed:
  726. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  727. case CommunicationState.Faulted:
  728. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  729. default:
  730. throw Fx.AssertAndThrow("ThrowIfDisposed: Unknown CommunicationObject.state");
  731. }
  732. }
  733. internal void ThrowIfClosedOrOpened()
  734. {
  735. ThrowPending();
  736. switch (this.state)
  737. {
  738. case CommunicationState.Created:
  739. break;
  740. case CommunicationState.Opening:
  741. break;
  742. case CommunicationState.Opened:
  743. throw TraceUtility.ThrowHelperError(this.CreateImmutableException(), Guid.Empty, this);
  744. case CommunicationState.Closing:
  745. throw TraceUtility.ThrowHelperError(this.CreateImmutableException(), Guid.Empty, this);
  746. case CommunicationState.Closed:
  747. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  748. case CommunicationState.Faulted:
  749. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  750. default:
  751. throw Fx.AssertAndThrow("ThrowIfClosedOrOpened: Unknown CommunicationObject.state");
  752. }
  753. }
  754. protected internal void ThrowIfDisposedOrImmutable()
  755. {
  756. ThrowPending();
  757. switch (this.state)
  758. {
  759. case CommunicationState.Created:
  760. break;
  761. case CommunicationState.Opening:
  762. throw TraceUtility.ThrowHelperError(this.CreateImmutableException(), Guid.Empty, this);
  763. case CommunicationState.Opened:
  764. throw TraceUtility.ThrowHelperError(this.CreateImmutableException(), Guid.Empty, this);
  765. case CommunicationState.Closing:
  766. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  767. case CommunicationState.Closed:
  768. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  769. case CommunicationState.Faulted:
  770. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  771. default:
  772. throw Fx.AssertAndThrow("ThrowIfDisposedOrImmutable: Unknown CommunicationObject.state");
  773. }
  774. }
  775. protected internal void ThrowIfDisposedOrNotOpen()
  776. {
  777. ThrowPending();
  778. switch (this.state)
  779. {
  780. case CommunicationState.Created:
  781. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  782. case CommunicationState.Opening:
  783. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  784. case CommunicationState.Opened:
  785. break;
  786. case CommunicationState.Closing:
  787. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  788. case CommunicationState.Closed:
  789. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  790. case CommunicationState.Faulted:
  791. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  792. default:
  793. throw Fx.AssertAndThrow("ThrowIfDisposedOrNotOpen: Unknown CommunicationObject.state");
  794. }
  795. }
  796. internal void ThrowIfNotOpened()
  797. {
  798. if (this.state == CommunicationState.Created || this.state == CommunicationState.Opening)
  799. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  800. }
  801. internal void ThrowIfClosedOrNotOpen()
  802. {
  803. ThrowPending();
  804. switch (this.state)
  805. {
  806. case CommunicationState.Created:
  807. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  808. case CommunicationState.Opening:
  809. throw TraceUtility.ThrowHelperError(this.CreateNotOpenException(), Guid.Empty, this);
  810. case CommunicationState.Opened:
  811. break;
  812. case CommunicationState.Closing:
  813. break;
  814. case CommunicationState.Closed:
  815. throw TraceUtility.ThrowHelperError(this.CreateClosedException(), Guid.Empty, this);
  816. case CommunicationState.Faulted:
  817. throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
  818. default:
  819. throw Fx.AssertAndThrow("ThrowIfClosedOrNotOpen: Unknown CommunicationObject.state");
  820. }
  821. }
  822. internal void ThrowPending()
  823. {
  824. ExceptionQueue queue = this.exceptionQueue;
  825. if (queue != null)
  826. {
  827. Exception exception = queue.GetException();
  828. if (exception != null)
  829. {
  830. throw TraceUtility.ThrowHelperError(exception, Guid.Empty, this);
  831. }
  832. }
  833. }
  834. //
  835. // State callbacks
  836. //
  837. protected abstract void OnAbort();
  838. protected abstract void OnClose(TimeSpan timeout);
  839. protected abstract void OnEndClose(IAsyncResult result);
  840. protected abstract IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state);
  841. protected abstract void OnOpen(TimeSpan timeout);
  842. protected abstract IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state);
  843. protected abstract void OnEndOpen(IAsyncResult result);
  844. class AlreadyClosedAsyncResult : CompletedAsyncResult
  845. {
  846. public AlreadyClosedAsyncResult(AsyncCallback callback, object state)
  847. : base(callback, state)
  848. {
  849. }
  850. }
  851. class ExceptionQueue
  852. {
  853. Queue<Exception> exceptions = new Queue<Exception>();
  854. object thisLock;
  855. internal ExceptionQueue(object thisLock)
  856. {
  857. this.thisLock = thisLock;
  858. }
  859. object ThisLock
  860. {
  861. get { return this.thisLock; }
  862. }
  863. public void AddException(Exception exception)
  864. {
  865. if (exception == null)
  866. {
  867. return;
  868. }
  869. lock (this.ThisLock)
  870. {
  871. this.exceptions.Enqueue(exception);
  872. }
  873. }
  874. public Exception GetException()
  875. {
  876. lock (this.ThisLock)
  877. {
  878. if (this.exceptions.Count > 0)
  879. {
  880. return this.exceptions.Dequeue();
  881. }
  882. }
  883. return null;
  884. }
  885. }
  886. class OpenAsyncResult : AsyncResult
  887. {
  888. static AsyncCompletion onOpenCompletion = new AsyncCompletion(OnOpenCompletion);
  889. CommunicationObject communicationObject;
  890. TimeoutHelper timeout;
  891. public OpenAsyncResult(CommunicationObject communicationObject, TimeSpan timeout, AsyncCallback callback, object state)
  892. : base(callback, state)
  893. {
  894. this.communicationObject = communicationObject;
  895. this.timeout = new TimeoutHelper(timeout);
  896. base.OnCompleting = new Action<AsyncResult, Exception>(OnOpenCompleted);
  897. if (InvokeOpen())
  898. {
  899. this.Complete(true);
  900. }
  901. }
  902. bool InvokeOpen()
  903. {
  904. IAsyncResult result = this.communicationObject.OnBeginOpen(this.timeout.RemainingTime(),
  905. base.PrepareAsyncCompletion(onOpenCompletion), this);
  906. if (result.CompletedSynchronously)
  907. {
  908. return OnOpenCompletion(result);
  909. }
  910. else
  911. {
  912. return false;
  913. }
  914. }
  915. void NotifyOpened()
  916. {
  917. this.communicationObject.OnOpened();
  918. if (!this.communicationObject.onOpenedCalled)
  919. {
  920. throw TraceUtility.ThrowHelperError(
  921. this.communicationObject.CreateBaseClassMethodNotCalledException("OnOpened"),
  922. Guid.Empty, this.communicationObject);
  923. }
  924. }
  925. void OnOpenCompleted(AsyncResult result, Exception exception)
  926. {
  927. if (exception != null)
  928. {
  929. if (DiagnosticUtility.ShouldTraceWarning)
  930. {
  931. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectOpenFailed,
  932. SR.GetString(SR.TraceCodeCommunicationObjectOpenFailed, this.communicationObject.GetCommunicationObjectType().ToString()),
  933. this, exception);
  934. }
  935. this.communicationObject.Fault();
  936. }
  937. }
  938. static bool OnOpenCompletion(IAsyncResult result)
  939. {
  940. OpenAsyncResult thisPtr = (OpenAsyncResult)result.AsyncState;
  941. thisPtr.communicationObject.OnEndOpen(result);
  942. thisPtr.NotifyOpened();
  943. return true;
  944. }
  945. public static void End(IAsyncResult result)
  946. {
  947. AsyncResult.End<OpenAsyncResult>(result);
  948. }
  949. }
  950. class CloseAsyncResult : TraceAsyncResult
  951. {
  952. static AsyncCompletion onCloseCompletion = new AsyncCompletion(OnCloseCompletion);
  953. CommunicationObject communicationObject;
  954. TimeoutHelper timeout;
  955. public CloseAsyncResult(CommunicationObject communicationObject, TimeSpan timeout, AsyncCallback callback, object state)
  956. : base(callback, state)
  957. {
  958. this.communicationObject = communicationObject;
  959. this.timeout = new TimeoutHelper(timeout);
  960. base.OnCompleting = new Action<AsyncResult, Exception>(OnCloseCompleted);
  961. if (InvokeClose())
  962. {
  963. this.Complete(true);
  964. }
  965. }
  966. bool InvokeClose()
  967. {
  968. IAsyncResult result = this.communicationObject.OnBeginClose(this.timeout.RemainingTime(),
  969. base.PrepareAsyncCompletion(onCloseCompletion), this);
  970. if (result.CompletedSynchronously)
  971. {
  972. return OnCloseCompletion(result);
  973. }
  974. else
  975. {
  976. return false;
  977. }
  978. }
  979. void NotifyClosed()
  980. {
  981. this.communicationObject.OnClosed();
  982. if (!this.communicationObject.onClosedCalled)
  983. {
  984. throw TraceUtility.ThrowHelperError(
  985. this.communicationObject.CreateBaseClassMethodNotCalledException("OnClosed"),
  986. Guid.Empty, this.communicationObject);
  987. }
  988. }
  989. void OnCloseCompleted(AsyncResult result, Exception exception)
  990. {
  991. if (exception != null)
  992. {
  993. if (DiagnosticUtility.ShouldTraceWarning)
  994. {
  995. TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectCloseFailed,
  996. SR.GetString(SR.TraceCodeCommunicationObjectCloseFailed, this.communicationObject.GetCommunicationObjectType().ToString()),
  997. this, exception);
  998. }
  999. this.communicationObject.Abort();
  1000. }
  1001. }
  1002. static bool OnCloseCompletion(IAsyncResult result)
  1003. {
  1004. CloseAsyncResult thisPtr = (CloseAsyncResult)result.AsyncState;
  1005. thisPtr.communicationObject.OnEndClose(result);
  1006. thisPtr.NotifyClosed();
  1007. return true;
  1008. }
  1009. public static void End(IAsyncResult result)
  1010. {
  1011. AsyncResult.End<CloseAsyncResult>(result);
  1012. }
  1013. }
  1014. }
  1015. }