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.

130 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_P2P_BASE_STUNREQUEST_H_
  28. #define TALK_P2P_BASE_STUNREQUEST_H_
  29. #include "talk/base/sigslot.h"
  30. #include "talk/base/thread.h"
  31. #include "talk/p2p/base/stun.h"
  32. #include <map>
  33. #include <string>
  34. namespace cricket {
  35. class StunRequest;
  36. // Manages a set of STUN requests, sending and resending until we receive a
  37. // response or determine that the request has timed out.
  38. class StunRequestManager {
  39. public:
  40. StunRequestManager(talk_base::Thread* thread);
  41. ~StunRequestManager();
  42. // Starts sending the given request (perhaps after a delay).
  43. void Send(StunRequest* request);
  44. void SendDelayed(StunRequest* request, int delay);
  45. // Removes a stun request that was added previously. This will happen
  46. // automatically when a request succeeds, fails, or times out.
  47. void Remove(StunRequest* request);
  48. // Removes all stun requests that were added previously.
  49. void Clear();
  50. // Determines whether the given message is a response to one of the
  51. // outstanding requests, and if so, processes it appropriately.
  52. bool CheckResponse(StunMessage* msg);
  53. bool CheckResponse(const char* data, size_t size);
  54. bool empty() { return requests_.empty(); }
  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. talk_base::Thread* thread_;
  60. RequestMap requests_;
  61. friend class StunRequest;
  62. };
  63. // Represents an individual request to be sent. The STUN message can either be
  64. // constructed beforehand or built on demand.
  65. class StunRequest : public talk_base::MessageHandler {
  66. public:
  67. StunRequest();
  68. StunRequest(StunMessage* request);
  69. virtual ~StunRequest();
  70. // Causes our wrapped StunMessage to be Prepared
  71. void Construct();
  72. // The manager handling this request (if it has been scheduled for sending).
  73. StunRequestManager* manager() { return manager_; }
  74. // Returns the transaction ID of this request.
  75. const std::string& id() { return msg_->transaction_id(); }
  76. // Returns the STUN type of the request message.
  77. int type();
  78. // Handles messages for sending and timeout.
  79. void OnMessage(talk_base::Message* pmsg);
  80. // Time elapsed since last send (in ms)
  81. uint32 Elapsed() const;
  82. protected:
  83. int count_;
  84. bool timeout_;
  85. // Fills in a request object to be sent. Note that request's transaction ID
  86. // will already be set and cannot be changed.
  87. virtual void Prepare(StunMessage* request) {}
  88. // Called when the message receives a response or times out.
  89. virtual void OnResponse(StunMessage* response) {}
  90. virtual void OnErrorResponse(StunMessage* response) {}
  91. virtual void OnTimeout() {}
  92. virtual int GetNextDelay();
  93. private:
  94. StunRequestManager* manager_;
  95. StunMessage* msg_;
  96. uint32 tstamp_;
  97. void set_manager(StunRequestManager* manager);
  98. friend class StunRequestManager;
  99. };
  100. } // namespace cricket
  101. #endif // TALK_P2P_BASE_STUNREQUEST_H_