blob: 3d9538daac17d02ac583e3c1e34f57bdc4f36e6b [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_RELAYPORT_H_
12#define WEBRTC_P2P_BASE_RELAYPORT_H_
13
14#include <deque>
15#include <string>
16#include <utility>
17#include <vector>
18
19#include "webrtc/p2p/base/port.h"
20#include "webrtc/p2p/base/stunrequest.h"
21
22namespace cricket {
23
24class RelayEntry;
25class RelayConnection;
26
27// Communicates using an allocated port on the relay server. For each
28// remote candidate that we try to send data to a RelayEntry instance
29// is created. The RelayEntry will try to reach the remote destination
30// by connecting to all available server addresses in a pre defined
31// order with a small delay in between. When a connection is
32// successful all other connection attemts are aborted.
33class RelayPort : public Port {
34 public:
35 typedef std::pair<rtc::Socket::Option, int> OptionValue;
36
37 // RelayPort doesn't yet do anything fancy in the ctor.
38 static RelayPort* Create(
39 rtc::Thread* thread, rtc::PacketSocketFactory* factory,
40 rtc::Network* network, const rtc::IPAddress& ip,
41 int min_port, int max_port, const std::string& username,
42 const std::string& password) {
43 return new RelayPort(thread, factory, network, ip, min_port, max_port,
44 username, password);
45 }
46 virtual ~RelayPort();
47
48 void AddServerAddress(const ProtocolAddress& addr);
49 void AddExternalAddress(const ProtocolAddress& addr);
50
51 const std::vector<OptionValue>& options() const { return options_; }
52 bool HasMagicCookie(const char* data, size_t size);
53
54 virtual void PrepareAddress();
55 virtual Connection* CreateConnection(const Candidate& address,
56 CandidateOrigin origin);
57 virtual int SetOption(rtc::Socket::Option opt, int value);
58 virtual int GetOption(rtc::Socket::Option opt, int* value);
59 virtual int GetError();
60
61 const ProtocolAddress * ServerAddress(size_t index) const;
62 bool IsReady() { return ready_; }
63
64 // Used for testing.
65 sigslot::signal1<const ProtocolAddress*> SignalConnectFailure;
66 sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout;
67
68 protected:
69 RelayPort(rtc::Thread* thread, rtc::PacketSocketFactory* factory,
70 rtc::Network*, const rtc::IPAddress& ip,
71 int min_port, int max_port, const std::string& username,
72 const std::string& password);
73 bool Init();
74
75 void SetReady();
76
77 virtual int SendTo(const void* data, size_t size,
78 const rtc::SocketAddress& addr,
79 const rtc::PacketOptions& options,
80 bool payload);
81
82 // Dispatches the given packet to the port or connection as appropriate.
83 void OnReadPacket(const char* data, size_t size,
84 const rtc::SocketAddress& remote_addr,
85 ProtocolType proto,
86 const rtc::PacketTime& packet_time);
87
88 private:
89 friend class RelayEntry;
90
91 std::deque<ProtocolAddress> server_addr_;
92 std::vector<ProtocolAddress> external_addr_;
93 bool ready_;
94 std::vector<RelayEntry*> entries_;
95 std::vector<OptionValue> options_;
96 int error_;
97};
98
99} // namespace cricket
100
101#endif // WEBRTC_P2P_BASE_RELAYPORT_H_