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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
22 #include "base/sys_byteorder.h" | 22 #include "base/sys_byteorder.h" |
23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
25 #include "components/cast_channel/cast_auth_util.h" | 25 #include "components/cast_channel/cast_auth_util.h" |
26 #include "components/cast_channel/cast_framer.h" | 26 #include "components/cast_channel/cast_framer.h" |
27 #include "components/cast_channel/cast_message_util.h" | 27 #include "components/cast_channel/cast_message_util.h" |
28 #include "components/cast_channel/cast_transport.h" | 28 #include "components/cast_channel/cast_transport.h" |
| 29 #include "components/cast_channel/keep_alive_delegate.h" |
29 #include "components/cast_channel/logger.h" | 30 #include "components/cast_channel/logger.h" |
30 #include "components/cast_channel/proto/cast_channel.pb.h" | 31 #include "components/cast_channel/proto/cast_channel.pb.h" |
31 #include "net/base/address_list.h" | 32 #include "net/base/address_list.h" |
32 #include "net/base/host_port_pair.h" | 33 #include "net/base/host_port_pair.h" |
33 #include "net/base/net_errors.h" | 34 #include "net/base/net_errors.h" |
34 #include "net/cert/cert_verifier.h" | 35 #include "net/cert/cert_verifier.h" |
35 #include "net/cert/cert_verify_result.h" | 36 #include "net/cert/cert_verify_result.h" |
36 #include "net/cert/ct_policy_enforcer.h" | 37 #include "net/cert/ct_policy_enforcer.h" |
37 #include "net/cert/multi_log_ct_verifier.h" | 38 #include "net/cert/multi_log_ct_verifier.h" |
38 #include "net/cert/x509_certificate.h" | 39 #include "net/cert/x509_certificate.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 verify_result->Reset(); | 81 verify_result->Reset(); |
81 verify_result->verified_cert = params.certificate(); | 82 verify_result->verified_cert = params.certificate(); |
82 return net::OK; | 83 return net::OK; |
83 } | 84 } |
84 }; | 85 }; |
85 | 86 |
86 } // namespace | 87 } // namespace |
87 | 88 |
88 CastSocketImpl::CastSocketImpl(const net::IPEndPoint& ip_endpoint, | 89 CastSocketImpl::CastSocketImpl(const net::IPEndPoint& ip_endpoint, |
89 net::NetLog* net_log, | 90 net::NetLog* net_log, |
90 const base::TimeDelta& timeout, | 91 base::TimeDelta timeout, |
91 bool keep_alive, | 92 base::TimeDelta liveness_timeout, |
| 93 base::TimeDelta ping_interval, |
92 const scoped_refptr<Logger>& logger, | 94 const scoped_refptr<Logger>& logger, |
93 uint64_t device_capabilities) | 95 uint64_t device_capabilities) |
94 : CastSocketImpl(ip_endpoint, | 96 : CastSocketImpl(ip_endpoint, |
95 net_log, | 97 net_log, |
96 timeout, | 98 timeout, |
97 keep_alive, | 99 liveness_timeout, |
| 100 ping_interval, |
98 logger, | 101 logger, |
99 device_capabilities, | 102 device_capabilities, |
100 AuthContext::Create()) {} | 103 AuthContext::Create()) {} |
101 | 104 |
102 CastSocketImpl::CastSocketImpl(const net::IPEndPoint& ip_endpoint, | 105 CastSocketImpl::CastSocketImpl(const net::IPEndPoint& ip_endpoint, |
103 net::NetLog* net_log, | 106 net::NetLog* net_log, |
104 const base::TimeDelta& timeout, | 107 base::TimeDelta timeout, |
105 bool keep_alive, | 108 base::TimeDelta liveness_timeout, |
| 109 base::TimeDelta ping_interval, |
106 const scoped_refptr<Logger>& logger, | 110 const scoped_refptr<Logger>& logger, |
107 uint64_t device_capabilities, | 111 uint64_t device_capabilities, |
108 const AuthContext& auth_context) | 112 const AuthContext& auth_context) |
109 : channel_id_(0), | 113 : channel_id_(0), |
110 ip_endpoint_(ip_endpoint), | 114 ip_endpoint_(ip_endpoint), |
111 net_log_(net_log), | 115 net_log_(net_log), |
112 keep_alive_(keep_alive), | 116 liveness_timeout_(liveness_timeout), |
| 117 ping_interval_(ping_interval), |
113 logger_(logger), | 118 logger_(logger), |
114 auth_context_(auth_context), | 119 auth_context_(auth_context), |
115 connect_timeout_(timeout), | 120 connect_timeout_(timeout), |
116 connect_timeout_timer_(new base::OneShotTimer), | 121 connect_timeout_timer_(new base::OneShotTimer), |
117 is_canceled_(false), | 122 is_canceled_(false), |
118 device_capabilities_(device_capabilities), | 123 device_capabilities_(device_capabilities), |
119 audio_only_(false), | 124 audio_only_(false), |
120 connect_state_(ConnectionState::START_CONNECT), | 125 connect_state_(ConnectionState::START_CONNECT), |
121 error_state_(ChannelError::NONE), | 126 error_state_(ChannelError::NONE), |
122 ready_state_(ReadyState::NONE), | 127 ready_state_(ReadyState::NONE), |
(...skipping 26 matching lines...) Expand all Loading... |
149 | 154 |
150 int CastSocketImpl::id() const { | 155 int CastSocketImpl::id() const { |
151 return channel_id_; | 156 return channel_id_; |
152 } | 157 } |
153 | 158 |
154 void CastSocketImpl::set_id(int id) { | 159 void CastSocketImpl::set_id(int id) { |
155 channel_id_ = id; | 160 channel_id_ = id; |
156 } | 161 } |
157 | 162 |
158 bool CastSocketImpl::keep_alive() const { | 163 bool CastSocketImpl::keep_alive() const { |
159 return keep_alive_; | 164 return liveness_timeout_ > base::TimeDelta(); |
160 } | 165 } |
161 | 166 |
162 bool CastSocketImpl::audio_only() const { | 167 bool CastSocketImpl::audio_only() const { |
163 return audio_only_; | 168 return audio_only_; |
164 } | 169 } |
165 | 170 |
166 std::unique_ptr<net::TCPClientSocket> CastSocketImpl::CreateTcpSocket() { | 171 std::unique_ptr<net::TCPClientSocket> CastSocketImpl::CreateTcpSocket() { |
167 net::AddressList addresses(ip_endpoint_); | 172 net::AddressList addresses(ip_endpoint_); |
168 return std::unique_ptr<net::TCPClientSocket>( | 173 return std::unique_ptr<net::TCPClientSocket>( |
169 new net::TCPClientSocket(addresses, nullptr, net_log_, net_log_source_)); | 174 new net::TCPClientSocket(addresses, nullptr, net_log_, net_log_source_)); |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 void CastSocketImpl::DoConnectCallback() { | 527 void CastSocketImpl::DoConnectCallback() { |
523 VLOG(1) << "DoConnectCallback (error_state = " | 528 VLOG(1) << "DoConnectCallback (error_state = " |
524 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; | 529 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; |
525 if (connect_callback_.is_null()) { | 530 if (connect_callback_.is_null()) { |
526 DLOG(FATAL) << "Connection callback invoked multiple times."; | 531 DLOG(FATAL) << "Connection callback invoked multiple times."; |
527 return; | 532 return; |
528 } | 533 } |
529 | 534 |
530 if (error_state_ == ChannelError::NONE) { | 535 if (error_state_ == ChannelError::NONE) { |
531 SetReadyState(ReadyState::OPEN); | 536 SetReadyState(ReadyState::OPEN); |
| 537 if (keep_alive()) { |
| 538 auto* keep_alive_delegate = |
| 539 new KeepAliveDelegate(this, logger_, std::move(delegate_), |
| 540 ping_interval_, liveness_timeout_); |
| 541 delegate_.reset(keep_alive_delegate); |
| 542 } |
532 transport_->SetReadDelegate(std::move(delegate_)); | 543 transport_->SetReadDelegate(std::move(delegate_)); |
533 } else { | 544 } else { |
534 CloseInternal(); | 545 CloseInternal(); |
535 } | 546 } |
536 | 547 |
537 base::ResetAndReturn(&connect_callback_).Run(error_state_); | 548 base::ResetAndReturn(&connect_callback_).Run(error_state_); |
538 } | 549 } |
539 | 550 |
540 void CastSocketImpl::Close(const net::CompletionCallback& callback) { | 551 void CastSocketImpl::Close(const net::CompletionCallback& callback) { |
541 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 552 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 598 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
588 VLOG_WITH_CONNECTION(1) << "SetErrorState " | 599 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
589 << ::cast_channel::ChannelErrorToString(error_state); | 600 << ::cast_channel::ChannelErrorToString(error_state); |
590 DCHECK_EQ(ChannelError::NONE, error_state_); | 601 DCHECK_EQ(ChannelError::NONE, error_state_); |
591 error_state_ = error_state; | 602 error_state_ = error_state; |
592 delegate_->OnError(error_state_); | 603 delegate_->OnError(error_state_); |
593 } | 604 } |
594 | 605 |
595 } // namespace cast_channel | 606 } // namespace cast_channel |
596 #undef VLOG_WITH_CONNECTION | 607 #undef VLOG_WITH_CONNECTION |
OLD | NEW |