Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
session_broker_transport.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-only
6
7#include <algorithm>
8#include <array>
9#include <cerrno>
10#include <cstring>
11#include <limits>
12#include <poll.h>
13#include <sys/socket.h>
14#include <sys/un.h>
15#include <unistd.h>
16
17namespace platf::session_broker::detail {
18 namespace {
19 using clock = std::chrono::steady_clock;
20
21 struct owned_fd {
22 int value;
23
24 ~owned_fd() {
25 if (value >= 0) {
26 ::close(value);
27 }
28 }
29 };
30
31 int remaining(clock::time_point deadline) {
32 const auto count = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - clock::now()).count();
33 return static_cast<int>(std::clamp<decltype(count)>(count, 0, std::numeric_limits<int>::max()));
34 }
35
36 bool wait_ready(int fd, short events, clock::time_point deadline) {
37 for (;;) {
38 const auto milliseconds = remaining(deadline);
39 if (milliseconds == 0) {
40 errno = ETIMEDOUT;
41 return false;
42 }
43 pollfd descriptor {fd, events, 0};
44 const auto result = ::poll(&descriptor, 1, milliseconds);
45 if (result > 0) {
46 return (descriptor.revents & (events | POLLERR | POLLHUP | POLLNVAL)) != 0;
47 }
48 if (result == 0) {
49 errno = ETIMEDOUT;
50 return false;
51 }
52 if (errno != EINTR) {
53 return false;
54 }
55 }
56 }
57
58 bool connect_socket(int fd, const sockaddr_un &address, clock::time_point deadline) {
59 while (remaining(deadline) > 0) {
60 if (::connect(fd, reinterpret_cast<const sockaddr *>(&address), sizeof(address)) == 0 || errno == EISCONN) {
61 return true;
62 }
63 if (errno == EINTR) {
64 continue;
65 }
66 if (errno == EAGAIN || errno == EWOULDBLOCK) {
67 // AF_UNIX has not connected when its listen queue is full. POLLOUT
68 // and SO_ERROR=0 alone would incorrectly call this a connected fd.
69 ::poll(nullptr, 0, std::min(5, remaining(deadline)));
70 continue;
71 }
72 if (errno != EINPROGRESS || !wait_ready(fd, POLLOUT, deadline)) {
73 return false;
74 }
75 int error = 0;
76 socklen_t length = sizeof(error);
77 if (::getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &length) != 0) {
78 return false;
79 }
80 if (error != 0) {
81 errno = error;
82 return false;
83 }
84 return true;
85 }
86 errno = ETIMEDOUT;
87 return false;
88 }
89
90 exchange_result failure(const char *operation) {
91 return {.reply = std::nullopt, .error = std::string {operation} + ": " + std::strerror(errno)};
92 }
93 } // namespace
94
95 std::chrono::milliseconds request_timeout(std::string_view request) {
96 const auto verb = request.substr(0, request.find_first_of(" \n"));
97 if (verb == "ENSURE" || verb == "START" || verb == "STOP" || verb == "PURGE") {
98 return std::chrono::minutes(3);
99 }
100 return std::chrono::seconds(15);
101 }
102
103 exchange_result exchange(const std::string &path, const std::string &request, std::chrono::milliseconds timeout) {
104 sockaddr_un address {};
105 address.sun_family = AF_UNIX;
106 if (path.empty() || path.size() >= sizeof(address.sun_path) || path.find('\0') != std::string::npos) {
107 return {.reply = std::nullopt, .error = "invalid Unix socket path"};
108 }
109 if (request.empty() || request.size() > 64 * 1024 || request.back() != '\n' || request.find('\0') != std::string::npos) {
110 return {.reply = std::nullopt, .error = "invalid or oversized request"};
111 }
112 std::memcpy(address.sun_path, path.data(), path.size());
113 owned_fd fd {::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)};
114 if (fd.value < 0) {
115 return failure("create socket");
116 }
117 // Keep queued request data bounded per connection. Large environments
118 // stream through this buffer; nonblocking writes still share the deadline.
119 const int send_buffer = 16 * 1024;
120 ::setsockopt(fd.value, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer));
121 const auto deadline = clock::now() + timeout;
122 if (!connect_socket(fd.value, address, deadline)) {
123 return failure("connect");
124 }
125 for (std::size_t offset = 0; offset < request.size();) {
126 if (!wait_ready(fd.value, POLLOUT, deadline)) {
127 return failure("send request");
128 }
129 const auto count = ::send(fd.value, request.data() + offset, request.size() - offset, MSG_NOSIGNAL);
130 if (count < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) {
131 continue;
132 }
133 if (count <= 0) {
134 return failure("send request");
135 }
136 offset += static_cast<std::size_t>(count);
137 }
138 ::shutdown(fd.value, SHUT_WR);
139
140 constexpr std::size_t maximum_reply = 4096;
141 std::string reply;
142 std::array<char, 512> buffer {};
143 while (reply.size() < maximum_reply) {
144 if (!wait_ready(fd.value, POLLIN, deadline)) {
145 return failure("read reply");
146 }
147 const auto count = ::read(fd.value, buffer.data(), std::min(buffer.size(), maximum_reply - reply.size()));
148 if (count < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) {
149 continue;
150 }
151 if (count < 0) {
152 return failure("read reply");
153 }
154 if (count == 0) {
155 return {.reply = std::nullopt, .error = "broker closed before a complete reply"};
156 }
157 reply.append(buffer.data(), static_cast<std::size_t>(count));
158 if (const auto newline = reply.find('\n'); newline != std::string::npos) {
159 reply.resize(newline);
160 if (reply.empty() || reply.find('\0') != std::string::npos) {
161 return {.reply = std::nullopt, .error = "empty or malformed broker reply"};
162 }
163 return {.reply = std::move(reply), .error = {}};
164 }
165 }
166 return {.reply = std::nullopt, .error = "broker reply exceeds 4096 bytes"};
167 }
168} // namespace platf::session_broker::detail
@ timeout
Timeout.
Definition common.h:438
exchange_result exchange(const std::string &path, const std::string &request, std::chrono::milliseconds timeout)
Send one request and read one complete reply within a shared deadline.
std::chrono::milliseconds request_timeout(std::string_view request)
Longer budgets for serialized account/lifetime mutations, short polls otherwise.
Deadline-bounded Unix transport for the session broker client.