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_RELAYPORT_H_
29
#define TALK_P2P_BASE_RELAYPORT_H_
36
#include "talk/p2p/base/port.h"
37
#include "talk/p2p/base/stunrequest.h"
41
extern const char RELAY_PORT_TYPE[];
43
class RelayConnection;
45
// Communicates using an allocated port on the relay server. For each
46
// remote candidate that we try to send data to a RelayEntry instance
47
// is created. The RelayEntry will try to reach the remote destination
48
// by connecting to all available server addresses in a pre defined
49
// order with a small delay in between. When a connection is
50
// successful all other connection attemts are aborted.
51
class RelayPort : public Port {
53
typedef std::pair<talk_base::Socket::Option, int> OptionValue;
55
// RelayPort doesn't yet do anything fancy in the ctor.
56
static RelayPort* Create(
57
talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
58
talk_base::Network* network, const talk_base::IPAddress& ip,
59
int min_port, int max_port, const std::string& username,
60
const std::string& password, const std::string& magic_cookie) {
61
return new RelayPort(thread, factory, network, ip, min_port, max_port,
62
username, password, magic_cookie);
66
void AddServerAddress(const ProtocolAddress& addr);
67
void AddExternalAddress(const ProtocolAddress& addr);
69
const std::vector<OptionValue>& options() const { return options_; }
70
const std::string& magic_cookie() const { return magic_cookie_; }
71
bool HasMagicCookie(const char* data, size_t size);
73
virtual void PrepareAddress();
74
virtual Connection* CreateConnection(const Candidate& address,
75
CandidateOrigin origin);
76
virtual int SetOption(talk_base::Socket::Option opt, int value);
77
virtual int GetError();
79
const ProtocolAddress * ServerAddress(size_t index) const;
80
bool IsReady() { return ready_; }
83
sigslot::signal1<const ProtocolAddress*> SignalConnectFailure;
84
sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout;
87
RelayPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
88
talk_base::Network*, const talk_base::IPAddress& ip,
89
int min_port, int max_port, const std::string& username,
90
const std::string& password, const std::string& magic_cookie);
95
virtual int SendTo(const void* data, size_t size,
96
const talk_base::SocketAddress& addr, bool payload);
98
// Dispatches the given packet to the port or connection as appropriate.
99
void OnReadPacket(const char* data, size_t size,
100
const talk_base::SocketAddress& remote_addr);
103
friend class RelayEntry;
105
std::deque<ProtocolAddress> server_addr_;
107
std::vector<RelayEntry*> entries_;
108
std::vector<OptionValue> options_;
109
std::string magic_cookie_;
113
} // namespace cricket
115
#endif // TALK_P2P_BASE_RELAYPORT_H_