RISA
risa::UDPServer Class Reference

#include <UDPServer.h>

Public Member Functions

 UDPServer (const std::string &addr, int port)
 
 ~UDPServer ()
 Clean up the UDP server. More...
 
int get_socket () const
 The socket used by this UDP server. More...
 
int get_port () const
 The port used by this UDP server. More...
 
std::string get_addr () const
 Return the address of this UDP server. More...
 
int recv (char *msg, size_t max_size)
 Wait on a message. More...
 
int timed_recv (char *msg, size_t max_size, int max_wait_ms)
 Wait for data to come in. More...
 

Private Attributes

int f_socket
 
int f_port
 
std::string f_addr
 
struct addrinfo * f_addrinfo
 

Detailed Description

Definition at line 41 of file UDPServer.h.

Constructor & Destructor Documentation

risa::UDPServer::UDPServer ( const std::string &  addr,
int  port 
)

Definition at line 79 of file UDPServer.cpp.

risa::UDPServer::~UDPServer ( )

Clean up the UDP server.

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

Definition at line 115 of file UDPServer.cpp.

Member Function Documentation

std::string risa::UDPServer::get_addr ( ) const

Return the address of this UDP server.

This function returns a verbatim copy of the address as passed to the constructor of the UDP server (i.e. it does not return the canonalized version of the address.)

Returns
The address as passed to the constructor.

Definition at line 153 of file UDPServer.cpp.

int risa::UDPServer::get_port ( ) const

The port used by this UDP server.

This function returns the port attached to the UDP server. It is a copy of the port specified in the constructor.

Returns
The port of the UDP server.

Definition at line 140 of file UDPServer.cpp.

int risa::UDPServer::get_socket ( ) const

The socket used by this UDP server.

This function returns the socket identifier. It can be useful if you are doing a select() on many sockets.

Returns
The socket of this UDP server.

Definition at line 128 of file UDPServer.cpp.

int risa::UDPServer::recv ( char *  msg,
size_t  max_size 
)

Wait on a message.

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.

Parameters
[in]msgThe buffer where the message is saved.
[in]max_sizeThe maximum size the message (i.e. size of the msg buffer.)
Returns
The number of bytes read or -1 if an error occurs.

Definition at line 176 of file UDPServer.cpp.

int risa::UDPServer::timed_recv ( char *  msg,
size_t  max_size,
int  max_wait_s 
)

Wait for data to come in.

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.

Parameters
[in]msgThe buffer where the message will be saved.
[in]max_sizeThe size of the msg buffer in bytes.
[in]max_wait_sThe maximum number of seconds to wait for a message.
Returns
-1 if an error occurs or the function timed out, the number of bytes received otherwise.

Definition at line 200 of file UDPServer.cpp.

Member Data Documentation

std::string risa::UDPServer::f_addr
private

Definition at line 57 of file UDPServer.h.

struct addrinfo* risa::UDPServer::f_addrinfo
private

Definition at line 58 of file UDPServer.h.

int risa::UDPServer::f_port
private

Definition at line 56 of file UDPServer.h.

int risa::UDPServer::f_socket
private

Definition at line 55 of file UDPServer.h.


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