orca-sim
|
#include <NetBridge.hpp>
Public Member Functions | |
udp_server (const std::string &addr, int port) | |
Initialize a UDP server object. More... | |
~udp_server () | |
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 |
Definition at line 93 of file NetBridge.hpp.
udp_server::udp_server | ( | const std::string & | addr, |
int | port | ||
) |
Initialize a UDP server object.
This function initializes a UDP server object making it ready to receive messages.
The server address and port are specified in the constructor so if you need to receive messages from several different addresses and/or port, you'll have to create a server for each.
The address is a string and it can represent an IPv4 or IPv6 address.
Note that this function calls connect() to connect the socket to the specified address. To accept data on different UDP addresses and ports, multiple UDP servers must be created.
udp_client_server_runtime_error | The udp_client_server_runtime_error exception is raised when the address and port combinaison cannot be resolved or if the socket cannot be opened. |
[in] | addr | The address we receive on. |
[in] | port | The port we receive from. |
Definition at line 508 of file NetBridge.cpp.
udp_server::~udp_server | ( | ) |
Clean up the UDP server.
This function frees the address info structures and close the socket.
Definition at line 550 of file NetBridge.cpp.
std::string udp_server::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.)
Definition at line 585 of file NetBridge.cpp.
int udp_server::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.
Definition at line 573 of file NetBridge.cpp.
int udp_server::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.
Definition at line 562 of file NetBridge.cpp.
int udp_server::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.
[in] | msg | The buffer where the message is saved. |
[in] | max_size | The maximum size the message (i.e. size of the msg buffer.) |
Definition at line 607 of file NetBridge.cpp.
int udp_server::timed_recv | ( | char * | msg, |
size_t | max_size, | ||
int | max_wait_ms | ||
) |
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.
[in] | msg | The buffer where the message will be saved. |
[in] | max_size | The size of the msg buffer in bytes. |
[in] | max_wait_ms | The maximum number of milliseconds to wait for a message. |
23/03/2019: Anderson Removed last two parameters to avoid -Werror=restrict original call: => int retval = select(f_socket + 1, &s, &s, &s, &timeout); TODO: make sure that these parameters won't break the function
Definition at line 630 of file NetBridge.cpp.
|
private |
Definition at line 108 of file NetBridge.hpp.
|
private |
Definition at line 109 of file NetBridge.hpp.
|
private |
Definition at line 107 of file NetBridge.hpp.
|
private |
Definition at line 106 of file NetBridge.hpp.