stun_request.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef P2P_BASE_STUN_REQUEST_H_
  11. #define P2P_BASE_STUN_REQUEST_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <map>
  15. #include <string>
  16. #include "api/transport/stun.h"
  17. #include "rtc_base/message_handler.h"
  18. #include "rtc_base/third_party/sigslot/sigslot.h"
  19. #include "rtc_base/thread.h"
  20. namespace cricket {
  21. class StunRequest;
  22. const int kAllRequests = 0;
  23. // Total max timeouts: 39.75 seconds
  24. // For years, this was 9.5 seconds, but for networks that experience moments of
  25. // high RTT (such as 40s on 2G networks), this doesn't work well.
  26. const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds
  27. // Manages a set of STUN requests, sending and resending until we receive a
  28. // response or determine that the request has timed out.
  29. class StunRequestManager {
  30. public:
  31. explicit StunRequestManager(rtc::Thread* thread);
  32. ~StunRequestManager();
  33. // Starts sending the given request (perhaps after a delay).
  34. void Send(StunRequest* request);
  35. void SendDelayed(StunRequest* request, int delay);
  36. // If |msg_type| is kAllRequests, sends all pending requests right away.
  37. // Otherwise, sends those that have a matching type right away.
  38. // Only for testing.
  39. void Flush(int msg_type);
  40. // Returns true if at least one request with |msg_type| is scheduled for
  41. // transmission. For testing only.
  42. bool HasRequest(int msg_type);
  43. // Removes a stun request that was added previously. This will happen
  44. // automatically when a request succeeds, fails, or times out.
  45. void Remove(StunRequest* request);
  46. // Removes all stun requests that were added previously.
  47. void Clear();
  48. // Determines whether the given message is a response to one of the
  49. // outstanding requests, and if so, processes it appropriately.
  50. bool CheckResponse(StunMessage* msg);
  51. bool CheckResponse(const char* data, size_t size);
  52. bool empty() { return requests_.empty(); }
  53. // Set the Origin header for outgoing stun messages.
  54. void set_origin(const std::string& origin) { origin_ = origin; }
  55. // Raised when there are bytes to be sent.
  56. sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
  57. private:
  58. typedef std::map<std::string, StunRequest*> RequestMap;
  59. rtc::Thread* const thread_;
  60. RequestMap requests_;
  61. std::string origin_;
  62. friend class StunRequest;
  63. };
  64. // Represents an individual request to be sent. The STUN message can either be
  65. // constructed beforehand or built on demand.
  66. class StunRequest : public rtc::MessageHandler {
  67. public:
  68. StunRequest();
  69. explicit StunRequest(StunMessage* request);
  70. ~StunRequest() override;
  71. // Causes our wrapped StunMessage to be Prepared
  72. void Construct();
  73. // The manager handling this request (if it has been scheduled for sending).
  74. StunRequestManager* manager() { return manager_; }
  75. // Returns the transaction ID of this request.
  76. const std::string& id() { return msg_->transaction_id(); }
  77. // Returns the reduced transaction ID of this request.
  78. uint32_t reduced_transaction_id() const {
  79. return msg_->reduced_transaction_id();
  80. }
  81. // the origin value
  82. const std::string& origin() const { return origin_; }
  83. void set_origin(const std::string& origin) { origin_ = origin; }
  84. // Returns the STUN type of the request message.
  85. int type();
  86. // Returns a const pointer to |msg_|.
  87. const StunMessage* msg() const;
  88. // Returns a mutable pointer to |msg_|.
  89. StunMessage* mutable_msg();
  90. // Time elapsed since last send (in ms)
  91. int Elapsed() const;
  92. protected:
  93. int count_;
  94. bool timeout_;
  95. std::string origin_;
  96. // Fills in a request object to be sent. Note that request's transaction ID
  97. // will already be set and cannot be changed.
  98. virtual void Prepare(StunMessage* request) {}
  99. // Called when the message receives a response or times out.
  100. virtual void OnResponse(StunMessage* response) {}
  101. virtual void OnErrorResponse(StunMessage* response) {}
  102. virtual void OnTimeout() {}
  103. // Called when the message is sent.
  104. virtual void OnSent();
  105. // Returns the next delay for resends.
  106. virtual int resend_delay();
  107. private:
  108. void set_manager(StunRequestManager* manager);
  109. // Handles messages for sending and timeout.
  110. void OnMessage(rtc::Message* pmsg) override;
  111. StunRequestManager* manager_;
  112. StunMessage* msg_;
  113. int64_t tstamp_;
  114. friend class StunRequestManager;
  115. };
  116. } // namespace cricket
  117. #endif // P2P_BASE_STUN_REQUEST_H_