diff --git a/.gitignore b/.gitignore index 8ed05b54..f95baf79 100644 --- a/.gitignore +++ b/.gitignore @@ -19,8 +19,8 @@ ev/libtool ev/stamp-h1 ev/autom4te.cache /ipch/ -/Debug/ -/Release/ +/Win32/ +/x64/ test/echo-demo test/test-ping-pong diff --git a/Makefile b/Makefile index 4b1a66fb..71a66725 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ all: test/echo-demo test/test-ping-pong -test/echo-demo: test/echo-demo.c test/echo.o ol.a - $(CC) -ansi -g -o test/echo-demo test/echo-demo.c test/echo.o ol.a -lm +test/echo-demo: test/echo-demo.c test/echo.o oio.a + $(CC) -ansi -g -o test/echo-demo test/echo-demo.c test/echo.o oio.a -lm -test/test-ping-pong: test/test-ping-pong.c test/echo.o ol.a - $(CC) -ansi -g -o test/test-ping-pong test/test-ping-pong.c test/echo.o ol.a -lm +test/test-ping-pong: test/test-ping-pong.c test/echo.o oio.a + $(CC) -ansi -g -o test/test-ping-pong test/test-ping-pong.c test/echo.o oio.a -lm -ol.a: ol-unix.o ev/ev.o - $(AR) rcs ol.a ol-unix.o ev/ev.o +oio.a: oio-unix.o ev/ev.o + $(AR) rcs oio.a oio-unix.o ev/ev.o -ol-unix.o: ol-unix.c ol.h ol-unix.h - $(CC) -ansi -g -c ol-unix.c -o ol-unix.o +oio-unix.o: oio-unix.c oio.h oio-unix.h + $(CC) -ansi -g -c oio-unix.c -o oio-unix.o test/echo.o: test/echo.c test/echo.h $(CC) -ansi -g -c test/echo.c -o test/echo.o diff --git a/libol-test.vcxproj b/liboio-test.vcxproj similarity index 98% rename from libol-test.vcxproj rename to liboio-test.vcxproj index c0fd32fd..6ade6135 100644 --- a/libol-test.vcxproj +++ b/liboio-test.vcxproj @@ -20,6 +20,7 @@ Win32Proj + {1D7C3F6C-A4AF-DD73-2D20-B2FC919B3744} @@ -150,7 +151,7 @@ - + {301fe650-cd34-14e5-6b63-42e383fa02bc} diff --git a/libol.sln b/liboio.sln similarity index 85% rename from libol.sln rename to liboio.sln index e3e753e3..77f39988 100644 --- a/libol.sln +++ b/liboio.sln @@ -1,9 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libol", "libol.vcxproj", "{301FE650-CD34-14E5-6B63-42E383FA02BC}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liboio", "liboio.vcxproj", "{301FE650-CD34-14E5-6B63-42E383FA02BC}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libol-test", "libol-test.vcxproj", "{1D7C3F6C-A4AF-DD73-2D20-B2FC919B3744}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liboio-test", "liboio-test.vcxproj", "{1D7C3F6C-A4AF-DD73-2D20-B2FC919B3744}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/libol.vcxproj b/liboio.vcxproj similarity index 95% rename from libol.vcxproj rename to liboio.vcxproj index aa2ff28e..067d38ab 100644 --- a/libol.vcxproj +++ b/liboio.vcxproj @@ -1,102 +1,103 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - Win32Proj - - - - StaticLibrary - true - - - StaticLibrary - true - - - StaticLibrary - false - - - StaticLibrary - false - - - - - - - - - - - - - - - - - - - $(SolutionDir)$(Platform)\$(Configuration)\ - $(Platform)\$(Configuration) - - - $(SolutionDir)$(Platform)\$(Configuration)\ - $(Platform)\$(Configuration) - - - $(SolutionDir)$(Platform)\$(Configuration)\ - $(Platform)\$(Configuration) - - - $(SolutionDir)$(Platform)\$(Configuration)\ - $(Platform)\$(Configuration) - - - - WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) - MultiThreadedDebugDLL - Level3 - ProgramDatabase - Disabled - - - - - WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) - MultiThreadedDebugDLL - Level3 - ProgramDatabase - Disabled - - - - - - - - - - - - - + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + Win32Proj + {301FE650-CD34-14E5-6B63-42E383FA02BC} + + + + StaticLibrary + true + + + StaticLibrary + true + + + StaticLibrary + false + + + StaticLibrary + false + + + + + + + + + + + + + + + + + + + $(SolutionDir)$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration) + + + $(SolutionDir)$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration) + + + $(SolutionDir)$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration) + + + $(SolutionDir)$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration) + + + + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreadedDebugDLL + Level3 + ProgramDatabase + Disabled + + + + + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreadedDebugDLL + Level3 + ProgramDatabase + Disabled + + + + + + + + + + + + + \ No newline at end of file diff --git a/ol-unix.c b/oio-unix.c similarity index 63% rename from ol-unix.c rename to oio-unix.c index caf4861f..65875dd1 100644 --- a/ol-unix.c +++ b/oio-unix.c @@ -1,4 +1,4 @@ -#include "ol.h" +#include "oio.h" #include /* printf */ @@ -25,23 +25,23 @@ size_t strnlen (register const char* s, size_t maxlen) { #endif /* strnlen */ -void ol_tcp_io(EV_P_ ev_io* watcher, int revents); -void ol_tcp_connect(ol_handle* handle, ol_req* req); -int ol_tcp_open(ol_handle*, int fd); -int ol_close_error(ol_handle* handle, ol_err err); +void oio_tcp_io(EV_P_ ev_io* watcher, int revents); +void oio_tcp_connect(oio_handle* handle, oio_req* req); +int oio_tcp_open(oio_handle*, int fd); +int oio_close_error(oio_handle* handle, oio_err err); -static ol_err ol_err_new(ol_handle* handle, int e) { +static oio_err oio_err_new(oio_handle* handle, int e) { handle->_.err = e; return e; } -ol_err ol_err_last(ol_handle *handle) { +oio_err oio_err_last(oio_handle *handle) { return handle->_.err; } -struct sockaddr_in ol_ip4_addr(char *ip, int port) { +struct sockaddr_in oio_ip4_addr(char *ip, int port) { struct sockaddr_in addr; addr.sin_family = AF_INET; @@ -52,40 +52,40 @@ struct sockaddr_in ol_ip4_addr(char *ip, int port) { } -int ol_close(ol_handle* handle) { - return ol_close_error(handle, 0); +int oio_close(oio_handle* handle) { + return oio_close_error(handle, 0); } -void ol_init() { +void oio_init() { ev_default_loop(0); } -int ol_run() { +int oio_run() { ev_run(EV_DEFAULT_ 0); } -ol_handle* ol_tcp_handle_new(ol_close_cb close_cb, void* data) { - ol_handle *handle = calloc(sizeof(ol_handle), 1); +oio_handle* oio_tcp_handle_new(oio_close_cb close_cb, void* data) { + oio_handle *handle = calloc(sizeof(oio_handle), 1); if (!handle) { - ol_err_new(NULL, ENOMEM); + oio_err_new(NULL, ENOMEM); return NULL; } - handle->type = OL_TCP; + handle->type = OIO_TCP; handle->close_cb = close_cb; handle->data = data; int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { - ol_err_new(handle, errno); + oio_err_new(handle, errno); free(handle); return NULL; } - if (ol_tcp_open(handle, fd)) { + if (oio_tcp_open(handle, fd)) { close(fd); free(handle); return NULL; @@ -96,7 +96,7 @@ ol_handle* ol_tcp_handle_new(ol_close_cb close_cb, void* data) { -int ol_bind(ol_handle* handle, struct sockaddr* addr) { +int oio_bind(oio_handle* handle, struct sockaddr* addr) { int addrsize; int domain; int r; @@ -116,11 +116,11 @@ int ol_bind(ol_handle* handle, struct sockaddr* addr) { r = bind(handle->_.fd, addr, addrsize); - return ol_err_new(handle, r); + return oio_err_new(handle, r); } -int ol_tcp_init_fd(int fd) { +int oio_tcp_init_fd(int fd) { int r; int yes = 1; r = fcntl(fd, F_SETFL, O_NONBLOCK); @@ -131,16 +131,16 @@ int ol_tcp_init_fd(int fd) { } -int ol_tcp_open(ol_handle* handle, int fd) { +int oio_tcp_open(oio_handle* handle, int fd) { /* Set non-blocking, etc */ - ol_tcp_init_fd(fd); + oio_tcp_init_fd(fd); handle->_.fd = fd; ngx_queue_init(&handle->_.read_reqs); - ev_io_init(&handle->_.read_watcher, ol_tcp_io, fd, EV_READ); - ev_io_init(&handle->_.write_watcher, ol_tcp_io, fd, EV_WRITE); + ev_io_init(&handle->_.read_watcher, oio_tcp_io, fd, EV_READ); + ev_io_init(&handle->_.write_watcher, oio_tcp_io, fd, EV_WRITE); handle->_.read_watcher.data = handle; handle->_.write_watcher.data = handle; @@ -149,8 +149,8 @@ int ol_tcp_open(ol_handle* handle, int fd) { } -void ol_server_io(EV_P_ ev_io* watcher, int revents) { - ol_handle* handle = watcher->data; +void oio_server_io(EV_P_ ev_io* watcher, int revents) { + oio_handle* handle = watcher->data; assert(revents == EV_READ); @@ -166,18 +166,18 @@ void ol_server_io(EV_P_ ev_io* watcher, int revents) { /* TODO special trick. unlock reserved socket, accept, close. */ return; } else { - ol_close_error(handle, ol_err_new(handle, errno)); + oio_close_error(handle, oio_err_new(handle, errno)); } } else { if (!handle->accept_cb) { close(fd); } else { - ol_handle* new_client = ol_tcp_handle_new(NULL, NULL); + oio_handle* new_client = oio_tcp_handle_new(NULL, NULL); if (!new_client) { /* Ignore error for now */ } else { - if (ol_tcp_open(new_client, fd)) { + if (oio_tcp_open(new_client, fd)) { /* Ignore error for now */ } else { ev_io_start(EV_DEFAULT_ &handle->_.read_watcher); @@ -190,16 +190,16 @@ void ol_server_io(EV_P_ ev_io* watcher, int revents) { } -int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb) { +int oio_listen(oio_handle* handle, int backlog, oio_accept_cb cb) { assert(handle->_.fd >= 0); int r = listen(handle->_.fd, backlog); if (r < 0) { - return ol_err_new(handle, errno); + return oio_err_new(handle, errno); } handle->accept_cb = cb; - ev_io_init(&handle->_.read_watcher, ol_server_io, handle->_.fd, EV_READ); + ev_io_init(&handle->_.read_watcher, oio_server_io, handle->_.fd, EV_READ); ev_io_start(EV_DEFAULT_ &handle->_.read_watcher); handle->_.read_watcher.data = handle; @@ -207,7 +207,7 @@ int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb) { } -int ol_close_error(ol_handle* handle, ol_err err) { +int oio_close_error(oio_handle* handle, oio_err err) { ev_io_stop(EV_DEFAULT_ &handle->_.read_watcher); close(handle->_.fd); handle->_.fd = -1; @@ -220,37 +220,37 @@ int ol_close_error(ol_handle* handle, ol_err err) { } -ol_req* ol_read_reqs_head(ol_handle* handle) { +oio_req* oio_read_reqs_head(oio_handle* handle) { ngx_queue_t* q = ngx_queue_head(&(handle->_.read_reqs)); if (!q) { return NULL; } - ol_req_private* p = ngx_queue_data(q, ol_req_private, read_reqs); + oio_req_private* p = ngx_queue_data(q, oio_req_private, read_reqs); assert(p); - int off = offsetof(ol_req, _); - ol_req* req = (ol_req*) ((char*)p - off); + int off = offsetof(oio_req, _); + oio_req* req = (oio_req*) ((char*)p - off); return req; } -int ol_read_reqs_empty(ol_handle* handle) { +int oio_read_reqs_empty(oio_handle* handle) { return ngx_queue_empty(&(handle->_.read_reqs)); } -void ol__read(ol_handle* handle) { +void oio__read(oio_handle* handle) { assert(handle->_.fd >= 0); /* Get the request at the head of the read_reqs queue. */ - ol_req* req = ol_read_reqs_head(handle); + oio_req* req = oio_read_reqs_head(handle); if (!req) { ev_io_stop(EV_DEFAULT_ &(handle->_.read_watcher)); return; } - /* Cast to iovec. We had to have our own ol_buf instead of iovec + /* Cast to iovec. We had to have our own oio_buf instead of iovec * because Windows's WSABUF is not an iovec. */ struct iovec* iov = (struct iovec*) req->_.read_bufs; @@ -263,18 +263,18 @@ void ol__read(ol_handle* handle) { ssize_t nread = readv(handle->_.fd, iov, iovcnt); - ol_read_cb cb = req->cb; + oio_read_cb cb = req->cb; if (nread < 0) { if (errno == EAGAIN) { /* Just wait for the next one. */ assert(ev_is_active(&(handle->_.read_watcher))); } else { - ol_err err = ol_err_new(handle, errno); + oio_err err = oio_err_new(handle, errno); if (cb) { cb(req, 0); } - ol_close_error(handle, errno); + oio_close_error(handle, errno); } } else { /* Successful read */ @@ -294,23 +294,23 @@ void ol__read(ol_handle* handle) { cb(req, nread); } - if (ol_read_reqs_empty(handle)) { + if (oio_read_reqs_empty(handle)) { ev_io_stop(EV_DEFAULT_ &(handle->_.read_watcher)); } } } -void ol_tcp_io(EV_P_ ev_io* watcher, int revents) { - ol_handle* handle = watcher->data; +void oio_tcp_io(EV_P_ ev_io* watcher, int revents) { + oio_handle* handle = watcher->data; assert(handle->_.fd >= 0); if (handle->_.connect_req) { - ol_tcp_connect(handle, handle->_.connect_req); + oio_tcp_connect(handle, handle->_.connect_req); } else { if (revents & EV_READ) { - ol__read(handle); + oio__read(handle); } if (revents & EV_WRITE) { @@ -325,7 +325,7 @@ void ol_tcp_io(EV_P_ ev_io* watcher, int revents) { * In order to determine if we've errored out or succeeded must call * getsockopt. */ -void ol_tcp_connect(ol_handle* handle, ol_req* req) { +void oio_tcp_connect(oio_handle* handle, oio_req* req) { assert(handle->_.fd >= 0); assert(req); @@ -334,16 +334,16 @@ void ol_tcp_connect(ol_handle* handle, ol_req* req) { getsockopt(handle->_.fd, SOL_SOCKET, SO_ERROR, &error, &errorsize); if (!error) { - ev_io_init(&handle->_.write_watcher, ol_tcp_io, handle->_.fd, EV_WRITE); - ev_set_cb(&handle->_.read_watcher, ol_tcp_io); + ev_io_init(&handle->_.write_watcher, oio_tcp_io, handle->_.fd, EV_WRITE); + ev_set_cb(&handle->_.read_watcher, oio_tcp_io); /* Successful connection */ - ol_connect_cb connect_cb = req->cb; + oio_connect_cb connect_cb = req->cb; if (connect_cb) { if (req->_.local) { - connect_cb(NULL, ol_err_new(handle, 0)); + connect_cb(NULL, oio_err_new(handle, 0)); } else { - connect_cb(req, ol_err_new(handle, 0)); + connect_cb(req, oio_err_new(handle, 0)); } } @@ -359,26 +359,26 @@ void ol_tcp_connect(ol_handle* handle, ol_req* req) { return; } else { - ol_err err = ol_err_new(handle, error); + oio_err err = oio_err_new(handle, error); if (req->_.connect_cb) { req->_.connect_cb(req, err); } - ol_close_error(handle, err); + oio_close_error(handle, err); } } -ol_req* ol_req_maybe_alloc(ol_handle* handle, ol_req* in_req) { +oio_req* oio_req_maybe_alloc(oio_handle* handle, oio_req* in_req) { if (in_req) { ngx_queue_init(&(in_req->_.read_reqs)); in_req->handle = handle; in_req->_.local = 0; return in_req; } else { - ol_req *req = malloc(sizeof(ol_req)); - ol_req_init(req, NULL); + oio_req *req = malloc(sizeof(oio_req)); + oio_req_init(req, NULL); req->handle = handle; ngx_queue_init(&(req->_.read_reqs)); req->_.local = 1; @@ -387,18 +387,18 @@ ol_req* ol_req_maybe_alloc(ol_handle* handle, ol_req* in_req) { } -int ol_connect(ol_handle* handle, ol_req *req_in, struct sockaddr* addr) { +int oio_connect(oio_handle* handle, oio_req *req_in, struct sockaddr* addr) { if (handle->_.connect_req) { - return ol_err_new(handle, EALREADY); + return oio_err_new(handle, EALREADY); } - if (handle->type != OL_TCP) { - return ol_err_new(handle, ENOTSOCK); + if (handle->type != OIO_TCP) { + return oio_err_new(handle, ENOTSOCK); } - ol_req *req = ol_req_maybe_alloc(handle, req_in); + oio_req *req = oio_req_maybe_alloc(handle, req_in); if (!req) { - return ol_err_new(handle, ENOMEM); + return oio_err_new(handle, ENOMEM); } handle->_.connect_req = req; @@ -419,30 +419,30 @@ int ol_connect(ol_handle* handle, ol_req *req_in, struct sockaddr* addr) { /* socket(2) failed */ if (handle->_.fd < 0) { - return ol_err_new(handle, errno); + return oio_err_new(handle, errno); } int r = connect(handle->_.fd, addr, addrsize); - ev_io_init(&handle->_.read_watcher, ol_tcp_io, handle->_.fd, EV_READ); - ev_io_init(&handle->_.write_watcher, ol_tcp_io, handle->_.fd, EV_WRITE); + ev_io_init(&handle->_.read_watcher, oio_tcp_io, handle->_.fd, EV_READ); + ev_io_init(&handle->_.write_watcher, oio_tcp_io, handle->_.fd, EV_WRITE); ev_io_start(EV_DEFAULT_ &handle->_.read_watcher); - return ol_err_new(handle, r); + return oio_err_new(handle, r); } -int ol_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt) { +int oio_write(oio_handle* handle, oio_req *req, oio_buf* bufs, int bufcnt) { assert(handle->_.fd >= 0); ssize_t r; r = writev(handle->_.fd, (struct iovec*)bufs, bufcnt); if (r < 0) { - return ol_err_new(handle, r); + return oio_err_new(handle, r); } else { if (req && req->cb) { - ol_write_cb cb = req->cb; + oio_write_cb cb = req->cb; cb(req); } return 0; @@ -450,19 +450,19 @@ int ol_write(ol_handle* handle, ol_req *req, ol_buf* bufs, int bufcnt) { } -int ol_write2(ol_handle* handle, const char* msg) { +int oio_write2(oio_handle* handle, const char* msg) { size_t len = strnlen(msg, 1024 * 1024); - ol_buf b; + oio_buf b; b.base = (char*)msg; b.len = len; - return ol_write(handle, NULL, &b, 1); + return oio_write(handle, NULL, &b, 1); } -int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) { +int oio_read(oio_handle* handle, oio_req *req_in, oio_buf* bufs, int bufcnt) { ssize_t nread = -1; errno = EAGAIN; - ol_read_cb cb = req_in->cb; + oio_read_cb cb = req_in->cb; assert(handle->_.fd >= 0); @@ -473,7 +473,7 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) { if (nread < 0 && errno != EAGAIN) { /* Real error. */ - ol_err err = ol_err_new(handle, errno); + oio_err err = oio_err_new(handle, errno); if (cb) { cb(req_in, nread); @@ -498,21 +498,21 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) { * - EAGAIN, meaning the socket is not wriable currently. We must wait for * it to become readable with the handle->_.read_watcher. * - The read_reqs queue already has reads. Meaning: the user has issued - * many ol_reads calls some of which are still waiting for the socket to + * many oio_reads calls some of which are still waiting for the socket to * become readable. * In the meantime we append the request to handle->_.read_reqs */ - ol_req* req = ol_req_maybe_alloc(handle, req_in); + oio_req* req = oio_req_maybe_alloc(handle, req_in); if (!req) { - return ol_err_new(handle, ENOMEM); + return oio_err_new(handle, ENOMEM); } - /* Copy the bufs data over into our ol_req struct. This is so the user can - * free the ol_buf array. The actual data inside the ol_bufs is however + /* Copy the bufs data over into our oio_req struct. This is so the user can + * free the oio_buf array. The actual data inside the oio_bufs is however * owned by the user and cannot be deallocated until the read completes. */ - req->_.read_bufs = malloc(sizeof(ol_buf) * bufcnt); - memcpy(req->_.read_bufs, bufs, bufcnt * sizeof(ol_buf)); + req->_.read_bufs = malloc(sizeof(oio_buf) * bufcnt); + memcpy(req->_.read_bufs, bufs, bufcnt * sizeof(oio_buf)); req->_.read_bufcnt = bufcnt; /* Append the request to read_reqs. */ @@ -520,19 +520,19 @@ int ol_read(ol_handle* handle, ol_req *req_in, ol_buf* bufs, int bufcnt) { ev_io_start(EV_DEFAULT_ &handle->_.read_watcher); - return ol_err_new(handle, EINPROGRESS); + return oio_err_new(handle, EINPROGRESS); } -void ol_free(ol_handle* handle) { +void oio_free(oio_handle* handle) { free(handle); /* lists? */ return; } -void ol_req_init(ol_req *req, void *cb) { - req->type = OL_UNKNOWN_REQ; +void oio_req_init(oio_req *req, void *cb) { + req->type = OIO_UNKNOWN_REQ; req->cb = cb; ngx_queue_init(&(req->_.read_reqs)); } diff --git a/ol-unix.h b/oio-unix.h similarity index 63% rename from ol-unix.h rename to oio-unix.h index e98edd3f..49d1b875 100644 --- a/ol-unix.h +++ b/oio-unix.h @@ -1,5 +1,5 @@ -#ifndef OL_UNIX_H -#define OL_UNIX_H +#ifndef OIO_UNIX_H +#define OIO_UNIX_H #include "ngx-queue.h" @@ -17,28 +17,28 @@ typedef struct { char* base; size_t len; -} ol_buf; +} oio_buf; typedef struct { int local; - ol_connect_cb connect_cb; + oio_connect_cb connect_cb; ngx_queue_t read_reqs; - ol_buf* read_bufs; + oio_buf* read_bufs; int read_bufcnt; -} ol_req_private; +} oio_req_private; typedef struct { int fd; - ol_err err; + oio_err err; - ol_read_cb read_cb; - ol_close_cb close_cb; + oio_read_cb read_cb; + oio_close_cb close_cb; - ol_req *connect_req; + oio_req *connect_req; ev_io read_watcher; ev_io write_watcher; @@ -46,7 +46,7 @@ typedef struct { ngx_queue_t write_queue; ngx_queue_t read_reqs; -} ol_handle_private; +} oio_handle_private; -#endif /* OL_UNIX_H */ +#endif /* OIO_UNIX_H */ diff --git a/ol-win.c b/oio-win.c similarity index 67% rename from ol-win.c rename to oio-win.c index 95492047..eb53b648 100644 --- a/ol-win.c +++ b/oio-win.c @@ -1,5 +1,5 @@ -#include "ol.h" +#include "oio.h" #include #include #include @@ -71,16 +71,16 @@ #endif /* - * Private ol_handle flags + * Private oio_handle flags */ -#define OL_HANDLE_CLOSING 0x01 -#define OL_HANDLE_CLOSED 0x03 +#define OIO_HANDLE_CLOSING 0x01 +#define OIO_HANDLE_CLOSED 0x03 /* - * Private ol_req flags. + * Private oio_req flags. */ /* The request is currently queued. */ -#define OL_REQ_PENDING 0x01 +#define OIO_REQ_PENDING 0x01 /* * Pointers to winsock extension functions that have to be retrieved dynamically @@ -94,21 +94,21 @@ LPFN_TRANSMITFILE pTransmitFile; /* * Global I/O completion port */ -HANDLE ol_iocp_; +HANDLE oio_iocp_; /* Global error code */ -int ol_errno_; +int oio_errno_; /* Reference count that keeps the event loop alive */ -int ol_refs_ = 0; +int oio_refs_ = 0; /* * Display an error message and abort the event loop. */ -void ol_fatal_error(const int errorno, const char *syscall) { +void oio_fatal_error(const int errorno, const char *syscall) { char *buf = NULL; const char *errmsg; @@ -142,7 +142,7 @@ void ol_fatal_error(const int errorno, const char *syscall) { /* * Retrieves the pointer to a winsock extension function. */ -void ol_get_extension_function(SOCKET socket, GUID guid, void **target) { +void oio_get_extension_function(SOCKET socket, GUID guid, void **target) { DWORD result, bytes; result = WSAIoctl(socket, @@ -157,12 +157,12 @@ void ol_get_extension_function(SOCKET socket, GUID guid, void **target) { if (result == SOCKET_ERROR) { *target = NULL; - ol_fatal_error(WSAGetLastError(), "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER)"); + oio_fatal_error(WSAGetLastError(), "WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER)"); } } -void ol_init() { +void oio_init() { const GUID wsaid_connectex = WSAID_CONNECTEX; const GUID wsaid_acceptex = WSAID_ACCEPTEX; const GUID wsaid_getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS; @@ -176,48 +176,48 @@ void ol_init() { /* Initialize winsock */ errorno = WSAStartup(MAKEWORD(2, 2), &wsa_data); if (errorno != 0) { - ol_fatal_error(errorno, "WSAStartup"); + oio_fatal_error(errorno, "WSAStartup"); } /* Retrieve the needed winsock extension function pointers. */ dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (dummy == INVALID_SOCKET) { - ol_fatal_error(WSAGetLastError(), "socket"); + oio_fatal_error(WSAGetLastError(), "socket"); } - ol_get_extension_function(dummy, wsaid_connectex, (void**)&pConnectEx ); - ol_get_extension_function(dummy, wsaid_acceptex, (void**)&pAcceptEx ); - ol_get_extension_function(dummy, wsaid_getacceptexsockaddrs, (void**)&pGetAcceptExSockAddrs); - ol_get_extension_function(dummy, wsaid_disconnectex, (void**)&pDisconnectEx ); - ol_get_extension_function(dummy, wsaid_transmitfile, (void**)&pTransmitFile ); + oio_get_extension_function(dummy, wsaid_connectex, (void**)&pConnectEx ); + oio_get_extension_function(dummy, wsaid_acceptex, (void**)&pAcceptEx ); + oio_get_extension_function(dummy, wsaid_getacceptexsockaddrs, (void**)&pGetAcceptExSockAddrs); + oio_get_extension_function(dummy, wsaid_disconnectex, (void**)&pDisconnectEx ); + oio_get_extension_function(dummy, wsaid_transmitfile, (void**)&pTransmitFile ); if (closesocket(dummy) == SOCKET_ERROR) { - ol_fatal_error(WSAGetLastError(), "closesocket"); + oio_fatal_error(WSAGetLastError(), "closesocket"); } /* Create an I/O completion port */ - ol_iocp_ = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); - if (ol_iocp_ == NULL) { - ol_fatal_error(GetLastError(), "CreateIoCompletionPort"); + oio_iocp_ = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); + if (oio_iocp_ == NULL) { + oio_fatal_error(GetLastError(), "CreateIoCompletionPort"); } } -void ol_req_init(ol_req* req, ol_handle* handle, void *cb) { - req->type = OL_UNKNOWN_REQ; +void oio_req_init(oio_req* req, oio_handle* handle, void *cb) { + req->type = OIO_UNKNOWN_REQ; req->flags = 0; req->handle = handle; req->cb = cb; } -ol_req* ol_overlapped_to_req(OVERLAPPED* overlapped) { - return CONTAINING_RECORD(overlapped, ol_req, overlapped); +oio_req* oio_overlapped_to_req(OVERLAPPED* overlapped) { + return CONTAINING_RECORD(overlapped, oio_req, overlapped); } -int ol_set_socket_options(SOCKET socket) { +int oio_set_socket_options(SOCKET socket) { DWORD yes = 1; /* Set the SO_REUSEADDR option on the socket */ @@ -230,17 +230,17 @@ int ol_set_socket_options(SOCKET socket) { /* Make the socket non-inheritable */ if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) { - ol_errno_ = GetLastError(); + oio_errno_ = GetLastError(); return -1; } /* Associate it with the I/O completion port. */ - /* Use ol_handle pointer as completion key. */ + /* Use oio_handle pointer as completion key. */ if (CreateIoCompletionPort((HANDLE)socket, - ol_iocp_, + oio_iocp_, (ULONG_PTR)socket, 0) == NULL) { - ol_errno_ = GetLastError(); + oio_errno_ = GetLastError(); return -1; } @@ -248,10 +248,10 @@ int ol_set_socket_options(SOCKET socket) { } -int ol_tcp_handle_init(ol_handle *handle, ol_close_cb close_cb, void* data) { +int oio_tcp_handle_init(oio_handle *handle, oio_close_cb close_cb, void* data) { handle->close_cb = close_cb; handle->data = data; - handle->type = OL_TCP; + handle->type = OIO_TCP; handle->flags = 0; handle->reqs_pending = 0; handle->error = 0; @@ -259,31 +259,31 @@ int ol_tcp_handle_init(ol_handle *handle, ol_close_cb close_cb, void* data) { handle->socket = socket(AF_INET, SOCK_STREAM, 0); if (handle->socket == INVALID_SOCKET) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); return -1; } - if (ol_set_socket_options(handle->socket) != 0) { + if (oio_set_socket_options(handle->socket) != 0) { closesocket(handle->socket); return -1; } - ol_refs_++; + oio_refs_++; return 0; } -int ol_tcp_handle_accept(ol_handle* server, ol_handle* client, ol_close_cb close_cb, void* data) { +int oio_tcp_handle_accept(oio_handle* server, oio_handle* client, oio_close_cb close_cb, void* data) { if (!server->accept_data || server->accept_data->socket == INVALID_SOCKET) { - ol_errno_ = WSAENOTCONN; + oio_errno_ = WSAENOTCONN; return -1; } client->close_cb = close_cb; client->data = data; - client->type = OL_TCP; + client->type = OIO_TCP; client->socket = server->accept_data->socket; client->flags = 0; client->reqs_pending = 0; @@ -291,39 +291,39 @@ int ol_tcp_handle_accept(ol_handle* server, ol_handle* client, ol_close_cb close client->accept_data = NULL; server->accept_data->socket = INVALID_SOCKET; - ol_refs_++; + oio_refs_++; return 0; } -int ol_close_error(ol_handle* handle, ol_err e) { - ol_req *req; +int oio_close_error(oio_handle* handle, oio_err e) { + oio_req *req; - if (handle->flags & OL_HANDLE_CLOSING) + if (handle->flags & OIO_HANDLE_CLOSING) return 0; handle->error = e; switch (handle->type) { - case OL_TCP: + case OIO_TCP: closesocket(handle->socket); if (handle->reqs_pending == 0) { /* If there are no operations queued for this socket, queue one */ - /* manually, so ol_poll will call close_cb. */ - req = (ol_req*)malloc(sizeof(*req)); + /* manually, so oio_poll will call close_cb. */ + req = (oio_req*)malloc(sizeof(*req)); req->handle = handle; - req->type = OL_CLOSE; + req->type = OIO_CLOSE; req->flags = 0; - if (!PostQueuedCompletionStatus(ol_iocp_, 0, (ULONG_PTR)handle, &req->overlapped)) - ol_fatal_error(GetLastError(), "PostQueuedCompletionStatus"); - req->flags |= OL_REQ_PENDING; + if (!PostQueuedCompletionStatus(oio_iocp_, 0, (ULONG_PTR)handle, &req->overlapped)) + oio_fatal_error(GetLastError(), "PostQueuedCompletionStatus"); + req->flags |= OIO_REQ_PENDING; handle->reqs_pending++; } - /* After all packets to come out, ol_poll will call close_cb. */ - handle->flags |= OL_HANDLE_CLOSING; + /* After all packets to come out, oio_poll will call close_cb. */ + handle->flags |= OIO_HANDLE_CLOSING; return 0; default: @@ -334,12 +334,12 @@ int ol_close_error(ol_handle* handle, ol_err e) { } -int ol_close(ol_handle* handle) { - return ol_close_error(handle, 0); +int oio_close(oio_handle* handle) { + return oio_close_error(handle, 0); } -struct sockaddr_in ol_ip4_addr(char *ip, int port) { +struct sockaddr_in oio_ip4_addr(char *ip, int port) { struct sockaddr_in addr; addr.sin_family = AF_INET; @@ -350,7 +350,7 @@ struct sockaddr_in ol_ip4_addr(char *ip, int port) { } -int ol_bind(ol_handle* handle, struct sockaddr* addr) { +int oio_bind(oio_handle* handle, struct sockaddr* addr) { int addrsize; if (addr->sa_family == AF_INET) { @@ -363,7 +363,7 @@ int ol_bind(ol_handle* handle, struct sockaddr* addr) { } if (bind(handle->socket, addr, addrsize) == SOCKET_ERROR) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); return -1; } @@ -371,8 +371,8 @@ int ol_bind(ol_handle* handle, struct sockaddr* addr) { } -void ol_queue_accept(ol_handle *handle) { - ol_accept_data* data; +void oio_queue_accept(oio_handle *handle) { + oio_accept_data* data; BOOL success; DWORD bytes; @@ -381,19 +381,19 @@ void ol_queue_accept(ol_handle *handle) { data->socket = socket(AF_INET, SOCK_STREAM, 0); if (data->socket == INVALID_SOCKET) { - ol_close_error(handle, WSAGetLastError()); + oio_close_error(handle, WSAGetLastError()); return; } - if (ol_set_socket_options(data->socket) != 0) { + if (oio_set_socket_options(data->socket) != 0) { closesocket(data->socket); - ol_close_error(handle, ol_errno_); + oio_close_error(handle, oio_errno_); return; } - /* Prepare the ol_req and OVERLAPPED structures. */ - assert(!(data->req.flags & OL_REQ_PENDING)); - data->req.flags |= OL_REQ_PENDING; + /* Prepare the oio_req and OVERLAPPED structures. */ + assert(!(data->req.flags & OIO_REQ_PENDING)); + data->req.flags |= OIO_REQ_PENDING; memset(&data->req.overlapped, 0, sizeof(data->req.overlapped)); success = pAcceptEx(handle->socket, @@ -406,58 +406,58 @@ void ol_queue_accept(ol_handle *handle) { &data->req.overlapped); if (!success && WSAGetLastError() != ERROR_IO_PENDING) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); /* destroy the preallocated client handle */ closesocket(data->socket); /* destroy ourselves */ - ol_close_error(handle, ol_errno_); + oio_close_error(handle, oio_errno_); return; } handle->reqs_pending++; - data->req.flags |= OL_REQ_PENDING; + data->req.flags |= OIO_REQ_PENDING; } -int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb) { - ol_accept_data *data; +int oio_listen(oio_handle* handle, int backlog, oio_accept_cb cb) { + oio_accept_data *data; if (handle->accept_data != NULL) { /* Already listening. */ - ol_errno_ = WSAEALREADY; + oio_errno_ = WSAEALREADY; return -1; } - data = (ol_accept_data*)malloc(sizeof(*data)); + data = (oio_accept_data*)malloc(sizeof(*data)); if (!data) { - ol_errno_ = WSAENOBUFS; + oio_errno_ = WSAENOBUFS; return -1; } data->socket = INVALID_SOCKET; - ol_req_init(&data->req, handle, (void*)cb); - data->req.type = OL_ACCEPT; + oio_req_init(&data->req, handle, (void*)cb); + data->req.type = OIO_ACCEPT; if (listen(handle->socket, backlog) == SOCKET_ERROR) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); free(data); return -1; } handle->accept_data = data; - ol_queue_accept(handle); + oio_queue_accept(handle); return 0; } -int ol_connect(ol_req* req, struct sockaddr* addr) { +int oio_connect(oio_req* req, struct sockaddr* addr) { int addrsize; BOOL success; DWORD bytes; - ol_handle* handle = req->handle; + oio_handle* handle = req->handle; - assert(!(req->flags & OL_REQ_PENDING)); + assert(!(req->flags & OIO_REQ_PENDING)); if (addr->sa_family == AF_INET) { addrsize = sizeof(struct sockaddr_in); @@ -469,7 +469,7 @@ int ol_connect(ol_req* req, struct sockaddr* addr) { } memset(&req->overlapped, 0, sizeof(req->overlapped)); - req->type = OL_CONNECT; + req->type = OIO_CONNECT; success = pConnectEx(handle->socket, addr, @@ -480,26 +480,26 @@ int ol_connect(ol_req* req, struct sockaddr* addr) { &req->overlapped); if (!success && WSAGetLastError() != ERROR_IO_PENDING) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); return -1; } - req->flags |= OL_REQ_PENDING; + req->flags |= OIO_REQ_PENDING; handle->reqs_pending++; return 0; } -int ol_write(ol_req *req, ol_buf* bufs, int bufcnt) { +int oio_write(oio_req *req, oio_buf* bufs, int bufcnt) { int result; DWORD bytes; - ol_handle* handle = req->handle; + oio_handle* handle = req->handle; - assert(!(req->flags & OL_REQ_PENDING)); + assert(!(req->flags & OIO_REQ_PENDING)); memset(&req->overlapped, 0, sizeof(req->overlapped)); - req->type = OL_WRITE; + req->type = OIO_WRITE; result = WSASend(handle->socket, (WSABUF*)bufs, @@ -509,26 +509,26 @@ int ol_write(ol_req *req, ol_buf* bufs, int bufcnt) { &req->overlapped, NULL); if (result != 0 && WSAGetLastError() != ERROR_IO_PENDING) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); return -1; } - req->flags |= OL_REQ_PENDING; + req->flags |= OIO_REQ_PENDING; handle->reqs_pending++; return 0; } -int ol_read(ol_req *req, ol_buf* bufs, int bufcnt) { +int oio_read(oio_req *req, oio_buf* bufs, int bufcnt) { int result; DWORD bytes, flags; - ol_handle* handle = req->handle; + oio_handle* handle = req->handle; - assert(!(req->flags & OL_REQ_PENDING)); + assert(!(req->flags & OIO_REQ_PENDING)); memset(&req->overlapped, 0, sizeof(req->overlapped)); - req->type = OL_READ; + req->type = OIO_READ; flags = 0; result = WSARecv(handle->socket, @@ -539,63 +539,63 @@ int ol_read(ol_req *req, ol_buf* bufs, int bufcnt) { &req->overlapped, NULL); if (result != 0 && WSAGetLastError() != ERROR_IO_PENDING) { - ol_errno_ = WSAGetLastError(); + oio_errno_ = WSAGetLastError(); return -1; } - req->flags |= OL_REQ_PENDING; + req->flags |= OIO_REQ_PENDING; handle->reqs_pending++; return 0; } -int ol_write2(ol_req *req, const char* msg) { - ol_buf buf; - ol_handle* handle = req->handle; +int oio_write2(oio_req *req, const char* msg) { + oio_buf buf; + oio_handle* handle = req->handle; buf.base = (char*)msg; buf.len = strlen(msg); - return ol_write(req, &buf, 1); + return oio_write(req, &buf, 1); } -ol_err ol_last_error() { - return ol_errno_; +oio_err oio_last_error() { + return oio_errno_; } -void ol_poll() { +void oio_poll() { BOOL success; DWORD bytes; ULONG_PTR key; OVERLAPPED* overlapped; - ol_req* req; - ol_handle* handle; - ol_accept_data *data; + oio_req* req; + oio_handle* handle; + oio_accept_data *data; - success = GetQueuedCompletionStatus(ol_iocp_, + success = GetQueuedCompletionStatus(oio_iocp_, &bytes, &key, &overlapped, INFINITE); if (!success && !overlapped) - ol_fatal_error(GetLastError(), "GetQueuedCompletionStatus"); + oio_fatal_error(GetLastError(), "GetQueuedCompletionStatus"); - req = ol_overlapped_to_req(overlapped); + req = oio_overlapped_to_req(overlapped); handle = req->handle; /* Mark the request non-pending */ - req->flags &= ~OL_REQ_PENDING; + req->flags &= ~OIO_REQ_PENDING; handle->reqs_pending--; /* If the related socket got closed in the meantime, disregard this */ /* result. If this is the last request pending, call the handle's close callback. */ - if (handle->flags & OL_HANDLE_CLOSING) { + if (handle->flags & OIO_HANDLE_CLOSING) { if (handle->reqs_pending == 0) { - handle->flags |= OL_HANDLE_CLOSED; + handle->flags |= OIO_HANDLE_CLOSED; if (handle->accept_data) { if (handle->accept_data) { if (handle->accept_data->socket) { @@ -608,41 +608,41 @@ void ol_poll() { if (handle->close_cb) { handle->close_cb(handle, handle->error); } - ol_refs_--; + oio_refs_--; } return; } switch (req->type) { - case OL_WRITE: + case OIO_WRITE: success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); if (!success) { - ol_close_error(handle, GetLastError()); + oio_close_error(handle, GetLastError()); } else if (req->cb) { - ((ol_write_cb)req->cb)(req); + ((oio_write_cb)req->cb)(req); } return; - case OL_READ: + case OIO_READ: success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); if (!success) { - ol_close_error(handle, GetLastError()); + oio_close_error(handle, GetLastError()); } else if (req->cb) { - ((ol_read_cb)req->cb)(req, bytes); + ((oio_read_cb)req->cb)(req, bytes); } break; - case OL_ACCEPT: + case OIO_ACCEPT: data = handle->accept_data; assert(data != NULL); assert(data->socket != INVALID_SOCKET); success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); if (success && req->cb) { - ((ol_accept_cb)req->cb)(handle); + ((oio_accept_cb)req->cb)(handle); } - /* accept_cb should call ol_accept_handle which sets data->socket */ + /* accept_cb should call oio_accept_handle which sets data->socket */ /* to INVALID_SOCKET. */ /* Just ignore failed accept if the listen socket is still healthy. */ if (data->socket != INVALID_SOCKET) { @@ -651,31 +651,31 @@ void ol_poll() { } /* Queue another accept */ - ol_queue_accept(handle); + oio_queue_accept(handle); return; - case OL_CONNECT: + case OIO_CONNECT: if (req->cb) { success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); if (success) { - ((ol_connect_cb)req->cb)(req, 0); + ((oio_connect_cb)req->cb)(req, 0); } else { - ((ol_connect_cb)req->cb)(req, GetLastError()); + ((oio_connect_cb)req->cb)(req, GetLastError()); } } return; - case OL_CLOSE: + case OIO_CLOSE: /* Should never get here */ assert(0); } } -int ol_run() { - while (ol_refs_ > 0) { - ol_poll(); +int oio_run() { + while (oio_refs_ > 0) { + oio_poll(); } - assert(ol_refs_ == 0); + assert(oio_refs_ == 0); return 0; } diff --git a/ol-win.h b/oio-win.h similarity index 67% rename from ol-win.h rename to oio-win.h index 03000b74..6477d339 100644 --- a/ol-win.h +++ b/oio-win.h @@ -6,37 +6,37 @@ /** - * It should be possible to cast ol_buf[] to WSABUF[] + * It should be possible to cast oio_buf[] to WSABUF[] * see http://msdn.microsoft.com/en-us/library/ms741542(v=vs.85).aspx */ -typedef struct _ol_buf { +typedef struct oio_buf { ULONG len; char* base; -} ol_buf; +} oio_buf; -struct ol_req_s { - struct ol_req_shared_s; +struct oio_req_s { + struct oio_req_shared_s; OVERLAPPED overlapped; int flags; }; typedef struct { - ol_req req; + oio_req req; SOCKET socket; /* AcceptEx specifies that the buffer must be big enough to at least hold */ /* two socket addresses plus 32 bytes. */ char buffer[sizeof(struct sockaddr_storage) * 2 + 32]; -} ol_accept_data; +} oio_accept_data; -struct ol_handle_s { - struct ol_handle_shared_s; +struct oio_handle_s { + struct oio_handle_shared_s; union { SOCKET socket; HANDLE handle; }; - ol_accept_data *accept_data; + oio_accept_data *accept_data; unsigned int flags; unsigned int reqs_pending; - ol_err error; + oio_err error; }; diff --git a/oio.h b/oio.h new file mode 100644 index 00000000..647b1645 --- /dev/null +++ b/oio.h @@ -0,0 +1,110 @@ +#ifndef OIO_H +#define OIO_H + +#include /* size_t */ + + +typedef int oio_err; /* FIXME */ + +typedef struct oio_req_s oio_req; +typedef struct oio_handle_s oio_handle; + +typedef void (*oio_read_cb)(oio_req* req, size_t nread); +typedef void (*oio_write_cb)(oio_req* req); +typedef void (*oio_accept_cb)(oio_handle* handle); +typedef void (*oio_close_cb)(oio_handle* handle, oio_err e); +typedef void (*oio_connect_cb)(oio_req* req, oio_err e); +typedef void (*oio_shutdown_cb)(oio_req* req); + + +typedef enum { + OIO_UNKNOWN_HANDLE = 0, + OIO_TCP, + OIO_NAMED_PIPE, + OIO_TTY, + OIO_FILE, +} oio_handle_type; + +typedef enum { + OIO_UNKNOWN_REQ = 0, + OIO_CONNECT, + OIO_ACCEPT, + OIO_READ, + OIO_WRITE, + OIO_SHUTDOWN, + OIO_CLOSE +} oio_req_type; + + +struct oio_handle_shared_s { + /* read-only */ + oio_handle_type type; + /* public */ + oio_close_cb close_cb; + void* data; +}; + +struct oio_req_shared_s { + /* read-only */ + oio_req_type type; + /* public */ + oio_handle* handle; + void* cb; + void* data; +}; + + +#if defined(__unix__) || defined(__POSIX__) || defined(__APPLE__) +# include "oio-unix.h" +#else +# include "oio-win.h" +#endif + + +/** + * Most functions return boolean: 0 for success and -1 for failure. + * On error the user should then call oio_last_error() to determine + * the error code. + */ +oio_err oio_last_error(); +const char* oio_err_str(oio_err err); + + +void oio_init(); +int oio_run(); + +void oio_req_init(oio_req* req, oio_handle* handle, void* cb); + +/* + * TODO: + * - oio_(pipe|pipe_tty)_handle_init + * - oio_bind_pipe(char *name) + * - oio_continuous_read(oio_handle *handle, oio_continuous_read_cb *cb) + * - A way to list cancelled oio_reqs after before/on oio_close_cb + */ + +/* TCP socket methods. */ +/* Handle and callback bust be set by calling oio_req_init. */ +int oio_tcp_handle_init(oio_handle *handle, oio_close_cb close_cb, void* data); +int oio_bind(oio_handle* handle, struct sockaddr* addr); +int oio_connect(oio_req* req, struct sockaddr* addr); +int oio_shutdown(oio_req* req); + +/* TCP server methods. */ +int oio_listen(oio_handle* handle, int backlog, oio_accept_cb cb); +int oio_tcp_handle_accept(oio_handle* server, oio_handle* client, oio_close_cb close_cb, void* data); + +/* Generic handle methods */ +int oio_read(oio_req* req, oio_buf* bufs, int bufcnt); +int oio_write(oio_req* req, oio_buf* bufs, int bufcnt); +int oio_write2(oio_req *req, const char* msg); + +/* Request handle to be closed. close_cb will be called */ +/* asynchronously after this call. */ +int oio_close(oio_handle* handle); + + +/* Utility */ +struct sockaddr_in oio_ip4_addr(char* ip, int port); + +#endif /* OIO_H */ diff --git a/ol.h b/ol.h deleted file mode 100644 index 73535509..00000000 --- a/ol.h +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef OL_H -#define OL_H - -#include /* size_t */ - - -typedef int ol_err; /* FIXME */ - -typedef struct ol_req_s ol_req; -typedef struct ol_handle_s ol_handle; - -typedef void (*ol_read_cb)(ol_req* req, size_t nread); -typedef void (*ol_write_cb)(ol_req* req); -typedef void (*ol_accept_cb)(ol_handle* handle); -typedef void (*ol_close_cb)(ol_handle* handle, ol_err e); -typedef void (*ol_connect_cb)(ol_req* req, ol_err e); -typedef void (*ol_shutdown_cb)(ol_req* req); - - -typedef enum { - OL_UNKNOWN_HANDLE = 0, - OL_TCP, - OL_NAMED_PIPE, - OL_TTY, - OL_FILE, -} ol_handle_type; - -typedef enum { - OL_UNKNOWN_REQ = 0, - OL_CONNECT, - OL_ACCEPT, - OL_READ, - OL_WRITE, - OL_SHUTDOWN, - OL_CLOSE -} ol_req_type; - - -struct ol_handle_shared_s { - /* read-only */ - ol_handle_type type; - /* public */ - ol_close_cb close_cb; - void* data; -}; - -struct ol_req_shared_s { - /* read-only */ - ol_req_type type; - /* public */ - ol_handle* handle; - void* cb; - void* data; -}; - - -#if defined(__unix__) || defined(__POSIX__) || defined(__APPLE__) -# include "ol-unix.h" -#else -# include "ol-win.h" -#endif - - -/** - * 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); - - -void ol_init(); -int ol_run(); - -void ol_req_init(ol_req* req, ol_handle* handle, void* cb); - -/* - * TODO: - * - ol_(pipe|pipe_tty)_handle_init - * - ol_bind_pipe(char *name) - * - ol_continuous_read(ol_handle *handle, ol_continuous_read_cb *cb) - * - A way to list cancelled ol_reqs after before/on ol_close_cb - */ - -/* TCP socket methods. */ -/* Handle and callback bust be set by calling ol_req_init. */ -int ol_tcp_handle_init(ol_handle *handle, ol_close_cb close_cb, void* data); -int ol_bind(ol_handle* handle, struct sockaddr* addr); -int ol_connect(ol_req* req, struct sockaddr* addr); -int ol_shutdown(ol_req* req); - -/* TCP server methods. */ -int ol_listen(ol_handle* handle, int backlog, ol_accept_cb cb); -int ol_tcp_handle_accept(ol_handle* server, ol_handle* client, ol_close_cb close_cb, void* data); - -/* Generic handle methods */ -int ol_read(ol_req* req, ol_buf* bufs, int bufcnt); -int ol_write(ol_req* req, ol_buf* bufs, int bufcnt); -int ol_write2(ol_req *req, const char* msg); - -/* Request handle to be closed. close_cb will be called */ -/* asynchronously after this call. */ -int ol_close(ol_handle* handle); - - -/* Utility */ -struct sockaddr_in ol_ip4_addr(char* ip, int port); - -#endif /* OL_H */ diff --git a/test/echo-server.c b/test/echo-server.c index f2fcaa02..6ebd4ebd 100644 --- a/test/echo-server.c +++ b/test/echo-server.c @@ -1,4 +1,4 @@ -#include "../ol.h" +#include "../oio.h" #include "test.h" #include #include @@ -6,39 +6,39 @@ #define BUFSIZE 1024 typedef struct { - ol_handle handle; - ol_req req; - ol_buf buf; + oio_handle handle; + oio_req req; + oio_buf buf; char read_buffer[BUFSIZE]; } peer_t; -ol_handle server; +oio_handle server; -void after_write(ol_req* req); -void after_read(ol_req* req, size_t nread); +void after_write(oio_req* req); +void after_read(oio_req* req, size_t nread); void try_read(peer_t* peer); -void on_close(ol_handle* peer, ol_err err); -void on_accept(ol_handle* handle); +void on_close(oio_handle* peer, oio_err err); +void on_accept(oio_handle* handle); -void after_write(ol_req* req) { +void after_write(oio_req* req) { peer_t* peer = (peer_t*) req->data; try_read(peer); } -void after_read(ol_req* req, size_t nread) { +void after_read(oio_req* req, size_t nread) { peer_t* peer; int r; if (nread == 0) { - ol_close(req->handle); + oio_close(req->handle); } else { peer = (peer_t*) req->data; peer->buf.len = nread; - ol_req_init(&peer->req, &peer->handle, after_write); + oio_req_init(&peer->req, &peer->handle, after_write); peer->req.data = peer; - r = ol_write(&peer->req, &peer->buf, 1); + r = oio_write(&peer->req, &peer->buf, 1); assert(!r); } } @@ -48,25 +48,25 @@ void try_read(peer_t* peer) { int r; peer->buf.len = BUFSIZE; - ol_req_init(&peer->req, &peer->handle, after_read); + oio_req_init(&peer->req, &peer->handle, after_read); peer->req.data = peer; - r = ol_read(&peer->req, &peer->buf, 1); + r = oio_read(&peer->req, &peer->buf, 1); assert(!r); } -void on_close(ol_handle* peer, ol_err err) { +void on_close(oio_handle* peer, oio_err err) { if (err) { fprintf(stdout, "Socket error\n"); } } -void on_accept(ol_handle* server) { +void on_accept(oio_handle* server) { peer_t* p = (peer_t*)malloc(sizeof(peer_t)); int r; - r = ol_tcp_handle_accept(server, &p->handle, on_close, (void*)p); + r = oio_tcp_handle_accept(server, &p->handle, on_close, (void*)p); assert(!r); p->buf.base = (char*)&p->read_buffer; @@ -75,7 +75,7 @@ void on_accept(ol_handle* server) { } -void on_server_close(ol_handle* handle, ol_err err) { +void on_server_close(oio_handle* handle, oio_err err) { assert(handle == &server); if (err) { @@ -85,24 +85,24 @@ void on_server_close(ol_handle* handle, ol_err err) { int echo_start(int port) { - struct sockaddr_in addr = ol_ip4_addr("0.0.0.0", port); + struct sockaddr_in addr = oio_ip4_addr("0.0.0.0", port); int r; - r = ol_tcp_handle_init(&server, on_server_close, NULL); + r = oio_tcp_handle_init(&server, on_server_close, NULL); if (r) { /* TODO: Error codes */ fprintf(stderr, "Socket creation error\n"); return 1; } - r = ol_bind(&server, (struct sockaddr*) &addr); + r = oio_bind(&server, (struct sockaddr*) &addr); if (r) { /* TODO: Error codes */ fprintf(stderr, "Bind error\n"); return 1; } - r = ol_listen(&server, 128, on_accept); + r = oio_listen(&server, 128, on_accept); if (r) { /* TODO: Error codes */ fprintf(stderr, "Listen error\n"); @@ -114,16 +114,16 @@ int echo_start(int port) { int echo_stop() { - return ol_close(&server); + return oio_close(&server); } TEST_IMPL(echo_server) { - ol_init(); + oio_init(); if (echo_start(TEST_PORT)) return 1; fprintf(stderr, "Listening!\n"); - ol_run(); + oio_run(); return 0; } \ No newline at end of file diff --git a/test/test-callback-stack.c b/test/test-callback-stack.c index 1ab1e607..50a83b78 100644 --- a/test/test-callback-stack.c +++ b/test/test-callback-stack.c @@ -1,4 +1,4 @@ -#include "../ol.h" +#include "../oio.h" #include "test.h" @@ -6,30 +6,30 @@ int nested = 0; int close_cb_called = 0; -void close_cb(ol_handle *handle, ol_err e) { - assert("ol_close error" && e == 0); - assert("ol_close_cb not called from a fresh stack" && nested == 0); +void close_cb(oio_handle *handle, oio_err e) { + assert("oio_close error" && e == 0); + assert("oio_close_cb not called from a fresh stack" && nested == 0); close_cb_called++; } TEST_IMPL(close_cb_stack) { - ol_handle handle; + oio_handle handle; int r; - ol_init(); + oio_init(); - r = ol_tcp_handle_init(&handle, &close_cb, NULL); + r = oio_tcp_handle_init(&handle, &close_cb, NULL); assert(!r); nested++; - r = ol_close(&handle); + r = oio_close(&handle); assert(!r); nested--; - ol_run(); + oio_run(); - assert("ol_close_cb not called exactly once" && close_cb_called); + assert("oio_close_cb not called exactly once" && close_cb_called); return 0; } \ No newline at end of file diff --git a/test/test-ping-pong.c b/test/test-ping-pong.c index 097db875..5e18065e 100644 --- a/test/test-ping-pong.c +++ b/test/test-ping-pong.c @@ -1,4 +1,4 @@ -#include "../ol.h" +#include "../oio.h" #include "test.h" #include #include @@ -17,17 +17,17 @@ static char PING[] = "PING\n"; typedef struct { int pongs; int state; - ol_handle handle; - ol_req connect_req; - ol_req read_req; - ol_buf buf; + oio_handle handle; + oio_req connect_req; + oio_req read_req; + oio_buf buf; char read_buffer[BUFSIZE]; } pinger_t; void pinger_try_read(pinger_t* pinger); -void pinger_on_close(ol_handle* handle, ol_err err) { +void pinger_on_close(oio_handle* handle, oio_err err) { pinger_t* pinger = (pinger_t*)handle->data; assert(!err); @@ -39,29 +39,29 @@ void pinger_on_close(ol_handle* handle, ol_err err) { } -void pinger_after_write(ol_req *req) { +void pinger_after_write(oio_req *req) { free(req); } void pinger_write_ping(pinger_t* pinger) { - ol_req *req; + oio_req *req; int r; - req = (ol_req*)malloc(sizeof(*req)); - ol_req_init(req, &pinger->handle, pinger_after_write); - r = ol_write2(req, (char*)&PING); + req = (oio_req*)malloc(sizeof(*req)); + oio_req_init(req, &pinger->handle, pinger_after_write); + r = oio_write2(req, (char*)&PING); assert(!r); } -void pinger_after_read(ol_req* req, size_t nread) { +void pinger_after_read(oio_req* req, size_t nread) { unsigned int i; pinger_t* pinger; pinger = (pinger_t*)req->handle->data; if (nread == 0) { - ol_close(&pinger->handle); + oio_close(&pinger->handle); return; } @@ -74,7 +74,7 @@ void pinger_after_read(ol_req* req, size_t nread) { if (pinger->pongs < NUM_PINGS) { pinger_write_ping(pinger); } else { - ol_close(&pinger->handle); + oio_close(&pinger->handle); return; } } @@ -85,11 +85,11 @@ void pinger_after_read(ol_req* req, size_t nread) { void pinger_try_read(pinger_t* pinger) { - ol_read(&pinger->read_req, &pinger->buf, 1); + oio_read(&pinger->read_req, &pinger->buf, 1); } -void pinger_on_connect(ol_req *req, ol_err err) { +void pinger_on_connect(oio_req *req, oio_err err) { pinger_t *pinger = (pinger_t*)req->handle->data; if (err) { @@ -102,8 +102,8 @@ void pinger_on_connect(ol_req *req, ol_err err) { int pinger_new(int port) { - struct sockaddr_in client_addr = ol_ip4_addr("0.0.0.0", 0); - struct sockaddr_in server_addr = ol_ip4_addr("145.94.50.9", TEST_PORT); + struct sockaddr_in client_addr = oio_ip4_addr("0.0.0.0", 0); + struct sockaddr_in server_addr = oio_ip4_addr("145.94.50.9", TEST_PORT); pinger_t *pinger; pinger = (pinger_t*)malloc(sizeof(*pinger)); @@ -113,28 +113,28 @@ int pinger_new(int port) { pinger->buf.base = (char*)&pinger->read_buffer; /* Try to connec to the server and do NUM_PINGS ping-pongs. */ - if (ol_tcp_handle_init(&pinger->handle, pinger_on_close, (void*)pinger)) { + if (oio_tcp_handle_init(&pinger->handle, pinger_on_close, (void*)pinger)) { return -1; } /* We are never doing multiple reads/connects at a time anyway. */ /* so these handles can be pre-initialized. */ - ol_req_init(&pinger->connect_req, &pinger->handle, pinger_on_connect); - ol_req_init(&pinger->read_req, &pinger->handle, pinger_after_read); + oio_req_init(&pinger->connect_req, &pinger->handle, pinger_on_connect); + oio_req_init(&pinger->read_req, &pinger->handle, pinger_after_read); - ol_bind(&pinger->handle, (struct sockaddr*)&client_addr); - return ol_connect(&pinger->connect_req, (struct sockaddr*)&server_addr); + oio_bind(&pinger->handle, (struct sockaddr*)&client_addr); + return oio_connect(&pinger->connect_req, (struct sockaddr*)&server_addr); } TEST_IMPL(ping_pong) { - ol_init(); + oio_init(); if (pinger_new(8000)) { return 2; } - ol_run(); + oio_run(); assert(completed_pingers == 1); diff --git a/test/test-runner-win32.c b/test/test-runner-win32.c index 638c229c..622aa444 100644 --- a/test/test-runner-win32.c +++ b/test/test-runner-win32.c @@ -19,7 +19,7 @@ int process_start(char *name, process_info_t *p) { if (GetTempPathW(sizeof(path) / sizeof(WCHAR), (WCHAR*)&path) == 0) goto error; - if (GetTempFileNameW((WCHAR*)&path, L"ol_", 0, (WCHAR*)&filename) == 0) + if (GetTempFileNameW((WCHAR*)&path, L"oio", 0, (WCHAR*)&filename) == 0) goto error; file = CreateFileW((WCHAR*)filename,