File: socket_handle_waiter_posix.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (93 lines) | stat: -rw-r--r-- 2,786 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/impl/socket_handle_waiter_posix.h"

#include <time.h>

#include <algorithm>
#include <vector>

#include "platform/base/error.h"
#include "platform/impl/socket_handle_posix.h"
#include "platform/impl/timeval_posix.h"
#include "platform/impl/udp_socket_posix.h"
#include "util/osp_logging.h"

namespace openscreen {

SocketHandleWaiterPosix::SocketHandleWaiterPosix(
    ClockNowFunctionPtr now_function)
    : SocketHandleWaiter(now_function) {}

SocketHandleWaiterPosix::~SocketHandleWaiterPosix() = default;

ErrorOr<std::vector<SocketHandleWaiterPosix::ReadyHandle>>
SocketHandleWaiterPosix::AwaitSocketsReadable(
    const std::vector<SocketHandleRef>& socket_handles,
    const Clock::duration& timeout) {
  int max_fd = -1;
  fd_set read_handles;
  fd_set write_handles;

  FD_ZERO(&read_handles);
  FD_ZERO(&write_handles);
  for (const SocketHandle& handle : socket_handles) {
    FD_SET(handle.fd, &read_handles);
    FD_SET(handle.fd, &write_handles);
    max_fd = std::max(max_fd, handle.fd);
  }
  if (max_fd < 0) {
    return Error::Code::kIOFailure;
  }

  struct timeval tv = ToTimeval(timeout);
  // This value is set to 'max_fd + 1' by convention. Also, select() is
  // level-triggered so incomplete reads/writes by the caller are fine and will
  // be picked up again on the next select() call.  For more information, see:
  // http://man7.org/linux/man-pages/man2/select.2.html
  int max_fd_to_watch = max_fd + 1;
  const int rv =
      select(max_fd_to_watch, &read_handles, &write_handles, nullptr, &tv);
  if (rv == -1) {
    // This is the case when an error condition is hit within the select(...)
    // command.
    return Error::Code::kIOFailure;
  } else if (rv == 0) {
    // This occurs when no sockets have a pending read.
    return Error::Code::kAgain;
  }

  std::vector<ReadyHandle> changed_handles;
  for (const SocketHandleRef& handle : socket_handles) {
    uint32_t flags = 0;
    if (FD_ISSET(handle.get().fd, &read_handles)) {
      flags |= Flags::kReadable;
    }
    if (FD_ISSET(handle.get().fd, &write_handles)) {
      flags |= Flags::kWriteable;
    }
    if (flags) {
      changed_handles.push_back({handle, flags});
    }
  }

  return changed_handles;
}

void SocketHandleWaiterPosix::RunUntilStopped() {
  const bool was_running = is_running_.exchange(true);
  OSP_CHECK(!was_running);

  constexpr Clock::duration kHandleReadyTimeout = std::chrono::milliseconds(50);
  while (is_running_) {
    ProcessHandles(kHandleReadyTimeout);
  }
}

void SocketHandleWaiterPosix::RequestStopSoon() {
  is_running_.store(false);
}

}  // namespace openscreen