blob: 44c1ebff564ed825dd852bfc340b236cf261c43c [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
honghaiz6b9ab922016-01-05 09:06:12 -080024const int kAllRequests = 0;
25
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026// Manages a set of STUN requests, sending and resending until we receive a
27// response or determine that the request has timed out.
28class StunRequestManager {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000029 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030 StunRequestManager(rtc::Thread* thread);
31 ~StunRequestManager();
32
33 // Starts sending the given request (perhaps after a delay).
34 void Send(StunRequest* request);
35 void SendDelayed(StunRequest* request, int delay);
36
honghaiz6b9ab922016-01-05 09:06:12 -080037 // If |msg_type| is kAllRequests, sends all pending requests right away.
38 // Otherwise, sends those that have a matching type right away.
39 // Only for testing.
40 void Flush(int msg_type);
Honghai Zhang85975432015-11-12 11:07:12 -080041
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 // Removes a stun request that was added previously. This will happen
43 // automatically when a request succeeds, fails, or times out.
44 void Remove(StunRequest* request);
45
46 // Removes all stun requests that were added previously.
47 void Clear();
48
49 // Determines whether the given message is a response to one of the
50 // outstanding requests, and if so, processes it appropriately.
51 bool CheckResponse(StunMessage* msg);
52 bool CheckResponse(const char* data, size_t size);
53
54 bool empty() { return requests_.empty(); }
55
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000056 // Set the Origin header for outgoing stun messages.
57 void set_origin(const std::string& origin) { origin_ = origin; }
58
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 // Raised when there are bytes to be sent.
60 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
61
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000062 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 typedef std::map<std::string, StunRequest*> RequestMap;
64
65 rtc::Thread* thread_;
66 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000067 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068
69 friend class StunRequest;
70};
71
72// Represents an individual request to be sent. The STUN message can either be
73// constructed beforehand or built on demand.
74class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000075 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 StunRequest();
77 StunRequest(StunMessage* request);
78 virtual ~StunRequest();
79
80 // Causes our wrapped StunMessage to be Prepared
81 void Construct();
82
83 // The manager handling this request (if it has been scheduled for sending).
84 StunRequestManager* manager() { return manager_; }
85
86 // Returns the transaction ID of this request.
87 const std::string& id() { return msg_->transaction_id(); }
88
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000089 // the origin value
90 const std::string& origin() const { return origin_; }
91 void set_origin(const std::string& origin) { origin_ = origin; }
92
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 // Returns the STUN type of the request message.
94 int type();
95
96 // Returns a const pointer to |msg_|.
97 const StunMessage* msg() const;
98
99 // Time elapsed since last send (in ms)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 uint32_t Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000102 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 int count_;
104 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000105 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106
107 // Fills in a request object to be sent. Note that request's transaction ID
108 // will already be set and cannot be changed.
109 virtual void Prepare(StunMessage* request) {}
110
111 // Called when the message receives a response or times out.
112 virtual void OnResponse(StunMessage* response) {}
113 virtual void OnErrorResponse(StunMessage* response) {}
114 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700115 // Called when the message is sent.
116 virtual void OnSent();
117 // Returns the next delay for resends.
118 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000120 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121 void set_manager(StunRequestManager* manager);
122
123 // Handles messages for sending and timeout.
124 void OnMessage(rtc::Message* pmsg);
125
126 StunRequestManager* manager_;
127 StunMessage* msg_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128 uint32_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129
130 friend class StunRequestManager;
131};
132
133} // namespace cricket
134
135#endif // WEBRTC_P2P_BASE_STUNREQUEST_H_