XPMP2
X-Plane multiplayer library 2 - using instancing
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
XPMP2::SocketNetworking Class Referenceabstract

Base class for any socket-based networking. More...

#include <Network.h>

+ Inheritance diagram for XPMP2::SocketNetworking:

Public Member Functions

 SocketNetworking ()
 Default constructor is not doing anything. More...
 
virtual ~SocketNetworking ()
 Destructor makes sure the socket is closed. More...
 
virtual void Open (const std::string &_addr, int _port, size_t _bufSize=512, unsigned _timeOut_ms=0, bool _bBroadcast=false)
 Creates a socket and binds it to the given local address. More...
 
virtual void Connect (const std::string &_addr, int _port, size_t _bufSize, unsigned _timeOut_ms=0)
 Creates a socket and connects it to the given remote server. More...
 
virtual void Close ()
 Thread-safely close the connection(s) and frees the buffer. More...
 
bool isOpen () const
 Is a socket open? More...
 
void SetBufSize (size_t _bufSize)
 (Re)Sets the buffer size (or clears it if _bufSize==0) More...
 
SOCKET getSocket () const
 the socket More...
 
int getPort () const
 the port More...
 
std::string getAddr () const
 the interface address More...
 
const char * getBuf () const
 return the buffer More...
 
void setBlocking (bool bBlock)
 Set blocking mode. More...
 
long recv (std::string *_pFromAddr=nullptr, SockAddrTy *_pFromSockAddr=nullptr)
 Waits to receive a message, ensures zero-termination in the buffer. More...
 
long timedRecv (int max_wait_ms, std::string *_pFromAddr=nullptr, SockAddrTy *_pFromSockAddr=nullptr)
 Waits to receive a message with timeout, ensures zero-termination in the buffer. More...
 
virtual bool send (const char *msg)
 send messages on session connection More...
 
bool broadcast (const char *msg)
 Sends a broadcast message. More...
 

Static Public Member Functions

static std::string GetLastErr ()
 Returns a human-readable text for the last error. More...
 
static std::string GetAddrString (const SockAddrTy &sa, bool withPort=true)
 Convert addresses to string. More...
 

Protected Member Functions

virtual void GetAddrHints (struct addrinfo &hints)=0
 Subclass to tell which addresses to look for. More...
 

Protected Attributes

SOCKET f_socket = INVALID_SOCKET
 
int f_port = 0
 
std::string f_addr
 
char * buf = NULL
 
size_t bufSize = 512
 

Detailed Description

Base class for any socket-based networking.

Exceptions
XPMP2::NetRuntimeErrorin case of any errors

Constructor & Destructor Documentation

◆ SocketNetworking()

XPMP2::SocketNetworking::SocketNetworking ( )
inline

Default constructor is not doing anything.

◆ ~SocketNetworking()

XPMP2::SocketNetworking::~SocketNetworking ( )
virtual

Destructor makes sure the socket is closed.

Member Function Documentation

◆ broadcast()

bool XPMP2::SocketNetworking::broadcast ( const char *  msg)

Sends a broadcast message.

◆ Close()

void XPMP2::SocketNetworking::Close ( )
virtual

Thread-safely close the connection(s) and frees the buffer.

Clean up the UDP server.

Close: This function frees the address info structures and close the socket.

Reimplemented in XPMP2::TCPConnection.

◆ Connect()

void XPMP2::SocketNetworking::Connect ( const std::string &  _addr,
int  _port,
size_t  _bufSize,
unsigned  _timeOut_ms = 0 
)
virtual

Creates a socket and connects it to the given remote server.

◆ getAddr()

std::string XPMP2::SocketNetworking::getAddr ( ) const
inline

the interface address

◆ GetAddrHints()

virtual void XPMP2::SocketNetworking::GetAddrHints ( struct addrinfo &  hints)
protectedpure virtual

Subclass to tell which addresses to look for.

