libuv/ol.h

117 lines
2.4 KiB
C
Raw Normal View History

2011-03-28 10:17:52 +00:00
#ifndef OL_H
#define OL_H
#include <stddef.h> /* size_t */
2011-03-28 10:17:52 +00:00
2011-03-29 23:40:27 +00:00
typedef int ol_err; /* FIXME */
2011-03-24 00:56:16 +00:00
2011-03-28 10:17:52 +00:00
typedef struct ol_req_s ol_req;
typedef struct ol_handle_s ol_handle;
typedef void (*ol_req_cb)(ol_req* req, ol_err e);
typedef void (*ol_read_cb)(ol_req* req, size_t nread, ol_err e);
typedef void (*ol_write_cb)(ol_req* req, ol_err e);
typedef void (*ol_accept_cb)(ol_handle* server, ol_handle* new_client);
typedef void (*ol_close_cb)(ol_handle* handle, ol_err e);
typedef ol_req_cb ol_connect_cb;
typedef ol_req_cb ol_shutdown_cb;
2011-03-30 03:03:56 +00:00
#if defined(__unix__) || defined(__POSIX__)
2011-03-28 10:17:52 +00:00
# include "ol-unix.h"
#else
# include "ol-win.h"
#endif
2011-03-23 08:39:59 +00:00
2011-03-27 21:50:51 +00:00
2011-03-24 03:40:55 +00:00
typedef enum {
2011-03-28 08:55:29 +00:00
OL_UNKNOWN_HANDLE = 0,
2011-03-28 10:17:52 +00:00
OL_TCP,
2011-03-24 03:40:55 +00:00
OL_NAMED_PIPE,
2011-03-28 08:55:29 +00:00
OL_TTY,
2011-03-24 03:40:55 +00:00
OL_FILE,
} ol_handle_type;
2011-03-28 10:17:52 +00:00
struct ol_handle_s {
2011-03-29 23:40:27 +00:00
/* read-only */
2011-03-28 08:55:29 +00:00
ol_handle_type type;
2011-03-29 23:40:27 +00:00
/* private */
2011-03-28 08:55:29 +00:00
ol_handle_private _;
2011-03-29 23:40:27 +00:00
/* public */
2011-03-28 08:55:29 +00:00
ol_accept_cb accept_cb;
ol_close_cb close_cb;
void* data;
2011-03-28 10:17:52 +00:00
};
2011-03-23 08:39:59 +00:00
2011-03-28 08:55:29 +00:00
typedef enum {
OL_UNKNOWN_REQ = 0,
OL_CONNECT,
2011-03-30 03:03:56 +00:00
OL_ACCEPT,
2011-03-28 08:55:29 +00:00
OL_READ,
2011-03-28 10:17:52 +00:00
OL_WRITE,
2011-03-28 08:55:29 +00:00
OL_SHUTDOWN
} ol_req_type;
2011-03-23 08:39:59 +00:00
2011-03-28 10:17:52 +00:00
struct ol_req_s {
2011-03-29 23:40:27 +00:00
/* read-only */
2011-03-28 08:55:29 +00:00
ol_req_type type;
2011-03-28 10:17:52 +00:00
ol_handle* handle;
2011-03-29 23:40:27 +00:00
/* private */
2011-03-28 08:55:29 +00:00
ol_req_private _;
2011-03-29 23:40:27 +00:00
/* public */
void* cb;
2011-03-28 08:55:29 +00:00
void *data;
2011-03-28 10:17:52 +00:00
};
2011-03-28 08:55:29 +00:00
2011-03-30 02:53:29 +00:00
/**
* Most functions return boolean: 0 for success and -1 for failure.
* On error the user should then call ol_last_error() to determine
* the error code.
*/
ol_err ol_last_error();
const char* ol_err_str(ol_err err);
2011-03-29 17:08:45 +00:00
void ol_init();
2011-03-28 08:55:29 +00:00
int ol_run();
2011-03-30 02:57:11 +00:00
ol_handle* ol_tcp_handle_new(ol_close_cb close_cb, void* data);
/* TODO:
* ol_named_pipe_handle_new
* ol_file_handle_new
* ol_tty_handle_new
*/
2011-03-28 08:55:29 +00:00
2011-03-29 23:40:27 +00:00
/* TCP server methods. */
2011-03-28 10:17:52 +00:00
int ol_bind(ol_handle* handle, struct sockaddr* addr);
2011-03-28 08:55:29 +00:00
int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb);
2011-03-29 23:40:27 +00:00
/* TCP socket methods. */
2011-03-28 10:17:52 +00:00
int ol_connect(ol_handle* handle, ol_req *req, struct sockaddr* addr);
2011-03-28 08:55:29 +00:00
int ol_read(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt);
int ol_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt);
2011-03-28 10:54:18 +00:00
int ol_write2(ol_handle* handle, const char* msg);
2011-03-28 08:55:29 +00:00
int ol_shutdown(ol_handle* handle, ol_req *req);
2011-03-29 23:40:27 +00:00
/* Request handle to be closed. close_cb will be made */
/* synchronously during this call. */
2011-03-28 08:55:29 +00:00
int ol_close(ol_handle* handle);
2011-03-29 23:40:27 +00:00
/* Must be called for all handles after close_cb. Handles that arrive
* via the accept_cb must use ol_free().
*/
2011-03-28 10:54:18 +00:00
void ol_free(ol_handle* handle);
2011-03-28 10:17:52 +00:00
2011-03-29 23:40:27 +00:00
/* Utility */
2011-03-28 10:17:52 +00:00
struct sockaddr_in ol_ip4_addr(char *ip, int port);
#endif /* OL_H */