blob: 5fefc2ff9c1cf497567ddf037c9f1ffed5d8d9fd [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
20namespace cricket {
21
22class 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.
26class StunRequestManager {
27public:
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
49 // Raised when there are bytes to be sent.
50 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
51
52private:
53 typedef std::map<std::string, StunRequest*> RequestMap;
54
55 rtc::Thread* thread_;
56 RequestMap requests_;
57
58 friend class StunRequest;
59};
60
61// Represents an individual request to be sent. The STUN message can either be
62// constructed beforehand or built on demand.
63class StunRequest : public rtc::MessageHandler {
64public:
65 StunRequest();
66 StunRequest(StunMessage* request);
67 virtual ~StunRequest();
68
69 // Causes our wrapped StunMessage to be Prepared
70 void Construct();
71
72 // The manager handling this request (if it has been scheduled for sending).
73 StunRequestManager* manager() { return manager_; }
74
75 // Returns the transaction ID of this request.
76 const std::string& id() { return msg_->transaction_id(); }
77
78 // Returns the STUN type of the request message.
79 int type();
80
81 // Returns a const pointer to |msg_|.
82 const StunMessage* msg() const;
83
84 // Time elapsed since last send (in ms)
85 uint32 Elapsed() const;
86
87protected:
88 int count_;
89 bool timeout_;
90
91 // Fills in a request object to be sent. Note that request's transaction ID
92 // will already be set and cannot be changed.
93 virtual void Prepare(StunMessage* request) {}
94
95 // Called when the message receives a response or times out.
96 virtual void OnResponse(StunMessage* response) {}
97 virtual void OnErrorResponse(StunMessage* response) {}
98 virtual void OnTimeout() {}
99 virtual int GetNextDelay();
100
101private:
102 void set_manager(StunRequestManager* manager);
103
104 // Handles messages for sending and timeout.
105 void OnMessage(rtc::Message* pmsg);
106
107 StunRequestManager* manager_;
108 StunMessage* msg_;
109 uint32 tstamp_;
110
111 friend class StunRequestManager;
112};
113
114} // namespace cricket
115
116#endif // WEBRTC_P2P_BASE_STUNREQUEST_H_