c-template
socket_server.h
Go to the documentation of this file.
1 /*! @file socket_server.h
2  * @author Bonedaddy
3  * @brief TCP socket servers, clients, and tooling for working with sockets
4  * @details uses wait_group.h to provide lightweight synchronization between pthreads
5  * @warning before use you must call setup_signal_handling() so that all threads get properly cleaned up on exit
6  * @note you will want to adjust `async_handle_conn_func` to suit your needs as right now it is just an echo client
7  * it is likely you will need to have `#define _POSIX_C_SOURCE 201112L`
8  * see the following for more information
9  * - https://stackoverflow.com/questions/39409846/why-does-gcc-not-complain-about-htons-but-complains-about-getaddrinfo-when-c/39410095#39410095
10  * - https://man7.org/linux/man-pages/man3/getaddrinfo.3.html
11 */
12 
13 #pragma once
14 
15 #include <pthread.h>
16 #include <pthread.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 // sys/time.h is needed for the timeval
28 // #include <time.h>
29 #include <stdbool.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <fcntl.h>
33 #include <sys/time.h>
34 #include "socket.h"
35 #include "../../utils/logger.h"
36 #include "../../sync/wait_group.h"
37 
38 /*! @typedef socket_server
39  * @struct socket_server
40  * @brief a generic tcp socket server using file descriptor socket_number
41  * it uses a dedicated pthread for accepting new client connections, with each
42  * connection being processed in another pthread in detached state.
43  * socket uses SO_REUSEADDR
44 */
45 typedef struct socket_server {
47  log_fn log; // alis for thl->log
48  log_fnf logf; // alias for thl->logf
50  pthread_t thread;
51  // pthread_attr_t taddr;
54 
55 /*! @typedef client_conn
56  * @struct client_conn
57  * @brief a structure containing a file descriptor and address information
58  * @todo
59  * - enable a queue/list of these
60 */
61 typedef struct client_conn {
64 } client_conn;
65 
66 /*! @typedef conn_handle_data
67  * @struct conn_handle_data
68  * @brief struct containing arguments passed into pthread
69 */
70 typedef struct conn_handle_data {
71  pthread_t thread;
75 
76 /*! @brief returns a new socket server bound to the port number and ready to accept connections
77 */
78 socket_server *new_socket_server(addr_info hints, thread_logger *thl, int max_conns, char *port);
79 
80 /*! @brief listens for new connections and spawns a thread to process the connection
81  * thread that is created to process the connection runs as a detached thread
82  * will poll for new connections to accept every 500 miliseconds
83  * @param data void pointer to a socket_server struct
84  * @note detached thread created calling async_handle_conn_func
85  * @warning may change the 500 milisecond sleep
86 */
87 void *async_listen_func(void *data);
88 
89 /*! @brief handles connections in a dedicated pthread
90  * is laucnched in a pthread by async_listen_func when any new connection is received
91  * @param data `void *` to a conn_handle_data object
92  * @note uses `select` to determine if we can read data from the connection
93  * @note select runs for 3 seconds before timing out and releasing resources with the connection
94  * @warning currently implements an example echo client
95  * @warning you will want to adapt to your specific use case
96 */
97 void *async_handle_conn_func(void *data);
98 
99 /*! @brief helper function for accepting client connections
100  * times out new attempts if they take 3 seconds or more
101  * @return Failure: NULL client conn failed
102  * @return Success: non-NULL populated client_conn object
103 */
socket_server::log
log_fn log
Definition: socket_server.h:47
log_fnf
void(* log_fnf)(struct thread_logger *thl, int file_descriptor, LOG_LEVELS level, char *message,...)
Definition: logger.h:56
sockaddr_storage
alias for struct sockaddr_storage
log_fn
void(* log_fn)(struct thread_logger *thl, int file_descriptor, char *message, LOG_LEVELS level)
Definition: logger.h:48
socket_server::socket_number
int socket_number
Definition: socket_server.h:46
async_listen_func
void * async_listen_func(void *data)
listens for new connections and spawns a thread to process the connection thread that is created to p...
new_socket_server
socket_server * new_socket_server(addr_info hints, thread_logger *thl, int max_conns, char *port)
returns a new socket server bound to the port number and ready to accept connections
async_handle_conn_func
void * async_handle_conn_func(void *data)
handles connections in a dedicated pthread is laucnched in a pthread by async_listen_func when any ...
conn_handle_data
struct conn_handle_data conn_handle_data
client_conn
a structure containing a file descriptor and address information
Definition: socket_server.h:61
socket_server::wg
wait_group_t * wg
Definition: socket_server.h:52
socket_server
struct socket_server socket_server
socket_server
a generic tcp socket server using file descriptor socket_number it uses a dedicated pthread for accep...
Definition: socket_server.h:45
thread_logger
Definition: logger.h:63
conn_handle_data
struct containing arguments passed into pthread
Definition: socket_server.h:70
socket_server::thl
thread_logger * thl
Definition: socket_server.h:49
wait_group_t
Definition: wait_group.h:21
client_conn
struct client_conn client_conn
socket_server::logf
log_fnf logf
Definition: socket_server.h:48
client_conn::address
sock_addr_storage * address
Definition: socket_server.h:63
socket_server::thread
pthread_t thread
Definition: socket_server.h:50
accept_client_conn
client_conn * accept_client_conn(socket_server *srv)
helper function for accepting client connections times out new attempts if they take 3 seconds or mor...
client_conn::socket_number
int socket_number
Definition: socket_server.h:62
conn_handle_data::srv
socket_server * srv
Definition: socket_server.h:72
socket.h
TCP socket servers, clients, and tooling for working with sockets.
conn_handle_data::thread
pthread_t thread
Definition: socket_server.h:71
conn_handle_data::conn
client_conn * conn
Definition: socket_server.h:73
addrinfo
alias for struct addrinfo