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

Unified Diff: components/cast_channel/cast_socket_service.cc

Issue 2925053005: [cast_channel] Implement CastSocketService::OpenSocket() (Closed)
Patch Set: rebase Created 3 years, 6 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 side-by-side diff with in-line comments
Download patch
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 fc43c5daaa40e00bc12ef91bebcd258017edc18c..a2b8e5e844c8c7d4d6da6684659e388dec9d0458 100644
--- a/components/cast_channel/cast_socket_service.cc
+++ b/components/cast_channel/cast_socket_service.cc
@@ -5,17 +5,32 @@
#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 LIVENESS_INTERVAL, it is considered gone.
mark a. foltz 2017/06/21 17:41:42 Where is LIVENESS_INTERVAL defined? Or should it
zhaobin 2017/06/21 21:58:25 Done.
+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_);
}
@@ -52,6 +67,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 = socket_for_test_.release();
mark a. foltz 2017/06/21 17:41:42 Nit: Slightly prefer to call AddSocket(std::move(s
zhaobin 2017/06/21 21:58:25 Done.
+ } 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 = std::find_if(
socket_observer_map_.begin(), socket_observer_map_.end(),
@@ -68,6 +138,11 @@ void CastSocketService::AddObserver(
socket_observer_map_.insert(std::make_pair(id, std::move(observer)));
}
+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

Powered by Google App Engine
This is Rietveld 408576698