Implemented in XPMP2::TCPConnection, XPMP2::UDPMulticast, and XPMP2::UDPReceiver.

◆ GetAddrString()

std::string XPMP2::SocketNetworking::GetAddrString ( const SockAddrTy sa,
bool  withPort = true 
)
static

Convert addresses to string.

◆ getBuf()

const char* XPMP2::SocketNetworking::getBuf ( ) const
inline

return the buffer

◆ GetLastErr()

std::string XPMP2::SocketNetworking::GetLastErr ( )
static

Returns a human-readable text for the last error.

◆ getPort()

int XPMP2::SocketNetworking::getPort ( ) const
inline

the port

◆ getSocket()

SOCKET XPMP2::SocketNetworking::getSocket ( ) const
inline

the socket

◆ isOpen()

bool XPMP2::SocketNetworking::isOpen ( ) const
inline

Is a socket open?

◆ Open()

void XPMP2::SocketNetworking::Open ( const std::string &  _addr,
int  _port,
size_t  _bufSize = 512,
unsigned  _timeOut_ms = 0,
bool  _bBroadcast = false 
)
virtual

Creates a socket and binds it to the given local address.

◆ recv()

long XPMP2::SocketNetworking::recv ( std::string *  _pFromAddr = nullptr,
SockAddrTy _pFromSockAddr = nullptr 
)

Waits to receive a message, ensures zero-termination in the buffer.

Parameters
[out]_pFromAddrAddress of sender as a string, including port
[out]_pFromSockAddrAddress of sender as a socket address
Returns
The number of bytes read or -1 if an error occurs.

This function waits until a message is received on this UDP server. There are no means to return from this function except by receiving a message. Remember that UDP does not have a connect state so whether another process quits does not change the status of this UDP server and thus it continues to wait forever.

Note that you may change the type of socket by making it non-blocking (use the get_socket() to retrieve the socket identifier) in which case this function will not block if no message is available. Instead it returns immediately.

◆ send()

bool XPMP2::SocketNetworking::send ( const char *  msg)
virtual

send messages on session connection

Reimplemented in XPMP2::TCPConnection.

◆ setBlocking()

void XPMP2::SocketNetworking::setBlocking ( bool  bBlock)

Set blocking mode.

See also
https://stackoverflow.com/a/1549344

◆ SetBufSize()

void XPMP2::SocketNetworking::SetBufSize ( size_t  _bufSize)

(Re)Sets the buffer size (or clears it if _bufSize==0)

◆ timedRecv()

long XPMP2::SocketNetworking::timedRecv ( int  max_wait_ms,
std::string *  _pFromAddr = nullptr,
SockAddrTy _pFromSockAddr = nullptr 
)

Waits to receive a message with timeout, ensures zero-termination in the buffer.

Parameters
max_wait_msTimeout in milliseconds
[out]_pFromAddrAddress of sender as a string, including port
[out]_pFromSockAddrAddress of sender as a socket address
Returns
-1 if an error occurs or the function timed out, the number of bytes received otherwise. errno is set to EAGAIN/WSAEWOULDBLOCK in case of a timeout.

This function waits for a given amount of time for data to come in. If no data comes in after max_wait_ms, the function returns with -1 and errno set to EAGAIN.

The socket is expected to be a blocking socket (the default,) although it is possible to setup the socket as non-blocking if necessary for some other reason.

This function blocks for a maximum amount of time as defined by max_wait_ms. It may return sooner with an error or a message.

Member Data Documentation

◆ buf

char* XPMP2::SocketNetworking::buf = NULL
protected

◆ bufSize

size_t XPMP2::SocketNetworking::bufSize = 512
protected

◆ f_addr

std::string XPMP2::SocketNetworking::f_addr
protected

◆ f_port

int XPMP2::SocketNetworking::f_port = 0
protected

◆ f_socket

SOCKET XPMP2::SocketNetworking::f_socket = INVALID_SOCKET
protected

The documentation for this class was generated from the following files: