Base class for all the socket types. More...
#include <SFML/Network/Socket.hpp>
Public Types | |
enum class | Status { Done , NotReady , Partial , Disconnected , Error } |
Status codes that may be returned by socket functions. More... | |
Public Member Functions | |
virtual | ~Socket () |
Destructor. | |
Socket (const Socket &)=delete | |
Deleted copy constructor. | |
Socket & | operator= (const Socket &)=delete |
Deleted copy assignment. | |
Socket (Socket &&socket) noexcept | |
Move constructor. | |
Socket & | operator= (Socket &&socket) noexcept |
Move assignment. | |
void | setBlocking (bool blocking) |
Set the blocking state of the socket. | |
bool | isBlocking () const |
Tell whether the socket is in blocking or non-blocking mode. | |
Static Public Attributes | |
static constexpr unsigned short | AnyPort {0} |
Some special values used by sockets. | |
Protected Types | |
enum class | Type { Tcp , Udp } |
Types of protocols that the socket can use. More... | |
Protected Member Functions | |
Socket (Type type) | |
Default constructor. | |
SocketHandle | getNativeHandle () const |
Return the internal handle of the socket. | |
void | create () |
Create the internal representation of the socket. | |
void | create (SocketHandle handle) |
Create the internal representation of the socket from a socket handle. | |
void | close () |
Close the socket gracefully. | |
Friends | |
class | SocketSelector |
Detailed Description
Base class for all the socket types.
This class mainly defines internal stuff to be used by derived classes.
The only public features that it defines, and which is therefore common to all the socket classes, is the blocking state. All sockets can be set as blocking or non-blocking.
In blocking mode, socket functions will hang until the operation completes, which means that the entire program (well, in fact the current thread if you use multiple ones) will be stuck waiting for your socket operation to complete.
In non-blocking mode, all the socket functions will return immediately. If the socket is not ready to complete the requested operation, the function simply returns the proper status code (Socket::Status::NotReady
).
The default mode, which is blocking, is the one that is generally used, in combination with threads or selectors. The non-blocking mode is rather used in real-time applications that run an endless loop that can poll the socket often enough, and cannot afford blocking this loop.
- See also
sf::TcpListener
,sf::TcpSocket
,sf::UdpSocket
Definition at line 41 of file Socket.hpp.
Member Enumeration Documentation
◆ Status
|
strong |
Status codes that may be returned by socket functions.
Definition at line 48 of file Socket.hpp.
◆ Type
|
strongprotected |
Types of protocols that the socket can use.
Enumerator | |
---|---|
Tcp | TCP protocol. |
Udp | UDP protocol. |
Definition at line 128 of file Socket.hpp.
Constructor & Destructor Documentation
◆ ~Socket()
|
virtual |
Destructor.
◆ Socket() [1/3]
|
delete |
Deleted copy constructor.
◆ Socket() [2/3]
|
noexcept |
Move constructor.
◆ Socket() [3/3]
|
explicitprotected |
Default constructor.
This constructor can only be accessed by derived classes.
- Parameters
-
type Type of the socket (TCP or UDP)
Member Function Documentation
◆ close()
|
protected |
Close the socket gracefully.
This function can only be accessed by derived classes.
◆ create() [1/2]
|
protected |
Create the internal representation of the socket.
This function can only be accessed by derived classes.
◆ create() [2/2]
|
protected |
Create the internal representation of the socket from a socket handle.
This function can only be accessed by derived classes.
- Parameters
-
handle OS-specific handle of the socket to wrap
◆ getNativeHandle()
|
nodiscardprotected |
Return the internal handle of the socket.
The returned handle may be invalid if the socket was not created yet (or already destroyed). This function can only be accessed by derived classes.
- Returns
- The internal (OS-specific) handle of the socket
◆ isBlocking()
|
nodiscard |
Tell whether the socket is in blocking or non-blocking mode.
- Returns
true
if the socket is blocking,false
otherwise
- See also
setBlocking
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ setBlocking()
void sf::Socket::setBlocking | ( | bool | blocking | ) |
Set the blocking state of the socket.
In blocking mode, calls will not return until they have completed their task. For example, a call to Receive in blocking mode won't return until some data was actually received. In non-blocking mode, calls will always return immediately, using the return code to signal whether there was data available or not. By default, all sockets are blocking.
- Parameters
-
blocking true
to set the socket as blocking,false
for non-blocking
- See also
isBlocking
Friends And Related Symbol Documentation
◆ SocketSelector
|
friend |
Definition at line 184 of file Socket.hpp.
Member Data Documentation
◆ AnyPort
|
staticconstexpr |
Some special values used by sockets.
Special value that tells the system to pick any available port
Definition at line 62 of file Socket.hpp.
The documentation for this class was generated from the following file: