3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#ifndef TALK_P2P_BASE_PORTALLOCATOR_H_
29
#define TALK_P2P_BASE_PORTALLOCATOR_H_
34
#include "talk/base/sigslot.h"
35
#include "talk/p2p/base/port.h"
39
// PortAllocator is responsible for allocating Port types for a given
40
// P2PSocket. It also handles port freeing.
42
// Clients can override this class to control port allocation, including
43
// what kinds of ports are allocated.
45
const uint32 PORTALLOCATOR_DISABLE_UDP = 0x01;
46
const uint32 PORTALLOCATOR_DISABLE_STUN = 0x02;
47
const uint32 PORTALLOCATOR_DISABLE_RELAY = 0x04;
48
const uint32 PORTALLOCATOR_DISABLE_TCP = 0x08;
49
const uint32 PORTALLOCATOR_ENABLE_SHAKER = 0x10;
50
const uint32 PORTALLOCATOR_ENABLE_BUNDLE = 0x20;
52
const uint32 kDefaultPortAllocatorFlags = 0;
54
class PortAllocatorSessionMuxer;
56
class PortAllocatorSession : public sigslot::has_slots<> {
58
// TODO Remove session_type argument (and other places), as
60
PortAllocatorSession(const std::string& name,
61
const std::string& session_type,
64
session_type_(session_type),
68
// Subclasses should clean up any ports created.
69
virtual ~PortAllocatorSession() {}
71
uint32 flags() const { return flags_; }
72
void set_flags(uint32 flags) { flags_ = flags; }
73
const std::string& name() const { return name_; }
74
const std::string& session_type() const { return session_type_; }
76
// Prepares an initial set of ports to try.
77
virtual void GetInitialPorts() = 0;
79
// Starts and stops the flow of additional ports to try.
80
virtual void StartGetAllPorts() = 0;
81
virtual void StopGetAllPorts() = 0;
82
virtual bool IsGettingAllPorts() = 0;
84
sigslot::signal2<PortAllocatorSession*, Port*> SignalPortReady;
85
sigslot::signal2<PortAllocatorSession*,
86
const std::vector<Candidate>&> SignalCandidatesReady;
87
sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
89
uint32 generation() { return generation_; }
90
void set_generation(uint32 generation) { generation_ = generation; }
91
sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
95
std::string session_type_;
102
class PortAllocator : public sigslot::has_slots<> {
105
flags_(kDefaultPortAllocatorFlags),
109
virtual ~PortAllocator();
111
PortAllocatorSession* CreateSession(
112
const std::string& sid,
113
const std::string& name,
114
const std::string& session_type);
116
PortAllocatorSessionMuxer* GetSessionMuxer(const std::string& sid) const;
117
void OnSessionMuxerDestroyed(PortAllocatorSessionMuxer* session);
119
uint32 flags() const { return flags_; }
120
void set_flags(uint32 flags) { flags_ = flags; }
122
const std::string& user_agent() const { return agent_; }
123
const talk_base::ProxyInfo& proxy() const { return proxy_; }
124
void set_proxy(const std::string& agent, const talk_base::ProxyInfo& proxy) {
129
// Gets/Sets the port range to use when choosing client ports.
130
int min_port() const { return min_port_; }
131
int max_port() const { return max_port_; }
132
bool SetPortRange(int min_port, int max_port) {
133
if (min_port > max_port) {
137
min_port_ = min_port;
138
max_port_ = max_port;
143
// TODO - Change this version of CreateSession name to avoid method hiding
144
// when called by the derived classes.
145
virtual PortAllocatorSession* CreateSession(const std::string &name,
146
const std::string &session_type) = 0;
148
typedef std::map<std::string, PortAllocatorSessionMuxer*> SessionMuxerMap;
152
talk_base::ProxyInfo proxy_;
155
SessionMuxerMap muxers_;
158
} // namespace cricket
160
#endif // TALK_P2P_BASE_PORTALLOCATOR_H_