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 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
|
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Diagnostics;
using System.Threading;
using System.Xml;
struct MessageAttemptInfo
{
readonly Message message;
readonly int retryCount;
readonly Int64 sequenceNumber;
readonly object state;
public MessageAttemptInfo(Message message, Int64 sequenceNumber, int retryCount, object state)
{
this.message = message;
this.sequenceNumber = sequenceNumber;
this.retryCount = retryCount;
this.state = state;
}
public Message Message
{
get { return this.message; }
}
public int RetryCount
{
get { return this.retryCount; }
}
public object State
{
get { return this.state; }
}
public Int64 GetSequenceNumber()
{
if (this.sequenceNumber <= 0)
{
throw Fx.AssertAndThrow("The caller is not allowed to get an invalid SequenceNumber.");
}
return this.sequenceNumber;
}
}
sealed class TransmissionStrategy
{
bool aborted;
bool closed;
int congestionControlModeAcks;
UniqueId id;
Int64 last = 0;
int lossWindowSize;
int maxWindowSize;
Int64 meanRtt;
ComponentExceptionHandler onException;
Int32 quotaRemaining;
ReliableMessagingVersion reliableMessagingVersion;
List<Int64> retransmissionWindow = new List<Int64>();
IOThreadTimer retryTimer;
RetryHandler retryTimeoutElapsedHandler;
bool requestAcks;
Int64 serrRtt;
int slowStartThreshold;
bool startup = true;
object thisLock = new object();
Int64 timeout;
Queue<IQueueAdder> waitQueue = new Queue<IQueueAdder>();
SlidingWindow window;
int windowSize = 1;
Int64 windowStart = 1;
public TransmissionStrategy(ReliableMessagingVersion reliableMessagingVersion, TimeSpan initRtt,
int maxWindowSize, bool requestAcks, UniqueId id)
{
if (initRtt < TimeSpan.Zero)
{
if (DiagnosticUtility.ShouldTrace(TraceEventType.Warning))
{
TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.WsrmNegativeElapsedTimeDetected,
SR.GetString(SR.TraceCodeWsrmNegativeElapsedTimeDetected), this);
}
initRtt = ReliableMessagingConstants.UnknownInitiationTime;
}
if (maxWindowSize <= 0)
{
throw Fx.AssertAndThrow("Argument maxWindow size must be positive.");
}
this.id = id;
this.maxWindowSize = this.lossWindowSize = maxWindowSize;
this.meanRtt = Math.Min((long)initRtt.TotalMilliseconds, Constants.MaxMeanRtt >> Constants.TimeMultiplier) << Constants.TimeMultiplier;
this.serrRtt = this.meanRtt >> 1;
this.window = new SlidingWindow(maxWindowSize);
this.slowStartThreshold = maxWindowSize;
this.timeout = Math.Max(((200 << Constants.TimeMultiplier) * 2) + this.meanRtt, this.meanRtt + (this.serrRtt << Constants.ChebychevFactor));
this.quotaRemaining = Int32.MaxValue;
this.retryTimer = new IOThreadTimer(new Action<object>(OnRetryElapsed), null, true);
this.requestAcks = requestAcks;
this.reliableMessagingVersion = reliableMessagingVersion;
}
public bool DoneTransmitting
{
get
{
return (this.last != 0 && this.windowStart == this.last + 1);
}
}
public bool HasPending
{
get
{
return (this.window.Count > 0 || this.waitQueue.Count > 0);
}
}
public Int64 Last
{
get
{
return this.last;
}
}
// now in 128ths of a millisecond.
static Int64 Now
{
get
{
return (Ticks.Now / TimeSpan.TicksPerMillisecond) << Constants.TimeMultiplier;
}
}
public ComponentExceptionHandler OnException
{
set
{
this.onException = value;
}
}
public RetryHandler RetryTimeoutElapsed
{
set
{
this.retryTimeoutElapsedHandler = value;
}
}
public int QuotaRemaining
{
get
{
return this.quotaRemaining;
}
}
object ThisLock
{
get
{
return this.thisLock;
}
}
public int Timeout
{
get
{
return (int)(this.timeout >> Constants.TimeMultiplier);
}
}
public void Abort(ChannelBase channel)
{
lock (this.ThisLock)
{
this.aborted = true;
if (this.closed)
return;
this.closed = true;
this.retryTimer.Cancel();
while (waitQueue.Count > 0)
waitQueue.Dequeue().Abort(channel);
window.Close();
}
}
public bool Add(Message message, TimeSpan timeout, object state, out MessageAttemptInfo attemptInfo)
{
return InternalAdd(message, false, timeout, state, out attemptInfo);
}
public MessageAttemptInfo AddLast(Message message, TimeSpan timeout, object state)
{
if (this.reliableMessagingVersion != ReliableMessagingVersion.WSReliableMessagingFebruary2005)
{
throw Fx.AssertAndThrow("Last message supported only in February 2005.");
}
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
InternalAdd(message, true, timeout, state, out attemptInfo);
return attemptInfo;
}
// Must call in a lock(this.ThisLock).
MessageAttemptInfo AddToWindow(Message message, bool isLast, object state)
{
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
Int64 sequenceNumber;
sequenceNumber = this.windowStart + this.window.Count;
WsrmUtilities.AddSequenceHeader(this.reliableMessagingVersion, message, this.id, sequenceNumber, isLast);
if (this.requestAcks && (this.window.Count == this.windowSize - 1 || this.quotaRemaining == 1)) // can't add any more
{
message.Properties.AllowOutputBatching = false;
WsrmUtilities.AddAckRequestedHeader(this.reliableMessagingVersion, message, this.id);
}
if (this.window.Count == 0)
{
this.retryTimer.Set(this.Timeout);
}
this.window.Add(message, Now, state);
this.quotaRemaining--;
if (isLast)
this.last = sequenceNumber;
int index = (int)(sequenceNumber - this.windowStart);
attemptInfo = new MessageAttemptInfo(this.window.GetMessage(index), sequenceNumber, 0, state);
return attemptInfo;
}
public IAsyncResult BeginAdd(Message message, TimeSpan timeout, object state, AsyncCallback callback, object asyncState)
{
return InternalBeginAdd(message, false, timeout, state, callback, asyncState);
}
public IAsyncResult BeginAddLast(Message message, TimeSpan timeout, object state, AsyncCallback callback, object asyncState)
{
if (this.reliableMessagingVersion != ReliableMessagingVersion.WSReliableMessagingFebruary2005)
{
throw Fx.AssertAndThrow("Last message supported only in February 2005.");
}
return InternalBeginAdd(message, true, timeout, state, callback, asyncState);
}
bool CanAdd()
{
return (this.window.Count < this.windowSize && // Does the message fit in the transmission window?
this.quotaRemaining > 0 && // Can the receiver handle another message?
this.waitQueue.Count == 0); // Don't get ahead of anyone in the wait queue.
}
public void Close()
{
lock (this.ThisLock)
{
if (this.closed)
return;
this.closed = true;
this.retryTimer.Cancel();
if (waitQueue.Count != 0)
{
throw Fx.AssertAndThrow("The reliable channel must throw prior to the call to Close() if there are outstanding send or request operations.");
}
window.Close();
}
}
public void DequeuePending()
{
Queue<IQueueAdder> adders = null;
lock (this.ThisLock)
{
if (this.closed || this.waitQueue.Count == 0)
return;
int count = Math.Min(this.windowSize, this.quotaRemaining) - this.window.Count;
if (count <= 0)
return;
count = Math.Min(count, this.waitQueue.Count);
adders = new Queue<IQueueAdder>(count);
while (count-- > 0)
{
IQueueAdder adder = waitQueue.Dequeue();
adder.Complete0();
adders.Enqueue(adder);
}
}
while (adders.Count > 0)
adders.Dequeue().Complete1();
}
public bool EndAdd(IAsyncResult result, out MessageAttemptInfo attemptInfo)
{
return InternalEndAdd(result, out attemptInfo);
}
public MessageAttemptInfo EndAddLast(IAsyncResult result)
{
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
InternalEndAdd(result, out attemptInfo);
return attemptInfo;
}
bool IsAddValid()
{
return (!this.aborted && !this.closed);
}
public void OnRetryElapsed(object state)
{
try
{
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
lock (this.ThisLock)
{
if (this.closed)
return;
if (this.window.Count == 0)
return;
this.window.RecordRetry(0, Now);
this.congestionControlModeAcks = 0;
this.slowStartThreshold = Math.Max(1, this.windowSize >> 1);
this.lossWindowSize = this.windowSize;
this.windowSize = 1;
this.timeout <<= 1;
this.startup = false;
attemptInfo = new MessageAttemptInfo(this.window.GetMessage(0), this.windowStart, this.window.GetRetryCount(0), this.window.GetState(0));
}
retryTimeoutElapsedHandler(attemptInfo);
lock (this.ThisLock)
{
if (!this.closed && (this.window.Count > 0))
{
this.retryTimer.Set(this.Timeout);
}
}
}
#pragma warning suppress 56500 // covered by FxCOP
catch (Exception e)
{
if (Fx.IsFatal(e))
throw;
this.onException(e);
}
}
public void Fault(ChannelBase channel)
{
lock (this.ThisLock)
{
if (this.closed)
return;
this.closed = true;
this.retryTimer.Cancel();
while (waitQueue.Count > 0)
waitQueue.Dequeue().Fault(channel);
window.Close();
}
}
public MessageAttemptInfo GetMessageInfoForRetry(bool remove)
{
lock (this.ThisLock)
{
// Closed, no need to retry.
if (this.closed)
{
return default(MessageAttemptInfo);
}
if (remove)
{
if (this.retransmissionWindow.Count == 0)
{
throw Fx.AssertAndThrow("The caller is not allowed to remove a message attempt when there are no message attempts.");
}
this.retransmissionWindow.RemoveAt(0);
}
while (this.retransmissionWindow.Count > 0)
{
Int64 next = this.retransmissionWindow[0];
if (next < this.windowStart)
{
// Already removed from the window, no need to retry.
this.retransmissionWindow.RemoveAt(0);
}
else
{
int index = (int)(next - this.windowStart);
if (this.window.GetTransferred(index))
this.retransmissionWindow.RemoveAt(0);
else
return new MessageAttemptInfo(this.window.GetMessage(index), next, this.window.GetRetryCount(index), this.window.GetState(index));
}
}
// Nothing left to retry.
return default(MessageAttemptInfo);
}
}
public bool SetLast()
{
if (this.reliableMessagingVersion != ReliableMessagingVersion.WSReliableMessaging11)
{
throw Fx.AssertAndThrow("SetLast supported only in 1.1.");
}
lock (this.ThisLock)
{
if (this.last != 0)
{
throw Fx.AssertAndThrow("Cannot set last more than once.");
}
this.last = this.windowStart + this.window.Count - 1;
return (this.last == 0) || this.DoneTransmitting;
}
}
bool InternalAdd(Message message, bool isLast, TimeSpan timeout, object state, out MessageAttemptInfo attemptInfo)
{
attemptInfo = default(MessageAttemptInfo);
WaitQueueAdder adder;
lock (this.ThisLock)
{
if (isLast && this.last != 0)
{
throw Fx.AssertAndThrow("Can't add more than one last message.");
}
if (!this.IsAddValid())
return false;
ThrowIfRollover();
if (CanAdd())
{
attemptInfo = AddToWindow(message, isLast, state);
return true;
}
adder = new WaitQueueAdder(this, message, isLast, state);
this.waitQueue.Enqueue(adder);
}
attemptInfo = adder.Wait(timeout);
return true;
}
IAsyncResult InternalBeginAdd(Message message, bool isLast, TimeSpan timeout, object state, AsyncCallback callback, object asyncState)
{
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
bool isAddValid;
lock (this.ThisLock)
{
if (isLast && this.last != 0)
{
throw Fx.AssertAndThrow("Can't add more than one last message.");
}
isAddValid = this.IsAddValid();
if (isAddValid)
{
ThrowIfRollover();
if (CanAdd())
{
attemptInfo = AddToWindow(message, isLast, state);
}
else
{
AsyncQueueAdder adder = new AsyncQueueAdder(message, isLast, timeout, state, this, callback, asyncState);
this.waitQueue.Enqueue(adder);
return adder;
}
}
}
return new CompletedAsyncResult<bool, MessageAttemptInfo>(isAddValid, attemptInfo, callback, asyncState);
}
bool InternalEndAdd(IAsyncResult result, out MessageAttemptInfo attemptInfo)
{
if (result is CompletedAsyncResult<bool, MessageAttemptInfo>)
{
return CompletedAsyncResult<bool, MessageAttemptInfo>.End(result, out attemptInfo);
}
else
{
attemptInfo = AsyncQueueAdder.End((AsyncQueueAdder)result);
return true;
}
}
public bool IsFinalAckConsistent(SequenceRangeCollection ranges)
{
lock (this.ThisLock)
{
if (this.closed)
{
return true;
}
// Nothing sent, ensure ack is empty.
if ((this.windowStart == 1) && (this.window.Count == 0))
{
return ranges.Count == 0;
}
// Ack is empty or first range is invalid.
if (ranges.Count == 0 || ranges[0].Lower != 1)
{
return false;
}
return ranges[0].Upper >= (this.windowStart - 1);
}
}
public void ProcessAcknowledgement(SequenceRangeCollection ranges, out bool invalidAck, out bool inconsistentAck)
{
invalidAck = false;
inconsistentAck = false;
bool newAck = false;
bool oldAck = false;
lock (this.ThisLock)
{
if (this.closed)
{
return;
}
Int64 lastMessageSent = this.windowStart + this.window.Count - 1;
Int64 lastMessageAcked = this.windowStart - 1;
int transferredInWindow = this.window.TransferredCount;
for (int i = 0; i < ranges.Count; i++)
{
SequenceRange range = ranges[i];
// Ack for a message not yet sent.
if (range.Upper > lastMessageSent)
{
invalidAck = true;
return;
}
if (((range.Lower > 1) && (range.Lower <= lastMessageAcked)) || (range.Upper < lastMessageAcked))
{
oldAck = true;
}
if (range.Upper >= this.windowStart)
{
if (range.Lower <= this.windowStart)
{
newAck = true;
}
if (!newAck)
{
int beginIndex = (int)(range.Lower - this.windowStart);
int endIndex = (int)((range.Upper > lastMessageSent) ? (this.window.Count - 1) : (range.Upper - this.windowStart));
newAck = this.window.GetTransferredInRangeCount(beginIndex, endIndex) < (endIndex - beginIndex + 1);
}
if (transferredInWindow > 0 && !oldAck)
{
int beginIndex = (int)((range.Lower < this.windowStart) ? 0 : (range.Lower - this.windowStart));
int endIndex = (int)((range.Upper > lastMessageSent) ? (this.window.Count - 1) : (range.Upper - this.windowStart));
transferredInWindow -= this.window.GetTransferredInRangeCount(beginIndex, endIndex);
}
}
}
if (transferredInWindow > 0)
oldAck = true;
}
inconsistentAck = oldAck && newAck;
}
// Called for RequestReply.
// Argument transferred is the request sequence number and it is assumed to be positive.
public bool ProcessTransferred(Int64 transferred, int quotaRemaining)
{
if (transferred <= 0)
{
throw Fx.AssertAndThrow("Argument transferred must be a valid sequence number.");
}
lock (this.ThisLock)
{
if (this.closed)
{
return false;
}
return ProcessTransferred(new SequenceRange(transferred), quotaRemaining);
}
}
// Called for Duplex and Output
public bool ProcessTransferred(SequenceRangeCollection ranges, int quotaRemaining)
{
if (ranges.Count == 0)
{
return false;
}
lock (this.ThisLock)
{
if (this.closed)
{
return false;
}
bool send = false;
for (int rangeIndex = 0; rangeIndex < ranges.Count; rangeIndex++)
{
if (this.ProcessTransferred(ranges[rangeIndex], quotaRemaining))
{
send = true;
}
}
return send;
}
}
// It is necessary that ProcessAcknowledgement be called prior, as
// this method does not check for valid ack ranges.
// This method returns true if the calling method should start sending retries
// obtained from GetMessageInfoForRetry.
bool ProcessTransferred(SequenceRange range, int quotaRemaining)
{
if (range.Upper < this.windowStart)
{
if (range.Upper == this.windowStart - 1 && (quotaRemaining != -1) && quotaRemaining > this.quotaRemaining)
this.quotaRemaining = quotaRemaining - Math.Min(this.windowSize, this.window.Count);
return false;
}
else if (range.Lower <= this.windowStart)
{
bool send = false;
this.retryTimer.Cancel();
Int64 slide = range.Upper - this.windowStart + 1;
// For Request Reply: Requests are transferred 1 at a time, (i.e. when the reply comes back).
// The TransmissionStrategy only removes messages if the window start is removed.
// Because of this, RequestReply messages transferred out of order will cause many, many retries.
// To avoid extraneous retries we mark each message transferred, and we remove our virtual slide.
if (slide == 1)
{
for (int i = 1; i < this.window.Count; i++)
{
if (this.window.GetTransferred(i))
{
slide++;
}
else
{
break;
}
}
}
Int64 now = Now;
Int64 oldWindowEnd = this.windowStart + this.windowSize;
for (int i = 0; i < (int)slide; i++)
UpdateStats(now, this.window.GetLastAttemptTime(i));
if (quotaRemaining != -1)
{
int inFlightAfterAck = Math.Min(this.windowSize, this.window.Count) - (int)slide;
this.quotaRemaining = quotaRemaining - Math.Max(0, inFlightAfterAck);
}
this.window.Remove((int)slide);
this.windowStart += slide;
int sendBeginIndex = 0;
if (this.windowSize <= this.slowStartThreshold)
{
this.windowSize = Math.Min(this.maxWindowSize, Math.Min(this.slowStartThreshold + 1, this.windowSize + (int)slide));
if (!startup)
sendBeginIndex = 0;
else
sendBeginIndex = Math.Max(0, (int)oldWindowEnd - (int)this.windowStart);
}
else
{
this.congestionControlModeAcks += (int)slide;
// EXPERIMENTAL, needs optimizing ///
int segmentSize = Math.Max(1, (this.lossWindowSize - this.slowStartThreshold) / 8);
int windowGrowthAckThreshold = ((this.windowSize - this.slowStartThreshold) * this.windowSize) / segmentSize;
if (this.congestionControlModeAcks > windowGrowthAckThreshold)
{
this.congestionControlModeAcks = 0;
this.windowSize = Math.Min(this.maxWindowSize, this.windowSize + 1);
}
sendBeginIndex = Math.Max(0, (int)oldWindowEnd - (int)this.windowStart);
}
int sendEndIndex = Math.Min(this.windowSize, this.window.Count);
if (sendBeginIndex < sendEndIndex)
{
send = (this.retransmissionWindow.Count == 0);
for (int i = sendBeginIndex; i < this.windowSize && i < this.window.Count; i++)
{
Int64 sequenceNumber = this.windowStart + i;
if (!this.window.GetTransferred(i) && !this.retransmissionWindow.Contains(sequenceNumber))
{
this.window.RecordRetry(i, Now);
retransmissionWindow.Add(sequenceNumber);
}
}
}
if (window.Count > 0)
{
this.retryTimer.Set(this.Timeout);
}
return send;
}
else
{
for (Int64 i = range.Lower; i <= range.Upper; i++)
{
this.window.SetTransferred((int)(i - this.windowStart));
}
}
return false;
}
bool RemoveAdder(IQueueAdder adder)
{
lock (this.ThisLock)
{
if (this.closed)
return false;
bool removed = false;
for (int i = 0; i < this.waitQueue.Count; i++)
{
IQueueAdder current = this.waitQueue.Dequeue();
if (Object.ReferenceEquals(adder, current))
removed = true;
else
this.waitQueue.Enqueue(current);
}
return removed;
}
}
void ThrowIfRollover()
{
if (this.windowStart + this.window.Count + this.waitQueue.Count == Int64.MaxValue)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageNumberRolloverFault(this.id).CreateException());
}
void UpdateStats(Int64 now, Int64 lastAttemptTime)
{
now = Math.Max(now, lastAttemptTime);
Int64 measuredRtt = now - lastAttemptTime;
Int64 error = measuredRtt - this.meanRtt;
this.serrRtt = Math.Min(this.serrRtt + ((Math.Abs(error) - this.serrRtt) >> Constants.Gain), Constants.MaxSerrRtt);
this.meanRtt = Math.Min(this.meanRtt + (error >> Constants.Gain), Constants.MaxMeanRtt);
this.timeout = Math.Max(((200 << Constants.TimeMultiplier) * 2) + this.meanRtt, this.meanRtt + (this.serrRtt << Constants.ChebychevFactor));
}
class AsyncQueueAdder : WaitAsyncResult, IQueueAdder
{
bool isLast;
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
TransmissionStrategy strategy;
public AsyncQueueAdder(Message message, bool isLast, TimeSpan timeout, object state, TransmissionStrategy strategy, AsyncCallback callback, object asyncState)
: base(timeout, true, callback, asyncState)
{
// MessageAttemptInfo(Message message, Int64 sequenceNumber, int retryCount, object state)
// this.attemptInfo is just a state bag, thus sequenceNumber can be 0 and should never be read.
this.attemptInfo = new MessageAttemptInfo(message, 0, 0, state);
this.isLast = isLast;
this.strategy = strategy;
base.Begin();
}
public void Abort(CommunicationObject communicationObject)
{
this.attemptInfo.Message.Close();
OnAborted(communicationObject);
}
public void Complete0()
{
this.attemptInfo = strategy.AddToWindow(this.attemptInfo.Message, this.isLast, this.attemptInfo.State);
}
public void Complete1()
{
OnSignaled();
}
public static MessageAttemptInfo End(AsyncQueueAdder result)
{
AsyncResult.End<AsyncQueueAdder>(result);
return result.attemptInfo;
}
public void Fault(CommunicationObject communicationObject)
{
this.attemptInfo.Message.Close();
OnFaulted(communicationObject);
}
protected override string GetTimeoutString(TimeSpan timeout)
{
return SR.GetString(SR.TimeoutOnAddToWindow, timeout);
}
protected override void OnTimerElapsed(object state)
{
if (this.strategy.RemoveAdder(this))
base.OnTimerElapsed(state);
}
}
static class Constants
{
// Used to adjust the timeout calculation, according to Chebychev's theorem,
// to fit ~98% of actual rtt's within our timeout.
public const int ChebychevFactor = 2;
// Gain of 0.125 (1/8). Shift right by 3 to apply the gain to a term.
public const int Gain = 3;
// 1ms == 128 of our time units. Shift left by 7 to perform the multiplication.
public const int TimeMultiplier = 7;
// These guarantee no overflows when calculating timeout.
public const long MaxMeanRtt = long.MaxValue / 3;
public const long MaxSerrRtt = MaxMeanRtt / 2;
}
interface IQueueAdder
{
void Abort(CommunicationObject communicationObject);
void Fault(CommunicationObject communicationObject);
void Complete0();
void Complete1();
}
class SlidingWindow
{
TransmissionInfo[] buffer;
int head = 0;
int tail = 0;
int maxSize;
public SlidingWindow(int maxSize)
{
this.maxSize = maxSize + 1;
this.buffer = new TransmissionInfo[this.maxSize];
}
public int Count
{
get
{
if (this.tail >= this.head)
return (this.tail - this.head);
else
return (this.tail - this.head + this.maxSize);
}
}
public int TransferredCount
{
get
{
if (this.Count == 0)
return 0;
else
return this.GetTransferredInRangeCount(0, this.Count - 1);
}
}
public void Add(Message message, Int64 addTime, object state)
{
if (this.Count >= (this.maxSize - 1))
{
throw Fx.AssertAndThrow("The caller is not allowed to add messages beyond the sliding window's maximum size.");
}
this.buffer[this.tail] = new TransmissionInfo(message, addTime, state);
this.tail = (this.tail + 1) % this.maxSize;
}
void AssertIndex(int index)
{
if (index >= Count)
{
throw Fx.AssertAndThrow("Argument index must be less than Count.");
}
if (index < 0)
{
throw Fx.AssertAndThrow("Argument index must be positive.");
}
}
public void Close()
{
this.Remove(Count);
}
public Int64 GetLastAttemptTime(int index)
{
this.AssertIndex(index);
return this.buffer[(head + index) % this.maxSize].LastAttemptTime;
}
public Message GetMessage(int index)
{
this.AssertIndex(index);
if (!this.buffer[(head + index) % this.maxSize].Transferred)
return this.buffer[(head + index) % this.maxSize].Buffer.CreateMessage();
else
return null;
}
public int GetRetryCount(int index)
{
this.AssertIndex(index);
return this.buffer[(this.head + index) % this.maxSize].RetryCount;
}
public object GetState(int index)
{
this.AssertIndex(index);
return this.buffer[(this.head + index) % this.maxSize].State;
}
public bool GetTransferred(int index)
{
this.AssertIndex(index);
return this.buffer[(this.head + index) % this.maxSize].Transferred;
}
public int GetTransferredInRangeCount(int beginIndex, int endIndex)
{
if (beginIndex < 0)
{
throw Fx.AssertAndThrow("Argument beginIndex cannot be negative.");
}
if (endIndex >= this.Count)
{
throw Fx.AssertAndThrow("Argument endIndex cannot be greater than Count.");
}
if (endIndex < beginIndex)
{
throw Fx.AssertAndThrow("Argument endIndex cannot be less than argument beginIndex.");
}
int result = 0;
for (int index = beginIndex; index <= endIndex; index++)
{
if (this.buffer[(head + index) % this.maxSize].Transferred)
result++;
}
return result;
}
public int RecordRetry(int index, Int64 retryTime)
{
this.AssertIndex(index);
this.buffer[(head + index) % this.maxSize].LastAttemptTime = retryTime;
return ++this.buffer[(head + index) % this.maxSize].RetryCount;
}
public void Remove(int count)
{
if (count > this.Count)
{
Fx.Assert("Cannot remove more messages than the window's Count.");
}
while (count-- > 0)
{
this.buffer[head].Buffer.Close();
this.buffer[head].Buffer = null;
this.head = (this.head + 1) % this.maxSize;
}
}
public void SetTransferred(int index)
{
this.AssertIndex(index);
this.buffer[(head + index) % this.maxSize].Transferred = true;
}
struct TransmissionInfo
{
internal MessageBuffer Buffer;
internal Int64 LastAttemptTime;
internal int RetryCount;
internal object State;
internal bool Transferred;
public TransmissionInfo(Message message, Int64 lastAttemptTime, object state)
{
this.Buffer = message.CreateBufferedCopy(int.MaxValue);
this.LastAttemptTime = lastAttemptTime;
this.RetryCount = 0;
this.State = state;
this.Transferred = false;
}
}
}
class WaitQueueAdder : IQueueAdder
{
ManualResetEvent completeEvent = new ManualResetEvent(false);
Exception exception;
bool isLast;
MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);
TransmissionStrategy strategy;
public WaitQueueAdder(TransmissionStrategy strategy, Message message, bool isLast, object state)
{
this.strategy = strategy;
this.isLast = isLast;
this.attemptInfo = new MessageAttemptInfo(message, 0, 0, state);
}
public void Abort(CommunicationObject communicationObject)
{
this.exception = communicationObject.CreateClosedException();
completeEvent.Set();
}
public void Complete0()
{
attemptInfo = this.strategy.AddToWindow(this.attemptInfo.Message, this.isLast, this.attemptInfo.State);
this.completeEvent.Set();
}
public void Complete1()
{
}
public void Fault(CommunicationObject communicationObject)
{
this.exception = communicationObject.GetTerminalException();
completeEvent.Set();
}
public MessageAttemptInfo Wait(TimeSpan timeout)
{
if (!TimeoutHelper.WaitOne(this.completeEvent, timeout))
{
if (this.strategy.RemoveAdder(this) && this.exception == null)
this.exception = new TimeoutException(SR.GetString(SR.TimeoutOnAddToWindow, timeout));
}
if (this.exception != null)
{
this.attemptInfo.Message.Close();
this.completeEvent.Close();
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.exception);
}
// This is safe because, Abort, Complete0, Fault, and RemoveAdder all occur under
// the TransmissionStrategy's lock and RemoveAdder ensures that the
// TransmissionStrategy will never call into this object again.
this.completeEvent.Close();
return this.attemptInfo;
}
}
}
}
|