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 (std::list<IceTransportInfo::IceCredentials>::const_iterator it = | |
Jamie
2015/10/27 21:32:27
Can you use auto here?
Sergey Ulanov
2015/10/27 23:03:58
Done.
| |
49 transport_info.ice_credentials.begin(); | |
50 it != transport_info.ice_credentials.end(); ++it) { | |
51 ChannelsMap::iterator channel = channels_.find(it->channel); | |
52 if (channel != channels_.end()) { | |
53 channel->second->SetRemoteCredentials(it->ufrag, it->password); | |
54 } else { | |
55 // Transport info was received before the channel was created. | |
56 // This could happen due to messages being reordered on the wire. | |
57 pending_remote_ice_credentials_.push_back(*it); | |
58 } | |
59 } | |
60 | |
61 for (std::list<IceTransportInfo::NamedCandidate>::const_iterator it = | |
62 transport_info.candidates.begin(); | |
63 it != transport_info.candidates.end(); ++it) { | |
64 ChannelsMap::iterator channel = channels_.find(it->name); | |
65 if (channel != channels_.end()) { | |
66 channel->second->AddRemoteCandidate(it->candidate); | |
67 } else { | |
68 // Transport info was received before the channel was created. | |
69 // This could happen due to messages being reordered on the wire. | |
70 pending_remote_candidates_.push_back(*it); | |
71 } | |
72 } | |
73 | |
74 return true; | |
75 } | |
76 | |
77 DatagramChannelFactory* IceTransportSession::GetDatagramChannelFactory() { | |
78 return this; | |
79 } | |
80 | |
81 StreamChannelFactory* IceTransportSession::GetStreamChannelFactory() { | |
82 return secure_channel_factory_.get(); | |
83 } | |
84 | |
85 StreamChannelFactory* IceTransportSession::GetMultiplexedChannelFactory() { | |
86 if (!channel_multiplexer_.get()) { | |
87 channel_multiplexer_.reset( | |
88 new ChannelMultiplexer(GetStreamChannelFactory(), kMuxChannelName)); | |
89 } | |
90 return channel_multiplexer_.get(); | |
91 } | |
92 | |
93 void IceTransportSession::CreateChannel( | |
94 const std::string& name, | |
95 const ChannelCreatedCallback& callback) { | |
96 DCHECK(!channels_[name]); | |
97 | |
98 scoped_ptr<Transport> channel = | |
99 libjingle_transport_factory_->CreateTransport(); | |
100 channel->Connect(name, this, callback); | |
101 AddPendingRemoteTransportInfo(channel.get()); | |
102 channels_[name] = channel.release(); | |
103 } | |
104 | |
105 void IceTransportSession::CancelChannelCreation(const std::string& name) { | |
106 ChannelsMap::iterator it = channels_.find(name); | |
107 if (it != channels_.end()) { | |
108 DCHECK(!it->second->is_connected()); | |
109 delete it->second; | |
110 DCHECK(channels_.find(name) == channels_.end()); | |
111 } | |
112 } | |
113 | |
114 void IceTransportSession::AddPendingRemoteTransportInfo(Transport* channel) { | |
115 std::list<IceTransportInfo::IceCredentials>::iterator credentials = | |
116 pending_remote_ice_credentials_.begin(); | |
117 while (credentials != pending_remote_ice_credentials_.end()) { | |
118 if (credentials->channel == channel->name()) { | |
119 channel->SetRemoteCredentials(credentials->ufrag, credentials->password); | |
120 credentials = pending_remote_ice_credentials_.erase(credentials); | |
121 } else { | |
122 ++credentials; | |
123 } | |
124 } | |
125 | |
126 std::list<IceTransportInfo::NamedCandidate>::iterator candidate = | |
127 pending_remote_candidates_.begin(); | |
128 while (candidate != pending_remote_candidates_.end()) { | |
129 if (candidate->name == channel->name()) { | |
130 channel->AddRemoteCandidate(candidate->candidate); | |
131 candidate = pending_remote_candidates_.erase(candidate); | |
132 } else { | |
133 ++candidate; | |
134 } | |
135 } | |
136 } | |
137 | |
138 void IceTransportSession::OnTransportIceCredentials( | |
139 Transport* transport, | |
140 const std::string& ufrag, | |
141 const std::string& password) { | |
142 EnsurePendingTransportInfoMessage(); | |
143 pending_transport_info_message_->ice_credentials.push_back( | |
144 IceTransportInfo::IceCredentials(transport->name(), ufrag, password)); | |
145 } | |
146 | |
147 void IceTransportSession::OnTransportCandidate( | |
148 Transport* transport, | |
149 const cricket::Candidate& candidate) { | |
150 EnsurePendingTransportInfoMessage(); | |
151 pending_transport_info_message_->candidates.push_back( | |
152 IceTransportInfo::NamedCandidate(transport->name(), candidate)); | |
153 } | |
154 | |
155 void IceTransportSession::OnTransportRouteChange(Transport* transport, | |
156 const TransportRoute& route) { | |
157 if (event_handler_) | |
158 event_handler_->OnTransportRouteChange(transport->name(), route); | |
159 } | |
160 | |
161 void IceTransportSession::OnTransportFailed(Transport* transport) { | |
162 event_handler_->OnTransportError(CHANNEL_CONNECTION_ERROR); | |
163 } | |
164 | |
165 void IceTransportSession::OnTransportDeleted(Transport* transport) { | |
166 ChannelsMap::iterator it = channels_.find(transport->name()); | |
167 DCHECK_EQ(it->second, transport); | |
168 channels_.erase(it); | |
169 } | |
170 | |
171 void IceTransportSession::EnsurePendingTransportInfoMessage() { | |
172 // |transport_info_timer_| must be running iff | |
173 // |pending_transport_info_message_| exists. | |
174 DCHECK_EQ(pending_transport_info_message_ != nullptr, | |
175 transport_info_timer_.IsRunning()); | |
176 | |
177 if (!pending_transport_info_message_) { | |
178 pending_transport_info_message_.reset(new IceTransportInfo()); | |
179 // Delay sending the new candidates in case we get more candidates | |
180 // that we can send in one message. | |
181 transport_info_timer_.Start( | |
182 FROM_HERE, base::TimeDelta::FromMilliseconds(kTransportInfoSendDelayMs), | |
183 this, &IceTransportSession::SendTransportInfo); | |
184 } | |
185 } | |
186 | |
187 void IceTransportSession::SendTransportInfo() { | |
188 DCHECK(pending_transport_info_message_); | |
189 event_handler_->OnOutgoingTransportInfo( | |
190 pending_transport_info_message_->ToXml()); | |
191 pending_transport_info_message_.reset(); | |
192 } | |
193 | |
194 } // namespace protocol | |
195 } // namespace remoting | |
OLD | NEW |