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