OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/protocol/ice_transport_session.h" | |
6 | |
7 #include "remoting/protocol/channel_authenticator.h" | |
8 #include "remoting/protocol/channel_multiplexer.h" | |
9 #include "remoting/protocol/libjingle_transport_factory.h" | |
10 #include "remoting/protocol/pseudotcp_channel_factory.h" | |
11 #include "remoting/protocol/secure_channel_factory.h" | |
12 #include "remoting/protocol/stream_channel_factory.h" | |
13 | |
14 namespace remoting { | |
15 namespace protocol { | |
16 | |
17 // Delay after candidate creation before sending transport-info message to | |
18 // accumulate multiple candidates. This is an optimization to reduce number of | |
19 // transport-info messages. | |
20 const int kTransportInfoSendDelayMs = 20; | |
21 | |
22 // Name of the multiplexed channel. | |
23 static const char kMuxChannelName[] = "mux"; | |
24 | |
25 IceTransportSession::IceTransportSession( | |
26 LibjingleTransportFactory* libjingle_transport_factory) | |
27 : libjingle_transport_factory_(libjingle_transport_factory) {} | |
28 | |
29 IceTransportSession::~IceTransportSession() { | |
30 channel_multiplexer_.reset(); | |
31 DCHECK(channels_.empty()); | |
32 } | |
33 | |
34 void IceTransportSession::Start(TransportSession::EventHandler* event_handler, | |
35 Authenticator* authenticator) { | |
36 event_handler_ = event_handler; | |
37 pseudotcp_channel_factory_.reset(new PseudoTcpChannelFactory(this)); | |
38 secure_channel_factory_.reset(new SecureChannelFactory( | |
39 pseudotcp_channel_factory_.get(), authenticator)); | |
40 } | |
41 | |
42 bool IceTransportSession::ProcessTransportInfo( | |
43 buzz::XmlElement* transport_info_xml) { | |
44 IceTransportInfo transport_info; | |
45 if (!transport_info.ParseXml(transport_info_xml)) | |
46 return false; | |
47 | |
48 for (auto it = transport_info.ice_credentials.begin(); | |
49 it != transport_info.ice_credentials.end(); ++it) { | |
50 ChannelsMap::iterator channel = channels_.find(it->channel); | |
51 if (channel != channels_.end()) { | |
52 channel->second->SetRemoteCredentials(it->ufrag, it->password); | |
53 } else { | |
54 // Transport info was received before the channel was created. | |
55 // This could happen due to messages being reordered on the wire. | |
56 pending_remote_ice_credentials_.push_back(*it); | |
57 } | |
58 } | |
59 | |
60 for (auto it = transport_info.candidates.begin(); | |
61 it != transport_info.candidates.end(); ++it) { | |
62 ChannelsMap::iterator channel = channels_.find(it->name); | |
63 if (channel != channels_.end()) { | |
64 channel->second->AddRemoteCandidate(it->candidate); | |
65 } else { | |
66 // Transport info was received before the channel was created. | |
67 // This could happen due to messages being reordered on the wire. | |
68 pending_remote_candidates_.push_back(*it); | |
69 } | |
70 } | |
71 | |
72 return true; | |
73 } | |
74 | |
75 DatagramChannelFactory* IceTransportSession::GetDatagramChannelFactory() { | |
76 return this; | |
77 } | |
78 | |
79 StreamChannelFactory* IceTransportSession::GetStreamChannelFactory() { | |
80 return secure_channel_factory_.get(); | |
81 } | |
82 | |
83 StreamChannelFactory* IceTransportSession::GetMultiplexedChannelFactory() { | |
84 if (!channel_multiplexer_.get()) { | |
85 channel_multiplexer_.reset( | |
86 new ChannelMultiplexer(GetStreamChannelFactory(), kMuxChannelName)); | |
87 } | |
88 return channel_multiplexer_.get(); | |
89 } | |
90 | |
91 void IceTransportSession::CreateChannel( | |
92 const std::string& name, | |
93 const ChannelCreatedCallback& callback) { | |
94 DCHECK(!channels_[name]); | |
95 | |
96 scoped_ptr<Transport> channel = | |
97 libjingle_transport_factory_->CreateTransport(); | |
98 channel->Connect(name, this, callback); | |
99 AddPendingRemoteTransportInfo(channel.get()); | |
100 channels_[name] = channel.release(); | |
101 } | |
102 | |
103 void IceTransportSession::CancelChannelCreation(const std::string& name) { | |
104 ChannelsMap::iterator it = channels_.find(name); | |
105 if (it != channels_.end()) { | |
106 DCHECK(!it->second->is_connected()); | |
107 delete it->second; | |
108 DCHECK(channels_.find(name) == channels_.end()); | |
109 } | |
110 } | |
111 | |
112 void IceTransportSession::AddPendingRemoteTransportInfo(Transport* channel) { | |
113 std::list<IceTransportInfo::IceCredentials>::iterator credentials = | |
114 pending_remote_ice_credentials_.begin(); | |
115 while (credentials != pending_remote_ice_credentials_.end()) { | |
116 if (credentials->channel == channel->name()) { | |
117 channel->SetRemoteCredentials(credentials->ufrag, credentials->password); | |
118 credentials = pending_remote_ice_credentials_.erase(credentials); | |
119 } else { | |
120 ++credentials; | |
121 } | |
122 } | |
123 | |
124 std::list<IceTransportInfo::NamedCandidate>::iterator candidate = | |
125 pending_remote_candidates_.begin(); | |
126 while (candidate != pending_remote_candidates_.end()) { | |
127 if (candidate->name == channel->name()) { | |
128 channel->AddRemoteCandidate(candidate->candidate); | |
129 candidate = pending_remote_candidates_.erase(candidate); | |
130 } else { | |
131 ++candidate; | |
132 } | |
133 } | |
134 } | |
135 | |
136 void IceTransportSession::OnTransportIceCredentials( | |
137 Transport* transport, | |
138 const std::string& ufrag, | |
139 const std::string& password) { | |
140 EnsurePendingTransportInfoMessage(); | |
141 pending_transport_info_message_->ice_credentials.push_back( | |
142 IceTransportInfo::IceCredentials(transport->name(), ufrag, password)); | |
143 } | |
144 | |
145 void IceTransportSession::OnTransportCandidate( | |
146 Transport* transport, | |
147 const cricket::Candidate& candidate) { | |
148 EnsurePendingTransportInfoMessage(); | |
149 pending_transport_info_message_->candidates.push_back( | |
150 IceTransportInfo::NamedCandidate(transport->name(), candidate)); | |
151 } | |
152 | |
153 void IceTransportSession::OnTransportRouteChange(Transport* transport, | |
154 const TransportRoute& route) { | |
155 if (event_handler_) | |
156 event_handler_->OnTransportRouteChange(transport->name(), route); | |
157 } | |
158 | |
159 void IceTransportSession::OnTransportFailed(Transport* transport) { | |
160 event_handler_->OnTransportError(CHANNEL_CONNECTION_ERROR); | |
161 } | |
162 | |
163 void IceTransportSession::OnTransportDeleted(Transport* transport) { | |
164 ChannelsMap::iterator it = channels_.find(transport->name()); | |
165 DCHECK_EQ(it->second, transport); | |
166 channels_.erase(it); | |
167 } | |
168 | |
169 void IceTransportSession::EnsurePendingTransportInfoMessage() { | |
170 // |transport_info_timer_| must be running iff | |
171 // |pending_transport_info_message_| exists. | |
172 DCHECK_EQ(pending_transport_info_message_ != nullptr, | |
173 transport_info_timer_.IsRunning()); | |
174 | |
175 if (!pending_transport_info_message_) { | |
176 pending_transport_info_message_.reset(new IceTransportInfo()); | |
177 // Delay sending the new candidates in case we get more candidates | |
178 // that we can send in one message. | |
179 transport_info_timer_.Start( | |
180 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), | |
181 this, &IceTransportSession::SendTransportInfo); | |
182 } | |
183 } | |
184 | |
185 void IceTransportSession::SendTransportInfo() { | |
186 DCHECK(pending_transport_info_message_); | |
187 event_handler_->OnOutgoingTransportInfo( | |
188 pending_transport_info_message_->ToXml()); | |
189 pending_transport_info_message_.reset(); | |
190 } | |
191 | |
192 } // namespace protocol | |
193 } // namespace remoting | |
OLD | NEW |