|
int | create_real_time_tcp_socket_errexit () |
| Create an IPv4 TCP socket with Nagle's algorithm disabled (TCP_NODELAY) and Delayed ACKs disabled (TCP_QUICKACK). Exits application on any error.
|
|
int | create_server (uint16_t port, int *final_socket, uint16_t *final_port, socket_type_t sock_type, bool increment_port_on_retry) |
| Create a TCP server that listens for socket connections.
|
|
int | accept_socket (int socket, int rti_socket) |
|
int | connect_to_socket (int sock, const char *hostname, int port) |
|
int | read_from_socket (int socket, size_t num_bytes, unsigned char *buffer) |
|
int | read_from_socket_close_on_error (int *socket, size_t num_bytes, unsigned char *buffer) |
|
void | read_from_socket_fail_on_error (int *socket, size_t num_bytes, unsigned char *buffer, lf_mutex_t *mutex, char *format,...) |
|
ssize_t | peek_from_socket (int socket, unsigned char *result) |
|
int | write_to_socket (int socket, size_t num_bytes, unsigned char *buffer) |
|
int | write_to_socket_close_on_error (int *socket, size_t num_bytes, unsigned char *buffer) |
|
void | write_to_socket_fail_on_error (int *socket, size_t num_bytes, unsigned char *buffer, lf_mutex_t *mutex, char *format,...) |
|
int accept_socket |
( |
int | socket, |
|
|
int | rti_socket ) |
Wait for an incoming connection request on the specified server socket. This blocks until a connection is successfully accepted. If an error occurs that is not temporary (e.g., EAGAIN
or EWOULDBLOCK
), it reports the error and exits. Temporary errors cause the function to retry accepting the connection.
If the rti_socket
is not -1, this function checks whether the specified socket is still open. If it is not open, then this function returns -1. This is useful for federates to determine whether they are still connected to the federation and to stop waiting when they are not.
- Parameters
-
socket | The server socket file descriptor that is listening for incoming connections. |
rti_socket | The rti socket for the federate to check if it is still open. |
- Returns
- The file descriptor for the newly accepted socket on success, or -1 on failure (with an appropriate error message printed).
int create_server |
( |
uint16_t | port, |
|
|
int * | final_socket, |
|
|
uint16_t * | final_port, |
|
|
socket_type_t | sock_type, |
|
|
bool | increment_port_on_retry ) |
Create a TCP server that listens for socket connections.
If the specified port number is greater than zero, this function will attempt to acquire that port. If the specified port number is zero, and the increment_port_on_retry is true, it will attempt to acquire DEFAULT_PORT. If it fails to acquire DEFAULT_PORT, then it will increment the port number from DEFAULT_PORT on each attempt until it has incremented MAX_NUM_PORT_ADDRESSES times, at which point it will cycle around and begin again with DEFAULT_PORT. If the port number is zero, and the increment_port_on_retry is false, it delegates to the operating system to provide an available port number. If acquiring the port fails, then this function will repeatedly attempt up to PORT_BIND_RETRY_LIMIT times with a delay of PORT_BIND_RETRY_INTERVAL in between each try.
- Parameters
-
port | The port number to use or 0 to let the OS pick or 1 to start trying at DEFAULT_PORT. |
final_socket | Pointer to the returned socket descriptor on which accepting connections will occur. |
final_port | Pointer to the final port the server will use. |
sock_type | Type of the socket, TCP or UDP. |
increment_port_on_retry | Boolean to retry port increment. |
- Returns
- 0 for success, -1 for failure.
ssize_t peek_from_socket |
( |
int | socket, |
|
|
unsigned char * | result ) |
Without blocking, peek at the specified socket and, if there is anything on the queue, put its first byte at the specified address and return 1. If there is nothing on the queue, return 0, and if an error occurs, return -1.
- Parameters
-
socket | The socket ID. |
result | Pointer to where to put the first byte available on the socket. |
int read_from_socket |
( |
int | socket, |
|
|
size_t | num_bytes, |
|
|
unsigned char * | buffer ) |
Read the specified number of bytes from the specified socket into the specified buffer. If an error occurs during this reading, return -1 and set errno to indicate the cause of the error. If the read succeeds in reading the specified number of bytes, return 0. If an EOF occurs before reading the specified number of bytes, return 1. This function repeats the read attempt until the specified number of bytes have been read, an EOF is read, or an error occurs. Specifically, errors EAGAIN, EWOULDBLOCK, and EINTR are not considered errors and instead trigger another attempt. A delay between attempts is given by DELAY_BETWEEN_SOCKET_RETRIES.
- Parameters
-
socket | The socket ID. |
num_bytes | The number of bytes to read. |
buffer | The buffer into which to put the bytes. |
- Returns
- 0 for success, 1 for EOF, and -1 for an error.
void read_from_socket_fail_on_error |
( |
int * | socket, |
|
|
size_t | num_bytes, |
|
|
unsigned char * | buffer, |
|
|
lf_mutex_t * | mutex, |
|
|
char * | format, |
|
|
| ... ) |
Read the specified number of bytes from the specified socket into the specified buffer. If a disconnect or an EOF occurs during this reading, then if format is non-null, report an error and exit. If the mutex argument is non-NULL, release the mutex before exiting. If format is null, then report the error, but do not exit. This function takes a formatted string and additional optional arguments similar to printf(format, ...) that is appended to the error messages.
- Parameters
-
socket | The socket ID. |
num_bytes | The number of bytes to read. |
buffer | The buffer into which to put the bytes. |
format | A printf-style format string, followed by arguments to fill the string, or NULL to not exit with an error message. |
- Returns
- The number of bytes read, or 0 if an EOF is received, or a negative number for an error.
int write_to_socket |
( |
int | socket, |
|
|
size_t | num_bytes, |
|
|
unsigned char * | buffer ) |
Write the specified number of bytes to the specified socket from the specified buffer. If an error occurs, return -1 and set errno to indicate the cause of the error. If the write succeeds, return 0. This function repeats the attempt until the specified number of bytes have been written or an error occurs. Specifically, errors EAGAIN, EWOULDBLOCK, and EINTR are not considered errors and instead trigger another attempt. A delay between attempts is given by DELAY_BETWEEN_SOCKET_RETRIES.
- Parameters
-
socket | The socket ID. |
num_bytes | The number of bytes to write. |
buffer | The buffer from which to get the bytes. |
- Returns
- 0 for success, -1 for failure.