orca-sim
Public Member Functions | Private Attributes | List of all members
orcasim::models::orca::udp_server Class Reference

#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
 

Detailed Description

Definition at line 93 of file NetBridge.hpp.

Constructor & Destructor Documentation

§ udp_server()

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.

Note
The socket is open in this process. If you fork() or exec() then the socket will be closed by the operating system.
Warning
We only make use of the first address found by getaddrinfo(). All the other addresses are ignored.
Exceptions
udp_client_server_runtime_errorThe udp_client_server_runtime_error exception is raised when the address and port combinaison cannot be resolved or if the socket cannot be opened.
Parameters
[in]addrThe address we receive on.
[in]portThe port we receive from.

Definition at line 508 of file NetBridge.cpp.

509  : f_port(port)
510  , f_addr(addr) {
511  char decimal_port[16];
512  snprintf(decimal_port, sizeof(decimal_port), "%d", f_port);
513  decimal_port[sizeof(decimal_port) / sizeof(decimal_port[0]) - 1] = '\0';
514  struct addrinfo hints;
515  memset(&hints, 0, sizeof(hints));
516  hints.ai_family = AF_UNSPEC;
517  hints.ai_socktype = SOCK_DGRAM;
518  hints.ai_protocol = IPPROTO_UDP;
519  int r(getaddrinfo(addr.c_str(), decimal_port, &hints, &f_addrinfo));
520 
521  if (r != 0 || f_addrinfo == NULL) {
523  ("invalid address or port for UDP socket: \"" + addr + ":"
524  + decimal_port + "\"").c_str());
525  }
526 
527  f_socket = socket(f_addrinfo->ai_family, SOCK_DGRAM |
528  SOCK_CLOEXEC, IPPROTO_UDP);
529 
530  if (f_socket == -1) {
531  freeaddrinfo(f_addrinfo);
533  ("could not create UDP socket for: \"" + addr + ":" + decimal_port
534  + "\"").c_str());
535  }
536  r = bind(f_socket, f_addrinfo->ai_addr, f_addrinfo->ai_addrlen);
537  if (r != 0) {
538  freeaddrinfo(f_addrinfo);
539  close(f_socket);
541  ("could not bind UDP socket with: \"" + addr + ":" + decimal_port
542  + "\"").c_str());
543  }
544 }

§ ~udp_server()

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.

550  {
551  freeaddrinfo(f_addrinfo);
552  close(f_socket);
553 }

Member Function Documentation

§ get_addr()

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.)

Returns
The address as passed to the constructor.

Definition at line 585 of file NetBridge.cpp.

585  {
586  return f_addr;
587 }

§ get_port()

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.

Returns
The port of the UDP server.

Definition at line 573 of file NetBridge.cpp.

573  {
574  return f_port;
575 }

§ get_socket()

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.

Returns
The socket of this UDP server.

Definition at line 562 of file NetBridge.cpp.

562  {
563  return f_socket;
564 }

§ recv()

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.

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 607 of file NetBridge.cpp.

607  {
608  return ::recv(f_socket, msg, max_size, 0);
609 }

§ timed_recv()

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.

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

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.

630  {
631  fd_set s;
632  FD_ZERO(&s);
633  FD_SET(f_socket, &s);
634  struct timeval timeout;
635  timeout.tv_sec = max_wait_ms / 1000;
636  timeout.tv_usec = (max_wait_ms % 1000) * 1000;
637 
645  int retval = select(f_socket + 1, &s, 0, 0, &timeout);
646  if (retval == -1) {
647  // select() set errno accordingly
648  return -1;
649  }
650 
651  if (retval > 0) {
652  // our socket has data
653  return ::recv(f_socket, msg, max_size, 0);
654  }
655 
656  // our socket has no data
657  errno = EAGAIN;
658  return -1;
659 }

Member Data Documentation

§ f_addr

std::string orcasim::models::orca::udp_server::f_addr
private

Definition at line 108 of file NetBridge.hpp.

§ f_addrinfo

struct addrinfo* orcasim::models::orca::udp_server::f_addrinfo
private

Definition at line 109 of file NetBridge.hpp.

§ f_port

int orcasim::models::orca::udp_server::f_port
private

Definition at line 107 of file NetBridge.hpp.

§ f_socket

int orcasim::models::orca::udp_server::f_socket
private

Definition at line 106 of file NetBridge.hpp.


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