SFML 3.0.2
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-2025 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
30#include <SFML/Network/Export.hpp>
31
32#include <SFML/Network/SocketHandle.hpp>
33
34
35 namespace sf
36{
41 class SFML_NETWORK_API Socket
42{
43 public:
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
123 protected:
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
183 private:
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
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
SocketHandle.hpp
sf::Socket::setBlocking
void setBlocking(bool blocking)
Set the blocking state of the socket.
sf::Socket::AnyPort
static constexpr unsigned short AnyPort
Some special values used by sockets.
Definition Socket.hpp:62
sf::Socket::operator=
Socket & operator=(const Socket &)=delete
Deleted copy assignment.
sf::Socket::SocketSelector
friend class SocketSelector
Definition Socket.hpp:184
sf::Socket::Socket
Socket(Socket &&socket) noexcept
Move constructor.
sf::Socket::Status
Status
Status codes that may be returned by socket functions.
Definition Socket.hpp:49
sf::Socket::Type
Type
Types of protocols that the socket can use.
Definition Socket.hpp:129
sf::Socket::getNativeHandle
SocketHandle getNativeHandle() const
Return the internal handle of the socket.
sf::Socket::close
void close()
Close the socket gracefully.
sf::Socket::operator=
Socket & operator=(Socket &&socket) noexcept
Move assignment.
sf::Socket::~Socket
virtual ~Socket()
Destructor.
sf::Socket::Socket
Socket(Type type)
Default constructor.
sf::Socket::Socket
Socket(const Socket &)=delete
Deleted copy constructor.
sf::Socket::create
void create()
Create the internal representation of the socket.
sf::Socket::isBlocking
bool isBlocking() const
Tell whether the socket is in blocking or non-blocking mode.
sf::Socket::create
void create(SocketHandle handle)
Create the internal representation of the socket from a socket handle.
sf
Definition AudioResource.hpp:36
sf::SocketHandle
int SocketHandle
Definition SocketHandle.hpp:48