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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 DCHECK(net_log_); | 129 DCHECK(net_log_); |
130 net_log_source_.type = net::NetLogSourceType::SOCKET; | 130 net_log_source_.type = net::NetLogSourceType::SOCKET; |
131 net_log_source_.id = net_log_->NextID(); | 131 net_log_source_.id = net_log_->NextID(); |
132 } | 132 } |
133 | 133 |
134 CastSocketImpl::~CastSocketImpl() { | 134 CastSocketImpl::~CastSocketImpl() { |
135 // Ensure that resources are freed but do not run pending callbacks that | 135 // Ensure that resources are freed but do not run pending callbacks that |
136 // would result in re-entrancy. | 136 // would result in re-entrancy. |
137 CloseInternal(); | 137 CloseInternal(); |
138 | 138 |
139 if (!connect_callback_.is_null()) | 139 for (auto& connect_callback : connect_callbacks_) |
140 base::ResetAndReturn(&connect_callback_).Run(ChannelError::UNKNOWN); | 140 connect_callback.Run(channel_id_, ChannelError::UNKNOWN); |
141 connect_callbacks_.clear(); | |
141 } | 142 } |
142 | 143 |
143 ReadyState CastSocketImpl::ready_state() const { | 144 ReadyState CastSocketImpl::ready_state() const { |
144 return ready_state_; | 145 return ready_state_; |
145 } | 146 } |
146 | 147 |
147 ChannelError CastSocketImpl::error_state() const { | 148 ChannelError CastSocketImpl::error_state() const { |
148 return error_state_; | 149 return error_state_; |
149 } | 150 } |
150 | 151 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 } | 234 } |
234 } | 235 } |
235 return result.success(); | 236 return result.success(); |
236 } | 237 } |
237 | 238 |
238 void CastSocketImpl::SetTransportForTesting( | 239 void CastSocketImpl::SetTransportForTesting( |
239 std::unique_ptr<CastTransport> transport) { | 240 std::unique_ptr<CastTransport> transport) { |
240 transport_ = std::move(transport); | 241 transport_ = std::move(transport); |
241 } | 242 } |
242 | 243 |
243 void CastSocketImpl::Connect(base::Callback<void(ChannelError)> callback) { | 244 void CastSocketImpl::Connect(const OnOpenCallback& callback) { |
245 switch (ready_state_) { | |
246 case ReadyState::NONE: | |
247 connect_callbacks_.push_back(callback); | |
248 Connect(); | |
249 break; | |
250 case ReadyState::CONNECTING: | |
251 connect_callbacks_.push_back(callback); | |
252 break; | |
253 case ReadyState::OPEN: | |
254 callback.Run(channel_id_, ChannelError::NONE); | |
255 break; | |
256 case ReadyState::CLOSING: | |
mark a. foltz
2017/06/21 17:41:42
It sounds like this can be removed as it's unused?
zhaobin
2017/06/21 21:58:25
Done.
| |
257 NOTREACHED(); | |
258 break; | |
259 case ReadyState::CLOSED: | |
260 callback.Run(channel_id_, ChannelError::CONNECT_ERROR); | |
261 break; | |
262 default: | |
263 NOTREACHED(); | |
mark a. foltz
2017/06/21 17:41:42
nit: << "Unknown ReadyState: " << ready_state_;
zhaobin
2017/06/21 21:58:25
Done.
| |
264 } | |
265 } | |
266 | |
267 void CastSocketImpl::Connect() { | |
244 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 268 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
245 VLOG_WITH_CONNECTION(1) << "Connect readyState = " | 269 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
246 << ::cast_channel::ReadyStateToString(ready_state_); | 270 << ::cast_channel::ReadyStateToString(ready_state_); |
247 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); | 271 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); |
248 | 272 |
249 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this); | 273 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this); |
250 | 274 |
251 if (ready_state_ != ReadyState::NONE) { | 275 DCHECK_EQ(ReadyState::NONE, ready_state_); |
252 callback.Run(ChannelError::CONNECT_ERROR); | |
253 return; | |
254 } | |
255 | 276 |
256 connect_callback_ = callback; | |
257 SetReadyState(ReadyState::CONNECTING); | 277 SetReadyState(ReadyState::CONNECTING); |
258 SetConnectState(ConnectionState::TCP_CONNECT); | 278 SetConnectState(ConnectionState::TCP_CONNECT); |
259 | 279 |
260 // Set up connection timeout. | 280 // Set up connection timeout. |
261 if (connect_timeout_.InMicroseconds() > 0) { | 281 if (connect_timeout_.InMicroseconds() > 0) { |
262 DCHECK(connect_timeout_callback_.IsCancelled()); | 282 DCHECK(connect_timeout_callback_.IsCancelled()); |
263 connect_timeout_callback_.Reset( | 283 connect_timeout_callback_.Reset( |
264 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 284 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
265 GetTimer()->Start(FROM_HERE, connect_timeout_, | 285 GetTimer()->Start(FROM_HERE, connect_timeout_, |
266 connect_timeout_callback_.callback()); | 286 connect_timeout_callback_.callback()); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
525 } | 545 } |
526 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; | 546 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; |
527 | 547 |
528 SetConnectState(ConnectionState::FINISHED); | 548 SetConnectState(ConnectionState::FINISHED); |
529 return net::OK; | 549 return net::OK; |
530 } | 550 } |
531 | 551 |
532 void CastSocketImpl::DoConnectCallback() { | 552 void CastSocketImpl::DoConnectCallback() { |
533 VLOG(1) << "DoConnectCallback (error_state = " | 553 VLOG(1) << "DoConnectCallback (error_state = " |
534 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; | 554 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; |
535 if (connect_callback_.is_null()) { | 555 if (connect_callbacks_.empty()) { |
536 DLOG(FATAL) << "Connection callback invoked multiple times."; | 556 DLOG(FATAL) << "Connection callback invoked multiple times."; |
537 return; | 557 return; |
538 } | 558 } |
539 | 559 |
540 if (error_state_ == ChannelError::NONE) { | 560 if (error_state_ == ChannelError::NONE) { |
541 SetReadyState(ReadyState::OPEN); | 561 SetReadyState(ReadyState::OPEN); |
542 if (keep_alive()) { | 562 if (keep_alive()) { |
543 auto* keep_alive_delegate = | 563 auto* keep_alive_delegate = |
544 new KeepAliveDelegate(this, logger_, std::move(delegate_), | 564 new KeepAliveDelegate(this, logger_, std::move(delegate_), |
545 ping_interval_, liveness_timeout_); | 565 ping_interval_, liveness_timeout_); |
546 delegate_.reset(keep_alive_delegate); | 566 delegate_.reset(keep_alive_delegate); |
547 } | 567 } |
548 transport_->SetReadDelegate(std::move(delegate_)); | 568 transport_->SetReadDelegate(std::move(delegate_)); |
549 } else { | 569 } else { |
550 CloseInternal(); | 570 CloseInternal(); |
551 } | 571 } |
552 | 572 |
553 base::ResetAndReturn(&connect_callback_).Run(error_state_); | 573 for (auto& connect_callback : connect_callbacks_) |
574 connect_callback.Run(channel_id_, error_state_); | |
575 connect_callbacks_.clear(); | |
554 } | 576 } |
555 | 577 |
556 void CastSocketImpl::Close(const net::CompletionCallback& callback) { | 578 void CastSocketImpl::Close(const net::CompletionCallback& callback) { |
557 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 579 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
558 CloseInternal(); | 580 CloseInternal(); |
559 // Run this callback last. It may delete the socket. | 581 // Run this callback last. It may delete the socket. |
560 callback.Run(net::OK); | 582 callback.Run(net::OK); |
561 } | 583 } |
562 | 584 |
563 void CastSocketImpl::CloseInternal() { | 585 void CastSocketImpl::CloseInternal() { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
626 void CastSocketImpl::CastSocketMessageDelegate::OnMessage( | 648 void CastSocketImpl::CastSocketMessageDelegate::OnMessage( |
627 const cast_channel::CastMessage& message) { | 649 const cast_channel::CastMessage& message) { |
628 for (auto& observer : socket_->observers_) | 650 for (auto& observer : socket_->observers_) |
629 observer.OnMessage(*socket_, message); | 651 observer.OnMessage(*socket_, message); |
630 } | 652 } |
631 | 653 |
632 void CastSocketImpl::CastSocketMessageDelegate::Start() {} | 654 void CastSocketImpl::CastSocketMessageDelegate::Start() {} |
633 | 655 |
634 } // namespace cast_channel | 656 } // namespace cast_channel |
635 #undef VLOG_WITH_CONNECTION | 657 #undef VLOG_WITH_CONNECTION |
OLD | NEW |