|
| 1 | +#ifndef SERVER_H |
| 2 | +#define SERVER_H |
| 3 | + |
| 4 | +/* |
| 5 | + * You should not want to know what is inside those structures. |
| 6 | + */ |
| 7 | +typedef struct server_data_t *server_t; |
| 8 | +typedef struct client_data_t *client_t; |
| 9 | + |
| 10 | +/* |
| 11 | + * The server will call this function whenever it gets a message ('len' bytes |
| 12 | + * of 'data') from the 'client'. |
| 13 | + */ |
| 14 | +typedef void (*onmessage_callback_t)(client_t client, size_t len, char *data); |
| 15 | + |
| 16 | +/* |
| 17 | + * The server will call this function whenever a new 'client' send the first |
| 18 | + * message. This callback gets called before the 'onmessage'. |
| 19 | + */ |
| 20 | +typedef void (*onconnect_callback_t)(client_t client); |
| 21 | + |
| 22 | +/* |
| 23 | + * The server will call this function whenever it considers the 'client' |
| 24 | + * disconnected. |
| 25 | + */ |
| 26 | +typedef void (*ondisconnect_callback_t)(client_t client); |
| 27 | + |
| 28 | +/* |
| 29 | + * Creates a new server that will listen on 'host:port' and call the specified |
| 30 | + * callbacks. Returns the server handle to use in other methods. |
| 31 | + */ |
| 32 | +server_t server_init( |
| 33 | + char *host, |
| 34 | + int port, |
| 35 | + onmessage_callback_t onmessage, |
| 36 | + onconnect_callback_t onconnect, |
| 37 | + ondisconnect_callback_t ondisconnect, |
| 38 | +); |
| 39 | + |
| 40 | +/* |
| 41 | + * Starts the server. Returns 'true' on success, 'false' otherwise. |
| 42 | + */ |
| 43 | +bool server_start(server_t server); |
| 44 | + |
| 45 | +/* |
| 46 | + * The main server loop. Does not return, so use the callbacks and signal |
| 47 | + * handlers to add more logic. |
| 48 | + */ |
| 49 | +void server_loop(server_t server); |
| 50 | + |
| 51 | +/* |
| 52 | + * These two methods allow you to set and get your custom 'userdata' for the |
| 53 | + * 'client'. The server does not care about this data and will not free it on |
| 54 | + * client disconnection. |
| 55 | + */ |
| 56 | +void client_set_data(client_t client, void *userdata); |
| 57 | +void *client_get_data(client_t client); |
| 58 | + |
| 59 | +/* |
| 60 | + * Puts an empty message header into the output buffer of the corresponding |
| 61 | + * socket. The message will not be sent until you call the _finish() method. |
| 62 | + * A call to this function may lead to a send() call if there is not enough |
| 63 | + * space in the buffer. |
| 64 | + * |
| 65 | + * Returns 'true' on success, 'false' otherwise. |
| 66 | + * |
| 67 | + * NOTE: Be careful not to call the _message_ methods for other clients until |
| 68 | + * you _finish() this message. This limitation is due to the fact that multiple |
| 69 | + * clients share the same socket. |
| 70 | + */ |
| 71 | +bool client_message_start(client_t client); |
| 72 | + |
| 73 | +/* |
| 74 | + * Appends 'len' bytes of 'data' to the buffer of the corresponding socket. |
| 75 | + * A call to this function may lead to a send() call if there is not enough |
| 76 | + * space in the buffer. |
| 77 | + * |
| 78 | + * Returns 'true' on success, 'false' otherwise. |
| 79 | + */ |
| 80 | +bool client_message_append(client_t client, size_t len, void *data); |
| 81 | + |
| 82 | +/* |
| 83 | + * Finalizes the message. After finalizing the message becomes ready to be sent |
| 84 | + * over the corresponding socket, and you may _start() another message. |
| 85 | + * |
| 86 | + * Returns 'true' on success, 'false' otherwise. |
| 87 | + */ |
| 88 | +bool client_message_finish(client_t client); |
| 89 | + |
| 90 | +#endif |
0 commit comments