You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Congestion.h 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright 2009-2018 Pierre Ossman for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifndef __RFB_CONGESTION_H__
  19. #define __RFB_CONGESTION_H__
  20. #include <list>
  21. namespace rfb {
  22. class Congestion {
  23. public:
  24. Congestion();
  25. ~Congestion();
  26. // updatePosition() registers the current stream position and can
  27. // and should be called often.
  28. void updatePosition(unsigned pos);
  29. // sentPing() must be called when a marker is placed on the
  30. // outgoing stream. gotPong() must be called when the response for
  31. // such a marker is received.
  32. void sentPing();
  33. void gotPong();
  34. // isCongested() determines if the transport is currently congested
  35. // or if more data can be sent.
  36. bool isCongested();
  37. // getUncongestedETA() returns the number of milliseconds until the
  38. // transport is no longer congested. Returns 0 if there is no
  39. // congestion, and -1 if it is unknown when the transport will no
  40. // longer be congested.
  41. int getUncongestedETA();
  42. // getBandwidth() returns the current bandwidth estimation in bytes
  43. // per second.
  44. size_t getBandwidth();
  45. // debugTrace() writes the current congestion window, as well as the
  46. // congestion window of the underlying TCP layer, to the specified
  47. // file
  48. void debugTrace(const char* filename, int fd);
  49. protected:
  50. unsigned getExtraBuffer();
  51. unsigned getInFlight();
  52. void updateCongestion();
  53. private:
  54. unsigned lastPosition;
  55. unsigned extraBuffer;
  56. struct timeval lastUpdate;
  57. struct timeval lastSent;
  58. unsigned baseRTT;
  59. unsigned congWindow;
  60. bool inSlowStart;
  61. unsigned safeBaseRTT;
  62. struct RTTInfo {
  63. struct timeval tv;
  64. unsigned pos;
  65. unsigned extra;
  66. bool congested;
  67. };
  68. std::list<struct RTTInfo> pings;
  69. struct RTTInfo lastPong;
  70. struct timeval lastPongArrival;
  71. int measurements;
  72. struct timeval lastAdjustment;
  73. unsigned minRTT, minCongestedRTT;
  74. };
  75. }
  76. #endif