WPILibC++ 2023.4.3-108-ge5452e3
Poll.h
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#ifndef WPINET_UV_POLL_H_
6#define WPINET_UV_POLL_H_
7
8#include <uv.h>
9
10#include <memory>
11
12#include <wpi/Signal.h>
13
14#include "wpinet/uv/Handle.h"
15
16namespace wpi::uv {
17
18class Loop;
19
20/**
21 * Poll handle.
22 */
23class Poll final : public HandleImpl<Poll, uv_poll_t> {
24 struct private_init {};
25
26 public:
27 explicit Poll(const private_init&) {}
28 ~Poll() noexcept override = default;
29
30 /**
31 * Create a poll handle using a file descriptor.
32 *
33 * @param loop Loop object where this handle runs.
34 * @param fd File descriptor.
35 */
36 static std::shared_ptr<Poll> Create(Loop& loop, int fd);
37
38 /**
39 * Create a poll handle using a file descriptor.
40 *
41 * @param loop Loop object where this handle runs.
42 * @param fd File descriptor.
43 */
44 static std::shared_ptr<Poll> Create(const std::shared_ptr<Loop>& loop,
45 int fd) {
46 return Create(*loop, fd);
47 }
48
49 /**
50 * Create a poll handle using a socket descriptor.
51 *
52 * @param loop Loop object where this handle runs.
53 * @param sock Socket descriptor.
54 */
55 static std::shared_ptr<Poll> CreateSocket(Loop& loop, uv_os_sock_t sock);
56
57 /**
58 * Create a poll handle using a socket descriptor.
59 *
60 * @param loop Loop object where this handle runs.
61 * @param sock Socket descriptor.
62 */
63 static std::shared_ptr<Poll> CreateSocket(const std::shared_ptr<Loop>& loop,
64 uv_os_sock_t sock) {
65 return CreateSocket(*loop, sock);
66 }
67
68 /**
69 * Reuse this handle. This closes the handle, and after the close completes,
70 * reinitializes it (identically to Create) and calls the provided callback.
71 * Unlike Close(), it does NOT emit the closed signal, however, IsClosing()
72 * will return true until the callback is called. This does nothing if
73 * IsClosing() is true (e.g. if Close() was called).
74 *
75 * @param fd File descriptor
76 * @param callback Callback
77 */
78 void Reuse(int fd, std::function<void()> callback);
79
80 /**
81 * Reuse this handle. This closes the handle, and after the close completes,
82 * reinitializes it (identically to CreateSocket) and calls the provided
83 * callback. Unlike Close(), it does NOT emit the closed signal, however,
84 * IsClosing() will return true until the callback is called. This does
85 * nothing if IsClosing() is true (e.g. if Close() was called).
86 *
87 * @param sock Socket descriptor.
88 * @param callback Callback
89 */
90 void ReuseSocket(uv_os_sock_t sock, std::function<void()> callback);
91
92 /**
93 * Start polling the file descriptor.
94 *
95 * @param events Bitmask of events (UV_READABLE, UV_WRITEABLE, UV_PRIORITIZED,
96 * and UV_DISCONNECT).
97 */
98 void Start(int events);
99
100 /**
101 * Stop polling the file descriptor.
102 */
104
105 /**
106 * Signal generated when a poll event occurs.
107 */
109
110 private:
111 struct ReuseData {
112 std::function<void()> callback;
113 bool isSocket;
114 int fd;
115 uv_os_sock_t sock;
116 };
117 std::unique_ptr<ReuseData> m_reuseData;
118};
119
120} // namespace wpi::uv
121
122#endif // WPINET_UV_POLL_H_
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
bool Invoke(F &&f, Args &&... args) const
Definition: Handle.h:251
Handle.
Definition: Handle.h:273
uv_poll_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:288
Event loop.
Definition: Loop.h:37
Poll handle.
Definition: Poll.h:23
sig::Signal< int > pollEvent
Signal generated when a poll event occurs.
Definition: Poll.h:108
~Poll() noexcept override=default
void Start(int events)
Start polling the file descriptor.
static std::shared_ptr< Poll > CreateSocket(Loop &loop, uv_os_sock_t sock)
Create a poll handle using a socket descriptor.
void ReuseSocket(uv_os_sock_t sock, std::function< void()> callback)
Reuse this handle.
Poll(const private_init &)
Definition: Poll.h:27
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.h:63
static std::shared_ptr< Poll > Create(Loop &loop, int fd)
Create a poll handle using a file descriptor.
void Reuse(int fd, std::function< void()> callback)
Reuse this handle.
void Stop()
Stop polling the file descriptor.
Definition: Poll.h:103
Definition: BFloat16.h:88
Definition: Buffer.h:18
int uv_os_sock_t
Definition: unix.h:119
UV_EXTERN int uv_poll_stop(uv_poll_t *handle)