1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
|
// <copyright>
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
namespace System.ServiceModel.Channels
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Runtime;
using System.Runtime.Diagnostics;
using System.ServiceModel.Diagnostics;
using System.Threading;
using System.Xml;
internal abstract class UdpOutputChannel : OutputChannel, IOutputChannel
{
private bool cleanedUp;
private volatile AsyncWaitHandle retransmissionDoneWaitHandle;
private UdpRetransmissionSettings retransmitSettings;
private volatile Dictionary<UniqueId, IUdpRetransmitter> retransmitList;
private SynchronizedRandom randomNumberGenerator;
private Uri via;
public UdpOutputChannel(
ChannelManagerBase factory,
MessageEncoder encoder,
BufferManager bufferManager,
UdpSocket[] sendSockets,
UdpRetransmissionSettings retransmissionSettings,
Uri via,
bool isMulticast)
: base(factory)
{
Fx.Assert(encoder != null, "encoder shouldn't be null");
Fx.Assert(bufferManager != null, "buffer manager shouldn't be null");
Fx.Assert(sendSockets != null, "sendSockets can't be null");
Fx.Assert(sendSockets.Length > 0, "sendSockets can't be empty");
Fx.Assert(retransmissionSettings != null, "retransmissionSettings can't be null");
Fx.Assert(via != null, "via can't be null");
this.BufferManager = bufferManager;
this.IsMulticast = isMulticast;
this.Encoder = encoder;
this.retransmitSettings = retransmissionSettings;
this.SendSockets = sendSockets;
this.via = via;
if (this.retransmitSettings.Enabled)
{
this.retransmitList = new Dictionary<UniqueId, IUdpRetransmitter>();
this.randomNumberGenerator = new SynchronizedRandom(AppDomain.CurrentDomain.GetHashCode() | Environment.TickCount);
}
}
private interface IUdpRetransmitter
{
bool IsMulticast { get; }
void CancelRetransmission();
}
public override EndpointAddress RemoteAddress
{
get { return null; }
}
public override Uri Via
{
get { return this.via; }
}
internal bool IsMulticast
{
get;
private set;
}
internal TimeSpan InternalSendTimeout
{
get { return this.DefaultSendTimeout; }
}
protected BufferManager BufferManager
{
get;
private set;
}
protected MessageEncoder Encoder
{
get;
private set;
}
protected UdpSocket[] SendSockets
{
get;
private set;
}
[SuppressMessage("Microsoft.StyleCop.CSharp.ReadabilityRules", "SA1100:DoNotPrefixCallsWithBaseUnlessLocalImplementationExists", Justification = "StyleCop 4.5 does not validate this rule properly.")]
public override T GetProperty<T>()
{
if (typeof(T) == typeof(IOutputChannel))
{
return (T)(object)this;
}
T messageEncoderProperty = this.Encoder.GetProperty<T>();
if (messageEncoderProperty != null)
{
return messageEncoderProperty;
}
return base.GetProperty<T>();
}
internal void CancelRetransmission(UniqueId messageId)
{
if (messageId != null && this.retransmitList != null)
{
lock (this.ThisLock)
{
if (this.retransmitList != null)
{
IUdpRetransmitter retransmitter;
if (this.retransmitList.TryGetValue(messageId, out retransmitter))
{
this.retransmitList.Remove(messageId);
retransmitter.CancelRetransmission();
}
}
}
}
}
protected static void LogMessage(ref Message message, ArraySegment<byte> messageData)
{
using (XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(messageData.Array, messageData.Offset, messageData.Count, null, XmlDictionaryReaderQuotas.Max, null))
{
MessageLogger.LogMessage(ref message, xmlDictionaryReader, MessageLoggingSource.TransportSend);
}
}
protected override void AddHeadersTo(Message message)
{
Fx.Assert(message != null, "Message can't be null");
if (message is NullMessage)
{
return;
}
if (message.Version.Addressing != AddressingVersion.None)
{
if (message.Headers.MessageId == null)
{
message.Headers.MessageId = new UniqueId();
}
}
else
{
if (this.retransmitSettings.Enabled == true)
{
// we should only get here if some channel above us starts producing messages that don't match the encoder's message version.
throw FxTrace.Exception.AsError(new ProtocolException(SR.RetransmissionRequiresAddressingOnMessage(message.Version.Addressing.ToString())));
}
}
}
protected override IAsyncResult OnBeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
{
if (message is NullMessage)
{
return new CompletedAsyncResult(callback, state);
}
return new SendAsyncResult(this, message, timeout, callback, state);
}
protected override void OnEndSend(IAsyncResult result)
{
if (result is CompletedAsyncResult)
{
CompletedAsyncResult.End(result);
}
else
{
SendAsyncResult.End(result);
}
}
protected override void OnSend(Message message, TimeSpan timeout)
{
if (message is NullMessage)
{
return;
}
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
IPEndPoint remoteEndPoint;
UdpSocket[] sendSockets;
Exception exceptionToBeThrown;
sendSockets = this.GetSendSockets(message, out remoteEndPoint, out exceptionToBeThrown);
if (exceptionToBeThrown != null)
{
throw FxTrace.Exception.AsError(exceptionToBeThrown);
}
if (timeoutHelper.RemainingTime() <= TimeSpan.Zero)
{
throw FxTrace.Exception.AsError(new TimeoutException(SR.SendTimedOut(remoteEndPoint, timeout)));
}
bool returnBuffer = false;
ArraySegment<byte> messageData = default(ArraySegment<byte>);
bool sendingMulticast = UdpUtility.IsMulticastAddress(remoteEndPoint.Address);
SynchronousRetransmissionHelper retransmitHelper = null;
RetransmitIterator retransmitIterator = null;
bool shouldRetransmit = this.ShouldRetransmitMessage(sendingMulticast);
try
{
if (shouldRetransmit)
{
retransmitIterator = this.CreateRetransmitIterator(sendingMulticast);
retransmitHelper = new SynchronousRetransmissionHelper(sendingMulticast);
this.RetransmitStarting(message.Headers.MessageId, retransmitHelper);
}
messageData = this.EncodeMessage(message);
returnBuffer = true;
this.TransmitMessage(messageData, sendSockets, remoteEndPoint, timeoutHelper);
if (shouldRetransmit)
{
while (retransmitIterator.MoveNext())
{
// wait for currentDelay time, then retransmit
if (retransmitIterator.CurrentDelay > 0)
{
retransmitHelper.Wait(retransmitIterator.CurrentDelay);
}
if (retransmitHelper.IsCanceled)
{
ThrowIfAborted();
return;
}
// since we only invoke the encoder once just before the initial send of the message
// we need to handle logging the message in the retransmission case
if (MessageLogger.LogMessagesAtTransportLevel)
{
UdpOutputChannel.LogMessage(ref message, messageData);
}
this.TransmitMessage(messageData, sendSockets, remoteEndPoint, timeoutHelper);
}
}
}
finally
{
if (returnBuffer)
{
this.BufferManager.ReturnBuffer(messageData.Array);
}
if (shouldRetransmit)
{
this.RetransmitStopping(message.Headers.MessageId);
if (retransmitHelper != null)
{
retransmitHelper.Dispose();
}
}
}
}
protected abstract UdpSocket[] GetSendSockets(Message message, out IPEndPoint remoteEndPoint, out Exception exceptionToBeThrown);
protected override void OnAbort()
{
this.Cleanup(true, TimeSpan.Zero);
}
[SuppressMessage("Microsoft.StyleCop.CSharp.ReadabilityRules", "SA1100:DoNotPrefixCallsWithBaseUnlessLocalImplementationExists", Justification = "If BeginClose is overridden we still pass base.BeginClose here")]
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
{
return new CloseAsyncResult(
this,
timeout,
callback,
state);
}
protected override void OnClose(TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
this.Cleanup(false, timeoutHelper.RemainingTime());
}
protected override void OnEndClose(IAsyncResult result)
{
CloseAsyncResult.End(result);
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
this.OnOpen(timeout);
return new CompletedAsyncResult(callback, state);
}
protected override void OnEndOpen(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
protected override void OnOpen(TimeSpan timeout)
{
for (int i = 0; i < this.SendSockets.Length; i++)
{
this.SendSockets[i].Open();
}
}
protected ArraySegment<byte> EncodeMessage(Message message)
{
return this.Encoder.WriteMessage(message, int.MaxValue, this.BufferManager);
}
protected ObjectDisposedException CreateObjectDisposedException()
{
return new ObjectDisposedException(null, SR.ObjectDisposed(this.GetType().Name));
}
private RetransmitIterator CreateRetransmitIterator(bool sendingMulticast)
{
Fx.Assert(this.retransmitSettings.Enabled, "CreateRetransmitCalculator called when no retransmission set to happen");
int lowerBound = this.retransmitSettings.GetDelayLowerBound();
int upperBound = this.retransmitSettings.GetDelayUpperBound();
int currentDelay = this.randomNumberGenerator.Next(lowerBound, upperBound);
int maxDelay = this.retransmitSettings.GetMaxDelayPerRetransmission();
int maxRetransmitCount = sendingMulticast ? this.retransmitSettings.MaxMulticastRetransmitCount : this.retransmitSettings.MaxUnicastRetransmitCount;
return new RetransmitIterator(currentDelay, maxDelay, maxRetransmitCount);
}
private void RetransmitStarting(UniqueId messageId, IUdpRetransmitter retransmitter)
{
Fx.Assert(this.retransmitSettings.Enabled, "RetransmitStarting called when retransmission is disabled");
lock (this.ThisLock)
{
ThrowIfDisposed();
if (this.retransmitList.ContainsKey(messageId))
{
// someone is sending a message with the same MessageId
// while a retransmission is still in progress for that ID.
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.RecycledMessageIdDuringRetransmission(messageId)));
}
else
{
this.retransmitList[messageId] = retransmitter;
}
}
}
private void RetransmitStopping(UniqueId messageId)
{
Fx.Assert(this.retransmitSettings.Enabled, "RetransmitStopping called when retransmission is disabled");
lock (this.ThisLock)
{
// Cleanup sets retransmitList to null, so check before using...
if (this.retransmitList != null)
{
this.retransmitList.Remove(messageId);
// if we are closing down, then we need to unblock the Cleanup code
// this.retransmissionDoneEvent only != null if on cleaning up; abort case means that it == null.
if (this.retransmitList.Count == 0 && this.retransmissionDoneWaitHandle != null)
{
this.retransmissionDoneWaitHandle.Set();
}
}
}
}
private bool ShouldRetransmitMessage(bool sendingMulticast)
{
if (sendingMulticast)
{
return this.retransmitSettings.MaxMulticastRetransmitCount > 0;
}
else
{
return this.retransmitSettings.MaxUnicastRetransmitCount > 0;
}
}
private void TransmitMessage(ArraySegment<byte> messageBytes, UdpSocket[] sockets, IPEndPoint remoteEndpoint, TimeoutHelper timeoutHelper)
{
Fx.Assert(messageBytes.Array != null, "message data array can't be null");
Fx.Assert(sockets != null, "sockets can't be null");
Fx.Assert(sockets.Length > 0, "sockets must contain at least one item");
Fx.Assert(remoteEndpoint != null, "remoteEndPoint can't be null");
for (int i = 0; i < sockets.Length; i++)
{
if (timeoutHelper.RemainingTime() <= TimeSpan.Zero)
{
throw FxTrace.Exception.AsError(new TimeoutException(SR.SendTimedOut(remoteEndpoint, timeoutHelper.OriginalTimeout)));
}
sockets[i].SendTo(messageBytes.Array, messageBytes.Offset, messageBytes.Count, remoteEndpoint);
}
}
// we're guaranteed by CommunicationObject that at most ONE of Close or BeginClose will be called once.
private void Cleanup(bool aborting, TimeSpan timeout)
{
bool needToWait = false;
if (this.cleanedUp)
{
return;
}
lock (this.ThisLock)
{
if (this.cleanedUp)
{
return;
}
if (!aborting && this.retransmitList != null && this.retransmitList.Count > 0)
{
needToWait = true;
this.retransmissionDoneWaitHandle = new AsyncWaitHandle(EventResetMode.ManualReset);
}
else
{
// copied this call here in order to avoid releasing then retaking lock
this.CleanupAfterWait(aborting);
}
}
if (needToWait)
{
if (!this.retransmissionDoneWaitHandle.Wait(timeout))
{
throw FxTrace.Exception.AsError(new TimeoutException(SR.TimeoutOnOperation(timeout)));
}
lock (this.ThisLock)
{
this.retransmissionDoneWaitHandle = null;
// another thread could have called Abort while Close() was waiting for retransmission to complete.
if (this.cleanedUp)
{
return;
}
this.CleanupAfterWait(aborting);
}
}
}
// must be called from within this.ThisLock
private void CleanupAfterWait(bool aborting)
{
Fx.Assert(!this.cleanedUp, "We should only clean up once");
if (this.retransmitList != null)
{
foreach (IUdpRetransmitter retransmitter in this.retransmitList.Values)
{
retransmitter.CancelRetransmission();
}
if (aborting && this.retransmissionDoneWaitHandle != null)
{
// If another thread has called close and is waiting for retransmission to complete,
// we need to make sure that thread gets unblocked.
this.retransmissionDoneWaitHandle.Set();
}
this.retransmitList = null;
}
for (int i = 0; i < this.SendSockets.Length; i++)
{
this.SendSockets[i].Close();
}
this.cleanedUp = true;
}
private class RetransmitIterator
{
private int maxDelay;
private int retransmitCount;
private int initialDelay;
internal RetransmitIterator(int initialDelay, int maxDelay, int retransmitCount)
{
Fx.Assert(initialDelay >= 0, "initialDelay cannot be negative");
Fx.Assert(maxDelay >= initialDelay, "maxDelay must be >= initialDelay");
Fx.Assert(retransmitCount > 0, "retransmitCount must be > 0");
this.CurrentDelay = -1;
this.initialDelay = initialDelay;
this.maxDelay = maxDelay;
this.retransmitCount = retransmitCount;
}
public int CurrentDelay
{
get;
private set;
}
// should be called before each retransmission to determine if
// another one is needed.
public bool MoveNext()
{
if (this.CurrentDelay < 0)
{
this.CurrentDelay = this.initialDelay;
return true;
}
bool shouldContinue = --this.retransmitCount > 0;
if (shouldContinue && this.CurrentDelay < this.maxDelay)
{
this.CurrentDelay = Math.Min(this.CurrentDelay * 2, this.maxDelay);
}
return shouldContinue;
}
}
private sealed class SynchronousRetransmissionHelper : IUdpRetransmitter, IDisposable
{
private ManualResetEvent cancelEvent;
private object thisLock;
private bool cleanedUp;
public SynchronousRetransmissionHelper(bool isMulticast)
{
this.thisLock = new object();
this.IsMulticast = isMulticast;
this.cancelEvent = new ManualResetEvent(false);
}
public bool IsMulticast
{
get;
private set;
}
public bool IsCanceled
{
get;
private set;
}
public void Wait(int millisecondsTimeout)
{
if (this.ResetEvent())
{
// Dispose should only be called by the same thread that
// is calling this function, making it so that we don't need a lock here...
this.cancelEvent.WaitOne(millisecondsTimeout);
}
}
public void CancelRetransmission()
{
lock (this.thisLock)
{
this.IsCanceled = true;
if (!this.cleanedUp)
{
this.cancelEvent.Set();
}
}
}
public void Dispose()
{
lock (this.thisLock)
{
if (!this.cleanedUp)
{
this.cleanedUp = true;
this.cancelEvent.Dispose();
}
}
}
private bool ResetEvent()
{
lock (this.thisLock)
{
if (!this.IsCanceled && !this.cleanedUp)
{
this.cancelEvent.Reset();
return true;
}
}
return false;
}
}
private class SendAsyncResult : AsyncResult, IUdpRetransmitter
{
private static AsyncCallback onSocketSendComplete = Fx.ThunkCallback(new AsyncCallback(OnSocketSendComplete));
private static Action<object> onRetransmitMessage = new Action<object>(OnRetransmitMessage);
private UdpOutputChannel channel;
private ArraySegment<byte> messageData;
private TimeoutHelper timeoutHelper;
private IPEndPoint remoteEndpoint;
private int currentSocket;
private UdpSocket[] sendSockets;
private IOThreadTimer retransmitTimer;
private RetransmitIterator retransmitIterator;
private Message message;
private bool retransmissionEnabled;
public SendAsyncResult(UdpOutputChannel channel, Message message, TimeSpan timeout, AsyncCallback callback, object state)
: base(callback, state)
{
this.timeoutHelper = new TimeoutHelper(timeout);
this.channel = channel;
this.message = message;
bool throwing = true;
bool completedSynchronously = false;
try
{
this.Initialize(message);
completedSynchronously = this.BeginTransmitMessage();
if (completedSynchronously && this.retransmissionEnabled)
{
// initial send completed sync, now we need to start the retransmission process...
completedSynchronously = this.BeginRetransmission();
}
throwing = false;
}
finally
{
if (throwing)
{
this.Cleanup();
}
}
if (completedSynchronously)
{
this.CompleteAndCleanup(true, null);
}
}
private enum RetransmitState
{
WaitCompleted,
TransmitCompleted,
}
public bool IsCanceled
{
get;
private set;
}
public bool IsMulticast
{
get;
private set;
}
public static void End(IAsyncResult result)
{
AsyncResult.End<SendAsyncResult>(result);
}
// tries to terminate retransmission early, but won't cancel async IO immediately
public void CancelRetransmission()
{
this.IsCanceled = true;
}
private static void OnSocketSendComplete(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
SendAsyncResult thisPtr = (SendAsyncResult)result.AsyncState;
bool completeSelf = false;
Exception completionException = null;
try
{
completeSelf = thisPtr.ContinueTransmitting(result);
if (completeSelf && thisPtr.retransmissionEnabled)
{
completeSelf = thisPtr.BeginRetransmission();
}
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
completionException = e;
completeSelf = true;
}
if (completeSelf)
{
thisPtr.CompleteAndCleanup(false, completionException);
}
}
private static void OnRetransmitMessage(object state)
{
SendAsyncResult thisPtr = (SendAsyncResult)state;
bool completeSelf = false;
Exception completionException = null;
try
{
completeSelf = thisPtr.ContinueRetransmission(RetransmitState.WaitCompleted);
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
completionException = e;
completeSelf = true;
}
if (completeSelf)
{
thisPtr.CompleteAndCleanup(false, completionException);
}
}
private bool BeginTransmitMessage()
{
this.currentSocket = 0;
return this.ContinueTransmitting(null);
}
private bool ContinueTransmitting(IAsyncResult socketAsyncResult)
{
while (this.currentSocket < this.sendSockets.Length)
{
if (socketAsyncResult == null)
{
socketAsyncResult = this.sendSockets[this.currentSocket].BeginSendTo(
this.messageData.Array,
this.messageData.Offset,
this.messageData.Count,
this.remoteEndpoint,
onSocketSendComplete,
this);
if (!socketAsyncResult.CompletedSynchronously)
{
return false;
}
}
this.sendSockets[this.currentSocket].EndSendTo(socketAsyncResult);
// check for timeout after calling socket.EndSendTo
// so that we don't leave the socket in a bad state/leak async results
this.ThrowIfTimedOut();
if (this.IsCanceled)
{
// don't send on the next socket and return true to cause Complete to be called.
return true;
}
this.currentSocket++;
socketAsyncResult = null;
}
return true;
}
private bool BeginRetransmission()
{
// BeginRetransmission should only be called in the case where transmission of the message
// completes synchronously.
return this.ContinueRetransmission(RetransmitState.TransmitCompleted);
}
private bool ContinueRetransmission(RetransmitState state)
{
this.ThrowIfTimedOut();
while (true)
{
switch (state)
{
case RetransmitState.TransmitCompleted:
if (!this.retransmitIterator.MoveNext())
{
// We are done retransmitting
return true;
}
if (this.retransmitIterator.CurrentDelay > 0)
{
this.retransmitTimer.Set(this.retransmitIterator.CurrentDelay);
return false;
}
state = RetransmitState.WaitCompleted;
break;
case RetransmitState.WaitCompleted:
if (this.IsCanceled)
{
this.channel.ThrowIfAborted();
return true;
}
// since we only invoke the encoder once just before the initial send of the message
// we need to handle logging the message in the retransmission case
if (MessageLogger.LogMessagesAtTransportLevel)
{
UdpOutputChannel.LogMessage(ref this.message, this.messageData);
}
// !completedSync
if (!this.BeginTransmitMessage())
{
return false;
}
state = RetransmitState.TransmitCompleted;
break;
default:
Fx.Assert("Unknown RetransmitState value encountered");
return true;
}
}
}
private void Initialize(Message message)
{
Exception exceptionToThrow;
this.sendSockets = this.channel.GetSendSockets(message, out this.remoteEndpoint, out exceptionToThrow);
if (exceptionToThrow != null)
{
throw FxTrace.Exception.AsError(exceptionToThrow);
}
this.IsMulticast = UdpUtility.IsMulticastAddress(this.remoteEndpoint.Address);
if (this.channel.ShouldRetransmitMessage(this.IsMulticast))
{
this.retransmissionEnabled = true;
this.channel.RetransmitStarting(this.message.Headers.MessageId, this);
this.retransmitTimer = new IOThreadTimer(onRetransmitMessage, this, false);
this.retransmitIterator = this.channel.CreateRetransmitIterator(this.IsMulticast);
}
this.messageData = this.channel.EncodeMessage(message);
}
private void ThrowIfTimedOut()
{
if (this.timeoutHelper.RemainingTime() <= TimeSpan.Zero)
{
throw FxTrace.Exception.AsError(new TimeoutException(SR.TimeoutOnOperation(this.timeoutHelper.OriginalTimeout)));
}
}
private void Cleanup()
{
if (this.retransmissionEnabled)
{
this.channel.RetransmitStopping(this.message.Headers.MessageId);
this.retransmitTimer.Cancel();
}
if (this.messageData.Array != null)
{
this.channel.BufferManager.ReturnBuffer(this.messageData.Array);
this.messageData = default(ArraySegment<byte>);
}
}
private void CompleteAndCleanup(bool completedSynchronously, Exception completionException)
{
this.Cleanup();
this.Complete(completedSynchronously, completionException);
}
}
// Control flow for async path
// We use this mechanism to avoid initializing two async objects as logically cleanup+close is one operation.
// At any point in the Begin* methods, we may go async. The steps are:
// - Cleanup channel
// - Close channel
private class CloseAsyncResult : AsyncResult
{
private static Action<object, TimeoutException> completeCleanupCallback = new Action<object, TimeoutException>(CompleteCleanup);
private UdpOutputChannel channel;
private TimeoutHelper timeoutHelper;
public CloseAsyncResult(UdpOutputChannel channel, TimeSpan timeout, AsyncCallback callback, object state)
: base(callback, state)
{
this.channel = channel;
this.timeoutHelper = new TimeoutHelper(timeout);
if (this.BeginCleanup())
{
this.Complete(true);
}
}
internal static void End(IAsyncResult result)
{
AsyncResult.End<CloseAsyncResult>(result);
}
private static void CompleteCleanup(object state, TimeoutException exception)
{
CloseAsyncResult thisPtr = (CloseAsyncResult)state;
Exception completionException = null;
if (exception != null)
{
Fx.Assert(exception.GetType() == typeof(TimeoutException), "Exception on callback should always be TimeoutException");
throw FxTrace.Exception.AsError(new TimeoutException(SR.TimeoutOnOperation(thisPtr.timeoutHelper.OriginalTimeout)));
}
try
{
lock (thisPtr.channel.ThisLock)
{
thisPtr.channel.retransmissionDoneWaitHandle = null;
// another thread could have called Abort while Close() was waiting for retransmission to complete.
if (!thisPtr.channel.cleanedUp)
{
// never aborting here
thisPtr.channel.CleanupAfterWait(false);
}
}
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
completionException = e;
}
thisPtr.Complete(false, completionException);
}
private bool BeginCleanup()
{
bool needToWait = false;
if (this.channel.cleanedUp)
{
return true;
}
lock (this.channel.ThisLock)
{
if (this.channel.cleanedUp)
{
return true;
}
// we're never aborting in this case...
if (this.channel.retransmitList != null && this.channel.retransmitList.Count > 0)
{
needToWait = true;
this.channel.retransmissionDoneWaitHandle = new AsyncWaitHandle(EventResetMode.ManualReset);
}
else
{
this.channel.CleanupAfterWait(false);
}
// we're guaranteed by CommunicationObject that at most ONE of Close or BeginClose will be called once.
// we don't null out retransmissionDoneEvent in the abort case; should be safe to use here.
return !needToWait || this.channel.retransmissionDoneWaitHandle.WaitAsync(completeCleanupCallback, this, this.timeoutHelper.RemainingTime());
}
}
}
}
}
|