Loading...
Searching...
No Matches
Socket.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2024 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
33
34
35namespace sf
36{
42{
43public:
48 enum class Status
49 {
50 Done,
51 NotReady,
52 Partial,
53 Disconnected,
54 Error
55 };
56
61 // NOLINTNEXTLINE(readability-identifier-naming)
62 static constexpr unsigned short AnyPort{0};
63
68 virtual ~Socket();
69
74 Socket(const Socket&) = delete;
75
80 Socket& operator=(const Socket&) = delete;
81
86 Socket(Socket&& socket) noexcept;
87
92 Socket& operator=(Socket&& socket) noexcept;
93
111 void setBlocking(bool blocking);
112
121 [[nodiscard]] bool isBlocking() const;
122
123protected:
128 enum class Type
129 {
130 Tcp,
131 Udp
132 };
133
142 explicit Socket(Type type);
143
154 [[nodiscard]] SocketHandle getNativeHandle() const;
155
162 void create();
163
173 void create(SocketHandle handle);
174
181 void close();
182
183private:
184 friend class SocketSelector;
185
187 // Member data
189 Type m_type;
190 SocketHandle m_socket;
191 bool m_isBlocking{true};
192};
193
194} // namespace sf
195
196
#define SFML_NETWORK_API
Multiplexer that allows to read from multiple sockets.
Base class for all the socket types.
Definition Socket.hpp:42
void setBlocking(bool blocking)
Set the blocking state of the socket.
Socket & operator=(const Socket &)=delete
Deleted copy assignment.
Socket(Socket &&socket) noexcept
Move constructor.
Status
Status codes that may be returned by socket functions.
Definition Socket.hpp:49
Type
Types of protocols that the socket can use.
Definition Socket.hpp:129
SocketHandle getNativeHandle() const
Return the internal handle of the socket.
void close()
Close the socket gracefully.
Socket & operator=(Socket &&socket) noexcept
Move assignment.
virtual ~Socket()
Destructor.
Socket(Type type)
Default constructor.
Socket(const Socket &)=delete
Deleted copy constructor.
void create()
Create the internal representation of the socket.
bool isBlocking() const
Tell whether the socket is in blocking or non-blocking mode.
void create(SocketHandle handle)
Create the internal representation of the socket from a socket handle.
int SocketHandle