| Index: components/cast_channel/cast_socket_service.cc
|
| diff --git a/components/cast_channel/cast_socket_service.cc b/components/cast_channel/cast_socket_service.cc
|
| index 5da94186046d554d355e2ac58d9ec0173cf5058f..26651140fd80206e6ea2ccde2ba9354179cd5d5d 100644
|
| --- a/components/cast_channel/cast_socket_service.cc
|
| +++ b/components/cast_channel/cast_socket_service.cc
|
| @@ -5,17 +5,33 @@
|
| #include "components/cast_channel/cast_socket_service.h"
|
|
|
| #include "base/memory/ptr_util.h"
|
| +#include "components/cast_channel/cast_socket.h"
|
| +#include "components/cast_channel/logger.h"
|
| #include "content/public/browser/browser_thread.h"
|
|
|
| using content::BrowserThread;
|
|
|
| +namespace {
|
| +// Connect timeout for connect calls.
|
| +const int kConnectTimeoutSecs = 10;
|
| +
|
| +// Ping interval
|
| +const int kPingIntervalInSecs = 5;
|
| +
|
| +// Liveness timeout for connect calls, in milliseconds. If no message is
|
| +// received from the receiver during kConnectLivenessTimeoutSecs, it is
|
| +// considered gone.
|
| +const int kConnectLivenessTimeoutSecs = kPingIntervalInSecs * 2;
|
| +} // namespace
|
| +
|
| namespace cast_channel {
|
|
|
| int CastSocketService::last_channel_id_ = 0;
|
|
|
| CastSocketService::CastSocketService()
|
| : RefcountedKeyedService(
|
| - BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)) {
|
| + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)),
|
| + logger_(new Logger()) {
|
| DETACH_FROM_THREAD(thread_checker_);
|
| }
|
|
|
| @@ -23,13 +39,15 @@ CastSocketService::~CastSocketService() {
|
| DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
| }
|
|
|
| -int CastSocketService::AddSocket(std::unique_ptr<CastSocket> socket) {
|
| +CastSocket* CastSocketService::AddSocket(std::unique_ptr<CastSocket> socket) {
|
| DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
| DCHECK(socket);
|
| int id = ++last_channel_id_;
|
| socket->set_id(id);
|
| +
|
| + auto* socket_ptr = socket.get();
|
| sockets_.insert(std::make_pair(id, std::move(socket)));
|
| - return id;
|
| + return socket_ptr;
|
| }
|
|
|
| std::unique_ptr<CastSocket> CastSocketService::RemoveSocket(int channel_id) {
|
| @@ -52,6 +70,61 @@ CastSocket* CastSocketService::GetSocket(int channel_id) const {
|
| return socket_it == sockets_.end() ? nullptr : socket_it->second.get();
|
| }
|
|
|
| +CastSocket* CastSocketService::GetSocket(
|
| + const net::IPEndPoint& ip_endpoint) const {
|
| + DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
| + auto it = std::find_if(
|
| + sockets_.begin(), sockets_.end(),
|
| + [&ip_endpoint](
|
| + const std::pair<const int, std::unique_ptr<CastSocket>>& pair) {
|
| + return pair.second->ip_endpoint() == ip_endpoint;
|
| + });
|
| + return it == sockets_.end() ? nullptr : it->second.get();
|
| +}
|
| +
|
| +int CastSocketService::OpenSocket(const net::IPEndPoint& ip_endpoint,
|
| + net::NetLog* net_log,
|
| + const base::TimeDelta& connect_timeout,
|
| + const base::TimeDelta& liveness_timeout,
|
| + const base::TimeDelta& ping_interval,
|
| + const scoped_refptr<Logger>& logger,
|
| + uint64_t device_capabilities,
|
| + const CastSocket::OnOpenCallback& open_cb,
|
| + CastSocket::Observer* observer) {
|
| + DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
| + DCHECK(observer);
|
| + auto* socket = GetSocket(ip_endpoint);
|
| +
|
| + if (!socket) {
|
| + // If cast socket does not exist.
|
| + if (socket_for_test_) {
|
| + socket = AddSocket(std::move(socket_for_test_));
|
| + } else {
|
| + socket = new CastSocketImpl(ip_endpoint, net_log, connect_timeout,
|
| + liveness_timeout, ping_interval, logger,
|
| + device_capabilities);
|
| + AddSocket(base::WrapUnique(socket));
|
| + }
|
| + socket->AddObserver(observer);
|
| + }
|
| +
|
| + socket->Connect(open_cb);
|
| + return socket->id();
|
| +}
|
| +
|
| +int CastSocketService::OpenSocket(const net::IPEndPoint& ip_endpoint,
|
| + net::NetLog* net_log,
|
| + const CastSocket::OnOpenCallback& open_cb,
|
| + CastSocket::Observer* observer) {
|
| + auto connect_timeout = base::TimeDelta::FromSeconds(kConnectTimeoutSecs);
|
| + auto ping_interval = base::TimeDelta::FromSeconds(kPingIntervalInSecs);
|
| + auto liveness_timeout =
|
| + base::TimeDelta::FromSeconds(kConnectLivenessTimeoutSecs);
|
| + return OpenSocket(ip_endpoint, net_log, connect_timeout, liveness_timeout,
|
| + ping_interval, logger_, CastDeviceCapability::NONE, open_cb,
|
| + observer);
|
| +}
|
| +
|
| CastSocket::Observer* CastSocketService::GetObserver(const std::string& id) {
|
| auto it = socket_observer_map_.find(id);
|
| return it == socket_observer_map_.end() ? nullptr : it->second.get();
|
| @@ -65,6 +138,11 @@ CastSocket::Observer* CastSocketService::AddObserver(
|
| return observer_ptr;
|
| }
|
|
|
| +void CastSocketService::SetSocketForTest(
|
| + std::unique_ptr<cast_channel::CastSocket> socket_for_test) {
|
| + socket_for_test_ = std::move(socket_for_test);
|
| +}
|
| +
|
| void CastSocketService::ShutdownOnUIThread() {}
|
|
|
| } // namespace cast_channel
|
|
|