blob: f6d216d1f714eca780f8d9b3574f616509bbc482 [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
honghaize2af9ef2016-03-03 08:27:47 -080042 // Returns true if at least one request with |msg_type| is scheduled for
43 // transmission. For testing only.
44 bool HasRequest(int msg_type);
45
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046 // Removes a stun request that was added previously. This will happen
47 // automatically when a request succeeds, fails, or times out.
48 void Remove(StunRequest* request);
49
50 // Removes all stun requests that were added previously.
51 void Clear();
52
53 // Determines whether the given message is a response to one of the
54 // outstanding requests, and if so, processes it appropriately.
55 bool CheckResponse(StunMessage* msg);
56 bool CheckResponse(const char* data, size_t size);
57
58 bool empty() { return requests_.empty(); }
59
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000060 // Set the Origin header for outgoing stun messages.
61 void set_origin(const std::string& origin) { origin_ = origin; }
62
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 // Raised when there are bytes to be sent.
64 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
65
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000066 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067 typedef std::map<std::string, StunRequest*> RequestMap;
68
69 rtc::Thread* thread_;
70 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000071 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072
73 friend class StunRequest;
74};
75
76// Represents an individual request to be sent. The STUN message can either be
77// constructed beforehand or built on demand.
78class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000079 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080 StunRequest();
81 StunRequest(StunMessage* request);
82 virtual ~StunRequest();
83
84 // Causes our wrapped StunMessage to be Prepared
85 void Construct();
86
87 // The manager handling this request (if it has been scheduled for sending).
88 StunRequestManager* manager() { return manager_; }
89
90 // Returns the transaction ID of this request.
91 const std::string& id() { return msg_->transaction_id(); }
92
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000093 // the origin value
94 const std::string& origin() const { return origin_; }
95 void set_origin(const std::string& origin) { origin_ = origin; }
96
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 // Returns the STUN type of the request message.
98 int type();
99
100 // Returns a const pointer to |msg_|.
101 const StunMessage* msg() const;
102
103 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700104 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000106 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 int count_;
108 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000109 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110
111 // Fills in a request object to be sent. Note that request's transaction ID
112 // will already be set and cannot be changed.
113 virtual void Prepare(StunMessage* request) {}
114
115 // Called when the message receives a response or times out.
116 virtual void OnResponse(StunMessage* response) {}
117 virtual void OnErrorResponse(StunMessage* response) {}
118 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700119 // Called when the message is sent.
120 virtual void OnSent();
121 // Returns the next delay for resends.
122 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000124 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 void set_manager(StunRequestManager* manager);
126
127 // Handles messages for sending and timeout.
128 void OnMessage(rtc::Message* pmsg);
129
130 StunRequestManager* manager_;
131 StunMessage* msg_;
honghaiz34b11eb2016-03-16 08:55:44 -0700132 int64_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133
134 friend class StunRequestManager;
135};
136
137} // namespace cricket
138
139#endif // WEBRTC_P2P_BASE_STUNREQUEST_H_