libipc/libipc.h

88 lines
4.2 KiB
C

#ifndef LIBIPC
#define LIBIPC
#include <stdint.h>
// Event types: the "t" parameter in "ipc_wait_event".
enum event_types {
ERROR = 0 // A problem occured.
, CONNECTION = 1 // New user.
, DISCONNECTION = 2 // User disconnected.
, MESSAGE_RX = 3 // New message.
, MESSAGE_TX = 4 // Message sent.
, TIMER = 5 // Timeout in the poll(2) function.
, EXTERNAL = 6 // Message received from a non IPC socket.
, SWITCH_RX = 7 // Message received from a switched FD.
, SWITCH_TX = 8 // Message sent to a switched fd.
};
// Return type of callback functions when switching.
enum cb_event_types {
CB_NO_ERROR = 0 // No error. A message was generated.
, CB_ERROR = 1 // Generic error.
, CB_FD_CLOSING = 2 // The fd is closing.
, CB_IGNORE = 3 // The message should be ignored (protocol specific).
};
int ipc_context_init (void** ptr); // Allocate memory for a context.
void ipc_context_deinit (void** ctx); // Free the context's memory.
void ipc_context_timer (void* ctx, int timer); // Change the timer (for timer events, in ms).
// Init (or connect to) a service. That's almost the same operation behind the scene.
int ipc_service_init (void* ctx, int* servicefd, const char* service_name, uint16_t service_name_len);
int ipc_connect_service (void* ctx, int* servicefd, const char* service_name, uint16_t service_name_len);
// Write a message or schedule it.
int ipc_write (void* ctx, int servicefd, const char* mcontent, size_t mlen);
int ipc_schedule (void* ctx, int servicefd, const char* mcontent, size_t mlen);
// Read a message from a client; either selected by an index in the context structure or by its file descriptor.
int ipc_read_fd (void* ctx, int fd, char* buffer, size_t* buflen);
int ipc_read (void* ctx, size_t index, char* buffer, size_t* buflen);
// Wait for an event.
// The "t" parameter is the type of the event (enum event_types).
// The "index" parameter is the index in the context structure of the origin of the event (client or server).
// The "originfd" parameter is the file descriptor on which the event occurs.
// The "newfd" parameter is the file descriptor of the new connected client, in case of a connection event.
// The "buffer" and "buflen" parameters contain respectively a copy of the received message and its length.
int ipc_wait_event (void* ctx, char* t, size_t* index, int* originfd, int* newfd, char* buffer, size_t* buflen);
// Close a client (or server) based on its file descriptor or its index in the context structure.
int ipc_close_fd (void* ctx, int fd);
int ipc_close (void* ctx, size_t index);
// Close all connections (probably right before the processus is terminated).
int ipc_close_all (void* ctx);
// Add a (possibly non-IPC) file descriptor to handle.
// Since it's not marked as an IPC connection, messages won't be automatically read;
// which enables to handle any communications for most protocols through the LibIPC API.
int ipc_add_external (void* ctx, int newfd);
/**
* SWITCH: enables the automatic exchange of messages between a pair of file descriptors.
*
* There are two operations available:
* - ipc_add_switch: add a new switch;
* - ipc_set_switch_callbacks: set callbacks (IO operations) for a file descriptor,
* enabling specific processing for a connection.
*
* Callbacks enable to handle non-IPC communications, for example a websocket connection,
* a non-IPC client communicating with JSON messages (or any other format), etc.
*
* Switch and IO callbacks enable to easily create "protocol IPC services" (such as TCPd) to
* bind IPC services to basically any available protocol through small, dedicated services
* handling all the nitty-gritty details of non-IPC protocols.
*/
// Add a new switch between a pair of file descriptors, enabling automatic exchange of messages
// between this pair of fds. Useful for "protocol services", such as TCPd.
int ipc_add_switch (void* ctx, int fd1, int fd2);
// Set IO callbacks for a file descriptor.
// Returned "char" is a cb_event_types enum.
int ipc_set_switch_callbacks (void* ctx, int fd
, char (*in (int orig, char *payload, size_t *mlen))
, char (*out(int dest, const char *payload, size_t mlen)));
#endif