Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor

  • Loading branch information...
commit e284badf079db519db8fdd2e86959f2ee17240be 1 parent c4bef9a
@kr authored
Showing with 77 additions and 116 deletions.
  1. +1 −1  Makefile
  2. +4 −6 dat.h
  3. +25 −11 srv.c → serv.c
  4. +26 −48 sock-bsd.c
  5. +21 −50 sock-linux.c
View
2  Makefile
@@ -23,7 +23,7 @@ OFILES=\
prot.o\
sd-daemon.o\
sock-$(OS).o\
- srv.o\
+ serv.o\
time.o\
tube.o\
util.o\
View
10 dat.h
@@ -26,7 +26,7 @@ typedef struct Wal Wal;
typedef void(*evh)(int, short, void *);
typedef void(*ms_event_fn)(ms a, void *item, size_t i);
-typedef void(*Handle)(void*, int rw); // rw can also be 'h' for hangup
+typedef void(*Handle)(void*, int rw);
typedef int(*Less)(void*, void*);
typedef void(*Record)(void*, int);
typedef int(FAlloc)(int, int);
@@ -89,10 +89,9 @@ struct Socket {
void *x;
int added;
};
-
-void sockinit(Handle tick, void *x, int64 ns);
-int sockwant(Socket *s, int rw);
-void sockmain(void); // does not return
+int sockinit(void);
+int sockwant(Socket*, int);
+int socknext(Socket**, int64);
struct ms {
size_t used, cap, last;
@@ -382,4 +381,3 @@ struct Server {
};
void srvserve(Server *srv);
void srvaccept(Server *s, int ev);
-void srvtick(Server *s, int ev);
View
36 srv.c → serv.c
@@ -17,8 +17,13 @@ void
srvserve(Server *s)
{
int r;
+ Socket *sock;
+ int64 period = 10*1000000; // 10ms
- sockinit((Handle)srvtick, s, 10*1000000); // 10ms
+ if (sockinit() == -1) {
+ twarnx("sockinit");
+ exit(1);
+ }
s->sock.x = s;
s->sock.f = (Handle)srvaccept;
@@ -37,21 +42,30 @@ srvserve(Server *s)
exit(2);
}
- sockmain();
- twarnx("sockmain");
- exit(1);
-}
+ int64 t = nanoseconds();
+ for (;;) {
+ int rw = socknext(&sock, period);
+ if (rw == -1) {
+ twarnx("socknext");
+ exit(1);
+ }
-void
-srvaccept(Server *s, int ev)
-{
- h_accept(s->sock.fd, ev, s);
+ int64 t1 = nanoseconds();
+ if (t1-t > period) {
+ prottick(s);
+ t = t1;
+ }
+
+ if (rw) {
+ sock->f(sock->x, rw);
+ }
+ }
}
void
-srvtick(Server *s, int ev)
+srvaccept(Server *s, int ev)
{
- prottick(s);
+ h_accept(s->sock.fd, ev, s);
}
View
74 sock-bsd.c
@@ -10,28 +10,18 @@ enum
Infinity = 1 << 30
};
-static void handle(Socket*, int, int);
+static int kq;
-static Handle tick;
-static void *tickval;
-static int kq;
-static int64 ival;
-static struct timespec ivalts;
-
-void
-sockinit(Handle f, void *x, int64 ns)
+int
+sockinit(void)
{
- tick = f;
- tickval = x;
- ival = ns;
- ivalts.tv_sec = ns / 1000000000;
- ivalts.tv_nsec = ns % 1000000000;
kq = kqueue();
if (kq == -1) {
twarn("kqueue");
- exit(1);
+ return -1;
}
+ return 0;
}
@@ -76,41 +66,29 @@ sockwant(Socket *s, int rw)
}
-void
-sockmain()
+int
+socknext(Socket **s, int64 timeout)
{
- int i, r, n = 1;
- int64 e, t = nanoseconds();
- struct kevent evs[n];
-
- for (;;) {
- r = kevent(kq, NULL, 0, evs, n, &ivalts);
- if (r == -1 && errno != EINTR) {
- twarn("kevent");
- exit(1);
- }
-
- // should tick?
- e = nanoseconds();
- if (e-t > ival) {
- tick(tickval, 0);
- t = e;
- }
-
- for (i=0; i<r; i++) {
- handle(evs[i].udata, evs[i].filter, evs[i].flags);
- }
-
+ int r;
+ struct kevent ev;
+ static struct timespec ts;
+
+ ts.tv_sec = timeout / 1000000000;
+ ts.tv_nsec = timeout % 1000000000;
+ r = kevent(kq, NULL, 0, &ev, 1, &ts);
+ if (r == -1 && errno != EINTR) {
+ twarn("kevent");
+ return -1;
}
-}
-
-static void
-handle(Socket *s, int filt, int flags)
-{
- if (filt == EVFILT_READ) {
- s->f(s->x, 'r');
- } else if (filt == EVFILT_WRITE) {
- s->f(s->x, 'w');
+ if (r > 0) {
+ *s = ev.udata;
+ switch (ev.filter) {
+ case EVFILT_READ:
+ return 'r';
+ case EVFILT_WRITE:
+ return 'w';
+ }
}
+ return 0;
}
View
71 sock-linux.c
@@ -7,25 +7,18 @@
#define EPOLLRDHUP 0x2000
#endif
-static void handle(Socket *s, int events);
+static int epfd;
-static Handle tick;
-static void *tickval;
-static int epfd;
-static int ival; // ms
-
-void
-sockinit(Handle f, void *x, int64 ns)
+int
+sockinit(void)
{
- tick = f;
- tickval = x;
- ival = ns / 1000000;
epfd = epoll_create(1);
if (epfd == -1) {
twarn("epoll_create");
- exit(1);
+ return -1;
}
+ return 0;
}
@@ -61,47 +54,25 @@ sockwant(Socket *s, int rw)
}
-void
-sockmain()
+int
+socknext(Socket **s, int64 timeout)
{
- int i, r, n = 1;
- int64 e, t = nanoseconds();
- struct epoll_event evs[n];
-
- for (;;) {
- r = epoll_wait(epfd, evs, n, ival);
- if (r == -1 && errno != EINTR) {
- twarn("epoll_wait");
- exit(1);
- }
-
- // should tick?
- e = nanoseconds();
- if ((e-t) / 1000000 > ival) {
- tick(tickval, 0);
- t = e;
- }
-
- for (i=0; i<r; i++) {
- handle(evs[i].data.ptr, evs[i].events);
- }
+ int r;
+ struct epoll_event ev;
+ r = epoll_wait(epfd, &ev, 1, (int)(timeout/1000000));
+ if (r == -1 && errno != EINTR) {
+ twarn("epoll_wait");
+ exit(1);
}
-}
-
-static void
-handle(Socket *s, int evset)
-{
- int c = 0;
-
- if (evset & (EPOLLHUP|EPOLLRDHUP)) {
- c = 'r';
- } else if (evset & EPOLLIN) {
- c = 'r';
- } else if (evset & EPOLLOUT) {
- c = 'w';
+ if (r) {
+ *s = ev.data.ptr;
+ if (ev.events & (EPOLLIN|EPOLLHUP|EPOLLRDHUP)) {
+ return 'r';
+ } else if (ev.events & EPOLLOUT) {
+ return 'w';
+ }
}
-
- s->f(s->x, c);
+ return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.