blob: 267b4a1959d3c779397d5943fce3c3392497308d [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 {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000027 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028 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.org0ba15332015-01-10 00:47:02 +000049 // Set the Origin header for outgoing stun messages.
50 void set_origin(const std::string& origin) { origin_ = origin; }
51
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 // Raised when there are bytes to be sent.
53 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
54
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000055 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000056 typedef std::map<std::string, StunRequest*> RequestMap;
57
58 rtc::Thread* thread_;
59 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000060 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061
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.
67class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000068 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 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.org0ba15332015-01-10 00:47:02 +000082 // the origin value
83 const std::string& origin() const { return origin_; }
84 void set_origin(const std::string& origin) { origin_ = origin; }
85
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000086 // 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)
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 uint32_t Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000095 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 int count_;
97 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000098 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099
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() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700108 // Called when the message is sent.
109 virtual void OnSent();
110 // Returns the next delay for resends.
111 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000113 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 void set_manager(StunRequestManager* manager);
115
116 // Handles messages for sending and timeout.
117 void OnMessage(rtc::Message* pmsg);
118
119 StunRequestManager* manager_;
120 StunMessage* msg_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200121 uint32_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122
123 friend class StunRequestManager;
124};
125
126} // namespace cricket
127
128#endif // WEBRTC_P2P_BASE_STUNREQUEST_H_