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