Skip to content

Commit 3a59002

Browse files
author
daumiller
committed
Asynchronous Sockets for OSX & Linux (kqueue/epoll)!
1 parent 931d832 commit 3a59002

16 files changed

+1429
-40
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
.DS_Store
2+
._*
3+
._.*
24
object/*
3-
5+
library/*
6+
proto/*
7+
test/bin/*
8+
*.old

Makefile

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@ DIROBJ = $(TOP)/object
55
DIRLIB = $(TOP)/library
66
DIRTST = $(TOP)/test
77
DIRBIN = $(TOP)/test/bin
8-
DIRPCRE = /Users/dillon/Development/builds/pcre
9-
DIRINST = /home/dillon/bin/clib.lib
8+
CC = clang
109
CFLAGS = -Wall -std=c99 -I$(DIRINC) -DPCREHEADER='"$(DIRPCRE)/include/pcre.h"'
11-
LIBLIB = $(DIRPCRE)/lib/libpcre.a
10+
STRIP = strip
11+
#ifdef __APPLE__
12+
DIRPCRE = /Users/dillon/Development/builds/pcre
13+
DIRINST = /Users/dillon/Development/builds/clib/install
1214
TSTLIB = $(DIRLIB)/clib.a -pthread
15+
#elif __linux__
16+
#DIRPCRE = /home/dillon/bin/pcre.lib
17+
#DIRINST = /home/dillon/bin/clib.lib
18+
#TSTLIB = $(DIRLIB)/clib.a -pthread -lrt
19+
#endif
20+
LIBLIB = $(DIRPCRE)/lib/libpcre.a
1321

1422
all : clib tests
1523

24+
debug :
25+
$(eval CFLAGS += -g )
26+
$(eval STRIP = touch)
27+
1628
install : clib
1729
rm -rf $(DIRINST)
1830
mkdir -p $(DIRINST)/lib
@@ -21,29 +33,33 @@ install : clib
2133

2234
clib : $(DIRLIB)/clib.a
2335

24-
$(DIRLIB)/clib.a : $(DIROBJ)/string.o \
25-
$(DIROBJ)/regex.o \
26-
$(DIROBJ)/hash.o \
27-
$(DIROBJ)/base64.o \
28-
$(DIROBJ)/list.o \
29-
$(DIROBJ)/sha256.o \
30-
$(DIROBJ)/arguments.o \
31-
$(DIROBJ)/threadPool.o
36+
$(DIRLIB)/clib.a : $(DIROBJ)/string.o \
37+
$(DIROBJ)/regex.o \
38+
$(DIROBJ)/hash.o \
39+
$(DIROBJ)/base64.o \
40+
$(DIROBJ)/list.o \
41+
$(DIROBJ)/sha256.o \
42+
$(DIROBJ)/arguments.o \
43+
$(DIROBJ)/threadPool.o \
44+
$(DIROBJ)/asockWorker.o \
45+
$(DIROBJ)/asock_sync.o \
46+
$(DIROBJ)/asock_async.o \
47+
$(DIROBJ)/fileSystem.o
3248
mkdir -p $(DIRLIB)
3349
cd $(DIROBJ); \
3450
ar -x $(LIBLIB); \
3551
ar rcs $(DIRLIB)/clib.a *.o
3652

3753
$(DIROBJ)/%.o : $(DIRSRC)/%.c
3854
mkdir -p $(DIROBJ)
39-
clang $(CFLAGS) $^ -c -o $@
55+
$(CC) $(CFLAGS) $^ -c -o $@
4056

4157
tests : $(patsubst $(DIRTST)/%.c, $(DIRBIN)/%, $(wildcard $(DIRTST)/*.c))
4258

4359
$(DIRBIN)/% : $(DIRTST)/%.c
4460
mkdir -p $(DIRBIN)
45-
clang $(CFLAGS) $< $(TSTLIB) -o $@
46-
strip $@
61+
$(CC) $(CFLAGS) $< $(TSTLIB) -o $@
62+
$(STRIP) $@
4763

4864
clean :
4965
rm -rf $(DIROBJ)

Notes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
* asock on Linux
2+
asock uses eventfd on linux, which requires a kernel version of 2.6.22 or greater.
3+
* pcre options
4+
pcre configured with "./configure --prefix=$(DIRPCRE) --disable-cpp --enable-jit --enable-utf"
5+

include/clib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <clib/sha256.h>
1313
#include <clib/arguments.h>
1414
#include <clib/threadPool.h>
15+
#include <clib/asock.h>
1516

1617
//==============================================================================
1718
#endif //CLIB_HEADER

include/clib/asock.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// asock.h
2+
//==============================================================================
3+
#ifndef CLIB_ASOCK_HEADER
4+
#define CLIB_ASOCK_HEADER
5+
//==============================================================================
6+
#include <pthread.h>
7+
#include <clib/types.h>
8+
#include <clib/threadPool.h>
9+
//==============================================================================
10+
11+
typedef enum
12+
{
13+
ASOCK_STATUS_OK = 0x0000, // okay
14+
ASOCK_STATUS_OKAY = 0x0000, // okay
15+
ASOCK_STATUS_BUSY = 0x0001, // asock operation in-progress
16+
ASOCK_STATUS_TIMEOUT = 0x0002, // timed out
17+
ASOCK_STATUS_DISCONN = 0x0003, // disconnected (already)
18+
ASOCK_STATUS_CONN = 0x0004, // connected (already)
19+
ASOCK_STATUS_FAILED = 0xFFFF // other/general failure
20+
} asockStatus;
21+
22+
typedef void (*asockComplete)(void *sock, void *data, u32 status, char *message);
23+
24+
typedef struct
25+
{
26+
pthread_t thPoll;
27+
threadPool *pool;
28+
u32 sockCount;
29+
pthread_mutex_t sockCountMutex;
30+
pthread_cond_t sockCountCond;
31+
int fdPoll;
32+
int fdPollAbort;
33+
} asockWorker;
34+
35+
typedef struct
36+
{
37+
int fd;
38+
u32 timeout;
39+
asockComplete complete;
40+
void *completeData;
41+
void *workOp;
42+
asockWorker *worker;
43+
bool connected;
44+
bool busy;
45+
pthread_mutex_t mutex;
46+
int workOpEvents;
47+
u32 workOpPartial;
48+
u32 workOpNumber;
49+
u64 workOpTimeout;
50+
void *workOpExtra;
51+
#ifdef __linux__
52+
int fdTimeout;
53+
#endif
54+
} asock;
55+
56+
asock *asockCreate(asockWorker *worker, u32 timeout);
57+
bool asockIsBusy(asock *sock);
58+
bool asockSetTimeout(asock *sock, u32 timeout);
59+
bool asockBind(asock *sock, char *ip, u16 port, u32 *errn, char **errs);
60+
bool asockListen(asock *sock, u32 *errn, char **errs);
61+
void asockAccept(asock *sock, asockComplete complete, void *data);
62+
void asockConnect(asock *sock, char *host, u16 port, asockComplete complete, void *data);
63+
void asockReadSome(asock *sock, void *buff, u32 max, asockComplete complete, void *data);
64+
void asockRead(asock *sock, void *buff, u32 length, asockComplete complete, void *data);
65+
void asockWrite(asock *sock, void *buff, u32 length, asockComplete complete, void *data);
66+
void asockDisconnect(asock *sock, asockComplete complete, void *data);
67+
void asockFree(asock **sock);
68+
void asockRegister(asock *sock);
69+
void asockUnregister(asock *sock);
70+
71+
asockWorker *asockWorkerCreate(u32 workers);
72+
void asockWorkerStart(asockWorker *worker);
73+
void asockWorkerWait(asockWorker *worker);
74+
void asockWorkerStop(asockWorker *worker, bool force, bool wait);
75+
void asockWorkerFree(asockWorker **worker);
76+
77+
//==============================================================================
78+
#endif //CLIB_ASOCK_HEADER
79+
80+
//==============================================================================
81+
//------------------------------------------------------------------------------
82+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

include/clib/string.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
#include <clib/types.h>
88
//==============================================================================
99

10-
//strdup/strndup may or may not be available (not portable)...
10+
//strdup may or may not be available (not portable)...
1111
char *clstrdup(char *str);
12-
char *clstrndup(char *str, u32 max);
12+
//strndup may not either, but it's max-length is annoying; provide strdupn instead
13+
char *clstrdupn(char *str, u32 length);
14+
//non-terminated clstrdupn
15+
void *clmemdup(void *data, u32 size);
1316

1417
//------------------------------------------------------------------------------
1518

include/clib/threadPool.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
typedef void (*threadPoolFunct)(void *);
1212

13-
typedef struct threadPoolTast_st
13+
typedef struct threadPoolTask_st
1414
{
1515
threadPoolFunct funct;
1616
void *arg;
17-
struct threadPoolTast_st *next;
17+
struct threadPoolTask_st *next;
1818
} threadPoolTask;
1919

2020
typedef struct
@@ -24,6 +24,7 @@ typedef struct
2424
pthread_mutex_t mutex;
2525
pthread_cond_t condition;
2626
i32 load;
27+
i32 running;
2728
threadPoolTask *origin;
2829
threadPoolTask *final;
2930
} threadPool;

source/asockInternal.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// asockInternal.h
2+
//==============================================================================
3+
#ifndef CLIB_ASOCK_INTERNAL_HEADER
4+
#define CLIB_ASOCK_INTERNAL_HEADER
5+
//==============================================================================
6+
#define MAXEVENTS 128
7+
#ifdef __linux__
8+
# define EPOLLTIMEOUT 0x00000BAD
9+
#elif __APPLE__
10+
# define EVFILT_HUP (-4331)
11+
#endif
12+
//==============================================================================
13+
14+
//==============================================================================
15+
#endif //CLIB_ASOCK_INTERNAL_HEADER
16+
17+
//==============================================================================
18+
//------------------------------------------------------------------------------
19+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

0 commit comments

Comments
 (0)