Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: components/cast_channel/cast_socket.cc

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: move |logger_| from cast_channel_api to cast_socket_service Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/cast_channel/cast_socket.h ('k') | components/cast_channel/cast_socket_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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::CLOSED:
257 callback.Run(channel_id_, ChannelError::CONNECT_ERROR);
258 break;
259 default:
260 NOTREACHED() << "Unknown ReadyState: "
261 << ReadyStateToString(ready_state_);
262 }
263 }
264
265 void CastSocketImpl::Connect() {
244 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 266 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
245 VLOG_WITH_CONNECTION(1) << "Connect readyState = " 267 VLOG_WITH_CONNECTION(1) << "Connect readyState = "
246 << ReadyStateToString(ready_state_); 268 << ReadyStateToString(ready_state_);
269 DCHECK_EQ(ReadyState::NONE, ready_state_);
247 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_); 270 DCHECK_EQ(ConnectionState::START_CONNECT, connect_state_);
248 271
249 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this); 272 delegate_ = base::MakeUnique<CastSocketMessageDelegate>(this);
250 273
251 if (ready_state_ != ReadyState::NONE) {
252 callback.Run(ChannelError::CONNECT_ERROR);
253 return;
254 }
255
256 connect_callback_ = callback;
257 SetReadyState(ReadyState::CONNECTING); 274 SetReadyState(ReadyState::CONNECTING);
258 SetConnectState(ConnectionState::TCP_CONNECT); 275 SetConnectState(ConnectionState::TCP_CONNECT);
259 276
260 // Set up connection timeout. 277 // Set up connection timeout.
261 if (connect_timeout_.InMicroseconds() > 0) { 278 if (connect_timeout_.InMicroseconds() > 0) {
262 DCHECK(connect_timeout_callback_.IsCancelled()); 279 DCHECK(connect_timeout_callback_.IsCancelled());
263 connect_timeout_callback_.Reset( 280 connect_timeout_callback_.Reset(
264 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); 281 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this)));
265 GetTimer()->Start(FROM_HERE, connect_timeout_, 282 GetTimer()->Start(FROM_HERE, connect_timeout_,
266 connect_timeout_callback_.callback()); 283 connect_timeout_callback_.callback());
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 } 542 }
526 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; 543 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded";
527 544
528 SetConnectState(ConnectionState::FINISHED); 545 SetConnectState(ConnectionState::FINISHED);
529 return net::OK; 546 return net::OK;
530 } 547 }
531 548
532 void CastSocketImpl::DoConnectCallback() { 549 void CastSocketImpl::DoConnectCallback() {
533 VLOG(1) << "DoConnectCallback (error_state = " 550 VLOG(1) << "DoConnectCallback (error_state = "
534 << ChannelErrorToString(error_state_) << ")"; 551 << ChannelErrorToString(error_state_) << ")";
535 if (connect_callback_.is_null()) { 552 if (connect_callbacks_.empty()) {
536 DLOG(FATAL) << "Connection callback invoked multiple times."; 553 DLOG(FATAL) << "Connection callback invoked multiple times.";
537 return; 554 return;
538 } 555 }
539 556
540 if (error_state_ == ChannelError::NONE) { 557 if (error_state_ == ChannelError::NONE) {
541 SetReadyState(ReadyState::OPEN); 558 SetReadyState(ReadyState::OPEN);
542 if (keep_alive()) { 559 if (keep_alive()) {
543 auto* keep_alive_delegate = 560 auto* keep_alive_delegate =
544 new KeepAliveDelegate(this, logger_, std::move(delegate_), 561 new KeepAliveDelegate(this, logger_, std::move(delegate_),
545 ping_interval_, liveness_timeout_); 562 ping_interval_, liveness_timeout_);
546 delegate_.reset(keep_alive_delegate); 563 delegate_.reset(keep_alive_delegate);
547 } 564 }
548 transport_->SetReadDelegate(std::move(delegate_)); 565 transport_->SetReadDelegate(std::move(delegate_));
549 } else { 566 } else {
550 CloseInternal(); 567 CloseInternal();
551 } 568 }
552 569
553 base::ResetAndReturn(&connect_callback_).Run(error_state_); 570 for (auto& connect_callback : connect_callbacks_)
571 connect_callback.Run(channel_id_, error_state_);
572 connect_callbacks_.clear();
554 } 573 }
555 574
556 void CastSocketImpl::Close(const net::CompletionCallback& callback) { 575 void CastSocketImpl::Close(const net::CompletionCallback& callback) {
557 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); 576 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
558 CloseInternal(); 577 CloseInternal();
559 // Run this callback last. It may delete the socket. 578 // Run this callback last. It may delete the socket.
560 callback.Run(net::OK); 579 callback.Run(net::OK);
561 } 580 }
562 581
563 void CastSocketImpl::CloseInternal() { 582 void CastSocketImpl::CloseInternal() {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 void CastSocketImpl::CastSocketMessageDelegate::OnMessage( 645 void CastSocketImpl::CastSocketMessageDelegate::OnMessage(
627 const CastMessage& message) { 646 const CastMessage& message) {
628 for (auto& observer : socket_->observers_) 647 for (auto& observer : socket_->observers_)
629 observer.OnMessage(*socket_, message); 648 observer.OnMessage(*socket_, message);
630 } 649 }
631 650
632 void CastSocketImpl::CastSocketMessageDelegate::Start() {} 651 void CastSocketImpl::CastSocketMessageDelegate::Start() {}
633 652
634 } // namespace cast_channel 653 } // namespace cast_channel
635 #undef VLOG_WITH_CONNECTION 654 #undef VLOG_WITH_CONNECTION
OLDNEW
« no previous file with comments | « components/cast_channel/cast_socket.h ('k') | components/cast_channel/cast_socket_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698