ReliableOutputConnection.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Runtime;
  7. using System.Threading;
  8. using System.Xml;
  9. delegate IAsyncResult BeginSendHandler(MessageAttemptInfo attemptInfo, TimeSpan timeout, bool maskUnhandledException, AsyncCallback asyncCallback, object state);
  10. delegate void EndSendHandler(IAsyncResult result);
  11. delegate void SendHandler(MessageAttemptInfo attemptInfo, TimeSpan timeout, bool maskUnhandledException);
  12. delegate void ComponentFaultedHandler(Exception faultException, WsrmFault fault);
  13. delegate void ComponentExceptionHandler(Exception exception);
  14. delegate void RetryHandler(MessageAttemptInfo attemptInfo);
  15. sealed class ReliableOutputConnection
  16. {
  17. BeginSendHandler beginSendHandler;
  18. OperationWithTimeoutBeginCallback beginSendAckRequestedHandler;
  19. bool closed = false;
  20. EndSendHandler endSendHandler;
  21. OperationEndCallback endSendAckRequestedHandler;
  22. UniqueId id;
  23. MessageVersion messageVersion;
  24. object mutex = new Object();
  25. static AsyncCallback onSendRetriesComplete = Fx.ThunkCallback(new AsyncCallback(OnSendRetriesComplete));
  26. static AsyncCallback onSendRetryComplete = Fx.ThunkCallback(new AsyncCallback(OnSendRetryComplete));
  27. ReliableMessagingVersion reliableMessagingVersion;
  28. Guard sendGuard = new Guard(Int32.MaxValue);
  29. SendHandler sendHandler;
  30. OperationWithTimeoutCallback sendAckRequestedHandler;
  31. static Action<object> sendRetries = new Action<object>(SendRetries);
  32. TimeSpan sendTimeout;
  33. InterruptibleWaitObject shutdownHandle = new InterruptibleWaitObject(false);
  34. TransmissionStrategy strategy;
  35. bool terminated = false;
  36. public ReliableOutputConnection(UniqueId id,
  37. int maxTransferWindowSize,
  38. MessageVersion messageVersion,
  39. ReliableMessagingVersion reliableMessagingVersion,
  40. TimeSpan initialRtt,
  41. bool requestAcks,
  42. TimeSpan sendTimeout)
  43. {
  44. this.id = id;
  45. this.messageVersion = messageVersion;
  46. this.reliableMessagingVersion = reliableMessagingVersion;
  47. this.sendTimeout = sendTimeout;
  48. this.strategy = new TransmissionStrategy(reliableMessagingVersion, initialRtt, maxTransferWindowSize,
  49. requestAcks, id);
  50. this.strategy.RetryTimeoutElapsed = OnRetryTimeoutElapsed;
  51. this.strategy.OnException = RaiseOnException;
  52. }
  53. public ComponentFaultedHandler Faulted;
  54. public ComponentExceptionHandler OnException;
  55. MessageVersion MessageVersion
  56. {
  57. get
  58. {
  59. return this.messageVersion;
  60. }
  61. }
  62. public BeginSendHandler BeginSendHandler
  63. {
  64. set
  65. {
  66. this.beginSendHandler = value;
  67. }
  68. }
  69. public OperationWithTimeoutBeginCallback BeginSendAckRequestedHandler
  70. {
  71. set
  72. {
  73. this.beginSendAckRequestedHandler = value;
  74. }
  75. }
  76. public bool Closed
  77. {
  78. get
  79. {
  80. return this.closed;
  81. }
  82. }
  83. public EndSendHandler EndSendHandler
  84. {
  85. set
  86. {
  87. this.endSendHandler = value;
  88. }
  89. }
  90. public OperationEndCallback EndSendAckRequestedHandler
  91. {
  92. set
  93. {
  94. this.endSendAckRequestedHandler = value;
  95. }
  96. }
  97. public Int64 Last
  98. {
  99. get
  100. {
  101. return this.strategy.Last;
  102. }
  103. }
  104. public SendHandler SendHandler
  105. {
  106. set
  107. {
  108. this.sendHandler = value;
  109. }
  110. }
  111. public OperationWithTimeoutCallback SendAckRequestedHandler
  112. {
  113. set
  114. {
  115. this.sendAckRequestedHandler = value;
  116. }
  117. }
  118. public TransmissionStrategy Strategy
  119. {
  120. get
  121. {
  122. return this.strategy;
  123. }
  124. }
  125. object ThisLock
  126. {
  127. get { return this.mutex; }
  128. }
  129. public void Abort(ChannelBase channel)
  130. {
  131. this.sendGuard.Abort();
  132. this.shutdownHandle.Abort(channel);
  133. this.strategy.Abort(channel);
  134. }
  135. void CompleteTransfer(TimeSpan timeout)
  136. {
  137. if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  138. {
  139. Message message = Message.CreateMessage(this.MessageVersion, WsrmFeb2005Strings.LastMessageAction);
  140. message.Properties.AllowOutputBatching = false;
  141. // Return value ignored.
  142. this.InternalAddMessage(message, timeout, null, true);
  143. }
  144. else if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11)
  145. {
  146. if (this.strategy.SetLast())
  147. {
  148. this.shutdownHandle.Set();
  149. }
  150. else
  151. {
  152. this.sendAckRequestedHandler(timeout);
  153. }
  154. }
  155. else
  156. {
  157. throw Fx.AssertAndThrow("Unsupported version.");
  158. }
  159. }
  160. public bool AddMessage(Message message, TimeSpan timeout, object state)
  161. {
  162. return this.InternalAddMessage(message, timeout, state, false);
  163. }
  164. public IAsyncResult BeginAddMessage(Message message, TimeSpan timeout, object state, AsyncCallback callback, object asyncState)
  165. {
  166. return new AddAsyncResult(message, false, timeout, state, this, callback, asyncState);
  167. }
  168. public bool EndAddMessage(IAsyncResult result)
  169. {
  170. return AddAsyncResult.End(result);
  171. }
  172. IAsyncResult BeginCompleteTransfer(TimeSpan timeout, AsyncCallback callback, object state)
  173. {
  174. if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  175. {
  176. Message message = Message.CreateMessage(this.MessageVersion, WsrmFeb2005Strings.LastMessageAction);
  177. message.Properties.AllowOutputBatching = false;
  178. return new AddAsyncResult(message, true, timeout, null, this, callback, state);
  179. }
  180. else if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11)
  181. {
  182. if (this.strategy.SetLast())
  183. {
  184. this.shutdownHandle.Set();
  185. return new AlreadyCompletedTransferAsyncResult(callback, state);
  186. }
  187. else
  188. {
  189. return this.beginSendAckRequestedHandler(timeout, callback, state);
  190. }
  191. }
  192. else
  193. {
  194. throw Fx.AssertAndThrow("Unsupported version.");
  195. }
  196. }
  197. void EndCompleteTransfer(IAsyncResult result)
  198. {
  199. if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  200. {
  201. AddAsyncResult.End(result);
  202. }
  203. else if (this.reliableMessagingVersion == ReliableMessagingVersion.WSReliableMessaging11)
  204. {
  205. AlreadyCompletedTransferAsyncResult completedResult = result as AlreadyCompletedTransferAsyncResult;
  206. if (completedResult != null)
  207. {
  208. completedResult.End();
  209. }
  210. else
  211. {
  212. this.endSendAckRequestedHandler(result);
  213. }
  214. }
  215. else
  216. {
  217. throw Fx.AssertAndThrow("Unsupported version.");
  218. }
  219. }
  220. public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  221. {
  222. bool completeTransfer = false;
  223. lock (this.ThisLock)
  224. {
  225. completeTransfer = !this.closed;
  226. this.closed = true;
  227. }
  228. OperationWithTimeoutBeginCallback[] beginCallbacks;
  229. OperationEndCallback[] endCallbacks;
  230. beginCallbacks = new OperationWithTimeoutBeginCallback[] {
  231. completeTransfer ? this.BeginCompleteTransfer : default(OperationWithTimeoutBeginCallback),
  232. this.shutdownHandle.BeginWait,
  233. this.sendGuard.BeginClose,
  234. this.beginSendAckRequestedHandler };
  235. endCallbacks = new OperationEndCallback[] {
  236. completeTransfer ? this.EndCompleteTransfer : default(OperationEndCallback),
  237. this.shutdownHandle.EndWait,
  238. this.sendGuard.EndClose,
  239. this.endSendAckRequestedHandler };
  240. return OperationWithTimeoutComposer.BeginComposeAsyncOperations(timeout, beginCallbacks, endCallbacks, callback, state);
  241. }
  242. public bool CheckForTermination()
  243. {
  244. return this.strategy.DoneTransmitting;
  245. }
  246. public void Close(TimeSpan timeout)
  247. {
  248. bool completeTransfer = false;
  249. lock (this.ThisLock)
  250. {
  251. completeTransfer = !this.closed;
  252. this.closed = true;
  253. }
  254. TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
  255. if (completeTransfer)
  256. {
  257. this.CompleteTransfer(timeoutHelper.RemainingTime());
  258. }
  259. this.shutdownHandle.Wait(timeoutHelper.RemainingTime());
  260. this.sendGuard.Close(timeoutHelper.RemainingTime());
  261. this.strategy.Close();
  262. }
  263. void CompleteSendRetries(IAsyncResult result)
  264. {
  265. while (true)
  266. {
  267. this.endSendHandler(result);
  268. this.sendGuard.Exit();
  269. this.strategy.DequeuePending();
  270. if (this.sendGuard.Enter())
  271. {
  272. MessageAttemptInfo attemptInfo = this.strategy.GetMessageInfoForRetry(true);
  273. if (attemptInfo.Message == null)
  274. {
  275. break;
  276. }
  277. else
  278. {
  279. result = this.beginSendHandler(attemptInfo, this.sendTimeout, true, onSendRetriesComplete, this);
  280. if (!result.CompletedSynchronously)
  281. {
  282. return;
  283. }
  284. }
  285. }
  286. else
  287. {
  288. return;
  289. }
  290. }
  291. // We are here if there are no more messages to retry.
  292. this.sendGuard.Exit();
  293. this.OnTransferComplete();
  294. }
  295. void CompleteSendRetry(IAsyncResult result)
  296. {
  297. try
  298. {
  299. this.endSendHandler(result);
  300. }
  301. finally
  302. {
  303. this.sendGuard.Exit();
  304. }
  305. }
  306. public void EndClose(IAsyncResult result)
  307. {
  308. OperationWithTimeoutComposer.EndComposeAsyncOperations(result);
  309. this.strategy.Close();
  310. }
  311. public void Fault(ChannelBase channel)
  312. {
  313. this.sendGuard.Abort();
  314. this.shutdownHandle.Fault(channel);
  315. this.strategy.Fault(channel);
  316. }
  317. bool InternalAddMessage(Message message, TimeSpan timeout, object state, bool isLast)
  318. {
  319. MessageAttemptInfo attemptInfo;
  320. TimeoutHelper helper = new TimeoutHelper(timeout);
  321. try
  322. {
  323. if (isLast)
  324. {
  325. if (state != null)
  326. {
  327. throw Fx.AssertAndThrow("The isLast overload does not take a state.");
  328. }
  329. attemptInfo = this.strategy.AddLast(message, helper.RemainingTime(), null);
  330. }
  331. else if (!this.strategy.Add(message, helper.RemainingTime(), state, out attemptInfo))
  332. {
  333. return false;
  334. }
  335. }
  336. catch (TimeoutException)
  337. {
  338. if (isLast)
  339. this.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.id, SR.GetString(SR.SequenceTerminatedAddLastToWindowTimedOut), null));
  340. // else - RM does not fault the channel based on a timeout exception trying to add a sequenced message to the window.
  341. throw;
  342. }
  343. catch (Exception e)
  344. {
  345. if (!Fx.IsFatal(e))
  346. this.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.id, SR.GetString(SR.SequenceTerminatedUnknownAddToWindowError), null));
  347. throw;
  348. }
  349. if (sendGuard.Enter())
  350. {
  351. try
  352. {
  353. this.sendHandler(attemptInfo, helper.RemainingTime(), false);
  354. }
  355. catch (QuotaExceededException)
  356. {
  357. this.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.id));
  358. throw;
  359. }
  360. finally
  361. {
  362. this.sendGuard.Exit();
  363. }
  364. }
  365. return true;
  366. }
  367. public bool IsFinalAckConsistent(SequenceRangeCollection ranges)
  368. {
  369. return this.strategy.IsFinalAckConsistent(ranges);
  370. }
  371. void OnRetryTimeoutElapsed(MessageAttemptInfo attemptInfo)
  372. {
  373. if (this.sendGuard.Enter())
  374. {
  375. IAsyncResult result = this.beginSendHandler(attemptInfo, this.sendTimeout, true, onSendRetryComplete, this);
  376. if (result.CompletedSynchronously)
  377. {
  378. this.CompleteSendRetry(result);
  379. }
  380. }
  381. }
  382. static void OnSendRetryComplete(IAsyncResult result)
  383. {
  384. if (!result.CompletedSynchronously)
  385. {
  386. ReliableOutputConnection outputConnection = (ReliableOutputConnection)result.AsyncState;
  387. try
  388. {
  389. outputConnection.CompleteSendRetry(result);
  390. }
  391. #pragma warning suppress 56500 // covered by FxCOP
  392. catch (Exception e)
  393. {
  394. if (Fx.IsFatal(e))
  395. throw;
  396. outputConnection.RaiseOnException(e);
  397. }
  398. }
  399. }
  400. static void OnSendRetriesComplete(IAsyncResult result)
  401. {
  402. if (!result.CompletedSynchronously)
  403. {
  404. ReliableOutputConnection outputConnection = (ReliableOutputConnection)result.AsyncState;
  405. try
  406. {
  407. outputConnection.CompleteSendRetries(result);
  408. }
  409. #pragma warning suppress 56500 // covered by FxCOP
  410. catch (Exception e)
  411. {
  412. if (Fx.IsFatal(e))
  413. throw;
  414. outputConnection.RaiseOnException(e);
  415. }
  416. }
  417. }
  418. void OnTransferComplete()
  419. {
  420. this.strategy.DequeuePending();
  421. if (this.strategy.DoneTransmitting)
  422. Terminate();
  423. }
  424. public void ProcessTransferred(Int64 transferred, SequenceRangeCollection ranges, int quotaRemaining)
  425. {
  426. if (transferred < 0)
  427. {
  428. throw Fx.AssertAndThrow("Argument transferred must be a valid sequence number or 0 for protocol messages.");
  429. }
  430. bool invalidAck;
  431. // ignored, TransmissionStrategy is being used to keep track of what must be re-sent.
  432. // In the Request-Reply case this state may not align with acks.
  433. bool inconsistentAck;
  434. this.strategy.ProcessAcknowledgement(ranges, out invalidAck, out inconsistentAck);
  435. invalidAck = (invalidAck || ((transferred != 0) && !ranges.Contains(transferred)));
  436. if (!invalidAck)
  437. {
  438. if ((transferred > 0) && this.strategy.ProcessTransferred(transferred, quotaRemaining))
  439. {
  440. ActionItem.Schedule(sendRetries, this);
  441. }
  442. else
  443. {
  444. this.OnTransferComplete();
  445. }
  446. }
  447. else
  448. {
  449. WsrmFault fault = new InvalidAcknowledgementFault(this.id, ranges);
  450. RaiseFault(fault.CreateException(), fault);
  451. }
  452. }
  453. public void ProcessTransferred(SequenceRangeCollection ranges, int quotaRemaining)
  454. {
  455. bool invalidAck;
  456. bool inconsistentAck;
  457. this.strategy.ProcessAcknowledgement(ranges, out invalidAck, out inconsistentAck);
  458. if (!invalidAck && !inconsistentAck)
  459. {
  460. if (this.strategy.ProcessTransferred(ranges, quotaRemaining))
  461. {
  462. ActionItem.Schedule(sendRetries, this);
  463. }
  464. else
  465. {
  466. this.OnTransferComplete();
  467. }
  468. }
  469. else
  470. {
  471. WsrmFault fault = new InvalidAcknowledgementFault(this.id, ranges);
  472. RaiseFault(fault.CreateException(), fault);
  473. }
  474. }
  475. void RaiseFault(Exception faultException, WsrmFault fault)
  476. {
  477. ComponentFaultedHandler handler = this.Faulted;
  478. if (handler != null)
  479. handler(faultException, fault);
  480. }
  481. void RaiseOnException(Exception exception)
  482. {
  483. ComponentExceptionHandler handler = this.OnException;
  484. if (handler != null)
  485. handler(exception);
  486. }
  487. void SendRetries()
  488. {
  489. IAsyncResult result = null;
  490. if (this.sendGuard.Enter())
  491. {
  492. MessageAttemptInfo attemptInfo = this.strategy.GetMessageInfoForRetry(false);
  493. if (attemptInfo.Message != null)
  494. {
  495. result = this.beginSendHandler(attemptInfo, this.sendTimeout, true, onSendRetriesComplete, this);
  496. }
  497. if (result != null)
  498. {
  499. if (result.CompletedSynchronously)
  500. {
  501. this.CompleteSendRetries(result);
  502. }
  503. }
  504. else
  505. {
  506. this.sendGuard.Exit();
  507. this.OnTransferComplete();
  508. }
  509. }
  510. }
  511. static void SendRetries(object state)
  512. {
  513. ReliableOutputConnection outputConnection = (ReliableOutputConnection)state;
  514. try
  515. {
  516. outputConnection.SendRetries();
  517. }
  518. #pragma warning suppress 56500 // covered by FxCOP
  519. catch (Exception e)
  520. {
  521. if (Fx.IsFatal(e))
  522. throw;
  523. outputConnection.RaiseOnException(e);
  524. }
  525. }
  526. public void Terminate()
  527. {
  528. lock (this.ThisLock)
  529. {
  530. if (this.terminated)
  531. return;
  532. this.terminated = true;
  533. }
  534. this.shutdownHandle.Set();
  535. }
  536. sealed class AddAsyncResult : AsyncResult
  537. {
  538. static AsyncCallback addCompleteStatic = Fx.ThunkCallback(new AsyncCallback(AddComplete));
  539. ReliableOutputConnection connection;
  540. bool isLast;
  541. static AsyncCallback sendCompleteStatic = Fx.ThunkCallback(new AsyncCallback(SendComplete));
  542. TimeoutHelper timeoutHelper;
  543. bool validAdd;
  544. public AddAsyncResult(Message message, bool isLast, TimeSpan timeout, object state,
  545. ReliableOutputConnection connection, AsyncCallback callback, object asyncState)
  546. : base(callback, asyncState)
  547. {
  548. this.connection = connection;
  549. this.timeoutHelper = new TimeoutHelper(timeout);
  550. this.isLast = isLast;
  551. bool complete = false;
  552. IAsyncResult result;
  553. try
  554. {
  555. if (isLast)
  556. {
  557. if (state != null)
  558. {
  559. throw Fx.AssertAndThrow("The isLast overload does not take a state.");
  560. }
  561. result = this.connection.strategy.BeginAddLast(message, this.timeoutHelper.RemainingTime(), state, addCompleteStatic, this);
  562. }
  563. else
  564. {
  565. result = this.connection.strategy.BeginAdd(message, this.timeoutHelper.RemainingTime(), state, addCompleteStatic, this);
  566. }
  567. }
  568. catch (TimeoutException)
  569. {
  570. if (isLast)
  571. this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedAddLastToWindowTimedOut), null));
  572. // else - RM does not fault the channel based on a timeout exception trying to add a sequenced message to the window.
  573. throw;
  574. }
  575. catch (Exception e)
  576. {
  577. if (!Fx.IsFatal(e))
  578. this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedUnknownAddToWindowError), null));
  579. throw;
  580. }
  581. if (result.CompletedSynchronously)
  582. complete = this.CompleteAdd(result);
  583. if (complete)
  584. this.Complete(true);
  585. }
  586. static void AddComplete(IAsyncResult result)
  587. {
  588. if (!result.CompletedSynchronously)
  589. {
  590. AddAsyncResult addResult = (AddAsyncResult)result.AsyncState;
  591. bool complete = false;
  592. Exception completeException = null;
  593. try
  594. {
  595. complete = addResult.CompleteAdd(result);
  596. }
  597. #pragma warning suppress 56500 // covered by FxCOP
  598. catch (Exception e)
  599. {
  600. if (Fx.IsFatal(e))
  601. throw;
  602. completeException = e;
  603. }
  604. if (complete || completeException != null)
  605. addResult.Complete(false, completeException);
  606. }
  607. }
  608. bool CompleteAdd(IAsyncResult result)
  609. {
  610. MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
  611. this.validAdd = true;
  612. try
  613. {
  614. if (this.isLast)
  615. {
  616. attemptInfo = this.connection.strategy.EndAddLast(result);
  617. }
  618. else if (!this.connection.strategy.EndAdd(result, out attemptInfo))
  619. {
  620. this.validAdd = false;
  621. return true;
  622. }
  623. }
  624. catch (TimeoutException)
  625. {
  626. if (this.isLast)
  627. this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedAddLastToWindowTimedOut), null));
  628. // else - RM does not fault the channel based on a timeout exception trying to add a sequenced message to the window.
  629. throw;
  630. }
  631. catch (Exception e)
  632. {
  633. if (!Fx.IsFatal(e))
  634. this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedUnknownAddToWindowError), null));
  635. throw;
  636. }
  637. if (this.connection.sendGuard.Enter())
  638. {
  639. bool throwing = true;
  640. try
  641. {
  642. result = this.connection.beginSendHandler(attemptInfo, this.timeoutHelper.RemainingTime(), false, sendCompleteStatic, this);
  643. throwing = false;
  644. }
  645. catch (QuotaExceededException)
  646. {
  647. this.connection.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.connection.id));
  648. throw;
  649. }
  650. finally
  651. {
  652. if (throwing)
  653. this.connection.sendGuard.Exit();
  654. }
  655. }
  656. else
  657. {
  658. return true;
  659. }
  660. if (result.CompletedSynchronously)
  661. {
  662. this.CompleteSend(result);
  663. return true;
  664. }
  665. else
  666. {
  667. return false;
  668. }
  669. }
  670. void CompleteSend(IAsyncResult result)
  671. {
  672. try
  673. {
  674. this.connection.endSendHandler(result);
  675. }
  676. catch (QuotaExceededException)
  677. {
  678. this.connection.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.connection.id));
  679. throw;
  680. }
  681. finally
  682. {
  683. this.connection.sendGuard.Exit();
  684. }
  685. }
  686. static void SendComplete(IAsyncResult result)
  687. {
  688. if (!result.CompletedSynchronously)
  689. {
  690. AddAsyncResult addResult = (AddAsyncResult)result.AsyncState;
  691. Exception completeException = null;
  692. try
  693. {
  694. addResult.CompleteSend(result);
  695. }
  696. #pragma warning suppress 56500 // covered by FxCOP
  697. catch (Exception e)
  698. {
  699. if (Fx.IsFatal(e))
  700. throw;
  701. completeException = e;
  702. }
  703. addResult.Complete(false, completeException);
  704. }
  705. }
  706. public static bool End(IAsyncResult result)
  707. {
  708. AsyncResult.End<AddAsyncResult>(result);
  709. return ((AddAsyncResult)result).validAdd;
  710. }
  711. }
  712. class AlreadyCompletedTransferAsyncResult : CompletedAsyncResult
  713. {
  714. public AlreadyCompletedTransferAsyncResult(AsyncCallback callback, object state)
  715. : base(callback, state)
  716. {
  717. }
  718. public void End()
  719. {
  720. AsyncResult.End<AlreadyCompletedTransferAsyncResult>(this);
  721. }
  722. }
  723. }
  724. }