SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CCRakNetUDT.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "defines.h"
17
18 #if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1
19
20 #ifndef __CONGESTION_CONTROL_UDT_H
21 #define __CONGESTION_CONTROL_UDT_H
22
23 #include "NativeTypes.h"
24 #include "time.h"
25 #include "types.h"
26 #include "DS_Queue.h"
27
29 #define CC_TIME_TYPE_BYTES 8
30
31 namespace SLNet
32 {
33
34 #if CC_TIME_TYPE_BYTES==8
35 typedef uint64_t CCTimeType;
36 #else
37 typedef uint32_t CCTimeType;
38 #endif
39
40 typedef uint24_t DatagramSequenceNumberType;
41 typedef double BytesPerMicrosecond;
42 typedef double BytesPerSecond;
43 typedef double MicrosecondsPerByte;
44
46 #define CC_RAKNET_UDT_PACKET_HISTORY_LENGTH 64
47 #define RTT_HISTORY_LENGTH 64
48
50 #define UDP_HEADER_SIZE 28
51
52 #define CC_DEBUG_PRINTF_1(x)
53 #define CC_DEBUG_PRINTF_2(x,y)
54 #define CC_DEBUG_PRINTF_3(x,y,z)
55 #define CC_DEBUG_PRINTF_4(x,y,z,a)
56 #define CC_DEBUG_PRINTF_5(x,y,z,a,b)
57 //#define CC_DEBUG_PRINTF_1(x) printf(x)
58 //#define CC_DEBUG_PRINTF_2(x,y) printf(x,y)
59 //#define CC_DEBUG_PRINTF_3(x,y,z) printf(x,y,z)
60 //#define CC_DEBUG_PRINTF_4(x,y,z,a) printf(x,y,z,a)
61 //#define CC_DEBUG_PRINTF_5(x,y,z,a,b) printf(x,y,z,a,b)
62
91 class CCRakNetUDT
92 {
93 public:
94
95 CCRakNetUDT();
96 ~CCRakNetUDT();
97
99 void Init(CCTimeType curTime, uint32_t maxDatagramPayload);
100
102 void Update(CCTimeType curTime, bool hasDataToSendOrResend);
103
104 int GetRetransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend);
105 int GetTransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend);
106
111 bool ShouldSendACKs(CCTimeType curTime, CCTimeType estimatedTimeToNextTick);
112
115 DatagramSequenceNumberType GetAndIncrementNextDatagramSequenceNumber(void);
116 DatagramSequenceNumberType GetNextDatagramSequenceNumber(void);
117
123 void OnSendBytes(CCTimeType curTime, uint32_t numBytes);
124
126 void OnGotPacketPair(DatagramSequenceNumberType datagramSequenceNumber, uint32_t sizeInBytes, CCTimeType curTime);
127
131 bool OnGotPacket(DatagramSequenceNumberType datagramSequenceNumber, bool isContinuousSend, CCTimeType curTime, uint32_t sizeInBytes, uint32_t *skippedMessageCount);
132
135 void OnResend(CCTimeType curTime, SLNet::TimeUS nextActionTime);
136 void OnNAK(CCTimeType curTime, DatagramSequenceNumberType nakSequenceNumber);
137
142 void OnAck(CCTimeType curTime, CCTimeType rtt, bool hasBAndAS, BytesPerMicrosecond _B, BytesPerMicrosecond _AS, double totalUserDataBytesAcked, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber );
143 void OnDuplicateAck( CCTimeType curTime, DatagramSequenceNumberType sequenceNumber ) {}
144
147 void OnSendAckGetBAndAS(CCTimeType curTime, bool *hasBAndAS, BytesPerMicrosecond *_B, BytesPerMicrosecond *_AS);
148
153 void OnSendAck(CCTimeType curTime, uint32_t numBytes);
154
157 void OnSendNACK(CCTimeType curTime, uint32_t numBytes);
158
165 CCTimeType GetRTOForRetransmission(unsigned char timesSent) const;
166
169 void SetMTU(uint32_t bytes);
170
172 uint32_t GetMTU(void) const;
173
175 BytesPerMicrosecond GetLocalSendRate(void) const {return 1.0 / SND;}
176 BytesPerMicrosecond GetLocalReceiveRate(CCTimeType currentTime) const;
177 BytesPerMicrosecond GetRemoveReceiveRate(void) const {return AS;}
178 //BytesPerMicrosecond GetEstimatedBandwidth(void) const {return B;}
179 BytesPerMicrosecond GetEstimatedBandwidth(void) const {return GetLinkCapacityBytesPerSecond()*1000000.0;}
180 double GetLinkCapacityBytesPerSecond(void) const {return estimatedLinkCapacityBytesPerSecond;};
181
183 double GetRTT(void) const;
184
185 bool GetIsInSlowStart(void) const {return isInSlowStart;}
186 uint32_t GetCWNDLimit(void) const {return (uint32_t) (CWND*MAXIMUM_MTU_INCLUDING_UDP_HEADER);}
187
188
190 static bool GreaterThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b);
192 static bool LessThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b);
193 // void SetTimeBetweenSendsLimit(unsigned int bitsPerSecond);
194 uint64_t GetBytesPerSecondLimitByCongestionControl(void) const;
195
196 protected:
197 // --------------------------- PROTECTED VARIABLES ---------------------------
203 MicrosecondsPerByte SND;
204
210 double CWND;
211
215 CCTimeType nextSYNUpdate;
216
217
220 int packetPairRecieptHistoryWriteIndex;
221
224 //BytesPerMicrosecond B;
225
231 double RTT;
232
237 // double RTTVar;
239 double minRTT, maxRTT;
240
247 BytesPerMicrosecond packetArrivalHistory[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH];
248 BytesPerMicrosecond packetArrivalHistoryContinuousGaps[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH];
249 unsigned char packetArrivalHistoryContinuousGapsIndex;
250 uint64_t continuousBytesReceived;
251 CCTimeType continuousBytesReceivedStartTime;
252 unsigned int packetArrivalHistoryWriteCount;
253
256 int packetArrivalHistoryWriteIndex;
257
259 CCTimeType lastPacketArrivalTime;
260
263 BytesPerMicrosecond AS;
264
267 CCTimeType lastTransmitOfBAndAS;
268
273 bool isInSlowStart;
274
277 uint32_t NAKCount;
278
282 uint32_t AvgNAKNum;
283
285 uint32_t DecCount;
286
288 uint32_t DecInterval;
289
291 DatagramSequenceNumberType nextDatagramSequenceNumber;
292
295 CCTimeType lastPacketPairPacketArrivalTime;
296
301 DatagramSequenceNumberType lastPacketPairSequenceNumber;
302
305 CCTimeType lastUpdateWindowSizeAndAck;
306
310 double ExpCount;
311
314 uint64_t totalUserDataBytesSent;
315
318 CCTimeType oldestUnsentAck;
319
320 // Maximum amount of bytes that the user can send, e.g. the size of one full datagram
321 uint32_t MAXIMUM_MTU_INCLUDING_UDP_HEADER;
322
323 // Max window size
324 double CWND_MAX_THRESHOLD;
325
328 DatagramSequenceNumberType expectedNextSequenceNumber;
329
330 // How many times have we sent B and AS? Used to force it to send at least CC_RAKNET_UDT_PACKET_HISTORY_LENGTH times
331 // Otherwise, the default values in the array generate inaccuracy
332 uint32_t sendBAndASCount;
333
336 BytesPerMicrosecond mostRecentPacketArrivalHistory;
337
338 bool hasWrittenToPacketPairReceiptHistory;
339
340 // uint32_t rttHistory[RTT_HISTORY_LENGTH];
341 // uint32_t rttHistoryIndex;
342 // uint32_t rttHistoryWriteCount;
343 // uint32_t rttSum, rttLow;
344 // CCTimeType lastSndUpdateTime;
345 double estimatedLinkCapacityBytesPerSecond;
346
347 // --------------------------- PROTECTED METHODS ---------------------------
349 void SetNextSYNUpdate(CCTimeType currentTime);
350
352 BytesPerMicrosecond ReceiverCalculateDataArrivalRate(CCTimeType curTime) const;
354 BytesPerMicrosecond ReceiverCalculateDataArrivalRateMedian(void) const;
355
357 static BytesPerMicrosecond CalculateListMedianRecursive(const BytesPerMicrosecond inputList[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH], int inputListLength, int lessThanSum, int greaterThanSum);
358 // static uint32_t CalculateListMedianRecursive(const uint32_t inputList[RTT_HISTORY_LENGTH], int inputListLength, int lessThanSum, int greaterThanSum);
359
362 CCTimeType GetSenderRTOForACK(void) const;
363
365 void EndSlowStart(void);
366
368 inline double BytesPerMicrosecondToPacketsPerMillisecond(BytesPerMicrosecond in);
369
371 //void UpdateRTT(CCTimeType rtt);
372
374 void UpdateWindowSizeAndAckOnAckPreSlowStart(double totalUserDataBytesAcked);
375
377 void UpdateWindowSizeAndAckOnAckPerSyn(CCTimeType curTime, CCTimeType rtt, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber);
378
379
381 void ResetOnDataArrivalHalveSNDOnNoDataTime(CCTimeType curTime);
382
383 // Init array
384 void InitPacketArrivalHistory(void);
385
386 // Printf
387 void PrintLowBandwidthWarning(void);
388
389 // Bug: SND can sometimes get super high - have seen 11693
390 void CapMinSnd(const char *file, int line);
391
392 void DecreaseTimeBetweenSends(void);
393 void IncreaseTimeBetweenSends(void);
394
395 int bytesCanSendThisTick;
396
397 CCTimeType lastRttOnIncreaseSendRate;
398 CCTimeType lastRtt;
399
400 DatagramSequenceNumberType nextCongestionControlBlock;
401 bool hadPacketlossThisBlock;
402 DataStructures::Queue<CCTimeType> pingsLastInterval;
403 };
404
405 }
406
407 #endif
408
409 #endif