UDPTrackerClient.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_UDP_TRACKER_CLIENT_H
  36. #define D_UDP_TRACKER_CLIENT_H
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <map>
  41. #include <memory>
  42. #include "TimerA2.h"
  43. namespace aria2 {
  44. #define UDPT_INITIAL_CONNECTION_ID 0x41727101980LL
  45. struct UDPTrackerRequest;
  46. enum UDPTrackerConnectionState { UDPT_CST_CONNECTING, UDPT_CST_CONNECTED };
  47. struct UDPTrackerConnection {
  48. int state;
  49. int64_t connectionId;
  50. Timer lastUpdated;
  51. UDPTrackerConnection()
  52. : state(UDPT_CST_CONNECTING),
  53. connectionId(UDPT_INITIAL_CONNECTION_ID),
  54. lastUpdated(Timer::zero())
  55. {
  56. }
  57. UDPTrackerConnection(int state, int64_t connectionId,
  58. const Timer& lastUpdated)
  59. : state(state), connectionId(connectionId), lastUpdated(lastUpdated)
  60. {
  61. }
  62. };
  63. class UDPTrackerClient {
  64. public:
  65. UDPTrackerClient();
  66. ~UDPTrackerClient();
  67. int receiveReply(std::shared_ptr<UDPTrackerRequest>& req,
  68. const unsigned char* data, size_t length,
  69. const std::string& remoteAddr, uint16_t remotePort,
  70. const Timer& now);
  71. // Creates data frame for the next pending request. This function
  72. // always processes first entry of pendingRequests_. If the data is
  73. // sent successfully, call requestSent(). Otherwise call
  74. // requestFail().
  75. ssize_t createRequest(unsigned char* data, size_t length,
  76. std::string& remoteAddr, uint16_t& remotePort,
  77. const Timer& now);
  78. // Tells this object that first entry of pendingRequests_ is
  79. // successfully sent.
  80. void requestSent(const Timer& now);
  81. // Tells this object that first entry of pendingRequests_ is not
  82. // successfully sent. The |error| should indicate error situation.
  83. void requestFail(int error);
  84. void addRequest(const std::shared_ptr<UDPTrackerRequest>& req);
  85. // Handles timeout for inflight requests.
  86. void handleTimeout(const Timer& now);
  87. const std::deque<std::shared_ptr<UDPTrackerRequest>>&
  88. getPendingRequests() const
  89. {
  90. return pendingRequests_;
  91. }
  92. const std::deque<std::shared_ptr<UDPTrackerRequest>>&
  93. getConnectRequests() const
  94. {
  95. return connectRequests_;
  96. }
  97. const std::deque<std::shared_ptr<UDPTrackerRequest>>&
  98. getInflightRequests() const
  99. {
  100. return inflightRequests_;
  101. }
  102. bool noRequest() const
  103. {
  104. return pendingRequests_.empty() && connectRequests_.empty() &&
  105. getInflightRequests().empty();
  106. }
  107. // Makes all contained requests fail.
  108. void failAll();
  109. int getNumWatchers() const { return numWatchers_; }
  110. void increaseWatchers();
  111. void decreaseWatchers();
  112. // Actually private function, but made public, to be used by unnamed
  113. // function.
  114. void failConnect(const std::string& remoteAddr, uint16_t remotePort,
  115. int error);
  116. private:
  117. std::shared_ptr<UDPTrackerRequest>
  118. findInflightRequest(const std::string& remoteAddr, uint16_t remotePort,
  119. uint32_t transactionId, bool remove);
  120. UDPTrackerConnection* getConnectionId(const std::string& remoteAddr,
  121. uint16_t remotePort, const Timer& now);
  122. std::map<std::pair<std::string, uint16_t>, UDPTrackerConnection>
  123. connectionIdCache_;
  124. std::deque<std::shared_ptr<UDPTrackerRequest>> inflightRequests_;
  125. std::deque<std::shared_ptr<UDPTrackerRequest>> pendingRequests_;
  126. std::deque<std::shared_ptr<UDPTrackerRequest>> connectRequests_;
  127. int numWatchers_;
  128. };
  129. ssize_t createUDPTrackerConnect(unsigned char* data, size_t length,
  130. std::string& remoteAddr, uint16_t& remotePort,
  131. const std::shared_ptr<UDPTrackerRequest>& req);
  132. ssize_t createUDPTrackerAnnounce(unsigned char* data, size_t length,
  133. std::string& remoteAddr, uint16_t& remotePort,
  134. const std::shared_ptr<UDPTrackerRequest>& req);
  135. const char* getUDPTrackerActionStr(int action);
  136. const char* getUDPTrackerEventStr(int event);
  137. } // namespace aria2
  138. #endif // D_UDP_TRACKER_CLIENT_H