OLD | NEW |
| (Empty) |
1 /* | |
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 #include "webrtc/p2p/base/transport.h" | |
12 #include "webrtc/p2p/base/transportchannelimpl.h" | |
13 #include "webrtc/p2p/base/transportchannelproxy.h" | |
14 #include "webrtc/base/common.h" | |
15 #include "webrtc/base/logging.h" | |
16 #include "webrtc/base/thread.h" | |
17 | |
18 namespace cricket { | |
19 | |
20 enum { | |
21 MSG_UPDATESTATE, | |
22 }; | |
23 | |
24 TransportChannelProxy::TransportChannelProxy(const std::string& content_name, | |
25 int component) | |
26 : TransportChannel(content_name, component), | |
27 impl_(NULL) { | |
28 worker_thread_ = rtc::Thread::Current(); | |
29 } | |
30 | |
31 TransportChannelProxy::~TransportChannelProxy() { | |
32 // Clearing any pending signal. | |
33 worker_thread_->Clear(this); | |
34 if (impl_) { | |
35 impl_->GetTransport()->DestroyChannel(impl_->component()); | |
36 } | |
37 } | |
38 | |
39 void TransportChannelProxy::SetImplementation(TransportChannelImpl* impl) { | |
40 ASSERT(rtc::Thread::Current() == worker_thread_); | |
41 | |
42 if (impl == impl_) { | |
43 // Ignore if the |impl| has already been set. | |
44 LOG(LS_WARNING) << "Ignored TransportChannelProxy::SetImplementation call " | |
45 << "with a same impl as the existing one."; | |
46 return; | |
47 } | |
48 | |
49 // Destroy any existing impl_. | |
50 if (impl_) { | |
51 impl_->GetTransport()->DestroyChannel(impl_->component()); | |
52 } | |
53 | |
54 // Adopt the supplied impl, and connect to its signals. | |
55 impl_ = impl; | |
56 | |
57 if (impl_) { | |
58 impl_->SignalReadableState.connect( | |
59 this, &TransportChannelProxy::OnReadableState); | |
60 impl_->SignalWritableState.connect( | |
61 this, &TransportChannelProxy::OnWritableState); | |
62 impl_->SignalReadPacket.connect( | |
63 this, &TransportChannelProxy::OnReadPacket); | |
64 impl_->SignalReadyToSend.connect( | |
65 this, &TransportChannelProxy::OnReadyToSend); | |
66 impl_->SignalRouteChange.connect( | |
67 this, &TransportChannelProxy::OnRouteChange); | |
68 for (const auto& pair : options_) { | |
69 impl_->SetOption(pair.first, pair.second); | |
70 } | |
71 | |
72 // Push down the SRTP ciphers, if any were set. | |
73 if (!pending_srtp_ciphers_.empty()) { | |
74 impl_->SetSrtpCiphers(pending_srtp_ciphers_); | |
75 } | |
76 } | |
77 | |
78 // Post ourselves a message to see if we need to fire state callbacks. | |
79 worker_thread_->Post(this, MSG_UPDATESTATE); | |
80 } | |
81 | |
82 int TransportChannelProxy::SendPacket(const char* data, size_t len, | |
83 const rtc::PacketOptions& options, | |
84 int flags) { | |
85 ASSERT(rtc::Thread::Current() == worker_thread_); | |
86 // Fail if we don't have an impl yet. | |
87 if (!impl_) { | |
88 return -1; | |
89 } | |
90 return impl_->SendPacket(data, len, options, flags); | |
91 } | |
92 | |
93 int TransportChannelProxy::SetOption(rtc::Socket::Option opt, int value) { | |
94 ASSERT(rtc::Thread::Current() == worker_thread_); | |
95 options_.push_back(OptionPair(opt, value)); | |
96 if (!impl_) { | |
97 return 0; | |
98 } | |
99 return impl_->SetOption(opt, value); | |
100 } | |
101 | |
102 bool TransportChannelProxy::GetOption(rtc::Socket::Option opt, int* value) { | |
103 ASSERT(rtc::Thread::Current() == worker_thread_); | |
104 if (impl_) { | |
105 return impl_->GetOption(opt, value); | |
106 } | |
107 | |
108 for (const auto& pair : options_) { | |
109 if (pair.first == opt) { | |
110 *value = pair.second; | |
111 return true; | |
112 } | |
113 } | |
114 return false; | |
115 } | |
116 | |
117 int TransportChannelProxy::GetError() { | |
118 ASSERT(rtc::Thread::Current() == worker_thread_); | |
119 if (!impl_) { | |
120 return 0; | |
121 } | |
122 return impl_->GetError(); | |
123 } | |
124 | |
125 TransportChannelState TransportChannelProxy::GetState() const { | |
126 ASSERT(rtc::Thread::Current() == worker_thread_); | |
127 if (!impl_) { | |
128 return TransportChannelState::STATE_CONNECTING; | |
129 } | |
130 return impl_->GetState(); | |
131 } | |
132 | |
133 bool TransportChannelProxy::GetStats(ConnectionInfos* infos) { | |
134 ASSERT(rtc::Thread::Current() == worker_thread_); | |
135 if (!impl_) { | |
136 return false; | |
137 } | |
138 return impl_->GetStats(infos); | |
139 } | |
140 | |
141 bool TransportChannelProxy::IsDtlsActive() const { | |
142 ASSERT(rtc::Thread::Current() == worker_thread_); | |
143 if (!impl_) { | |
144 return false; | |
145 } | |
146 return impl_->IsDtlsActive(); | |
147 } | |
148 | |
149 bool TransportChannelProxy::GetSslRole(rtc::SSLRole* role) const { | |
150 ASSERT(rtc::Thread::Current() == worker_thread_); | |
151 if (!impl_) { | |
152 return false; | |
153 } | |
154 return impl_->GetSslRole(role); | |
155 } | |
156 | |
157 bool TransportChannelProxy::SetSslRole(rtc::SSLRole role) { | |
158 ASSERT(rtc::Thread::Current() == worker_thread_); | |
159 if (!impl_) { | |
160 return false; | |
161 } | |
162 return impl_->SetSslRole(role); | |
163 } | |
164 | |
165 bool TransportChannelProxy::SetSrtpCiphers(const std::vector<std::string>& | |
166 ciphers) { | |
167 ASSERT(rtc::Thread::Current() == worker_thread_); | |
168 pending_srtp_ciphers_ = ciphers; // Cache so we can send later, but always | |
169 // set so it stays consistent. | |
170 if (impl_) { | |
171 return impl_->SetSrtpCiphers(ciphers); | |
172 } | |
173 return true; | |
174 } | |
175 | |
176 bool TransportChannelProxy::GetSrtpCipher(std::string* cipher) { | |
177 ASSERT(rtc::Thread::Current() == worker_thread_); | |
178 if (!impl_) { | |
179 return false; | |
180 } | |
181 return impl_->GetSrtpCipher(cipher); | |
182 } | |
183 | |
184 bool TransportChannelProxy::GetSslCipher(std::string* cipher) { | |
185 ASSERT(rtc::Thread::Current() == worker_thread_); | |
186 if (!impl_) { | |
187 return false; | |
188 } | |
189 return impl_->GetSslCipher(cipher); | |
190 } | |
191 | |
192 rtc::scoped_refptr<rtc::RTCCertificate> | |
193 TransportChannelProxy::GetLocalCertificate() const { | |
194 ASSERT(rtc::Thread::Current() == worker_thread_); | |
195 if (!impl_) { | |
196 return nullptr; | |
197 } | |
198 return impl_->GetLocalCertificate(); | |
199 } | |
200 | |
201 bool TransportChannelProxy::GetRemoteSSLCertificate( | |
202 rtc::SSLCertificate** cert) const { | |
203 ASSERT(rtc::Thread::Current() == worker_thread_); | |
204 if (!impl_) { | |
205 return false; | |
206 } | |
207 return impl_->GetRemoteSSLCertificate(cert); | |
208 } | |
209 | |
210 bool TransportChannelProxy::ExportKeyingMaterial(const std::string& label, | |
211 const uint8* context, | |
212 size_t context_len, | |
213 bool use_context, | |
214 uint8* result, | |
215 size_t result_len) { | |
216 ASSERT(rtc::Thread::Current() == worker_thread_); | |
217 if (!impl_) { | |
218 return false; | |
219 } | |
220 return impl_->ExportKeyingMaterial(label, context, context_len, use_context, | |
221 result, result_len); | |
222 } | |
223 | |
224 IceRole TransportChannelProxy::GetIceRole() const { | |
225 ASSERT(rtc::Thread::Current() == worker_thread_); | |
226 if (!impl_) { | |
227 return ICEROLE_UNKNOWN; | |
228 } | |
229 return impl_->GetIceRole(); | |
230 } | |
231 | |
232 void TransportChannelProxy::OnReadableState(TransportChannel* channel) { | |
233 ASSERT(rtc::Thread::Current() == worker_thread_); | |
234 ASSERT(channel == impl_); | |
235 set_readable(impl_->readable()); | |
236 // Note: SignalReadableState fired by set_readable. | |
237 } | |
238 | |
239 void TransportChannelProxy::OnWritableState(TransportChannel* channel) { | |
240 ASSERT(rtc::Thread::Current() == worker_thread_); | |
241 ASSERT(channel == impl_); | |
242 set_writable(impl_->writable()); | |
243 // Note: SignalWritableState fired by set_readable. | |
244 } | |
245 | |
246 void TransportChannelProxy::OnReadPacket( | |
247 TransportChannel* channel, const char* data, size_t size, | |
248 const rtc::PacketTime& packet_time, int flags) { | |
249 ASSERT(rtc::Thread::Current() == worker_thread_); | |
250 ASSERT(channel == impl_); | |
251 SignalReadPacket(this, data, size, packet_time, flags); | |
252 } | |
253 | |
254 void TransportChannelProxy::OnReadyToSend(TransportChannel* channel) { | |
255 ASSERT(rtc::Thread::Current() == worker_thread_); | |
256 ASSERT(channel == impl_); | |
257 SignalReadyToSend(this); | |
258 } | |
259 | |
260 void TransportChannelProxy::OnRouteChange(TransportChannel* channel, | |
261 const Candidate& candidate) { | |
262 ASSERT(rtc::Thread::Current() == worker_thread_); | |
263 ASSERT(channel == impl_); | |
264 SignalRouteChange(this, candidate); | |
265 } | |
266 | |
267 void TransportChannelProxy::OnMessage(rtc::Message* msg) { | |
268 ASSERT(rtc::Thread::Current() == worker_thread_); | |
269 if (msg->message_id == MSG_UPDATESTATE) { | |
270 // If impl_ is already readable or writable, push up those signals. | |
271 set_readable(impl_ ? impl_->readable() : false); | |
272 set_writable(impl_ ? impl_->writable() : false); | |
273 } | |
274 } | |
275 | |
276 } // namespace cricket | |
OLD | NEW |