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
// P2PTransportChannel wraps up the state management of the connection between
29
// two P2P clients. Clients have candidate ports for connecting, and
30
// connections which are combinations of candidates from each end (Alice and
31
// Bob each have candidates, one candidate from Alice and one candidate from
32
// Bob are used to make a connection, repeat to make many connections).
34
// When all of the available connections become invalid (non-writable), we
35
// kick off a process of determining more candidates and more connections.
37
#ifndef TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
38
#define TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_
43
#include "talk/base/sigslot.h"
44
#include "talk/p2p/base/candidate.h"
45
#include "talk/p2p/base/port.h"
46
#include "talk/p2p/base/portallocator.h"
47
#include "talk/p2p/base/transport.h"
48
#include "talk/p2p/base/transportchannelimpl.h"
49
#include "talk/p2p/base/p2ptransport.h"
53
// Adds the port on which the candidate originated.
54
class RemoteCandidate : public Candidate {
56
RemoteCandidate(const Candidate& c, Port* origin_port)
57
: Candidate(c), origin_port_(origin_port) {}
59
Port* origin_port() { return origin_port_; }
65
// P2PTransportChannel manages the candidates and connection process to keep
66
// two P2P clients connected to each other.
67
class P2PTransportChannel : public TransportChannelImpl,
68
public talk_base::MessageHandler {
70
P2PTransportChannel(const std::string &name,
71
const std::string &content_type,
72
P2PTransport* transport,
73
PortAllocator *allocator);
74
virtual ~P2PTransportChannel();
76
// From TransportChannelImpl:
77
virtual Transport* GetTransport() { return transport_; }
78
virtual void Connect();
80
virtual void OnSignalingReady();
82
// From TransportChannel:
83
virtual int SendPacket(const char *data, size_t len);
84
virtual int SetOption(talk_base::Socket::Option opt, int value);
85
virtual int GetError() { return error_; }
87
// This hack is here to allow the SocketMonitor to downcast to the
88
// P2PTransportChannel safely.
89
virtual P2PTransportChannel* GetP2PChannel() { return this; }
91
// These are used by the connection monitor.
92
sigslot::signal1<P2PTransportChannel*> SignalConnectionMonitor;
93
const std::vector<Connection *>& connections() const { return connections_; }
94
Connection* best_connection() const { return best_connection_; }
96
void set_incoming_only(bool value) { incoming_only_ = value; }
98
// Handler for internal messages.
99
virtual void OnMessage(talk_base::Message *pmsg);
101
virtual void OnCandidate(const Candidate& candidate);
105
void CancelPendingAllocate();
106
void UpdateConnectionStates();
108
void SortConnections();
109
void SwitchBestConnectionTo(Connection* conn);
110
void UpdateChannelState();
111
void HandleWritable();
112
void HandleNotWritable();
113
void HandleAllTimedOut();
114
Connection* GetBestConnectionOnNetwork(talk_base::Network* network);
115
bool CreateConnections(const Candidate &remote_candidate, Port* origin_port,
117
bool CreateConnection(Port* port, const Candidate& remote_candidate,
118
Port* origin_port, bool readable);
119
bool FindConnection(cricket::Connection* connection) const;
120
void RememberRemoteCandidate(const Candidate& remote_candidate,
122
void OnUnknownAddress(Port *port, const talk_base::SocketAddress &addr,
123
StunMessage *stun_msg,
124
const std::string &remote_username, bool port_muxed);
125
void OnPortReady(PortAllocatorSession *session, Port* port);
126
void OnCandidatesReady(PortAllocatorSession *session,
127
const std::vector<Candidate>& candidates);
128
void OnCandidatesAllocationDone(PortAllocatorSession* session);
129
void OnConnectionStateChange(Connection *connection);
130
void OnConnectionDestroyed(Connection *connection);
131
void OnPortDestroyed(Port* port);
132
void OnReadPacket(Connection *connection, const char *data, size_t len);
135
bool IsPingable(Connection* conn);
136
Connection* FindNextPingableConnection();
137
uint32 NumPingableConnections();
138
PortAllocatorSession* allocator_session() {
139
return allocator_sessions_.back();
141
void AddAllocatorSession(PortAllocatorSession* session);
143
talk_base::Thread* thread() const { return worker_thread_; }
145
P2PTransport* transport_;
146
PortAllocator *allocator_;
147
talk_base::Thread *worker_thread_;
149
bool waiting_for_signaling_;
151
std::vector<PortAllocatorSession*> allocator_sessions_;
152
std::vector<Port *> ports_;
153
std::vector<Connection *> connections_;
154
Connection *best_connection_;
155
std::vector<RemoteCandidate> remote_candidates_;
156
// indicates whether StartGetAllCandidates has been called
157
bool pinging_started_;
158
bool sort_dirty_; // indicates whether another sort is needed right now
161
typedef std::map<talk_base::Socket::Option, int> OptionMap;
164
DISALLOW_EVIL_CONSTRUCTORS(P2PTransportChannel);
167
} // namespace cricket
169
#endif // TALK_P2P_BASE_P2PTRANSPORTCHANNEL_H_