Base class for any socket-based networking.
More...
#include <Network.h>
|
| 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...
|
|
|
virtual void | GetAddrHints (struct addrinfo &hints)=0 |
| Subclass to tell which addresses to look for. More...
|
|
Base class for any socket-based networking.
- Exceptions
-
◆ SocketNetworking()
XPMP2::SocketNetworking::SocketNetworking |
( |
| ) |
|
|
inline |
Default constructor is not doing anything.
◆ ~SocketNetworking()
XPMP2::SocketNetworking::~SocketNetworking |
( |
| ) |
|
|
virtual |
Destructor makes sure the socket is closed.
◆ 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 |
◆ GetAddrHints()
virtual void XPMP2::SocketNetworking::GetAddrHints |
( |
struct addrinfo & |
hints | ) |
|
|
protectedpure virtual |
◆ 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 |
◆ GetLastErr()
std::string XPMP2::SocketNetworking::GetLastErr |
( |
| ) |
|
|
static |
Returns a human-readable text for the last error.
◆ getPort()
int XPMP2::SocketNetworking::getPort |
( |
| ) |
const |
|
inline |
◆ getSocket()
SOCKET XPMP2::SocketNetworking::getSocket |
( |
| ) |
const |
|
inline |
◆ isOpen()
bool XPMP2::SocketNetworking::isOpen |
( |
| ) |
const |
|
inline |
◆ 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] | _pFromAddr | Address of sender as a string, including port |
[out] | _pFromSockAddr | Address 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 |
◆ setBlocking()
void XPMP2::SocketNetworking::setBlocking |
( |
bool |
bBlock | ) |
|
◆ 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_ms | Timeout in milliseconds |
[out] | _pFromAddr | Address of sender as a string, including port |
[out] | _pFromSockAddr | Address 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.
◆ 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
The documentation for this class was generated from the following files: