|
| 1 | +#ifndefSERVER_H |
| 2 | +#defineSERVER_H |
| 3 | + |
| 4 | +/* |
| 5 | + * You should not want to know what is inside those structures. |
| 6 | + */ |
| 7 | +typedefstructserver_data_t*server_t; |
| 8 | +typedefstructclient_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 | +typedefvoid (*onmessage_callback_t)(client_tclient,size_tlen,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 | +typedefvoid (*onconnect_callback_t)(client_tclient); |
| 21 | + |
| 22 | +/* |
| 23 | + * The server will call this function whenever it considers the 'client' |
| 24 | + * disconnected. |
| 25 | + */ |
| 26 | +typedefvoid (*ondisconnect_callback_t)(client_tclient); |
| 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_tserver_init( |
| 33 | +char*host, |
| 34 | +intport, |
| 35 | +onmessage_callback_tonmessage, |
| 36 | +onconnect_callback_tonconnect, |
| 37 | +ondisconnect_callback_tondisconnect, |
| 38 | +); |
| 39 | + |
| 40 | +/* |
| 41 | + * Starts the server. Returns 'true' on success, 'false' otherwise. |
| 42 | + */ |
| 43 | +boolserver_start(server_tserver); |
| 44 | + |
| 45 | +/* |
| 46 | + * The main server loop. Does not return, so use the callbacks and signal |
| 47 | + * handlers to add more logic. |
| 48 | + */ |
| 49 | +voidserver_loop(server_tserver); |
| 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 | +voidclient_set_data(client_tclient,void*userdata); |
| 57 | +void*client_get_data(client_tclient); |
| 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 | +boolclient_message_start(client_tclient); |
| 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 | +boolclient_message_append(client_tclient,size_tlen,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 | +boolclient_message_finish(client_tclient); |
| 89 | + |
| 90 | +#endif |