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-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
34
35
36namespace sf
37{
43{
44public:
49 enum class Status
50 {
51 Done,
52 NotReady,
53 Partial,
54 Disconnected,
55 Error
56 };
57
62 // NOLINTNEXTLINE(readability-identifier-naming)
63 static constexpr unsigned short AnyPort{0};
64
69 virtual ~Socket();
70
75 Socket(const Socket&) = delete;
76
81 Socket& operator=(const Socket&) = delete;
82
87 Socket(Socket&& socket) noexcept;
88
93 Socket& operator=(Socket&& socket) noexcept;
94
112 void setBlocking(bool blocking);
113
122 [[nodiscard]] bool isBlocking() const;
123
124protected:
129 enum class Type
130 {
131 Tcp,
132 Udp
133 };
134
143 explicit Socket(Type type);
144
155 [[nodiscard]] SocketHandle getNativeHandle() const;
156
166
176 void create(SocketHandle handle);
177
184 void close();
185
186private:
187 friend class SocketSelector;
188
190 // Member data
192 Type m_type;
193 SocketHandle m_socket;
194 bool m_isBlocking{true};
195};
196
197} // namespace sf
198
199
#define SFML_NETWORK_API
Type
Type of IP address.
Definition IpAddress.hpp:58
@ IpV4
IPv4 address.
Definition IpAddress.hpp:59
void setBlocking(bool blocking)
Set the blocking state of the socket.
static constexpr unsigned short AnyPort
Some special values used by sockets.
Definition Socket.hpp:63
Socket & operator=(const Socket &)=delete
Deleted copy assignment.
friend class SocketSelector
Definition Socket.hpp:187
Socket(Socket &&socket) noexcept
Move constructor.
Status
Status codes that may be returned by socket functions.
Definition Socket.hpp:50
void create(IpAddress::Type addressType=IpAddress::Type::IpV4)
Create the internal representation of the socket.
Type
Types of protocols that the socket can use.
Definition Socket.hpp:130
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.
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