UDPTrackerClient.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 {
  47. UDPT_CST_CONNECTING,
  48. UDPT_CST_CONNECTED
  49. };
  50. struct UDPTrackerConnection {
  51. int state;
  52. int64_t connectionId;
  53. Timer lastUpdated;
  54. UDPTrackerConnection()
  55. : state(UDPT_CST_CONNECTING),
  56. connectionId(UDPT_INITIAL_CONNECTION_ID),
  57. lastUpdated(0)
  58. {}
  59. UDPTrackerConnection(int state, int64_t connectionId,
  60. const Timer& lastUpdated)
  61. : state(state),
  62. connectionId(connectionId),
  63. lastUpdated(lastUpdated)
  64. {}
  65. };
  66. class UDPTrackerClient {
  67. public:
  68. UDPTrackerClient();
  69. ~UDPTrackerClient();
  70. int receiveReply
  71. (const unsigned char* data, size_t length, const std::string& remoteAddr,
  72. uint16_t remotePort, const Timer& now);
  73. // Creates data frame for the next pending request. This function
  74. // always processes first entry of pendingRequests_. If the data is
  75. // sent successfully, call requestSent(). Otherwise call
  76. // requestFail().
  77. ssize_t createRequest
  78. (unsigned char* data, size_t length, std::string& remoteAddr,
  79. uint16_t& remotePort, const Timer& now);
  80. // Tells this object that first entry of pendingRequests_ is
  81. // successfully sent.
  82. void requestSent(const Timer& now);
  83. // Tells this object that first entry of pendingRequests_ is not
  84. // successfully sent. The |error| should indicate error situation.
  85. void requestFail(int error);
  86. void addRequest(const std::shared_ptr<UDPTrackerRequest>& req);
  87. // Handles timeout for inflight requests.
  88. void handleTimeout(const Timer& now);
  89. const std::deque<std::shared_ptr<UDPTrackerRequest> >&
  90. getPendingRequests() const
  91. {
  92. return pendingRequests_;
  93. }
  94. const std::deque<std::shared_ptr<UDPTrackerRequest> >&
  95. getConnectRequests() const
  96. {
  97. return connectRequests_;
  98. }
  99. const std::deque<std::shared_ptr<UDPTrackerRequest> >&
  100. getInflightRequests() const
  101. {
  102. return inflightRequests_;
  103. }
  104. bool noRequest() const
  105. {
  106. return pendingRequests_.empty() && connectRequests_.empty() &&
  107. getInflightRequests().empty();
  108. }
  109. // Makes all contained requests fail.
  110. void failAll();
  111. int getNumWatchers() const
  112. {
  113. return numWatchers_;
  114. }
  115. void increaseWatchers();
  116. void decreaseWatchers();
  117. // Actually private function, but made public, to be used by unnamed
  118. // function.
  119. void failConnect(const std::string& remoteAddr, uint16_t remotePort,
  120. int error);
  121. private:
  122. std::shared_ptr<UDPTrackerRequest> findInflightRequest
  123. (const std::string& remoteAddr, uint16_t remotePort, int32_t transactionId,
  124. bool remove);
  125. UDPTrackerConnection* getConnectionId
  126. (const std::string& remoteAddr, uint16_t remotePort, const Timer& now);
  127. std::map<std::pair<std::string, uint16_t>,
  128. UDPTrackerConnection> connectionIdCache_;
  129. std::deque<std::shared_ptr<UDPTrackerRequest> > inflightRequests_;
  130. std::deque<std::shared_ptr<UDPTrackerRequest> > pendingRequests_;
  131. std::deque<std::shared_ptr<UDPTrackerRequest> > connectRequests_;
  132. int numWatchers_;
  133. };
  134. ssize_t createUDPTrackerConnect
  135. (unsigned char* data, size_t length, std::string& remoteAddr,
  136. uint16_t& remotePort, const std::shared_ptr<UDPTrackerRequest>& req);
  137. ssize_t createUDPTrackerAnnounce
  138. (unsigned char* data, size_t length, std::string& remoteAddr,
  139. uint16_t& remotePort, const std::shared_ptr<UDPTrackerRequest>& req);
  140. const char* getUDPTrackerActionStr(int action);
  141. const char* getUDPTrackerEventStr(int event);
  142. } // namespace aria2
  143. #endif // D_UDP_TRACKER_CLIENT_H