MCPcopy Create free account
hub / github.com/COVESA/vsomeip / async_read

Method async_read

test/common/src/external/process_unix.cpp:310–368  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

308}
309
310void Process::async_read() noexcept {
311 if (data.id <= 0 || (!stdout_fd && !stderr_fd))
312 return;
313
314 stdout_stderr_thread = std::thread([this] {
315 std::vector<pollfd> pollfds;
316 std::bitset<2> fd_is_stdout;
317 if (stdout_fd) {
318 fd_is_stdout.set(pollfds.size());
319 pollfds.emplace_back();
320 pollfds.back().fd = fcntl(*stdout_fd, F_SETFL, fcntl(*stdout_fd, F_GETFL) | O_NONBLOCK) == 0 ? *stdout_fd : -1;
321 pollfds.back().events = POLLIN;
322 }
323 if (stderr_fd) {
324 pollfds.emplace_back();
325 pollfds.back().fd = fcntl(*stderr_fd, F_SETFL, fcntl(*stderr_fd, F_GETFL) | O_NONBLOCK) == 0 ? *stderr_fd : -1;
326 pollfds.back().events = POLLIN;
327 }
328 auto buffer = std::unique_ptr<char[]>(new char[config.buffer_size]);
329 bool any_open = !pollfds.empty();
330 while (any_open && (poll(pollfds.data(), static_cast<nfds_t>(pollfds.size()), -1) > 0 || errno == EINTR)) {
331 any_open = false;
332 for (size_t i = 0; i < pollfds.size(); ++i) {
333 if (pollfds[i].fd >= 0) {
334 if (pollfds[i].revents & POLLIN) {
335 const ssize_t n = read(pollfds[i].fd, buffer.get(), config.buffer_size);
336 if (n > 0) {
337 if (fd_is_stdout[i])
338 read_stdout(buffer.get(), static_cast<size_t>(n));
339 else
340 read_stderr(buffer.get(), static_cast<size_t>(n));
341 } else if (n == 0 || (n < 0 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) {
342 if (fd_is_stdout[i]) {
343 if (config.on_stdout_close)
344 config.on_stdout_close();
345 } else {
346 if (config.on_stderr_close)
347 config.on_stderr_close();
348 }
349 pollfds[i].fd = -1;
350 continue;
351 }
352 } else if (pollfds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) {
353 if (fd_is_stdout[i]) {
354 if (config.on_stdout_close)
355 config.on_stdout_close();
356 } else {
357 if (config.on_stderr_close)
358 config.on_stderr_close();
359 }
360 pollfds[i].fd = -1;
361 continue;
362 }
363 any_open = true;
364 }
365 }
366 }
367 });

Callers

nothing calls this directly

Calls 4

setMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected