ol -> oio

This commit is contained in:
Bert Belder 2011-04-07 11:02:54 +02:00
parent 0d85eb252b
commit 39d31a100f
15 changed files with 535 additions and 533 deletions

4
.gitignore vendored
View File

@ -19,8 +19,8 @@ ev/libtool
ev/stamp-h1 ev/stamp-h1
ev/autom4te.cache ev/autom4te.cache
/ipch/ /ipch/
/Debug/ /Win32/
/Release/ /x64/
test/echo-demo test/echo-demo
test/test-ping-pong test/test-ping-pong

View File

@ -1,16 +1,16 @@
all: test/echo-demo test/test-ping-pong all: test/echo-demo test/test-ping-pong
test/echo-demo: test/echo-demo.c test/echo.o ol.a 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 ol.a -lm $(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 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 ol.a -lm $(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 oio.a: oio-unix.o ev/ev.o
$(AR) rcs ol.a ol-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 oio-unix.o: oio-unix.c oio.h oio-unix.h
$(CC) -ansi -g -c ol-unix.c -o ol-unix.o $(CC) -ansi -g -c oio-unix.c -o oio-unix.o
test/echo.o: test/echo.c test/echo.h test/echo.o: test/echo.c test/echo.h
$(CC) -ansi -g -c test/echo.c -o test/echo.o $(CC) -ansi -g -c test/echo.c -o test/echo.o

View File

@ -20,6 +20,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<ProjectGuid>{1D7C3F6C-A4AF-DD73-2D20-B2FC919B3744}</ProjectGuid>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -150,7 +151,7 @@
<ClInclude Include="test\test.h" /> <ClInclude Include="test\test.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="libol.vcxproj"> <ProjectReference Include="liboio.vcxproj">
<Project>{301fe650-cd34-14e5-6b63-42e383fa02bc}</Project> <Project>{301fe650-cd34-14e5-6b63-42e383fa02bc}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>

View File

@ -1,9 +1,9 @@
 
Microsoft Visual Studio Solution File, Format Version 11.00 Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010 # 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,102 +1,103 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64"> <ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32"> <ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Release|x64"> <ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
</PropertyGroup> <ProjectGuid>{301FE650-CD34-14E5-6B63-42E383FA02BC}</ProjectGuid>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<ConfigurationType>StaticLibrary</ConfigurationType> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries> <ConfigurationType>StaticLibrary</ConfigurationType>
</PropertyGroup> <UseDebugLibraries>true</UseDebugLibraries>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> </PropertyGroup>
<ConfigurationType>StaticLibrary</ConfigurationType> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries> <ConfigurationType>StaticLibrary</ConfigurationType>
</PropertyGroup> <UseDebugLibraries>true</UseDebugLibraries>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> </PropertyGroup>
<ConfigurationType>StaticLibrary</ConfigurationType> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries> <ConfigurationType>StaticLibrary</ConfigurationType>
</PropertyGroup> <UseDebugLibraries>false</UseDebugLibraries>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> </PropertyGroup>
<ConfigurationType>StaticLibrary</ConfigurationType> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries> <ConfigurationType>StaticLibrary</ConfigurationType>
</PropertyGroup> <UseDebugLibraries>false</UseDebugLibraries>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> </PropertyGroup>
<ImportGroup Label="ExtensionSettings"> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
</ImportGroup> <ImportGroup Label="ExtensionSettings">
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> </ImportGroup>
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
</ImportGroup> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets"> </ImportGroup>
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
</ImportGroup> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> </ImportGroup>
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
</ImportGroup> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets"> </ImportGroup>
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
</ImportGroup> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<PropertyGroup Label="UserMacros" /> </ImportGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Label="UserMacros" />
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IntDir>$(Platform)\$(Configuration)</IntDir> <OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> <IntDir>$(Platform)\$(Configuration)</IntDir>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> </PropertyGroup>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IntDir>$(Platform)\$(Configuration)</IntDir> <OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> <IntDir>$(Platform)\$(Configuration)</IntDir>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> </PropertyGroup>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IntDir>$(Platform)\$(Configuration)</IntDir> <OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> <IntDir>$(Platform)\$(Configuration)</IntDir>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> </PropertyGroup>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IntDir>$(Platform)\$(Configuration)</IntDir> <OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> <IntDir>$(Platform)\$(Configuration)</IntDir>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> </PropertyGroup>
<ClCompile> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ClCompile>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<WarningLevel>Level3</WarningLevel> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile> <Optimization>Disabled</Optimization>
</ItemDefinitionGroup> </ClCompile>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> </ItemDefinitionGroup>
<ClCompile> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ClCompile>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<WarningLevel>Level3</WarningLevel> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
</ClCompile> <Optimization>Disabled</Optimization>
</ItemDefinitionGroup> </ClCompile>
<ItemGroup> </ItemDefinitionGroup>
<ClInclude Include="ngx-queue.h" /> <ItemGroup>
<ClInclude Include="ol-win.h" /> <ClInclude Include="ngx-queue.h" />
<ClInclude Include="ol.h" /> <ClInclude Include="oio-win.h" />
</ItemGroup> <ClInclude Include="oio.h" />
<ItemGroup> </ItemGroup>
<ClCompile Include="ol-win.c" /> <ItemGroup>
</ItemGroup> <ClCompile Include="oio-win.c" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> </ItemGroup>
<ImportGroup Label="ExtensionTargets"> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</ImportGroup> <ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project> </Project>

View File

@ -1,4 +1,4 @@
#include "ol.h" #include "oio.h"
#include <stdio.h> /* printf */ #include <stdio.h> /* printf */
@ -25,23 +25,23 @@ size_t strnlen (register const char* s, size_t maxlen) {
#endif /* strnlen */ #endif /* strnlen */
void ol_tcp_io(EV_P_ ev_io* watcher, int revents); void oio_tcp_io(EV_P_ ev_io* watcher, int revents);
void ol_tcp_connect(ol_handle* handle, ol_req* req); void oio_tcp_connect(oio_handle* handle, oio_req* req);
int ol_tcp_open(ol_handle*, int fd); int oio_tcp_open(oio_handle*, int fd);
int ol_close_error(ol_handle* handle, ol_err err); 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; handle->_.err = e;
return e; return e;
} }
ol_err ol_err_last(ol_handle *handle) { oio_err oio_err_last(oio_handle *handle) {
return handle->_.err; 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; struct sockaddr_in addr;
addr.sin_family = AF_INET; 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) { int oio_close(oio_handle* handle) {
return ol_close_error(handle, 0); return oio_close_error(handle, 0);
} }
void ol_init() { void oio_init() {
ev_default_loop(0); ev_default_loop(0);
} }
int ol_run() { int oio_run() {
ev_run(EV_DEFAULT_ 0); ev_run(EV_DEFAULT_ 0);
} }
ol_handle* ol_tcp_handle_new(ol_close_cb close_cb, void* data) { oio_handle* oio_tcp_handle_new(oio_close_cb close_cb, void* data) {
ol_handle *handle = calloc(sizeof(ol_handle), 1); oio_handle *handle = calloc(sizeof(oio_handle), 1);
if (!handle) { if (!handle) {
ol_err_new(NULL, ENOMEM); oio_err_new(NULL, ENOMEM);
return NULL; return NULL;
} }
handle->type = OL_TCP; handle->type = OIO_TCP;
handle->close_cb = close_cb; handle->close_cb = close_cb;
handle->data = data; handle->data = data;
int fd = socket(AF_INET, SOCK_STREAM, 0); int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) { if (fd < 0) {
ol_err_new(handle, errno); oio_err_new(handle, errno);
free(handle); free(handle);
return NULL; return NULL;
} }
if (ol_tcp_open(handle, fd)) { if (oio_tcp_open(handle, fd)) {
close(fd); close(fd);
free(handle); free(handle);
return NULL; 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 addrsize;
int domain; int domain;
int r; int r;
@ -116,11 +116,11 @@ int ol_bind(ol_handle* handle, struct sockaddr* addr) {
r = bind(handle->_.fd, addr, addrsize); 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 r;
int yes = 1; int yes = 1;
r = fcntl(fd, F_SETFL, O_NONBLOCK); 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 */ /* Set non-blocking, etc */
ol_tcp_init_fd(fd); oio_tcp_init_fd(fd);
handle->_.fd = fd; handle->_.fd = fd;
ngx_queue_init(&handle->_.read_reqs); ngx_queue_init(&handle->_.read_reqs);
ev_io_init(&handle->_.read_watcher, ol_tcp_io, fd, EV_READ); ev_io_init(&handle->_.read_watcher, oio_tcp_io, fd, EV_READ);
ev_io_init(&handle->_.write_watcher, ol_tcp_io, fd, EV_WRITE); ev_io_init(&handle->_.write_watcher, oio_tcp_io, fd, EV_WRITE);
handle->_.read_watcher.data = handle; handle->_.read_watcher.data = handle;
handle->_.write_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) { void oio_server_io(EV_P_ ev_io* watcher, int revents) {
ol_handle* handle = watcher->data; oio_handle* handle = watcher->data;
assert(revents == EV_READ); 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. */ /* TODO special trick. unlock reserved socket, accept, close. */
return; return;
} else { } else {
ol_close_error(handle, ol_err_new(handle, errno)); oio_close_error(handle, oio_err_new(handle, errno));
} }
} else { } else {
if (!handle->accept_cb) { if (!handle->accept_cb) {
close(fd); close(fd);
} else { } else {
ol_handle* new_client = ol_tcp_handle_new(NULL, NULL); oio_handle* new_client = oio_tcp_handle_new(NULL, NULL);
if (!new_client) { if (!new_client) {
/* Ignore error for now */ /* Ignore error for now */
} else { } else {
if (ol_tcp_open(new_client, fd)) { if (oio_tcp_open(new_client, fd)) {
/* Ignore error for now */ /* Ignore error for now */
} else { } else {
ev_io_start(EV_DEFAULT_ &handle->_.read_watcher); 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); assert(handle->_.fd >= 0);
int r = listen(handle->_.fd, backlog); int r = listen(handle->_.fd, backlog);
if (r < 0) { if (r < 0) {
return ol_err_new(handle, errno); return oio_err_new(handle, errno);
} }
handle->accept_cb = cb; 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); ev_io_start(EV_DEFAULT_ &handle->_.read_watcher);
handle->_.read_watcher.data = handle; 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); ev_io_stop(EV_DEFAULT_ &handle->_.read_watcher);
close(handle->_.fd); close(handle->_.fd);
handle->_.fd = -1; 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)); ngx_queue_t* q = ngx_queue_head(&(handle->_.read_reqs));
if (!q) { if (!q) {
return NULL; 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); assert(p);
int off = offsetof(ol_req, _); int off = offsetof(oio_req, _);
ol_req* req = (ol_req*) ((char*)p - off); oio_req* req = (oio_req*) ((char*)p - off);
return req; 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)); return ngx_queue_empty(&(handle->_.read_reqs));
} }
void ol__read(ol_handle* handle) { void oio__read(oio_handle* handle) {
assert(handle->_.fd >= 0); assert(handle->_.fd >= 0);
/* Get the request at the head of the read_reqs queue. */ /* 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) { if (!req) {
ev_io_stop(EV_DEFAULT_ &(handle->_.read_watcher)); ev_io_stop(EV_DEFAULT_ &(handle->_.read_watcher));
return; 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. * because Windows's WSABUF is not an iovec.
*/ */
struct iovec* iov = (struct iovec*) req->_.read_bufs; 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); ssize_t nread = readv(handle->_.fd, iov, iovcnt);
ol_read_cb cb = req->cb; oio_read_cb cb = req->cb;
if (nread < 0) { if (nread < 0) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
/* Just wait for the next one. */ /* Just wait for the next one. */
assert(ev_is_active(&(handle->_.read_watcher))); assert(ev_is_active(&(handle->_.read_watcher)));
} else { } else {
ol_err err = ol_err_new(handle, errno); oio_err err = oio_err_new(handle, errno);
if (cb) { if (cb) {
cb(req, 0); cb(req, 0);
} }
ol_close_error(handle, errno); oio_close_error(handle, errno);
} }
} else { } else {
/* Successful read */ /* Successful read */
@ -294,23 +294,23 @@ void ol__read(ol_handle* handle) {
cb(req, nread); cb(req, nread);
} }
if (ol_read_reqs_empty(handle)) { if (oio_read_reqs_empty(handle)) {
ev_io_stop(EV_DEFAULT_ &(handle->_.read_watcher)); ev_io_stop(EV_DEFAULT_ &(handle->_.read_watcher));
} }
} }
} }
void ol_tcp_io(EV_P_ ev_io* watcher, int revents) { void oio_tcp_io(EV_P_ ev_io* watcher, int revents) {
ol_handle* handle = watcher->data; oio_handle* handle = watcher->data;
assert(handle->_.fd >= 0); assert(handle->_.fd >= 0);
if (handle->_.connect_req) { if (handle->_.connect_req) {
ol_tcp_connect(handle, handle->_.connect_req); oio_tcp_connect(handle, handle->_.connect_req);
} else { } else {
if (revents & EV_READ) { if (revents & EV_READ) {
ol__read(handle); oio__read(handle);
} }
if (revents & EV_WRITE) { 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 * In order to determine if we've errored out or succeeded must call
* getsockopt. * 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(handle->_.fd >= 0);
assert(req); 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); getsockopt(handle->_.fd, SOL_SOCKET, SO_ERROR, &error, &errorsize);
if (!error) { if (!error) {
ev_io_init(&handle->_.write_watcher, ol_tcp_io, handle->_.fd, EV_WRITE); ev_io_init(&handle->_.write_watcher, oio_tcp_io, handle->_.fd, EV_WRITE);
ev_set_cb(&handle->_.read_watcher, ol_tcp_io); ev_set_cb(&handle->_.read_watcher, oio_tcp_io);
/* Successful connection */ /* Successful connection */
ol_connect_cb connect_cb = req->cb; oio_connect_cb connect_cb = req->cb;
if (connect_cb) { if (connect_cb) {
if (req->_.local) { if (req->_.local) {
connect_cb(NULL, ol_err_new(handle, 0)); connect_cb(NULL, oio_err_new(handle, 0));
} else { } 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; return;
} else { } else {
ol_err err = ol_err_new(handle, error); oio_err err = oio_err_new(handle, error);
if (req->_.connect_cb) { if (req->_.connect_cb) {
req->_.connect_cb(req, err); 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) { if (in_req) {
ngx_queue_init(&(in_req->_.read_reqs)); ngx_queue_init(&(in_req->_.read_reqs));
in_req->handle = handle; in_req->handle = handle;
in_req->_.local = 0; in_req->_.local = 0;
return in_req; return in_req;
} else { } else {
ol_req *req = malloc(sizeof(ol_req)); oio_req *req = malloc(sizeof(oio_req));
ol_req_init(req, NULL); oio_req_init(req, NULL);
req->handle = handle; req->handle = handle;
ngx_queue_init(&(req->_.read_reqs)); ngx_queue_init(&(req->_.read_reqs));
req->_.local = 1; 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) { if (handle->_.connect_req) {
return ol_err_new(handle, EALREADY); return oio_err_new(handle, EALREADY);
} }
if (handle->type != OL_TCP) { if (handle->type != OIO_TCP) {
return ol_err_new(handle, ENOTSOCK); 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) { if (!req) {
return ol_err_new(handle, ENOMEM); return oio_err_new(handle, ENOMEM);
} }
handle->_.connect_req = req; handle->_.connect_req = req;
@ -419,30 +419,30 @@ int ol_connect(ol_handle* handle, ol_req *req_in, struct sockaddr* addr) {
/* socket(2) failed */ /* socket(2) failed */
if (handle->_.fd < 0) { if (handle->_.fd < 0) {
return ol_err_new(handle, errno); return oio_err_new(handle, errno);
} }
int r = connect(handle->_.fd, addr, addrsize); int r = connect(handle->_.fd, addr, addrsize);
ev_io_init(&handle->_.read_watcher, ol_tcp_io, handle->_.fd, EV_READ); ev_io_init(&handle->_.read_watcher, oio_tcp_io, handle->_.fd, EV_READ);
ev_io_init(&handle->_.write_watcher, ol_tcp_io, handle->_.fd, EV_WRITE); ev_io_init(&handle->_.write_watcher, oio_tcp_io, handle->_.fd, EV_WRITE);
ev_io_start(EV_DEFAULT_ &handle->_.read_watcher); 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); assert(handle->_.fd >= 0);
ssize_t r; ssize_t r;
r = writev(handle->_.fd, (struct iovec*)bufs, bufcnt); r = writev(handle->_.fd, (struct iovec*)bufs, bufcnt);
if (r < 0) { if (r < 0) {
return ol_err_new(handle, r); return oio_err_new(handle, r);
} else { } else {
if (req && req->cb) { if (req && req->cb) {
ol_write_cb cb = req->cb; oio_write_cb cb = req->cb;
cb(req); cb(req);
} }
return 0; 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); size_t len = strnlen(msg, 1024 * 1024);
ol_buf b; oio_buf b;
b.base = (char*)msg; b.base = (char*)msg;
b.len = len; 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; ssize_t nread = -1;
errno = EAGAIN; errno = EAGAIN;
ol_read_cb cb = req_in->cb; oio_read_cb cb = req_in->cb;
assert(handle->_.fd >= 0); 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) { if (nread < 0 && errno != EAGAIN) {
/* Real error. */ /* Real error. */
ol_err err = ol_err_new(handle, errno); oio_err err = oio_err_new(handle, errno);
if (cb) { if (cb) {
cb(req_in, nread); 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 * - EAGAIN, meaning the socket is not wriable currently. We must wait for
* it to become readable with the handle->_.read_watcher. * it to become readable with the handle->_.read_watcher.
* - The read_reqs queue already has reads. Meaning: the user has issued * - 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. * become readable.
* In the meantime we append the request to handle->_.read_reqs * 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) { 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 /* Copy the bufs data over into our oio_req struct. This is so the user can
* free the ol_buf array. The actual data inside the ol_bufs is however * 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. * owned by the user and cannot be deallocated until the read completes.
*/ */
req->_.read_bufs = malloc(sizeof(ol_buf) * bufcnt); req->_.read_bufs = malloc(sizeof(oio_buf) * bufcnt);
memcpy(req->_.read_bufs, bufs, bufcnt * sizeof(ol_buf)); memcpy(req->_.read_bufs, bufs, bufcnt * sizeof(oio_buf));
req->_.read_bufcnt = bufcnt; req->_.read_bufcnt = bufcnt;
/* Append the request to read_reqs. */ /* 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); 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); free(handle);
/* lists? */ /* lists? */
return; return;
} }
void ol_req_init(ol_req *req, void *cb) { void oio_req_init(oio_req *req, void *cb) {
req->type = OL_UNKNOWN_REQ; req->type = OIO_UNKNOWN_REQ;
req->cb = cb; req->cb = cb;
ngx_queue_init(&(req->_.read_reqs)); ngx_queue_init(&(req->_.read_reqs));
} }

View File

@ -1,5 +1,5 @@
#ifndef OL_UNIX_H #ifndef OIO_UNIX_H
#define OL_UNIX_H #define OIO_UNIX_H
#include "ngx-queue.h" #include "ngx-queue.h"
@ -17,28 +17,28 @@
typedef struct { typedef struct {
char* base; char* base;
size_t len; size_t len;
} ol_buf; } oio_buf;
typedef struct { typedef struct {
int local; int local;
ol_connect_cb connect_cb; oio_connect_cb connect_cb;
ngx_queue_t read_reqs; ngx_queue_t read_reqs;
ol_buf* read_bufs; oio_buf* read_bufs;
int read_bufcnt; int read_bufcnt;
} ol_req_private; } oio_req_private;
typedef struct { typedef struct {
int fd; int fd;
ol_err err; oio_err err;
ol_read_cb read_cb; oio_read_cb read_cb;
ol_close_cb close_cb; oio_close_cb close_cb;
ol_req *connect_req; oio_req *connect_req;
ev_io read_watcher; ev_io read_watcher;
ev_io write_watcher; ev_io write_watcher;
@ -46,7 +46,7 @@ typedef struct {
ngx_queue_t write_queue; ngx_queue_t write_queue;
ngx_queue_t read_reqs; ngx_queue_t read_reqs;
} ol_handle_private; } oio_handle_private;
#endif /* OL_UNIX_H */ #endif /* OIO_UNIX_H */

View File

@ -1,5 +1,5 @@
#include "ol.h" #include "oio.h"
#include <assert.h> #include <assert.h>
#include <malloc.h> #include <malloc.h>
#include <stdio.h> #include <stdio.h>
@ -71,16 +71,16 @@
#endif #endif
/* /*
* Private ol_handle flags * Private oio_handle flags
*/ */
#define OL_HANDLE_CLOSING 0x01 #define OIO_HANDLE_CLOSING 0x01
#define OL_HANDLE_CLOSED 0x03 #define OIO_HANDLE_CLOSED 0x03
/* /*
* Private ol_req flags. * Private oio_req flags.
*/ */
/* The request is currently queued. */ /* 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 * Pointers to winsock extension functions that have to be retrieved dynamically
@ -94,21 +94,21 @@ LPFN_TRANSMITFILE pTransmitFile;
/* /*
* Global I/O completion port * Global I/O completion port
*/ */
HANDLE ol_iocp_; HANDLE oio_iocp_;
/* Global error code */ /* Global error code */
int ol_errno_; int oio_errno_;
/* Reference count that keeps the event loop alive */ /* 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. * 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; char *buf = NULL;
const char *errmsg; 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. * 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; DWORD result, bytes;
result = WSAIoctl(socket, result = WSAIoctl(socket,
@ -157,12 +157,12 @@ void ol_get_extension_function(SOCKET socket, GUID guid, void **target) {
if (result == SOCKET_ERROR) { if (result == SOCKET_ERROR) {
*target = NULL; *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_connectex = WSAID_CONNECTEX;
const GUID wsaid_acceptex = WSAID_ACCEPTEX; const GUID wsaid_acceptex = WSAID_ACCEPTEX;
const GUID wsaid_getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS; const GUID wsaid_getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
@ -176,48 +176,48 @@ void ol_init() {
/* Initialize winsock */ /* Initialize winsock */
errorno = WSAStartup(MAKEWORD(2, 2), &wsa_data); errorno = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (errorno != 0) { if (errorno != 0) {
ol_fatal_error(errorno, "WSAStartup"); oio_fatal_error(errorno, "WSAStartup");
} }
/* Retrieve the needed winsock extension function pointers. */ /* Retrieve the needed winsock extension function pointers. */
dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); dummy = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (dummy == INVALID_SOCKET) { if (dummy == INVALID_SOCKET) {
ol_fatal_error(WSAGetLastError(), "socket"); oio_fatal_error(WSAGetLastError(), "socket");
} }
ol_get_extension_function(dummy, wsaid_connectex, (void**)&pConnectEx ); oio_get_extension_function(dummy, wsaid_connectex, (void**)&pConnectEx );
ol_get_extension_function(dummy, wsaid_acceptex, (void**)&pAcceptEx ); oio_get_extension_function(dummy, wsaid_acceptex, (void**)&pAcceptEx );
ol_get_extension_function(dummy, wsaid_getacceptexsockaddrs, (void**)&pGetAcceptExSockAddrs); oio_get_extension_function(dummy, wsaid_getacceptexsockaddrs, (void**)&pGetAcceptExSockAddrs);
ol_get_extension_function(dummy, wsaid_disconnectex, (void**)&pDisconnectEx ); oio_get_extension_function(dummy, wsaid_disconnectex, (void**)&pDisconnectEx );
ol_get_extension_function(dummy, wsaid_transmitfile, (void**)&pTransmitFile ); oio_get_extension_function(dummy, wsaid_transmitfile, (void**)&pTransmitFile );
if (closesocket(dummy) == SOCKET_ERROR) { if (closesocket(dummy) == SOCKET_ERROR) {
ol_fatal_error(WSAGetLastError(), "closesocket"); oio_fatal_error(WSAGetLastError(), "closesocket");
} }
/* Create an I/O completion port */ /* Create an I/O completion port */
ol_iocp_ = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); oio_iocp_ = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (ol_iocp_ == NULL) { if (oio_iocp_ == NULL) {
ol_fatal_error(GetLastError(), "CreateIoCompletionPort"); oio_fatal_error(GetLastError(), "CreateIoCompletionPort");
} }
} }
void ol_req_init(ol_req* req, ol_handle* handle, void *cb) { void oio_req_init(oio_req* req, oio_handle* handle, void *cb) {
req->type = OL_UNKNOWN_REQ; req->type = OIO_UNKNOWN_REQ;
req->flags = 0; req->flags = 0;
req->handle = handle; req->handle = handle;
req->cb = cb; req->cb = cb;
} }
ol_req* ol_overlapped_to_req(OVERLAPPED* overlapped) { oio_req* oio_overlapped_to_req(OVERLAPPED* overlapped) {
return CONTAINING_RECORD(overlapped, ol_req, 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; DWORD yes = 1;
/* Set the SO_REUSEADDR option on the socket */ /* Set the SO_REUSEADDR option on the socket */
@ -230,17 +230,17 @@ int ol_set_socket_options(SOCKET socket) {
/* Make the socket non-inheritable */ /* Make the socket non-inheritable */
if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) { if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) {
ol_errno_ = GetLastError(); oio_errno_ = GetLastError();
return -1; return -1;
} }
/* Associate it with the I/O completion port. */ /* 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, if (CreateIoCompletionPort((HANDLE)socket,
ol_iocp_, oio_iocp_,
(ULONG_PTR)socket, (ULONG_PTR)socket,
0) == NULL) { 0) == NULL) {
ol_errno_ = GetLastError(); oio_errno_ = GetLastError();
return -1; 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->close_cb = close_cb;
handle->data = data; handle->data = data;
handle->type = OL_TCP; handle->type = OIO_TCP;
handle->flags = 0; handle->flags = 0;
handle->reqs_pending = 0; handle->reqs_pending = 0;
handle->error = 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); handle->socket = socket(AF_INET, SOCK_STREAM, 0);
if (handle->socket == INVALID_SOCKET) { if (handle->socket == INVALID_SOCKET) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
return -1; return -1;
} }
if (ol_set_socket_options(handle->socket) != 0) { if (oio_set_socket_options(handle->socket) != 0) {
closesocket(handle->socket); closesocket(handle->socket);
return -1; return -1;
} }
ol_refs_++; oio_refs_++;
return 0; 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 || if (!server->accept_data ||
server->accept_data->socket == INVALID_SOCKET) { server->accept_data->socket == INVALID_SOCKET) {
ol_errno_ = WSAENOTCONN; oio_errno_ = WSAENOTCONN;
return -1; return -1;
} }
client->close_cb = close_cb; client->close_cb = close_cb;
client->data = data; client->data = data;
client->type = OL_TCP; client->type = OIO_TCP;
client->socket = server->accept_data->socket; client->socket = server->accept_data->socket;
client->flags = 0; client->flags = 0;
client->reqs_pending = 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; client->accept_data = NULL;
server->accept_data->socket = INVALID_SOCKET; server->accept_data->socket = INVALID_SOCKET;
ol_refs_++; oio_refs_++;
return 0; return 0;
} }
int ol_close_error(ol_handle* handle, ol_err e) { int oio_close_error(oio_handle* handle, oio_err e) {
ol_req *req; oio_req *req;
if (handle->flags & OL_HANDLE_CLOSING) if (handle->flags & OIO_HANDLE_CLOSING)
return 0; return 0;
handle->error = e; handle->error = e;
switch (handle->type) { switch (handle->type) {
case OL_TCP: case OIO_TCP:
closesocket(handle->socket); closesocket(handle->socket);
if (handle->reqs_pending == 0) { if (handle->reqs_pending == 0) {
/* If there are no operations queued for this socket, queue one */ /* If there are no operations queued for this socket, queue one */
/* manually, so ol_poll will call close_cb. */ /* manually, so oio_poll will call close_cb. */
req = (ol_req*)malloc(sizeof(*req)); req = (oio_req*)malloc(sizeof(*req));
req->handle = handle; req->handle = handle;
req->type = OL_CLOSE; req->type = OIO_CLOSE;
req->flags = 0; req->flags = 0;
if (!PostQueuedCompletionStatus(ol_iocp_, 0, (ULONG_PTR)handle, &req->overlapped)) if (!PostQueuedCompletionStatus(oio_iocp_, 0, (ULONG_PTR)handle, &req->overlapped))
ol_fatal_error(GetLastError(), "PostQueuedCompletionStatus"); oio_fatal_error(GetLastError(), "PostQueuedCompletionStatus");
req->flags |= OL_REQ_PENDING; req->flags |= OIO_REQ_PENDING;
handle->reqs_pending++; handle->reqs_pending++;
} }
/* After all packets to come out, ol_poll will call close_cb. */ /* After all packets to come out, oio_poll will call close_cb. */
handle->flags |= OL_HANDLE_CLOSING; handle->flags |= OIO_HANDLE_CLOSING;
return 0; return 0;
default: default:
@ -334,12 +334,12 @@ int ol_close_error(ol_handle* handle, ol_err e) {
} }
int ol_close(ol_handle* handle) { int oio_close(oio_handle* handle) {
return ol_close_error(handle, 0); 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; struct sockaddr_in addr;
addr.sin_family = AF_INET; 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; int addrsize;
if (addr->sa_family == AF_INET) { 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) { if (bind(handle->socket, addr, addrsize) == SOCKET_ERROR) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
return -1; return -1;
} }
@ -371,8 +371,8 @@ int ol_bind(ol_handle* handle, struct sockaddr* addr) {
} }
void ol_queue_accept(ol_handle *handle) { void oio_queue_accept(oio_handle *handle) {
ol_accept_data* data; oio_accept_data* data;
BOOL success; BOOL success;
DWORD bytes; DWORD bytes;
@ -381,19 +381,19 @@ void ol_queue_accept(ol_handle *handle) {
data->socket = socket(AF_INET, SOCK_STREAM, 0); data->socket = socket(AF_INET, SOCK_STREAM, 0);
if (data->socket == INVALID_SOCKET) { if (data->socket == INVALID_SOCKET) {
ol_close_error(handle, WSAGetLastError()); oio_close_error(handle, WSAGetLastError());
return; return;
} }
if (ol_set_socket_options(data->socket) != 0) { if (oio_set_socket_options(data->socket) != 0) {
closesocket(data->socket); closesocket(data->socket);
ol_close_error(handle, ol_errno_); oio_close_error(handle, oio_errno_);
return; return;
} }
/* Prepare the ol_req and OVERLAPPED structures. */ /* Prepare the oio_req and OVERLAPPED structures. */
assert(!(data->req.flags & OL_REQ_PENDING)); assert(!(data->req.flags & OIO_REQ_PENDING));
data->req.flags |= OL_REQ_PENDING; data->req.flags |= OIO_REQ_PENDING;
memset(&data->req.overlapped, 0, sizeof(data->req.overlapped)); memset(&data->req.overlapped, 0, sizeof(data->req.overlapped));
success = pAcceptEx(handle->socket, success = pAcceptEx(handle->socket,
@ -406,58 +406,58 @@ void ol_queue_accept(ol_handle *handle) {
&data->req.overlapped); &data->req.overlapped);
if (!success && WSAGetLastError() != ERROR_IO_PENDING) { if (!success && WSAGetLastError() != ERROR_IO_PENDING) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
/* destroy the preallocated client handle */ /* destroy the preallocated client handle */
closesocket(data->socket); closesocket(data->socket);
/* destroy ourselves */ /* destroy ourselves */
ol_close_error(handle, ol_errno_); oio_close_error(handle, oio_errno_);
return; return;
} }
handle->reqs_pending++; 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) { int oio_listen(oio_handle* handle, int backlog, oio_accept_cb cb) {
ol_accept_data *data; oio_accept_data *data;
if (handle->accept_data != NULL) { if (handle->accept_data != NULL) {
/* Already listening. */ /* Already listening. */
ol_errno_ = WSAEALREADY; oio_errno_ = WSAEALREADY;
return -1; return -1;
} }
data = (ol_accept_data*)malloc(sizeof(*data)); data = (oio_accept_data*)malloc(sizeof(*data));
if (!data) { if (!data) {
ol_errno_ = WSAENOBUFS; oio_errno_ = WSAENOBUFS;
return -1; return -1;
} }
data->socket = INVALID_SOCKET; data->socket = INVALID_SOCKET;
ol_req_init(&data->req, handle, (void*)cb); oio_req_init(&data->req, handle, (void*)cb);
data->req.type = OL_ACCEPT; data->req.type = OIO_ACCEPT;
if (listen(handle->socket, backlog) == SOCKET_ERROR) { if (listen(handle->socket, backlog) == SOCKET_ERROR) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
free(data); free(data);
return -1; return -1;
} }
handle->accept_data = data; handle->accept_data = data;
ol_queue_accept(handle); oio_queue_accept(handle);
return 0; return 0;
} }
int ol_connect(ol_req* req, struct sockaddr* addr) { int oio_connect(oio_req* req, struct sockaddr* addr) {
int addrsize; int addrsize;
BOOL success; BOOL success;
DWORD bytes; 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) { if (addr->sa_family == AF_INET) {
addrsize = sizeof(struct sockaddr_in); 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)); memset(&req->overlapped, 0, sizeof(req->overlapped));
req->type = OL_CONNECT; req->type = OIO_CONNECT;
success = pConnectEx(handle->socket, success = pConnectEx(handle->socket,
addr, addr,
@ -480,26 +480,26 @@ int ol_connect(ol_req* req, struct sockaddr* addr) {
&req->overlapped); &req->overlapped);
if (!success && WSAGetLastError() != ERROR_IO_PENDING) { if (!success && WSAGetLastError() != ERROR_IO_PENDING) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
return -1; return -1;
} }
req->flags |= OL_REQ_PENDING; req->flags |= OIO_REQ_PENDING;
handle->reqs_pending++; handle->reqs_pending++;
return 0; 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; int result;
DWORD bytes; 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)); memset(&req->overlapped, 0, sizeof(req->overlapped));
req->type = OL_WRITE; req->type = OIO_WRITE;
result = WSASend(handle->socket, result = WSASend(handle->socket,
(WSABUF*)bufs, (WSABUF*)bufs,
@ -509,26 +509,26 @@ int ol_write(ol_req *req, ol_buf* bufs, int bufcnt) {
&req->overlapped, &req->overlapped,
NULL); NULL);
if (result != 0 && WSAGetLastError() != ERROR_IO_PENDING) { if (result != 0 && WSAGetLastError() != ERROR_IO_PENDING) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
return -1; return -1;
} }
req->flags |= OL_REQ_PENDING; req->flags |= OIO_REQ_PENDING;
handle->reqs_pending++; handle->reqs_pending++;
return 0; 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; int result;
DWORD bytes, flags; 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)); memset(&req->overlapped, 0, sizeof(req->overlapped));
req->type = OL_READ; req->type = OIO_READ;
flags = 0; flags = 0;
result = WSARecv(handle->socket, result = WSARecv(handle->socket,
@ -539,63 +539,63 @@ int ol_read(ol_req *req, ol_buf* bufs, int bufcnt) {
&req->overlapped, &req->overlapped,
NULL); NULL);
if (result != 0 && WSAGetLastError() != ERROR_IO_PENDING) { if (result != 0 && WSAGetLastError() != ERROR_IO_PENDING) {
ol_errno_ = WSAGetLastError(); oio_errno_ = WSAGetLastError();
return -1; return -1;
} }
req->flags |= OL_REQ_PENDING; req->flags |= OIO_REQ_PENDING;
handle->reqs_pending++; handle->reqs_pending++;
return 0; return 0;
} }
int ol_write2(ol_req *req, const char* msg) { int oio_write2(oio_req *req, const char* msg) {
ol_buf buf; oio_buf buf;
ol_handle* handle = req->handle; oio_handle* handle = req->handle;
buf.base = (char*)msg; buf.base = (char*)msg;
buf.len = strlen(msg); buf.len = strlen(msg);
return ol_write(req, &buf, 1); return oio_write(req, &buf, 1);
} }
ol_err ol_last_error() { oio_err oio_last_error() {
return ol_errno_; return oio_errno_;
} }
void ol_poll() { void oio_poll() {
BOOL success; BOOL success;
DWORD bytes; DWORD bytes;
ULONG_PTR key; ULONG_PTR key;
OVERLAPPED* overlapped; OVERLAPPED* overlapped;
ol_req* req; oio_req* req;
ol_handle* handle; oio_handle* handle;
ol_accept_data *data; oio_accept_data *data;
success = GetQueuedCompletionStatus(ol_iocp_, success = GetQueuedCompletionStatus(oio_iocp_,
&bytes, &bytes,
&key, &key,
&overlapped, &overlapped,
INFINITE); INFINITE);
if (!success && !overlapped) 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; handle = req->handle;
/* Mark the request non-pending */ /* Mark the request non-pending */
req->flags &= ~OL_REQ_PENDING; req->flags &= ~OIO_REQ_PENDING;
handle->reqs_pending--; handle->reqs_pending--;
/* If the related socket got closed in the meantime, disregard this */ /* 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. */ /* 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) { 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) { if (handle->accept_data) {
if (handle->accept_data->socket) { if (handle->accept_data->socket) {
@ -608,41 +608,41 @@ void ol_poll() {
if (handle->close_cb) { if (handle->close_cb) {
handle->close_cb(handle, handle->error); handle->close_cb(handle, handle->error);
} }
ol_refs_--; oio_refs_--;
} }
return; return;
} }
switch (req->type) { switch (req->type) {
case OL_WRITE: case OIO_WRITE:
success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE);
if (!success) { if (!success) {
ol_close_error(handle, GetLastError()); oio_close_error(handle, GetLastError());
} else if (req->cb) { } else if (req->cb) {
((ol_write_cb)req->cb)(req); ((oio_write_cb)req->cb)(req);
} }
return; return;
case OL_READ: case OIO_READ:
success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE);
if (!success) { if (!success) {
ol_close_error(handle, GetLastError()); oio_close_error(handle, GetLastError());
} else if (req->cb) { } else if (req->cb) {
((ol_read_cb)req->cb)(req, bytes); ((oio_read_cb)req->cb)(req, bytes);
} }
break; break;
case OL_ACCEPT: case OIO_ACCEPT:
data = handle->accept_data; data = handle->accept_data;
assert(data != NULL); assert(data != NULL);
assert(data->socket != INVALID_SOCKET); assert(data->socket != INVALID_SOCKET);
success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE);
if (success && req->cb) { 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. */ /* to INVALID_SOCKET. */
/* Just ignore failed accept if the listen socket is still healthy. */ /* Just ignore failed accept if the listen socket is still healthy. */
if (data->socket != INVALID_SOCKET) { if (data->socket != INVALID_SOCKET) {
@ -651,31 +651,31 @@ void ol_poll() {
} }
/* Queue another accept */ /* Queue another accept */
ol_queue_accept(handle); oio_queue_accept(handle);
return; return;
case OL_CONNECT: case OIO_CONNECT:
if (req->cb) { if (req->cb) {
success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE); success = GetOverlappedResult(handle->handle, overlapped, &bytes, FALSE);
if (success) { if (success) {
((ol_connect_cb)req->cb)(req, 0); ((oio_connect_cb)req->cb)(req, 0);
} else { } else {
((ol_connect_cb)req->cb)(req, GetLastError()); ((oio_connect_cb)req->cb)(req, GetLastError());
} }
} }
return; return;
case OL_CLOSE: case OIO_CLOSE:
/* Should never get here */ /* Should never get here */
assert(0); assert(0);
} }
} }
int ol_run() { int oio_run() {
while (ol_refs_ > 0) { while (oio_refs_ > 0) {
ol_poll(); oio_poll();
} }
assert(ol_refs_ == 0); assert(oio_refs_ == 0);
return 0; return 0;
} }

View File

@ -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 * see http://msdn.microsoft.com/en-us/library/ms741542(v=vs.85).aspx
*/ */
typedef struct _ol_buf { typedef struct oio_buf {
ULONG len; ULONG len;
char* base; char* base;
} ol_buf; } oio_buf;
struct ol_req_s { struct oio_req_s {
struct ol_req_shared_s; struct oio_req_shared_s;
OVERLAPPED overlapped; OVERLAPPED overlapped;
int flags; int flags;
}; };
typedef struct { typedef struct {
ol_req req; oio_req req;
SOCKET socket; SOCKET socket;
/* AcceptEx specifies that the buffer must be big enough to at least hold */ /* AcceptEx specifies that the buffer must be big enough to at least hold */
/* two socket addresses plus 32 bytes. */ /* two socket addresses plus 32 bytes. */
char buffer[sizeof(struct sockaddr_storage) * 2 + 32]; char buffer[sizeof(struct sockaddr_storage) * 2 + 32];
} ol_accept_data; } oio_accept_data;
struct ol_handle_s { struct oio_handle_s {
struct ol_handle_shared_s; struct oio_handle_shared_s;
union { union {
SOCKET socket; SOCKET socket;
HANDLE handle; HANDLE handle;
}; };
ol_accept_data *accept_data; oio_accept_data *accept_data;
unsigned int flags; unsigned int flags;
unsigned int reqs_pending; unsigned int reqs_pending;
ol_err error; oio_err error;
}; };

110
oio.h Normal file
View File

@ -0,0 +1,110 @@
#ifndef OIO_H
#define OIO_H
#include <stddef.h> /* 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 */

110
ol.h
View File

@ -1,110 +0,0 @@
#ifndef OL_H
#define OL_H
#include <stddef.h> /* 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 */

View File

@ -1,4 +1,4 @@
#include "../ol.h" #include "../oio.h"
#include "test.h" #include "test.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -6,39 +6,39 @@
#define BUFSIZE 1024 #define BUFSIZE 1024
typedef struct { typedef struct {
ol_handle handle; oio_handle handle;
ol_req req; oio_req req;
ol_buf buf; oio_buf buf;
char read_buffer[BUFSIZE]; char read_buffer[BUFSIZE];
} peer_t; } peer_t;
ol_handle server; oio_handle server;
void after_write(ol_req* req); void after_write(oio_req* req);
void after_read(ol_req* req, size_t nread); void after_read(oio_req* req, size_t nread);
void try_read(peer_t* peer); void try_read(peer_t* peer);
void on_close(ol_handle* peer, ol_err err); void on_close(oio_handle* peer, oio_err err);
void on_accept(ol_handle* handle); 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; peer_t* peer = (peer_t*) req->data;
try_read(peer); try_read(peer);
} }
void after_read(ol_req* req, size_t nread) { void after_read(oio_req* req, size_t nread) {
peer_t* peer; peer_t* peer;
int r; int r;
if (nread == 0) { if (nread == 0) {
ol_close(req->handle); oio_close(req->handle);
} else { } else {
peer = (peer_t*) req->data; peer = (peer_t*) req->data;
peer->buf.len = nread; 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; peer->req.data = peer;
r = ol_write(&peer->req, &peer->buf, 1); r = oio_write(&peer->req, &peer->buf, 1);
assert(!r); assert(!r);
} }
} }
@ -48,25 +48,25 @@ void try_read(peer_t* peer) {
int r; int r;
peer->buf.len = BUFSIZE; 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; peer->req.data = peer;
r = ol_read(&peer->req, &peer->buf, 1); r = oio_read(&peer->req, &peer->buf, 1);
assert(!r); assert(!r);
} }
void on_close(ol_handle* peer, ol_err err) { void on_close(oio_handle* peer, oio_err err) {
if (err) { if (err) {
fprintf(stdout, "Socket error\n"); 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)); peer_t* p = (peer_t*)malloc(sizeof(peer_t));
int r; 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); assert(!r);
p->buf.base = (char*)&p->read_buffer; 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); assert(handle == &server);
if (err) { if (err) {
@ -85,24 +85,24 @@ void on_server_close(ol_handle* handle, ol_err err) {
int echo_start(int port) { 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; int r;
r = ol_tcp_handle_init(&server, on_server_close, NULL); r = oio_tcp_handle_init(&server, on_server_close, NULL);
if (r) { if (r) {
/* TODO: Error codes */ /* TODO: Error codes */
fprintf(stderr, "Socket creation error\n"); fprintf(stderr, "Socket creation error\n");
return 1; return 1;
} }
r = ol_bind(&server, (struct sockaddr*) &addr); r = oio_bind(&server, (struct sockaddr*) &addr);
if (r) { if (r) {
/* TODO: Error codes */ /* TODO: Error codes */
fprintf(stderr, "Bind error\n"); fprintf(stderr, "Bind error\n");
return 1; return 1;
} }
r = ol_listen(&server, 128, on_accept); r = oio_listen(&server, 128, on_accept);
if (r) { if (r) {
/* TODO: Error codes */ /* TODO: Error codes */
fprintf(stderr, "Listen error\n"); fprintf(stderr, "Listen error\n");
@ -114,16 +114,16 @@ int echo_start(int port) {
int echo_stop() { int echo_stop() {
return ol_close(&server); return oio_close(&server);
} }
TEST_IMPL(echo_server) { TEST_IMPL(echo_server) {
ol_init(); oio_init();
if (echo_start(TEST_PORT)) if (echo_start(TEST_PORT))
return 1; return 1;
fprintf(stderr, "Listening!\n"); fprintf(stderr, "Listening!\n");
ol_run(); oio_run();
return 0; return 0;
} }

View File

@ -1,4 +1,4 @@
#include "../ol.h" #include "../oio.h"
#include "test.h" #include "test.h"
@ -6,30 +6,30 @@ int nested = 0;
int close_cb_called = 0; int close_cb_called = 0;
void close_cb(ol_handle *handle, ol_err e) { void close_cb(oio_handle *handle, oio_err e) {
assert("ol_close error" && e == 0); assert("oio_close error" && e == 0);
assert("ol_close_cb not called from a fresh stack" && nested == 0); assert("oio_close_cb not called from a fresh stack" && nested == 0);
close_cb_called++; close_cb_called++;
} }
TEST_IMPL(close_cb_stack) { TEST_IMPL(close_cb_stack) {
ol_handle handle; oio_handle handle;
int r; 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); assert(!r);
nested++; nested++;
r = ol_close(&handle); r = oio_close(&handle);
assert(!r); assert(!r);
nested--; 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; return 0;
} }

View File

@ -1,4 +1,4 @@
#include "../ol.h" #include "../oio.h"
#include "test.h" #include "test.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
@ -17,17 +17,17 @@ static char PING[] = "PING\n";
typedef struct { typedef struct {
int pongs; int pongs;
int state; int state;
ol_handle handle; oio_handle handle;
ol_req connect_req; oio_req connect_req;
ol_req read_req; oio_req read_req;
ol_buf buf; oio_buf buf;
char read_buffer[BUFSIZE]; char read_buffer[BUFSIZE];
} pinger_t; } pinger_t;
void pinger_try_read(pinger_t* pinger); 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; pinger_t* pinger = (pinger_t*)handle->data;
assert(!err); 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); free(req);
} }
void pinger_write_ping(pinger_t* pinger) { void pinger_write_ping(pinger_t* pinger) {
ol_req *req; oio_req *req;
int r; int r;
req = (ol_req*)malloc(sizeof(*req)); req = (oio_req*)malloc(sizeof(*req));
ol_req_init(req, &pinger->handle, pinger_after_write); oio_req_init(req, &pinger->handle, pinger_after_write);
r = ol_write2(req, (char*)&PING); r = oio_write2(req, (char*)&PING);
assert(!r); 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; unsigned int i;
pinger_t* pinger; pinger_t* pinger;
pinger = (pinger_t*)req->handle->data; pinger = (pinger_t*)req->handle->data;
if (nread == 0) { if (nread == 0) {
ol_close(&pinger->handle); oio_close(&pinger->handle);
return; return;
} }
@ -74,7 +74,7 @@ void pinger_after_read(ol_req* req, size_t nread) {
if (pinger->pongs < NUM_PINGS) { if (pinger->pongs < NUM_PINGS) {
pinger_write_ping(pinger); pinger_write_ping(pinger);
} else { } else {
ol_close(&pinger->handle); oio_close(&pinger->handle);
return; return;
} }
} }
@ -85,11 +85,11 @@ void pinger_after_read(ol_req* req, size_t nread) {
void pinger_try_read(pinger_t* pinger) { 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; pinger_t *pinger = (pinger_t*)req->handle->data;
if (err) { if (err) {
@ -102,8 +102,8 @@ void pinger_on_connect(ol_req *req, ol_err err) {
int pinger_new(int port) { int pinger_new(int port) {
struct sockaddr_in client_addr = ol_ip4_addr("0.0.0.0", 0); struct sockaddr_in client_addr = oio_ip4_addr("0.0.0.0", 0);
struct sockaddr_in server_addr = ol_ip4_addr("145.94.50.9", TEST_PORT); struct sockaddr_in server_addr = oio_ip4_addr("145.94.50.9", TEST_PORT);
pinger_t *pinger; pinger_t *pinger;
pinger = (pinger_t*)malloc(sizeof(*pinger)); pinger = (pinger_t*)malloc(sizeof(*pinger));
@ -113,28 +113,28 @@ int pinger_new(int port) {
pinger->buf.base = (char*)&pinger->read_buffer; pinger->buf.base = (char*)&pinger->read_buffer;
/* Try to connec to the server and do NUM_PINGS ping-pongs. */ /* 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; return -1;
} }
/* We are never doing multiple reads/connects at a time anyway. */ /* We are never doing multiple reads/connects at a time anyway. */
/* so these handles can be pre-initialized. */ /* so these handles can be pre-initialized. */
ol_req_init(&pinger->connect_req, &pinger->handle, pinger_on_connect); oio_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->read_req, &pinger->handle, pinger_after_read);
ol_bind(&pinger->handle, (struct sockaddr*)&client_addr); oio_bind(&pinger->handle, (struct sockaddr*)&client_addr);
return ol_connect(&pinger->connect_req, (struct sockaddr*)&server_addr); return oio_connect(&pinger->connect_req, (struct sockaddr*)&server_addr);
} }
TEST_IMPL(ping_pong) { TEST_IMPL(ping_pong) {
ol_init(); oio_init();
if (pinger_new(8000)) { if (pinger_new(8000)) {
return 2; return 2;
} }
ol_run(); oio_run();
assert(completed_pingers == 1); assert(completed_pingers == 1);

View File

@ -19,7 +19,7 @@ int process_start(char *name, process_info_t *p) {
if (GetTempPathW(sizeof(path) / sizeof(WCHAR), (WCHAR*)&path) == 0) if (GetTempPathW(sizeof(path) / sizeof(WCHAR), (WCHAR*)&path) == 0)
goto error; goto error;
if (GetTempFileNameW((WCHAR*)&path, L"ol_", 0, (WCHAR*)&filename) == 0) if (GetTempFileNameW((WCHAR*)&path, L"oio", 0, (WCHAR*)&filename) == 0)
goto error; goto error;
file = CreateFileW((WCHAR*)filename, file = CreateFileW((WCHAR*)filename,