My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions
communication.c File Reference

General-purpose socket communication functions used from global controller, runtime, or MSUs. More...

#include "communication.h"
#include "logging.h"
#include <arpa/inet.h>

Go to the source code of this file.

Macros

#define MAX_READ_ATTEMPTS   100
 The maximum number of times that a call to read() can be attempted for a single buffer before giving up. More...
 
#define BACKLOG   1024
 The backlog size for listening sockets. More...
 

Functions

int read_payload (int fd, size_t size, void *buff)
 Reads a buffer of a given size from a file descriptor. More...
 
ssize_t send_to_endpoint (int fd, void *data, size_t data_len)
 Writes a buffer of a given size to a file descriptor. More...
 
int init_connected_socket (struct sockaddr_in *addr)
 Initializes a socket that is connected to a given address. More...
 
int init_bound_socket (int port)
 Initializes a socket which is bound to a given port (and any local IP address). More...
 
int init_listening_socket (int port)
 Initializes a socket which is bound to and listening on the given port. More...
 

Detailed Description

General-purpose socket communication functions used from global controller, runtime, or MSUs.

Definition in file communication.c.

Macro Definition Documentation

#define BACKLOG   1024

The backlog size for listening sockets.

Definition at line 145 of file communication.c.

#define MAX_READ_ATTEMPTS   100

The maximum number of times that a call to read() can be attempted for a single buffer before giving up.

Definition at line 35 of file communication.c.

Function Documentation

int init_bound_socket ( int  port)

Initializes a socket which is bound to a given port (and any local IP address).

Sets REUSEPORT and REUSEADDR on the socket.

Parameters
portThe port to which to bind
Returns
the file descriptor on success, -1 on error

Definition at line 114 of file communication.c.

int init_connected_socket ( struct sockaddr_in *  addr)

Initializes a socket that is connected to a given address.

Blocks until the connection has been estabslished. Sets port and address to be reusable

Parameters
addrThe address to connect to
Returns
file descriptor on sucecss, -1 on error

Definition at line 81 of file communication.c.

int init_listening_socket ( int  port)

Initializes a socket which is bound to and listening on the given port.

Sets backlog on socket to be equal to BACKLOG

Parameters
portThe port on which to listen
Returns
The file descriptor on success, -1 on error

Definition at line 147 of file communication.c.

int read_payload ( int  fd,
size_t  size,
void *  buff 
)

Reads a buffer of a given size from a file descriptor.

Loops until enough bytes have been read off of the socket, until the socket is closed, or until it has tried more than MAX_READ_ATTEMPTS times.

Parameters
fdThe file descriptor from which to read
sizeThe number of bytes to read off of the file desriptor
buffThe buffer into which the read the bytes
Returns
0 on success, -1 on error

Definition at line 37 of file communication.c.

ssize_t send_to_endpoint ( int  fd,
void *  data,
size_t  data_len 
)

Writes a buffer of a given size to a file descriptor.

Loops on the write call until all of the bytes have been sent or it encounters an error.

Parameters
fdThe file descriptor to which the data is to be written
dataThe buffer to write to the file descriptor
data_lenThe size of the buffer to write
Returns
Number of bytes writen. 0 if no bytes could be written.

Definition at line 66 of file communication.c.