MAOS
Multithreaded Adaptive Optics Simulator
Loading...
Searching...
No Matches
sock.h File Reference

Data Structures

struct  listen_opt_t

Functions

int socket_recv_timeout (int sock, long sec)
int socket_send_timeout (int sock, long sec)
int connect_port (const char *hostname, int port, int block, int nodelay)
void listen_port (listen_opt_t opt)
void listen_port_add (int sock, short events, int(*handler)(struct pollfd *, int), const char *caller)
void listen_port_del (int sock, int toclose, const char *caller)
int socket_nopipe (int sock)
int socket_block (int sock, int block)
int bind_socket (int protocol, const char *ipaddr, uint16_t port)
int socket_port (int sock)
in_addr_t socket_peer (int sock)
const char * addr2name (in_addr_t s_addr)

Detailed Description

Routines to establish socket connection


Data Structure Documentation

◆ listen_opt_t

struct listen_opt_t
Collaboration diagram for listen_opt_t:
Data Fields
const char * localpath
const char * ipaddr
int port
int fd
int(*)(struct pollfd *, int) responder
int(*)(struct pollfd *, int) http_responder
void(*)() timeout_fun
double timeout_sec
int nodelay

Function Documentation

◆ socket_recv_timeout()

int socket_recv_timeout ( int sock,
long sec )

Set recv timeout in seconds

◆ socket_send_timeout()

int socket_send_timeout ( int sock,
long sec )

Set recv timeout in seconds

◆ connect_port()

int connect_port ( const char * hostname,
int port,
int block,
int nodelay )

Connect to a host at port. If hostname already includes port after :, port is ignored. hostname may start with / to indicate a local UNIX port

Parameters
hostnameThe hostname can be just name or name:port.
portThe port if hostname does not include it
blockDo we block until connection is established
nodelayset TCP nodelay flag

◆ listen_port()

void listen_port ( listen_opt_t opt)

Open a port and listen to it. Calls respond(sock) to handle data. If timeout_fun is not NULL, it will be called when 1) connection is establed/handled, 2) every timeout_sec if timeout_sec>0. This function only return at error. if localpath is not null and start with /, open the local unix port also if localpath is not null and contains ip address, only bind to that ip address

◆ listen_port_add()

void listen_port_add ( int sock,
short events,
int(* handler )(struct pollfd *, int),
const char * name )

Add a socket to the pollfd array for event monitoring.

Parameters
sockThe socket file descriptor to add.
eventsThe events to monitor (e.g., POLLIN, POLLOUT).
handlerThe function to handle events on this socket (can be NULL for internal handling).
nameA descriptive name for this socket (for debugging/logging).

If the socket is already present, updates its handler and name if necessary. Allocates memory for new entries as needed.

◆ listen_port_del()

void listen_port_del ( int sock,
int toclose,
const char * caller )

Remove a socket from the pollfd array, optionally closing it.

Parameters
sockThe socket file descriptor to remove; if -2, remove all sockets.
tocloseIf nonzero, close the socket(s) before removing.
callerA string indicating the caller for logging/debugging purposes.

This function does not close the socket if toclose is zero. If sock is -2, all sockets are removed.

◆ socket_block()

int socket_block ( int sock,
int block )

Set socket to unblocking mode

◆ bind_socket()

int bind_socket ( int protocol,
const char * ipaddr,
uint16_t port )

make a server port and bind to localhost on all addresses. protocl is usually SOCK_DGRAM or SOCK_STREAM. ip is usually zero. if port is 0, will bind an assigned port

◆ socket_port()

int socket_port ( int sock)

determine the socket port

◆ socket_peer()

in_addr_t socket_peer ( int sock)

determine the socket peer IP address

◆ addr2name()

const char * addr2name ( in_addr_t s_addr)

convert in_addr_t to host name for print out