henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
| 11 | #ifndef WEBRTC_P2P_BASE_STUNREQUEST_H_ |
| 12 | #define WEBRTC_P2P_BASE_STUNREQUEST_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <string> |
| 16 | #include "webrtc/p2p/base/stun.h" |
| 17 | #include "webrtc/base/sigslot.h" |
| 18 | #include "webrtc/base/thread.h" |
| 19 | |
| 20 | namespace cricket { |
| 21 | |
| 22 | class StunRequest; |
| 23 | |
| 24 | // Manages a set of STUN requests, sending and resending until we receive a |
| 25 | // response or determine that the request has timed out. |
| 26 | class StunRequestManager { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 27 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 28 | StunRequestManager(rtc::Thread* thread); |
| 29 | ~StunRequestManager(); |
| 30 | |
| 31 | // Starts sending the given request (perhaps after a delay). |
| 32 | void Send(StunRequest* request); |
| 33 | void SendDelayed(StunRequest* request, int delay); |
| 34 | |
| 35 | // Removes a stun request that was added previously. This will happen |
| 36 | // automatically when a request succeeds, fails, or times out. |
| 37 | void Remove(StunRequest* request); |
| 38 | |
| 39 | // Removes all stun requests that were added previously. |
| 40 | void Clear(); |
| 41 | |
| 42 | // Determines whether the given message is a response to one of the |
| 43 | // outstanding requests, and if so, processes it appropriately. |
| 44 | bool CheckResponse(StunMessage* msg); |
| 45 | bool CheckResponse(const char* data, size_t size); |
| 46 | |
| 47 | bool empty() { return requests_.empty(); } |
| 48 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 49 | // Set the Origin header for outgoing stun messages. |
| 50 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 51 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 52 | // Raised when there are bytes to be sent. |
| 53 | sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket; |
| 54 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 55 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 56 | typedef std::map<std::string, StunRequest*> RequestMap; |
| 57 | |
| 58 | rtc::Thread* thread_; |
| 59 | RequestMap requests_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 60 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | |
| 62 | friend class StunRequest; |
| 63 | }; |
| 64 | |
| 65 | // Represents an individual request to be sent. The STUN message can either be |
| 66 | // constructed beforehand or built on demand. |
| 67 | class StunRequest : public rtc::MessageHandler { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 68 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | StunRequest(); |
| 70 | StunRequest(StunMessage* request); |
| 71 | virtual ~StunRequest(); |
| 72 | |
| 73 | // Causes our wrapped StunMessage to be Prepared |
| 74 | void Construct(); |
| 75 | |
| 76 | // The manager handling this request (if it has been scheduled for sending). |
| 77 | StunRequestManager* manager() { return manager_; } |
| 78 | |
| 79 | // Returns the transaction ID of this request. |
| 80 | const std::string& id() { return msg_->transaction_id(); } |
| 81 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 82 | // the origin value |
| 83 | const std::string& origin() const { return origin_; } |
| 84 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 85 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 86 | // Returns the STUN type of the request message. |
| 87 | int type(); |
| 88 | |
| 89 | // Returns a const pointer to |msg_|. |
| 90 | const StunMessage* msg() const; |
| 91 | |
| 92 | // Time elapsed since last send (in ms) |
| 93 | uint32 Elapsed() const; |
| 94 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 95 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 96 | int count_; |
| 97 | bool timeout_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 98 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | |
| 100 | // Fills in a request object to be sent. Note that request's transaction ID |
| 101 | // will already be set and cannot be changed. |
| 102 | virtual void Prepare(StunMessage* request) {} |
| 103 | |
| 104 | // Called when the message receives a response or times out. |
| 105 | virtual void OnResponse(StunMessage* response) {} |
| 106 | virtual void OnErrorResponse(StunMessage* response) {} |
| 107 | virtual void OnTimeout() {} |
| 108 | virtual int GetNextDelay(); |
| 109 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 110 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | void set_manager(StunRequestManager* manager); |
| 112 | |
| 113 | // Handles messages for sending and timeout. |
| 114 | void OnMessage(rtc::Message* pmsg); |
| 115 | |
| 116 | StunRequestManager* manager_; |
| 117 | StunMessage* msg_; |
| 118 | uint32 tstamp_; |
| 119 | |
| 120 | friend class StunRequestManager; |
| 121 | }; |
| 122 | |
| 123 | } // namespace cricket |
| 124 | |
| 125 | #endif // WEBRTC_P2P_BASE_STUNREQUEST_H_ |