Loading...
Searching...
No Matches

Base class for all the socket types. More...

#include <SFML/Network/Socket.hpp>

Inheritance diagram for sf::Socket:
sf::TcpListener sf::TcpSocket sf::UdpSocket

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.
 
Socketoperator= (const Socket &)=delete
 Deleted copy assignment.
 
 Socket (Socket &&socket) noexcept
 Move constructor.
 
Socketoperator= (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

enum class sf::Socket::Status
strong

Status codes that may be returned by socket functions.

Enumerator
Done 

The socket has sent / received the data.

NotReady 

The socket is not ready to send / receive data yet.

Partial 

The socket sent a part of the data.

Disconnected 

The TCP socket has been disconnected.

Error 

An unexpected error happened.

Definition at line 48 of file Socket.hpp.

◆ Type

enum class sf::Socket::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 sf::Socket::~Socket ( )
virtual

Destructor.

◆ Socket() [1/3]

sf::Socket::Socket ( const Socket & )
delete

Deleted copy constructor.

◆ Socket() [2/3]

sf::Socket::Socket ( Socket && socket)
noexcept

Move constructor.

◆ Socket() [3/3]

sf::Socket::Socket ( Type type)
explicitprotected

Default constructor.

This constructor can only be accessed by derived classes.

Parameters
typeType of the socket (TCP or UDP)

Member Function Documentation

◆ close()

void sf::Socket::close ( )
protected

Close the socket gracefully.

This function can only be accessed by derived classes.

◆ create() [1/2]

void sf::Socket::create ( )
protected

Create the internal representation of the socket.

This function can only be accessed by derived classes.

◆ create() [2/2]

void sf::Socket::create ( SocketHandle handle)
protected

Create the internal representation of the socket from a socket handle.

This function can only be accessed by derived classes.

Parameters
handleOS-specific handle of the socket to wrap

◆ getNativeHandle()

SocketHandle sf::Socket::getNativeHandle ( ) const
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()

bool sf::Socket::isBlocking ( ) const
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]

Socket & sf::Socket::operator= ( const Socket & )
delete

Deleted copy assignment.

◆ operator=() [2/2]

Socket & sf::Socket::operator= ( Socket && socket)
noexcept

Move assignment.

◆ 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
blockingtrue to set the socket as blocking, false for non-blocking
See also
isBlocking

Friends And Related Symbol Documentation

◆ SocketSelector

friend class SocketSelector
friend

Definition at line 184 of file Socket.hpp.

Member Data Documentation

◆ AnyPort

unsigned short sf::Socket::AnyPort {0}
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: