1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "platform/impl/platform_client_posix.h"
6
7 #include <functional>
8 #include <utility>
9 #include <vector>
10
11 #include "platform/impl/udp_socket_reader_posix.h"
12
13 namespace openscreen {
14
15 // static
16 PlatformClientPosix* PlatformClientPosix::instance_ = nullptr;
17
18 // static
Create(Clock::duration networking_operation_timeout,std::unique_ptr<TaskRunnerImpl> task_runner)19 void PlatformClientPosix::Create(Clock::duration networking_operation_timeout,
20 std::unique_ptr<TaskRunnerImpl> task_runner) {
21 SetInstance(new PlatformClientPosix(networking_operation_timeout,
22 std::move(task_runner)));
23 }
24
25 // static
Create(Clock::duration networking_operation_timeout)26 void PlatformClientPosix::Create(Clock::duration networking_operation_timeout) {
27 SetInstance(new PlatformClientPosix(networking_operation_timeout));
28 }
29
30 // static
ShutDown()31 void PlatformClientPosix::ShutDown() {
32 OSP_DCHECK(instance_);
33 delete instance_;
34 instance_ = nullptr;
35 }
36
tls_data_router()37 TlsDataRouterPosix* PlatformClientPosix::tls_data_router() {
38 std::call_once(tls_data_router_initialization_, [this]() {
39 tls_data_router_ =
40 std::make_unique<TlsDataRouterPosix>(socket_handle_waiter());
41 tls_data_router_created_.store(true);
42 });
43 return tls_data_router_.get();
44 }
45
udp_socket_reader()46 UdpSocketReaderPosix* PlatformClientPosix::udp_socket_reader() {
47 std::call_once(udp_socket_reader_initialization_, [this]() {
48 udp_socket_reader_ =
49 std::make_unique<UdpSocketReaderPosix>(socket_handle_waiter());
50 });
51 return udp_socket_reader_.get();
52 }
53
GetTaskRunner()54 TaskRunner* PlatformClientPosix::GetTaskRunner() {
55 return task_runner_.get();
56 }
57
~PlatformClientPosix()58 PlatformClientPosix::~PlatformClientPosix() {
59 OSP_DVLOG << "Shutting down the Task Runner...";
60 task_runner_->RequestStopSoon();
61 if (task_runner_thread_ && task_runner_thread_->joinable()) {
62 task_runner_thread_->join();
63 OSP_DVLOG << "\tTask Runner shutdown complete!";
64 }
65
66 OSP_DVLOG << "Shutting down network operations...";
67 networking_loop_running_.store(false);
68 networking_loop_thread_.join();
69 OSP_DVLOG << "\tNetwork operation shutdown complete!";
70 }
71
72 // static
SetInstance(PlatformClientPosix * instance)73 void PlatformClientPosix::SetInstance(PlatformClientPosix* instance) {
74 OSP_DCHECK(!instance_);
75 instance_ = instance;
76 }
77
PlatformClientPosix(Clock::duration networking_operation_timeout)78 PlatformClientPosix::PlatformClientPosix(
79 Clock::duration networking_operation_timeout)
80 : task_runner_(new TaskRunnerImpl(Clock::now)),
81 networking_loop_timeout_(networking_operation_timeout),
82 networking_loop_thread_(&PlatformClientPosix::RunNetworkLoopUntilStopped,
83 this),
84 task_runner_thread_(
85 std::thread(&TaskRunnerImpl::RunUntilStopped, task_runner_.get())) {}
86
PlatformClientPosix(Clock::duration networking_operation_timeout,std::unique_ptr<TaskRunnerImpl> task_runner)87 PlatformClientPosix::PlatformClientPosix(
88 Clock::duration networking_operation_timeout,
89 std::unique_ptr<TaskRunnerImpl> task_runner)
90 : task_runner_(std::move(task_runner)),
91 networking_loop_timeout_(networking_operation_timeout),
92 networking_loop_thread_(&PlatformClientPosix::RunNetworkLoopUntilStopped,
93 this) {}
94
socket_handle_waiter()95 SocketHandleWaiterPosix* PlatformClientPosix::socket_handle_waiter() {
96 std::call_once(waiter_initialization_, [this]() {
97 waiter_ = std::make_unique<SocketHandleWaiterPosix>(&Clock::now);
98 waiter_created_.store(true);
99 });
100 return waiter_.get();
101 }
102
RunNetworkLoopUntilStopped()103 void PlatformClientPosix::RunNetworkLoopUntilStopped() {
104 while (networking_loop_running_.load()) {
105 if (!waiter_created_.load()) {
106 std::this_thread::sleep_for(networking_loop_timeout_);
107 continue;
108 }
109 socket_handle_waiter()->ProcessHandles(networking_loop_timeout_);
110 }
111 }
112
113 } // namespace openscreen
114