Debian Codegraph @

src/TCPSocket.cpp

raw
1 /*
2 * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * See LICENSE.md for more information.
6 */
7
8 #include "TCPSocket.h"
9
10 #include <chrono>
11
12 namespace
13 {
14
15 uint64_t MillisecondsSinceEpoch()
16 {
17 const auto duration = std::chrono::system_clock::now().time_since_epoch();
18 return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
19 }
20
21 } // unnamed namespace
22
23 using namespace vdrvnsi;
24
25 TCPSocket::TCPSocket(const std::string& host, uint16_t port) : m_endpoint(host, port)
26 {
27 }
28
29 TCPSocket::~TCPSocket()
30 {
31 Close();
32 }
33
34 bool TCPSocket::Open(uint64_t iTimeoutMs /*= 0*/)
35 {
36 try
37 {
38 auto socket = GetSocket(true);
39
40 int status = socket->connect(iTimeoutMs).value; // connect non-blocking
41
42 if (status == kissnet::socket_status::valid)
43 socket->set_tcp_no_delay(true);
44
45 return status == kissnet::socket_status::valid;
46 }
47 catch (std::runtime_error const&)
48 {
49 ResetSocket();
50 return false;
51 }
52 }
53
54 void TCPSocket::Shutdown()
55 {
56 auto socket = GetSocket();
57 if (!socket)
58 return;
59
60 try
61 {
62 socket->shutdown();
63 }
64 catch (std::runtime_error const&)
65 {
66 }
67 }
68
69 void TCPSocket::Close()
70 {
71 auto socket = GetSocket();
72 if (!socket)
73 return;
74
75 try
76 {
77 socket->close();
78 ResetSocket();
79 }
80 catch (std::runtime_error const&)
81 {
82 }
83 }
84
85 int64_t TCPSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /*= 0*/)
86 {
87 auto socket = GetSocket();
88 if (!socket)
89 return -1;
90
91 try
92 {
93 uint64_t iNow = 0;
94 uint64_t iTarget = 0;
95 int64_t iBytesRead = 0;
96
97 if (iTimeoutMs > 0)
98 {
99 iNow = MillisecondsSinceEpoch();
100 iTarget = iNow + iTimeoutMs;
101 }
102
103 while (iBytesRead >= 0 && iBytesRead < static_cast<int64_t>(len) &&
104 (iTimeoutMs == 0 || iTarget > iNow))
105 {
106 if (iTimeoutMs > 0)
107 {
108 const kissnet::socket_status status = socket->select(kissnet::fds_read, iTimeoutMs);
109
110 if (status.value == kissnet::socket_status::errored)
111 {
112 m_lastSocketError = SocketError::Errored;
113 return -1;
114 }
115 else if (status.value == kissnet::socket_status::timed_out)
116 {
117 m_lastSocketError = SocketError::TimeOut;
118 return -1;
119 }
120 }
121
122 const auto [iReadResult, status] =
123 (iTimeoutMs > 0) ? socket->recv(static_cast<std::byte*>(data) + iBytesRead,
124 len - iBytesRead, false /* no wait */)
125 : socket->recv(static_cast<std::byte*>(data), len, true /* wait */);
126
127 if (iTimeoutMs > 0)
128 iNow = MillisecondsSinceEpoch();
129
130 if (iReadResult < 0)
131 {
132 if (iTimeoutMs > 0 &&
133 status.value == kissnet::socket_status::non_blocking_would_have_blocked)
134 continue; // again
135
136 return (iBytesRead > 0) ? iBytesRead : -1;
137 }
138 else if (iReadResult == 0 || (iReadResult != static_cast<int64_t>(len) && iTimeoutMs == 0))
139 {
140 m_lastSocketError = SocketError::ReadFailure;
141 return -1;
142 }
143
144 iBytesRead += iReadResult;
145 }
146
147 return iBytesRead;
148 }
149 catch (std::runtime_error const&)
150 {
151 m_lastSocketError = SocketError::Errored;
152 return -1;
153 }
154 }
155
156 int64_t TCPSocket::Write(void* data, size_t len)
157 {
158 auto socket = GetSocket();
159 if (!socket)
160 return -1;
161
162 try
163 {
164 const auto [data_size, status] = socket->send(static_cast<std::byte*>(data), len);
165 return data_size;
166 }
167 catch (std::runtime_error const&)
168 {
169 return -1;
170 }
171 }
172
173 std::shared_ptr<kissnet::tcp_socket> TCPSocket::GetSocket(bool bCreate /*= false*/)
174 {
175 std::lock_guard<std::recursive_mutex> lock(m_mutex);
176
177 if (bCreate && !m_socket)
178 m_socket.reset(new kissnet::tcp_socket(m_endpoint));
179
180 return m_socket;
181 }
182
183 void TCPSocket::ResetSocket()
184 {
185 std::lock_guard<std::recursive_mutex> lock(m_mutex);
186 m_socket.reset();
187 }