OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cast_channel/cast_socket.h" | 5 #include "components/cast_channel/cast_socket.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 } | 233 } |
234 } | 234 } |
235 return result.success(); | 235 return result.success(); |
236 } | 236 } |
237 | 237 |
238 void CastSocketImpl::SetTransportForTesting( | 238 void CastSocketImpl::SetTransportForTesting( |
239 std::unique_ptr<CastTransport> transport) { | 239 std::unique_ptr<CastTransport> transport) { |
240 transport_ = std::move(transport); | 240 transport_ = std::move(transport); |
241 } | 241 } |
242 | 242 |
243 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 243 void CastSocketImpl::Connect(base::Callback<void(ChannelError)> callback) { |
244 base::Callback<void(ChannelError)> callback) { | |
245 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 244 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
246 VLOG_WITH_CONNECTION(1) << "Connect readyState = " | 245 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
247 << ::cast_channel::ReadyStateToString(ready_state_); | 246 << ::cast_channel::ReadyStateToString(ready_state_); |
248 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); | 247 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); |
249 | 248 |
250 delegate_ = std::move(delegate); | 249 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this); |
251 | 250 |
252 if (ready_state_ != ReadyState::NONE) { | 251 if (ready_state_ != ReadyState::NONE) { |
253 callback.Run(ChannelError::CONNECT_ERROR); | 252 callback.Run(ChannelError::CONNECT_ERROR); |
254 return; | 253 return; |
255 } | 254 } |
256 | 255 |
257 connect_callback_ = callback; | 256 connect_callback_ = callback; |
258 SetReadyState(ReadyState::CONNECTING); | 257 SetReadyState(ReadyState::CONNECTING); |
259 SetConnectState(ConnectionState::TCP_CONNECT); | 258 SetConnectState(ConnectionState::TCP_CONNECT); |
260 | 259 |
261 // Set up connection timeout. | 260 // Set up connection timeout. |
262 if (connect_timeout_.InMicroseconds() > 0) { | 261 if (connect_timeout_.InMicroseconds() > 0) { |
263 DCHECK(connect_timeout_callback_.IsCancelled()); | 262 DCHECK(connect_timeout_callback_.IsCancelled()); |
264 connect_timeout_callback_.Reset( | 263 connect_timeout_callback_.Reset( |
265 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 264 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
266 GetTimer()->Start(FROM_HERE, connect_timeout_, | 265 GetTimer()->Start(FROM_HERE, connect_timeout_, |
267 connect_timeout_callback_.callback()); | 266 connect_timeout_callback_.callback()); |
268 } | 267 } |
269 | 268 |
270 DoConnectLoop(net::OK); | 269 DoConnectLoop(net::OK); |
271 } | 270 } |
272 | 271 |
273 CastTransport* CastSocketImpl::transport() const { | 272 CastTransport* CastSocketImpl::transport() const { |
274 return transport_.get(); | 273 return transport_.get(); |
275 } | 274 } |
276 | 275 |
276 void CastSocketImpl::AddObserver(Observer* observer) { | |
277 DCHECK(observer); | |
278 if (!observers_.HasObserver(observer)) | |
279 observers_.AddObserver(observer); | |
280 } | |
281 | |
277 void CastSocketImpl::OnConnectTimeout() { | 282 void CastSocketImpl::OnConnectTimeout() { |
278 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 283 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
279 // Stop all pending connection setup tasks and report back to the client. | 284 // Stop all pending connection setup tasks and report back to the client. |
280 is_canceled_ = true; | 285 is_canceled_ = true; |
281 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | 286 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
282 SetErrorState(ChannelError::CONNECT_TIMEOUT); | 287 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
283 DoConnectCallback(); | 288 DoConnectCallback(); |
284 } | 289 } |
285 | 290 |
286 void CastSocketImpl::ResetConnectLoopCallback() { | 291 void CastSocketImpl::ResetConnectLoopCallback() { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
596 } | 601 } |
597 | 602 |
598 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 603 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
599 VLOG_WITH_CONNECTION(1) << "SetErrorState " | 604 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
600 << ::cast_channel::ChannelErrorToString(error_state); | 605 << ::cast_channel::ChannelErrorToString(error_state); |
601 DCHECK_EQ(ChannelError::NONE, error_state_); | 606 DCHECK_EQ(ChannelError::NONE, error_state_); |
602 error_state_ = error_state; | 607 error_state_ = error_state; |
603 delegate_->OnError(error_state_); | 608 delegate_->OnError(error_state_); |
604 } | 609 } |
605 | 610 |
611 CastSocketImpl::CastSocketMessageDelegate::CastSocketMessageDelegate( | |
612 cast_channel::CastSocketImpl* socket) | |
613 : socket_(socket) { | |
614 DCHECK(socket_); | |
615 } | |
616 | |
617 CastSocketImpl::CastSocketMessageDelegate::~CastSocketMessageDelegate() {} | |
mark a. foltz
2017/06/20 01:16:43
Nit: prefer = default, but be consistent with surr
zhaobin
2017/06/20 17:50:05
Keep {} to be consistent with
~FakeCertVerifier()
| |
618 | |
619 // CastTransport::Delegate implementation. | |
620 void CastSocketImpl::CastSocketMessageDelegate::OnError( | |
621 cast_channel::ChannelError error_state) { | |
622 for (auto& observer : socket_->observers_) | |
623 observer.OnError(*socket_, error_state); | |
624 } | |
625 | |
626 void CastSocketImpl::CastSocketMessageDelegate::OnMessage( | |
627 const cast_channel::CastMessage& message) { | |
628 for (auto& observer : socket_->observers_) | |
629 observer.OnMessage(*socket_, message); | |
630 } | |
631 | |
632 void CastSocketImpl::CastSocketMessageDelegate::Start() {} | |
633 | |
606 } // namespace cast_channel | 634 } // namespace cast_channel |
607 #undef VLOG_WITH_CONNECTION | 635 #undef VLOG_WITH_CONNECTION |
OLD | NEW |