WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Poll.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9
10#include <uv.h>
11
12#include "wpi/net/uv/Handle.hpp"
13#include "wpi/util/Signal.h"
14
15namespace wpi::net::uv {
16
17class Loop;
18
19/**
20 * Poll handle.
21 */
22class Poll final : public HandleImpl<Poll, uv_poll_t> {
23 struct private_init {};
24
25 public:
26 explicit Poll(const private_init&) {}
27 ~Poll() noexcept override = default;
28
29 /**
30 * Create a poll handle using a file descriptor.
31 *
32 * @param loop Loop object where this handle runs.
33 * @param fd File descriptor.
34 */
35 static std::shared_ptr<Poll> Create(Loop& loop, int fd);
36
37 /**
38 * Create a poll handle using a file descriptor.
39 *
40 * @param loop Loop object where this handle runs.
41 * @param fd File descriptor.
42 */
43 static std::shared_ptr<Poll> Create(const std::shared_ptr<Loop>& loop,
44 int fd) {
45 return Create(*loop, fd);
46 }
47
48 /**
49 * Create a poll handle using a socket descriptor.
50 *
51 * @param loop Loop object where this handle runs.
52 * @param sock Socket descriptor.
53 */
54 static std::shared_ptr<Poll> CreateSocket(Loop& loop, uv_os_sock_t sock);
55
56 /**
57 * Create a poll handle using a socket descriptor.
58 *
59 * @param loop Loop object where this handle runs.
60 * @param sock Socket descriptor.
61 */
62 static std::shared_ptr<Poll> CreateSocket(const std::shared_ptr<Loop>& loop,
63 uv_os_sock_t sock) {
64 return CreateSocket(*loop, sock);
65 }
66
67 /**
68 * Reuse this handle. This closes the handle, and after the close completes,
69 * reinitializes it (identically to Create) and calls the provided callback.
70 * Unlike Close(), it does NOT emit the closed signal, however, IsClosing()
71 * will return true until the callback is called. This does nothing if
72 * IsClosing() is true (e.g. if Close() was called).
73 *
74 * @param fd File descriptor
75 * @param callback Callback
76 */
77 void Reuse(int fd, std::function<void()> callback);
78
79 /**
80 * Reuse this handle. This closes the handle, and after the close completes,
81 * reinitializes it (identically to CreateSocket) and calls the provided
82 * callback. Unlike Close(), it does NOT emit the closed signal, however,
83 * IsClosing() will return true until the callback is called. This does
84 * nothing if IsClosing() is true (e.g. if Close() was called).
85 *
86 * @param sock Socket descriptor.
87 * @param callback Callback
88 */
89 void ReuseSocket(uv_os_sock_t sock, std::function<void()> callback);
90
91 /**
92 * Start polling the file descriptor.
93 *
94 * @param events Bitmask of events (UV_READABLE, UV_WRITEABLE, UV_PRIORITIZED,
95 * and UV_DISCONNECT).
96 */
97 void Start(int events);
98
99 /**
100 * Stop polling the file descriptor.
101 */
103
104 /**
105 * Signal generated when a poll event occurs.
106 */
108
109 private:
110 struct ReuseData {
111 std::function<void()> callback;
112 bool isSocket;
113 int fd;
114 uv_os_sock_t sock;
115 };
116 std::unique_ptr<ReuseData> m_reuseData;
117};
118
119} // namespace wpi::net::uv
bool Invoke(F &&f, Args &&... args) const
Definition Handle.hpp:265
uv_poll_t * GetRaw() const noexcept
Definition Handle.hpp:303
HandleImpl()
Definition Handle.hpp:308
Event loop.
Definition Loop.hpp:35
void Reuse(int fd, std::function< void()> callback)
Reuse this handle.
~Poll() noexcept override=default
void Start(int events)
Start polling the file descriptor.
static std::shared_ptr< Poll > Create(Loop &loop, int fd)
Create a poll handle using a file descriptor.
Poll(const private_init &)
Definition Poll.hpp:26
void ReuseSocket(uv_os_sock_t sock, std::function< void()> callback)
Reuse this handle.
static std::shared_ptr< Poll > CreateSocket(const std::shared_ptr< Loop > &loop, uv_os_sock_t sock)
Create a poll handle using a socket descriptor.
Definition Poll.hpp:62
void Stop()
Stop polling the file descriptor.
Definition Poll.hpp:102
static std::shared_ptr< Poll > CreateSocket(Loop &loop, uv_os_sock_t sock)
Create a poll handle using a socket descriptor.
wpi::util::sig::Signal< int > pollEvent
Signal generated when a poll event occurs.
Definition Poll.hpp:107
Definition StringMap.hpp:773
Definition Prepare.hpp:14
SignalBase< detail::NullMutex, T... > Signal
Specialization of SignalBase to be used in single threaded contexts.
Definition Signal.h:809
UV_EXTERN int uv_poll_stop(uv_poll_t *handle)