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

#include <NetBridge.hpp>

Public Member Functions

 udp_client (const std::string &addr, int port)
 Initialize a UDP client object. More...
 
 ~udp_client ()
 Clean up the UDP client object. More...
 
int get_socket () const
 Retrieve a copy of the socket identifier. More...
 
int get_port () const
 Retrieve the port used by this UDP client. More...
 
std::string get_addr () const
 Retrieve a copy of the address. More...
 
int send (const char *msg, size_t size)
 Send a message through this UDP client. More...
 

Private Attributes

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

Detailed Description

Definition at line 74 of file NetBridge.hpp.

Constructor & Destructor Documentation

§ udp_client()

udp_client::udp_client ( const std::string &  addr,
int  port 
)

Initialize a UDP client object.

This function initializes the UDP client object using the address and the port as specified.

The port is expected to be a host side port number (i.e. 59200).

The addr parameter is a textual address. It may be an IPv4 or IPv6 address and it can represent a host name or an address defined with just numbers. If the address cannot be resolved then an error occurs and constructor throws.

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 server could not be initialized properly. Either the address cannot be resolved, the port is incompatible or not available, or the socket could not be created.
Parameters
[in]addrThe address to convert to a numeric IP.
[in]portThe port number.

Definition at line 380 of file NetBridge.cpp.

381  : f_port(port)
382  , f_addr(addr) {
383  char decimal_port[16];
384  snprintf(decimal_port, sizeof(decimal_port), "%d", f_port);
385  decimal_port[sizeof(decimal_port) / sizeof(decimal_port[0]) - 1] = '\0';
386  struct addrinfo hints;
387  memset(&hints, 0, sizeof(hints));
388  hints.ai_family = AF_UNSPEC;
389  hints.ai_socktype = SOCK_DGRAM;
390  hints.ai_protocol = IPPROTO_UDP;
391  int r(getaddrinfo(addr.c_str(), decimal_port, &hints, &f_addrinfo));
392  if (r != 0 || f_addrinfo == NULL) {
393  throw udp_client_server_runtime_error(("invalid address or port: \""
394  + addr + ":" + decimal_port + "\"").c_str());
395  }
396 
397  f_socket = socket(f_addrinfo->ai_family, SOCK_DGRAM |
398  SOCK_CLOEXEC, IPPROTO_UDP);
399 
400  if (f_socket == -1) {
401  freeaddrinfo(f_addrinfo);
402  throw udp_client_server_runtime_error(("could not create socket for: \""
403  + addr + ":" + decimal_port + "\"").c_str());
404  }
405 }

§ ~udp_client()

udp_client::~udp_client ( )

Clean up the UDP client object.

This function frees the address information structure and close the socket before returning.

Definition at line 412 of file NetBridge.cpp.

412  {
413  freeaddrinfo(f_addrinfo);
414  close(f_socket);
415 }

Member Function Documentation

§ get_addr()

std::string udp_client::get_addr ( ) const

Retrieve a copy of the address.

This function returns a copy of the address as it was specified in the constructor. This does not return a canonalized version of the address.

The address cannot be modified. If you need to send data on a different address, create a new UDP client.

Returns
A string with a copy of the constructor input address.

Definition at line 449 of file NetBridge.cpp.

449  {
450  return f_addr;
451 }

§ get_port()

int udp_client::get_port ( ) const

Retrieve the port used by this UDP client.

This function returns the port used by this UDP client. The port is defined as an integer, host side.

Returns
The port as expected in a host integer.

Definition at line 435 of file NetBridge.cpp.

435  {
436  return f_port;
437 }

§ get_socket()

int udp_client::get_socket ( ) const

Retrieve a copy of the socket identifier.

This function return the socket identifier as returned by the socket() function. This can be used to change some flags.

Returns
The socket used by this UDP client.

Definition at line 424 of file NetBridge.cpp.

424  {
425  return f_socket;
426 }

§ send()

int udp_client::send ( const char *  msg,
size_t  size 
)

Send a message through this UDP client.

This function sends msg through the UDP client socket. The function cannot be used to change the destination as it was defined when creating the udp_client object.

The size must be small enough for the message to fit. In most cases we use these in Snap! to send very small signals (i.e. 4 bytes signalands.) Any data we would want to share remains in the Cassandra database so that way we can avoid losing it because of a UDP message.

Parameters
[in]msgThe message to send.
[in]sizeThe number of bytes representing this message.
Returns
-1 if an error occurs, otherwise the number of bytes sent. errno is set accordingly on error.

Definition at line 470 of file NetBridge.cpp.

470  {
471  return sendto(f_socket, msg, size, 0, f_addrinfo->ai_addr,
472  f_addrinfo->ai_addrlen);
473 }

Member Data Documentation

§ f_addr

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

Definition at line 78 of file NetBridge.hpp.

§ f_addrinfo

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

Definition at line 79 of file NetBridge.hpp.

§ f_port

int orcasim::models::orca::udp_client::f_port
private

Definition at line 77 of file NetBridge.hpp.

§ f_socket

int orcasim::models::orca::udp_client::f_socket
private

Definition at line 76 of file NetBridge.hpp.


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