Loading...
Searching...
No Matches
SocketSelector.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
32#include <SFML/System/Time.hpp>
33
34#include <functional>
35#include <memory>
36
37#include <cstdint>
38
39
40namespace sf
41{
42class Socket;
43
49{
50public:
51 using ReadinessType = std::uint32_t;
52
57 enum : ReadinessType
58 {
59 Receive = 1 << 0,
60 Send = 1 << 1,
61 };
62
68
74
82
92
98
103 SocketSelector& operator=(SocketSelector&&) noexcept;
104
164 bool add(const Socket& socket,
165 ReadinessType readinessType = Receive,
166 std::function<void(ReadinessType readinessType)> readyCallback = {});
167
181 bool remove(const Socket& socket);
182
193 void clear();
194
212 [[nodiscard]] bool wait(Time timeout = Time::Zero);
213
232 [[nodiscard]] bool isReady(const Socket& socket, ReadinessType readinessType = Receive) const;
233
253
254private:
255 struct SocketSelectorImpl;
256
258 // Member data
260 std::unique_ptr<SocketSelectorImpl> m_impl;
261};
262
263} // namespace sf
264
265
#define SFML_NETWORK_API
std::uint32_t ReadinessType
Bitwise combination of readiness types.
SocketSelector(const SocketSelector &copy)
Copy constructor.
@ Send
Check if sockets are ready to be sent to.
@ Receive
Check if sockets are ready to be received from.
bool isReady(const Socket &socket, ReadinessType readinessType=Receive) const
Test a socket to know if it is ready to receive or send data.
SocketSelector()
Default constructor.
bool remove(const Socket &socket)
Remove a socket from the selector.
void clear()
Remove all the sockets stored in the selector.
SocketSelector(SocketSelector &&) noexcept
Move constructor.
~SocketSelector()
Destructor.
bool wait(Time timeout=Time::Zero)
Wait until one or more sockets are ready to receive or send.
void dispatchReadyCallbacks()
Dispatch callbacks of ready sockets.
bool add(const Socket &socket, ReadinessType readinessType=Receive, std::function< void(ReadinessType readinessType)> readyCallback={})
Add a new socket to the selector.
SocketSelector & operator=(const SocketSelector &right)
Overload of assignment operator.
Base class for all the socket types.
Definition Socket.hpp:43
Represents a time value.
Definition Time.hpp:42
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110