[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r8305: - all system calls ready - all major bugs and todos fixed - (bsockets/trunk)
Author: chiussi
Date: 2006-08-30 04:34:56 -0400 (Wed, 30 Aug 2006)
New Revision: 8305
Modified:
bsockets/trunk/accept.c
bsockets/trunk/bsocket.h
bsockets/trunk/callback.c
bsockets/trunk/event.c
bsockets/trunk/event.h
bsockets/trunk/io.c
bsockets/trunk/io.h
bsockets/trunk/list.c
bsockets/trunk/misc.c
bsockets/trunk/misc.h
bsockets/trunk/notes
bsockets/trunk/select.c
bsockets/trunk/select.h
bsockets/trunk/socket.c
bsockets/trunk/socket.h
bsockets/trunk/sync.c
bsockets/trunk/test.c
bsockets/trunk/test.h
bsockets/trunk/wait.c
bsockets/trunk/wait.h
Log:
- all system calls ready
- all major bugs and todos fixed
- merging begins tommorow ...
Modified: bsockets/trunk/accept.c
===================================================================
--- bsockets/trunk/accept.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/accept.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -108,8 +108,8 @@
}
- //todo -- should we raise exception if accept() fails?
s = accept(b->s,name,namelen);
+ //no reason to raise exception on accept() failure
CHECK(s != INVALID_SOCKET,0);
post_accepted(b,s,env);
@@ -140,7 +140,6 @@
fail:
if (b != NULL) {
- //todo verify that bsocket doesnt alter errno
bsocket_release(fd,AS_READ,env);
}
Modified: bsockets/trunk/bsocket.h
===================================================================
--- bsockets/trunk/bsocket.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/bsocket.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -9,11 +9,11 @@
/*user options*/
/**************/
+#define MAX_BSOCKETS 2048
+
/*these should be throttled carefully, as too many open half-open sockets
or listening sockets wreak havoc on non-server kernels*/
-#define MAX_BSOCKETS 2048
-
#define MAX_SIMUL_CONNECT 9
#define MAX_SIMUL_LISTEN 5
Modified: bsockets/trunk/callback.c
===================================================================
--- bsockets/trunk/callback.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/callback.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -14,6 +14,9 @@
IN WSAOVERLAPPED *wo,
IN DWORD flags) {
+ struct socket_env *env2;
+ int fd2;
+
struct _msg *msg;
int err2;
@@ -21,14 +24,16 @@
int z;
msg = (struct _msg*) wo->hEvent;
+ env2 = msg->env;
+ fd2 = msg->fd;
if (err == 0) {
if (msg->len == len) {
socket_raise(msg->fd,IS_WRITABLE,TRUE,msg->env);
- msg_free(msg);
+
} else {
//it is pretty nasty to do the write operation twice, but
@@ -58,6 +63,9 @@
socket_exception(msg->fd,unixify_wsaerr(err2),msg->env);
}
+ } else {
+ //i dont like this, but it makes the code prettier as a whole
+ return;
}
}
@@ -69,7 +77,11 @@
}
}
+ msg_free(msg);
+ free(wo);
+ env2->b[fd2]->writing = FALSE;
+
}
void CALLBACK callback_read(
@@ -78,21 +90,33 @@
IN WSAOVERLAPPED *wo,
IN DWORD flags) {
+ struct socket_env *env2;
+ int fd2;
+
struct bsocket *b;
struct _msg *msg;
int r;
+ int delivered;
+
+ delivered = FALSE;
+
msg = (struct _msg*) wo->hEvent;
+ env2 = msg->env;
+ fd2 = msg->fd;
if (err == 0) {
ASSERT(len >= 0);
-
ASSERT(len <= msg->len);
if (len > 0) {
+ //important!!
+ //after we have placed message into input queue, we are no longer
+ //allowed to do anything with it, copy the stuff we care about so
+ //we can use it later
b = bsocket_get(msg->fd,AS_READ,msg->env);
ASSERT(b != NULL);
@@ -100,41 +124,39 @@
msg->len = len;
+ //place message into destination input queue
r = (int) list_enqueue(msg,b->in_q );
+ if (r != 0) {
+ delivered = TRUE;
+ }
- bsocket_release(msg->fd,AS_READ,msg->env);
+ bsocket_release(fd2,AS_READ,env2);
if (r != 0) {
ASSERT(len > 0);
- post_read(b,msg->env);
+ post_read(b,env2);
+ socket_raise(fd2,IS_READABLE,TRUE,env2);
- socket_raise(msg->fd,IS_READABLE,TRUE,msg->env);
-
} else {
-
- //todo -- dealloc msg?
- socket_exception(b->fd,errno,msg->env);
-
+ socket_exception(fd2,errno,env2);
}
-
} else {
-
- socket_exception(msg->fd,0,msg->env);
-
+ socket_exception(msg->fd,0,msg->env);
}
} else {
-
- //if err is WSAINVAL it means that the OVERLAPPED argument has become invalid.
- //this happens when there is an outstanding read operation when the socket is
- //deallocated. the best thing to do is nothing.
- if (err != WSAEINVAL) {
socket_exception(msg->fd,unixify_wsaerr(err),msg->env);
- }
}
+ if (!delivered) {
+ msg_free(msg);
+ }
+
+ free(wo);
+ env2->b[fd2]->reading = FALSE;
+
}
//caller must guarantee list atomicity
@@ -163,11 +185,8 @@
l[fd]->connecting = FALSE;
if (err) {
-
bsocket_exception(l[fd],err,env);
-
} else {
-
make_connected(l[fd],-1,env);
}
Modified: bsockets/trunk/event.c
===================================================================
--- bsockets/trunk/event.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/event.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -155,7 +155,6 @@
e->sig = w;
e->err = err;
-
MUTEX_ACQUIRE(env->post_m);
if (list_enqueue(e,env->event_q) == NULL) {
@@ -163,7 +162,6 @@
}
ASSERT(WSASetEvent(env->post_e));
-
MUTEX_RELEASE(env->post_m);
if (w != NULL ) {
@@ -171,7 +169,6 @@
}
fail:
-
if (w != NULL) {
ASSERT(CloseHandle(w));
}
@@ -247,9 +244,8 @@
return out;
}
-int post_write(struct bsocket *b, int *ret, int *err, struct socket_env *env) {
- //todo assert is writable
- return event_post(b,EV_WRITE,&b->w_wo,ret,err,TRUE,env);
+int post_write(struct bsocket *b, WSAOVERLAPPED *wo, int *ret, int *err, struct socket_env *env) {
+ return event_post(b,EV_WRITE,wo,ret,err,TRUE,env);
}
int post_exception(struct bsocket *b, int err, struct socket_env *env) {
@@ -261,7 +257,7 @@
}
int post_read(struct bsocket *b, struct socket_env *env) {
- return event_post(b,EV_READ,&b->r_wo,NULL,NULL,FALSE,env);
+ return event_post(b,EV_READ,NULL,NULL,NULL,FALSE,env);
}
int post_listen(struct bsocket *b, int backlog, int *res, int *err, struct socket_env *env) {
@@ -272,6 +268,11 @@
return event_post(b,EV_ACCEPT,(void*)s,NULL,NULL,TRUE,env);
}
+int post_shutdown(struct socket_env *env) {
+ return event_post(NULL,EV_SHUTDOWN,NULL,NULL,NULL,FALSE,env);
+
+}
+
struct _event *event_next(struct socket_env *env) {
struct _event *e;
@@ -279,7 +280,6 @@
MUTEX_ACQUIRE(env->post_m);
if (list_dequeue(&e,env->event_q)) {
-
WSAResetEvent(env->post_e);
e = NULL;
errno = EEMPTY;
@@ -288,14 +288,13 @@
MUTEX_RELEASE(env->post_m);
return e;
-
}
#define E_NOTIFY (0+WAIT_OBJECT_0)
#define E_CONNECT (1+WAIT_OBJECT_0)
#define E_ACCEPT (2+WAIT_OBJECT_0)
-#define E_INSERT(S,FD,TYPE) for (_j=1; _j<WSA_MAXIMUM_WAIT_EVENTS; ) {\
+#define E_INSERT(S,FD,TYPE) for (_j=1; _j<WSA_MAXIMUM_WAIT_EVENTS; ) {\
if (ev[_j].fd == -1) {\
ASSERT( WSAEventSelect(S,we[_j],TYPE) ==0 );\
ev[_j].fd = FD;\
@@ -374,12 +373,9 @@
CHECK(connect_q != NULL, 0);
for (i=1; i<WSA_MAXIMUM_WAIT_EVENTS; i++) {
-
we[i] = WSACreateEvent();
CHECK_(we[i] != WSA_INVALID_EVENT);
-
ev[i].fd = -1;
-
}
for (i=0; i<MAX_BSOCKETS; i++) {
@@ -415,9 +411,6 @@
z = 0;
- //todo -- previously the index was was ev->fd before,
- //what in the nameof christ was i thinking?
- //furthermore, _why_ was it working!
if ((i = connect_lookup[e->fd]) != -1) {
E_DELETE(i);
z++;
@@ -431,13 +424,28 @@
ASSERT(z <= 1);
- //todo cancel any outstanding i/o (do we need to?)
- if (e->p_fd == -1)
- ASSERT(closesocket(e->s) == 0);
+ if (e->p_fd == -1) {
- //todo -- this is a major sync violation, but it feels safe, why?
- SetEvent(env->b[e->fd]->exception_e);
+ if (closesocket(e->s) == SOCKET_ERROR) {
+ socket_set_err(e->fd,unixify_wsaerr(WSAGetLastError()),env);
+ }
+ /*despite what this may look like, it is not busy
+ looping. the SleepEx says "put the thread in an
+ state that can run callbacks".
+ */
+ while (env->b[e->fd]->reading || env->b[e->fd]->writing) {
+ if (SleepEx(0,TRUE) == 0) {
+ ASSERT(env->b[e->fd]->reading == FALSE);
+ ASSERT(env->b[e->fd]->writing == FALSE);
+ }
+ }
+
+ }
+
+ //this is safe because if close() is raising this exception, it is waiting for us
+ //to respond, it will not remove anything from env->b yet
+ SetEvent(env->b[e->fd]->exception_e);
event_respond(e,0,0);
break;
@@ -448,7 +456,6 @@
break;
case EV_CONNECT:
-
//schedule connection request
if ( (c = e->data) != NULL) {
@@ -475,8 +482,7 @@
} else {
- //todo -- errno should = 0?
- event_respond(e,-1,errno);
+ event_respond(e,0,0);
}
@@ -491,8 +497,7 @@
if (z == 0) {
- //todo -- start reading
- socket_raise(e->fd,IS_WRITABLE,TRUE,env);
+ complete_connect(e->fd,0,env);
} else {
@@ -504,7 +509,6 @@
} else {
- //todo -- is there a memory leak here?
complete_connect(
e->fd,
unixify_wsaerr(WSAGetLastError()),
@@ -514,13 +518,15 @@
}
+ //todo -- free event here?
+
}
break;
case EV_READ:
- invoke_read(e->s,e->fd,(WSAOVERLAPPED*) e->data,env);
+ invoke_read(e->s,e->fd,env);
break;
@@ -576,15 +582,11 @@
event_respond(e,msg->len,0);
} else {
-
event_respond(e,-1,errno);
-
}
} else {
-
event_respond(e,-1,errno);
-
}
}
@@ -612,8 +614,8 @@
break;
+ //deassociate accepted socket with network events
case EV_ACCEPT:
- //deassociate accepted socket with network events
z = listen_lookup[e->fd];
@@ -624,7 +626,6 @@
WSAEventSelect((SOCKET) e->data,we[z],0)
!= SOCKET_ERROR
);
-
event_respond(e,0,0);
break;
@@ -632,9 +633,7 @@
default:
ASSERT(FALSE);
break;
-
}
-
break;
default:
@@ -644,62 +643,48 @@
ASSERT(i < WSA_MAXIMUM_WAIT_EVENTS);
z = WSAEnumNetworkEvents(ev[i].s,we[i],&ne);
+ ASSERT(z != SOCKET_ERROR);
- if (z != SOCKET_ERROR) {
+ ASSERT( ne.lNetworkEvents & ev[i].type);
+ //make sure no other events are getting set
+ ASSERT( (ne.lNetworkEvents & ~(ev[i].type)) == 0);
- ASSERT( ne.lNetworkEvents & ev[i].type);
- //make sure no other events are getting set
- ASSERT( (ne.lNetworkEvents & ~(ev[i].type)) == 0);
+ switch (ev[i].type) {
- switch (ev[i].type) {
+ case FD_CONNECT:
+ err = ne.iErrorCode[FD_CONNECT_BIT];
- case FD_CONNECT:
- err = ne.iErrorCode[FD_CONNECT_BIT];
+ if (err) {
+ complete_connect(ev[i].fd,unixify_wsaerr(err),env);
+ } else {
+ complete_connect(ev[i].fd,0,env);
+ }
- if (err) {
- complete_connect(ev[i].fd,unixify_wsaerr(err),env);
- } else {
- complete_connect(ev[i].fd,0,env);
- }
+ E_DELETE(i);
+ connect_count--;
+ ASSERT(event_post(NULL,EV_CONNECT,NULL,NULL,NULL,FALSE,env) == 0);
- E_DELETE(i);
- connect_count--;
+ break;
- ASSERT(event_post(NULL,EV_CONNECT,NULL,NULL,NULL,FALSE,env) == 0);
+ case FD_ACCEPT:
+ err = ne.iErrorCode[FD_ACCEPT_BIT];
- break;
+ if (err) {
+ socket_exception(ev[i].fd,unixify_wsaerr(err),env);
+ E_DELETE(i);
+ listen_count--;
- case FD_ACCEPT:
- err = ne.iErrorCode[FD_ACCEPT_BIT];
+ } else {
+ socket_client_in(ev[i].fd,env);
+ }
- if (err) {
- socket_exception(ev[i].fd,unixify_wsaerr(err),env);
- E_DELETE(i);
- listen_count--;
+ break;
- } else {
- socket_client_in(ev[i].fd,env);
- }
-
-
- break;
-
- default:
- ASSERT(FALSE);
- break;
- }
-
-
- } else {
-
- //complete_connect(ev[i].fd,unixify_wsaerr(err),env);
- //socket_exception(ev[i].fd,unix);
- //todo -- rethink this part
- ASSERT(FALSE);
-
+ default:
+ ASSERT(FALSE);
+ break;
}
-
break;
case WSA_WAIT_FAILED:
@@ -719,13 +704,10 @@
ASSERT(list_dequeue(NULL,env->event_q) != 0);
for (i=1; i<WSA_MAXIMUM_WAIT_EVENTS; i++) {
-
- //todo -- assert nothing is using these events
ASSERT(WSACloseEvent(we[i]));
}
fail:
-
if (connect_q != NULL)
list_free(connect_q);
Modified: bsockets/trunk/event.h
===================================================================
--- bsockets/trunk/event.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/event.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -50,9 +50,11 @@
int post_connect(struct bsocket *, struct sockaddr *, size_t len, struct socket_env *env);
int post_exception(struct bsocket *, int, struct socket_env*);
int post_read(struct bsocket *, struct socket_env *);
-int post_write(struct bsocket *, int*, int*, struct socket_env*);
+int post_write(struct bsocket *, WSAOVERLAPPED*, int*, int*, struct socket_env*);
int post_close(struct bsocket *, struct socket_env*);
int post_listen(struct bsocket *, int, int*, int*, struct socket_env*);
int post_accepted(struct bsocket *, SOCKET, struct socket_env*);
+int post_shutdown(struct socket_env*);
+
#endif
Modified: bsockets/trunk/io.c
===================================================================
--- bsockets/trunk/io.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/io.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -9,7 +9,7 @@
#include "list.h"
#include "io.h"
-struct _msg *msg_new(int fd, int len, void *data, struct socket_env *env) {
+struct _msg *msg_new(int fd, SOCKET s, int len, void *data, struct socket_env *env) {
struct _msg *out;
@@ -33,24 +33,18 @@
out->buf_wb.buf = out->o_data;
out->buf_wb.len = len;
-
-
out->env = env;
out->fd = fd;
+ out->s = s;
} else {
-
free(out);
out = NULL;
-
errno = ENOMEM;
-
}
} else {
-
errno = ENOMEM;
-
}
return out;
@@ -61,11 +55,12 @@
free(msg->o_data);
free(msg);
-
}
-void invoke_read(SOCKET s, int fd, WSAOVERLAPPED *wo, struct socket_env *env) {
+void invoke_read(SOCKET s, int fd, struct socket_env *env) {
+ WSAOVERLAPPED *wo;
+
struct _msg *msg;
int err;
@@ -79,11 +74,23 @@
z = 0;
- msg = msg_new(fd,4096,NULL,env);
+ //no matter how tempting it is, do include WSAOVERLAPPED structures
+ //in bsocket strucutre. this opens a nasty can of concurrency worms.
+ //allocating a different one for each invoked read is good enough for now
+ //think of a better way some day
+ wo = (WSAOVERLAPPED*) malloc(sizeof(WSAOVERLAPPED));
+
+ msg = msg_new(fd,s,4096,NULL,env);
CHECK(msg != NULL,0);
wo->hEvent = (WSAEVENT) msg;
+
+ //possible sync issue
+ //make sure we are not reading twice
+ ASSERT(env->b[fd]->reading == FALSE);
+ env->b[fd]->reading = TRUE;
+
r = WSARecv(
s,
&msg->buf_wb,
@@ -95,9 +102,7 @@
);
if (r == SOCKET_ERROR) {
-
err = WSAGetLastError();
-
CHECK(err == WSA_IO_PENDING,err);
}
@@ -118,7 +123,6 @@
int recv_win32(int fd, void *buf, size_t len, int flags, struct socket_env *env) {
struct bsocket *b;
-
struct _msg *msg;
int out;
@@ -180,7 +184,6 @@
if (msg->len == 0 ) {
msg_free(msg);
-
ASSERT(list_dequeue(NULL,b->in_q) == 0);
} else {
@@ -192,16 +195,12 @@
data_read += copy_len;
} else {
-
space_left = 0;
-
}
} else {
socket_raise(fd,IS_READABLE,FALSE,env);
-
}
-
}
out = data_read;
@@ -218,6 +217,8 @@
int send_win32 (int fd, void *buf, size_t len, int flags, struct socket_env *env) {
+ WSAOVERLAPPED *wo;
+
struct bsocket *b;
struct _msg *msg;
@@ -231,22 +232,31 @@
CHECK(b->connected,ENOTCONN);
if (b->blocking == FALSE) {
- //todo we need a new way to check if this socket is writable
+ CHECK(!b->writing,EAGAIN);
}
r = wait_until(b,IS_WRITABLE,env);
+ ASSERT(b->writing == FALSE);
+
+ if (b->partner == -1) {
+ b->writing = TRUE;
+ }
+
socket_raise(fd,IS_WRITABLE,0,env);
CHECK(r == 0,0);
- msg = msg_new(fd,len,buf,env);
+ msg = msg_new(fd,b->s,len,buf,env);
CHECK(msg != NULL,0);
- b->w_wo.hEvent = (WSAEVENT) msg;
+ wo = (WSAOVERLAPPED*) malloc(sizeof(WSAOVERLAPPED));
+ CHECK_(wo != NULL);
- r = post_write(b,&z,&err,env);
+ wo->hEvent = (WSAEVENT) msg;
+ r = post_write(b,wo,&z,&err,env);
+
if (r != 0) {
errno = err;
out = -1;
Modified: bsockets/trunk/io.h
===================================================================
--- bsockets/trunk/io.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/io.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -17,9 +17,9 @@
};
-void invoke_read(SOCKET, int, WSAOVERLAPPED *, struct socket_env *);
+void invoke_read(SOCKET, int, struct socket_env *);
-struct _msg *msg_new(int, int, void*, struct socket_env*);
+struct _msg *msg_new(int, SOCKET, int, void*, struct socket_env*);
void msg_free(struct _msg *);
int send_win32(int, void*, size_t, int, struct socket_env*);
Modified: bsockets/trunk/list.c
===================================================================
--- bsockets/trunk/list.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/list.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -4,19 +4,13 @@
int list_is_empty(struct _list *list) {
-
return list->head == NULL;
-
}
void list_data(void *data, struct _list_node *n) {
- //todo -- this is ugly, why?
if (data != NULL) {
-
*((void**) data) = n->data;
-
}
-
}
struct _list_node *list_enqueue(void *data, struct _list *list) {
@@ -107,23 +101,16 @@
int out;
if ( (out = list_queuepeek(data,list)) == 0) {
-
list_node_detach(list->head);
-
}
return out;
}
-
void list_node_free(struct _list_node *node) {
-
free(node);
-
}
-
-
struct _list *list_new() {
struct _list *out;
@@ -132,10 +119,8 @@
malloc(sizeof(struct _list));
if (out != NULL) {
-
out->head = NULL;
out->tail = NULL;
-
} else {
errno = ENOMEM;
}
@@ -150,5 +135,4 @@
while (list_dequeue(&n,list) == 0);
free(list);
-
}
Modified: bsockets/trunk/misc.c
===================================================================
--- bsockets/trunk/misc.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/misc.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -5,9 +5,7 @@
char _G_VERY_BAD_NEWS_[] = "\nSerious error, can't cope. Here is as much information as we can give...\n\tfile:%s line:%d errno:%d win32 err:%d\n";
HANDLE make_thread (void *func, void *data) {
-
return CreateThread(NULL,0,func, data, 0, NULL);
-
}
/*"borrowed" from msdn*/
@@ -21,7 +19,6 @@
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
-
return 1;
}
@@ -39,8 +36,6 @@
//im not going to think about overflow. if you want to wait more than 5
//years or something like that, TS
int timeval_to_millis(struct timeval *tv) {
-
return (tv->tv_sec)*1000;
-
}
Modified: bsockets/trunk/misc.h
===================================================================
--- bsockets/trunk/misc.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/misc.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -23,19 +23,24 @@
goto fail;\
}\
-#define ASSERT(X) if (!(X)) { fflush(stdout); VERY_BAD_NEWS ; _exit(1); }
+#define ASSERT(X) if (!(X)) { fflush(stdout); VERY_BAD_NEWS ; _exit(666); }
#define MUTEX_ACQUIRE(X) ASSERT(WaitForSingleObject(X,INFINITE) == WAIT_OBJECT_0)
#define MUTEX_RELEASE(X) ASSERT(ReleaseMutex(X))
-#define LA //printf("list acquire: %s:%d\n",__func__,__LINE__); fflush(stdout);
-#define LR //printf("list release: %s:%d\n",__func__,__LINE__); fflush(stdout);
-
int winsock_start();
HANDLE make_thread(void*,void*);
-//todo -- if timeval not defined, give it something to work with
+//todo -- define this in configure script
+#define HAVE_TIMEVAL
+#ifndef HAVE_TIMEVAL
+struct timeval {
+ time_t tv_sec;
+ unsigned int tv_usec;
+};
+#endif
+
int timeval_to_millis(struct timeval *);
#endif
Modified: bsockets/trunk/notes
===================================================================
--- bsockets/trunk/notes 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/notes 2006-08-30 08:34:56 UTC (rev 8305)
@@ -1,37 +1,16 @@
Whats Left? (sorted by importance in non-increasing order)
===========
-1. baccept()
- - blisten(): tells event manager to set desired socket to IS_READABLE when
- FD_ACCEPT happens
- - baccept(): calls accept() with underlying socket as argument
-
-2. brecv()
- - when callback_write occurs, and the entire message has not been sent
- reiterate
- - figure out a way to test the above (ie, force WSARecv() to not send
- entire message)
-
-3. Socket options/bfcntl()
- - figure out how we are going to handle SO_LINGER
- (wait until socket is writable again?)
+1. Socket options/bfcntl()
- should we forward options to the underlying socket if bsocket doesn't
support/need them ?
-3. Synchronization issues
- - decide if we need a seperate mutex for wait objects. having global
- ownership is fine, but it slows everything down.
- - ensure atomicity on wl_wait_many. problem because in time between
- wait list monitor placement and wait list activation, anohter activation
- may occur, which is currently undefined behavior. To observe this, run
- test 25 with a large number of connections
-
-4. Memory leaks
+2. Memory leaks
- There is a small memory leak observed when simply calling
bsocket()/bclose(). Why?
- For each malloc, make sure there is a free.
-5. Complete remaining test ideas (todos in test.c).
+3. Complete remaining test ideas (todos in test.c).
Misc
@@ -43,8 +22,4 @@
even a portability issue? I don't want to lose my dollar to nick over this :)
- Make sure that private functions are declared as static so they don't cause
any collisions
-
-
-
-
-
+- For now, we are not concerning ourselves with SO_LONGER
Modified: bsockets/trunk/select.c
===================================================================
--- bsockets/trunk/select.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/select.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -49,69 +49,46 @@
struct bsocket **l;
- LA;
-
l = bsocket_list_get(env);
+ ASSERT(l != NULL);
+ ASSERT(l[fd] != NULL);
- //todo -- assert is not null
- if (l != NULL) {
+ bsocket_raise(l[fd],type,active,env);
- if (l[fd] != NULL) {
- bsocket_raise(l[fd],type,active,env);
- }
+ bsocket_list_release(env);
- LR;
- bsocket_list_release(env);
-
- }
-
}
void socket_set_err(int fd, int err, struct socket_env *env) {
struct bsocket **l;
- LA
l = bsocket_list_get(env);
- if (l != NULL) {
+ ASSERT(l != NULL);
+ ASSERT(l[fd] != NULL);
- if (l[fd] != NULL) {
+ l[fd]->err = err;
- l[fd]->err = err;
+ bsocket_list_release(env);
- }
-
- LR
- bsocket_list_release(env);
-
- }
-
}
void socket_last_err(int fd, int *err, struct socket_env *env) {
struct bsocket **l;
- LA
l = bsocket_list_get(env);
- if (l != NULL) {
+ ASSERT(l != NULL);
+ ASSERT(l[fd] != NULL);
- if (l[fd] != NULL) {
+ *err = l[fd]->err;
+ l[fd]->err = 0;
- *err = l[fd]->err;
- l[fd]->err = 0;
-
- }
- LR
- bsocket_list_release(env);
-
- }
-
+ bsocket_list_release(env);
}
-//requires list ownership to call
void bsocket_exception(struct bsocket *b, int err, struct socket_env *env) {
if (!b->excepted) {
@@ -122,7 +99,6 @@
b->excepted = TRUE;
b->connected = FALSE;
- b->eof = TRUE;
//NULL represents EOF
list_enqueue(NULL,b->in_q);
@@ -138,49 +114,38 @@
if (b->partner != -1) {
bsocket_exception(env->b[b->partner],err,env);
}
-
}
-
}
void socket_exception(int fd, int err, struct socket_env *env) {
struct bsocket **l;
- LA
l = bsocket_list_get(env);
- if (l != NULL) {
+ ASSERT(l != NULL);
+ ASSERT(l[fd] != NULL);
- if (l[fd] != NULL) {
- bsocket_exception(l[fd],err,env);
- }
+ bsocket_exception(l[fd],err,env);
+ bsocket_list_release(env);
- LR
-
- bsocket_list_release(env);
-
- }
-
}
int wait_until_group(struct bsocket **b, int *flag, int num, int timeout, struct socket_env *env) {
struct _wait_list **wl;
- int i,j;
+ int i;
int out;
int r;
out = 0;
wl = (struct _wait_list **) malloc(sizeof(struct _wait_list*)*(num+1));
-
CHECK(wl != NULL,ENOMEM);
for (i=0; i<num; i++) {
-
switch(flag[i]) {
case IS_READABLE:
@@ -200,35 +165,21 @@
break;
}
-
ASSERT(wl[i] != NULL);
-
}
- for (i=0; i<num; i++) {
-
- for (j=0; j<i; j++) {
-
- ASSERT(wl[j] != wl[i]);
- }
-
- }
-
- //todo == major sync issue here, fix next
r = wait_many_timeout(wl,num,timeout);
if (r == -1) {
ASSERT( (errno == ETIMEDOUT && timeout != -1)
||
(errno == ECLOSED)
-
);
out = -1;
} else {
out = r;
}
-
fail:
if (wl != NULL) {
@@ -251,9 +202,6 @@
}
-
-
-
#define M_INSERT(X) objects[count] = X;\
count++;\
@@ -264,7 +212,6 @@
}\
mutex_count--;\
-
int select_win32(int z, bfd_set *readfds, bfd_set *writefds, bfd_set *exceptfds,
struct timeval *timeout, struct socket_env *env) {
@@ -273,7 +220,6 @@
bfd_set *fds[sets];
int *t;
-
struct bsocket **b;
struct bsocket **l;
struct bsocket *c;
@@ -288,6 +234,7 @@
int types[sets];
+ //make these values in env
fds[0] = readfds;
fds[1] = writefds;
fds[2] = exceptfds;
@@ -296,7 +243,6 @@
types[1] = IS_WRITABLE;
types[2] = IS_EXCEPTED;
-
b = NULL;
fd_count = 0;
l = NULL;
@@ -310,15 +256,12 @@
CHECK(fd_count > 0,EINVAL);
-
b = (struct bsocket**) malloc((sizeof(struct bsocket*))*(fd_count+1));
-
t = (int*) malloc((sizeof (int))*(fd_count+1));
CHECK(b != NULL,ENOMEM);
CHECK(t != NULL,ENOMEM);
- LA
l = bsocket_list_get(env);
CHECK(l != NULL,0);
@@ -326,37 +269,31 @@
count = 0;
for (i=0; i<sets; i++) {
-
if (fds[i] != NULL) {
-
for (j=0; j<fds[i]->count; j++) {
b[count] = l[fds[i]->list[j]];
t[count] = types[i];
-
count++;
}
}
}
- LR
bsocket_list_release(env);
l = NULL;
- /*convert timeval to milliseconds*/
if (timeout != NULL) {
to = timeval_to_millis(timeout);
} else {
to = -1;
}
- //todo -- major, what happens if someone closes socket right here
- //(before we start waiting on it?)
-
+ //note -- undefined behavior occurs if the user close()s while selecting
+ //on a socket. to cope with this would require a major overhaul, since I don't
+ //plan to stick with select(), we'll leave it as is for now
r = wait_until_group(b,t,fd_count,to,env);
if (r != -1) {
-
//we need to know that socket is still valid
c = bsocket_get(b[r]->fd,AS_GLOBAL,env);
CHECK(c != NULL,0);
@@ -390,23 +327,18 @@
default:
ASSERT(FALSE);
break;
+
}
-
bsocket_release(c->fd,AS_GLOBAL,env);
-
out = 1;
} else {
if (errno == ETIMEDOUT) {
-
out = 0;
-
} else {
-
out = -1;
-
}
}
Modified: bsockets/trunk/select.h
===================================================================
--- bsockets/trunk/select.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/select.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -3,7 +3,6 @@
#include "socket.h"
-int bsocket_last_err (struct bsocket *, int *, struct socket_env*);
void bsocket_set_err (struct bsocket *, int, struct socket_env*);
void socket_last_err(int, int*,struct socket_env*);
Modified: bsockets/trunk/socket.c
===================================================================
--- bsockets/trunk/socket.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/socket.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -15,6 +15,7 @@
#include "misc.h"
#include "callback.h"
+
struct socket_env *__GLOBAL_BSOCKET_ENV_;
//support only turning off and on socket blocking, will add more features as needed
@@ -73,7 +74,6 @@
int out;
l = NULL;
- LA
l = bsocket_list_get(env);
CHECK(l[fd] != NULL,EBADF);
@@ -110,13 +110,11 @@
}
fail:
- LR
bsocket_list_release(env);
return out;
}
-
int connect_win32(int fd, struct sockaddr *name, size_t len, struct socket_env *env) {
struct bsocket *b;
@@ -126,10 +124,8 @@
out = 0;
-
b = bsocket_get(fd,AS_WRITE,env);
-
CHECK(b != NULL,0);
CHECK(b->connected == FALSE,EISCONN);
CHECK(b->connecting == FALSE,EALREADY);
@@ -147,21 +143,15 @@
socket_last_err(fd,&err,env);
if (err) {
-
errno = err;
out = -1;
-
} else {
-
out = 0;
-
}
} else {
-
errno = EINPROGRESS;
out = -1;
-
}
fail:
@@ -170,12 +160,10 @@
bsocket_release(fd,AS_WRITE,env);
}
-
return out;
}
-
void bsocket_free(struct bsocket *b) {
if (b->read_wl != NULL) {
@@ -209,8 +197,6 @@
CloseHandle(b->exception_e);
}
- //todo -- remove any messages waiting
-
list_free(b->in_q);
free(b);
@@ -221,6 +207,7 @@
struct bsocket *b;
int out;
+ int err;
int r;
//manually ensure that socket exists
@@ -246,16 +233,21 @@
MUTEX_ACQUIRE(env->b[fd]->read_m);
MUTEX_ACQUIRE(env->b[fd]->write_m);
- //todo -- if this fails fd is lost forever (TS, or cope?)
+ //if this fails fd is lost forever -- TS
list_enqueue((void*) b->fd, env->free_q);
+ err = env->b[fd]->err;
env->b[fd] = NULL;
MUTEX_RELEASE(env->list_m);
bsocket_free(b);
- //todo out = last error
+ if (err != 110014) {
+ errno = err;
+ out = -1;
+ }
+
fail:
return out;
@@ -294,6 +286,9 @@
b->bound = FALSE;
b->listening = FALSE;
+ b->reading = FALSE;
+ b->writing = FALSE;
+
b->read_m = CreateMutex(NULL,FALSE,NULL);
CHECK_(b->read_m != NULL);
@@ -323,8 +318,6 @@
CHECK(b->except_wl != NULL, 0);
//do not open except list
- b->write_buf = NULL;
-
b->in_q = list_new();
CHECK(b->in_q != NULL,0);
@@ -342,11 +335,10 @@
ASSERT(FALSE);
}
-//todo -- make sure type is SOCK_STREAM
-//todo -- review
+//note: argument type is ignored, I'm not sure exactly what is supposed to happen
+//in a UDP socketpair, but lets assume it behaves similarly to a SOCK_STREAM
int socketpair_win32(int domain, int type, int protocol, int fd[2], struct socket_env *env){
-
struct bsocket *b[2];
struct bsocket **l;
@@ -478,10 +470,10 @@
ASSERT( sizeof(int) == sizeof(DWORD));
if (env == NULL) {
-
env = (struct socket_env *)
malloc(sizeof(struct socket_env));
__GLOBAL_BSOCKET_ENV_ = env;
+
}
CHECK(env != NULL,ENOMEM);
@@ -495,9 +487,7 @@
for (i=0; i<MAX_BSOCKETS; i++) {
env->b[i] = NULL;
-
CHECK(list_enqueue((void*) i,env->free_q) != NULL,0);
-
}
env->post_e = WSACreateEvent();
@@ -541,33 +531,26 @@
out = 0;
was_null = FALSE;
- //todo -- i can smell a sync issue here
if (env == NULL) {
env = __GLOBAL_BSOCKET_ENV_;
was_null = TRUE;
-
}
for (i=0; i<MAX_BSOCKETS; i++) {
-
close_win32(i,env);
}
- LA
MUTEX_ACQUIRE(env->list_m);
ASSERT(SetEvent(env->shutdown_e));
- //todo replace with post_shutdown
- ASSERT(event_post(NULL,EV_SHUTDOWN,NULL,NULL,NULL,FALSE,env) == 0);
+ post_shutdown(env);
r = WaitForSingleObject (env->event_t,1000);
ASSERT(r == WAIT_OBJECT_0);
//make sure we have cleaned up properly
- //todo - get rid of this when we are confident it isn't a problem
for (i=0; i<MAX_BSOCKETS; i++) {
-
ASSERT(env->b[i] == NULL);
}
@@ -586,6 +569,7 @@
if (was_null) {
__GLOBAL_BSOCKET_ENV_ = NULL;
+
}
goto fail;
Modified: bsockets/trunk/socket.h
===================================================================
--- bsockets/trunk/socket.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/socket.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -10,8 +10,6 @@
#include "sync.h"
#include "wait.h"
-//todo move this checking stuff into another header
-
#define AS_READ (1)
#define AS_WRITE (2)
#define AS_ERR (4)
@@ -47,26 +45,24 @@
int closed;
int err;
- int eof;
int excepted;
int fd;
/*writing business*/
- WSAOVERLAPPED w_wo;
- void *write_buf;
- void *write_pointer;
- int write_len;
-
int bound;
int listening;
- /*reading business*/
- WSAOVERLAPPED r_wo;
+ int writing;
+
+ /*writing business*/
struct _list *in_q;
+ int reading;
+
+
//-1 if not in a socketpair, >0 otherwise*/
int partner;
Modified: bsockets/trunk/sync.c
===================================================================
--- bsockets/trunk/sync.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/sync.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -50,22 +50,6 @@
release(-1,AE_LIST);
}
-int wait_lock(struct socket_env *env) {
-
- //todo is there any acceptable reason for this to fail?
- ASSERT(atomic(-1,AE_WAIT) == 0);
-
- return 0;
-
-}
-
-void wait_lock_release(struct socket_env *env) {
-
- release(-1,AE_WAIT);
-
-}
-
-//todo -- FOOL! don't own list while waiting on socket mutexes (change!!)
struct bsocket *bsocket_get(int fd, int access, struct socket_env *env) {
struct bsocket **l;
@@ -82,18 +66,14 @@
}
out = NULL;
- LA
l = bsocket_list_get(env);
if (l != NULL) {
- //todo -- if l[fd] == NULL?
if (!atomic(fd,access)) {
out = l[fd];
}
-
- LR
bsocket_list_release(env);
}
@@ -211,9 +191,7 @@
while (mutex_count > 1) {
- SetLastError(0);
- //todo -- get rid of this
r = WaitForMultipleObjects(mutex_count,desired_objects,FALSE,INFINITE);
switch(r) {
@@ -241,20 +219,16 @@
default:
M_DELETE(r - WAIT_OBJECT_0);
-
break;
}
}
if (out != -1) {
-
ASSERT(mutex_count == 1);
ASSERT(desired_objects[0] == close_event);
}
-
return out;
-
}
void release_win32(int fd, int type, struct socket_env *env) {
@@ -265,26 +239,16 @@
//on the first iteration of shutdown, it is closing the socket and failing
//because it thinks it doesnt exist because it isn't in the list
-
r = lookup_mutexes(fd,type,env,desired_events);
//dont use this with type as zero
ASSERT( r > 1);
- //printf("start ",r);
//rember events[0] is close event, we dont want to touch that yet
while (r > 1) {
- /*todo assert around this*/
- // printf("%d ",desired_events[r-1]);
- // fflush(stdout);
ASSERT(ReleaseMutex(desired_events[r-1]));
r--;
}
-
- //printf("\n");
- //fflush(stdout);
-
-
}
Modified: bsockets/trunk/test.c
===================================================================
--- bsockets/trunk/test.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/test.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -17,10 +17,8 @@
#include "io.h"
#include "accept.h"
-#define TEST_MSG "hello!"
+static int res, result2;
-int res;
-
//test ideas
/*
1. multiple non-blocking connect()s on same socket (should fail with EAGAIN?)
@@ -29,6 +27,8 @@
added it?
4. assert event_q is empty when exiting event manager
5. test select timeout
+6. close with pending write operation, make sure msg gets deallocated
+7. test for memory leaks
*/
@@ -135,45 +135,43 @@
int z;
int len;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd, 0);
//you better not be running a local service on port 81
bfcntl(fd,F_SETFL,O_NONBLOCK);
r = bconnect(fd,(struct sockaddr*)&localhost,sizeof(localhost));
- TEST(r == -1);
+ TEST(r, -1);
- TEST(errno == EINPROGRESS);
+ TEST(errno, EINPROGRESS);
BFD_ZERO(&fds);
- TEST(fds.count == 0);
+ TEST(fds.count,0);
BFD_SET(fd,&fds);
- TEST(fds.count == 1);
+ TEST(fds.count,1);
r = bselect(0,NULL,&fds,NULL,NULL);
- TEST(r == 1);
+ TEST(r,1);
- TEST(BFD_ISSET(fd,&fds));
+ TEST(BFD_ISSET(fd,&fds),TRUE);
len = sizeof(z);
//printf("%d\n",1);
//fflush(stdout);
- TEST(bgetsockopt(fd,SOL_SOCKET,SO_ERROR,&z,&len) == 0);
+ TEST(bgetsockopt(fd,SOL_SOCKET,SO_ERROR,&z,&len), 0);
- TEST(z == ECONNREFUSED);
+ TEST(z,ECONNREFUSED);
+ TEST(bsocket_shutdown(NULL),0);
-
- TEST(bsocket_shutdown(NULL) == 0);
-
return 0;
}
@@ -183,20 +181,20 @@
int fd;
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL),0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd,0);
errno = 0;
r = bconnect(fd, (struct sockaddr*) &localhost,sizeof(localhost));
- TEST(r == -1);
+ TEST(r, -1);
- TEST(errno == ECONNREFUSED);
+ TEST(errno, ECONNREFUSED);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
}
@@ -209,8 +207,8 @@
r = bconnect(_g_fd_,(struct sockaddr*)name,sizeof(struct sockaddr_in));
- SILENT_TEST(r == -1);
- SILENT_TEST(errno == ECLOSED);
+ SILENT_TEST(r,-1);
+ SILENT_TEST(errno,ECLOSED);
return 0;
@@ -224,11 +222,11 @@
int fd;
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd, 0);
in.sin_family = AF_INET;
in.sin_addr.s_addr = inet_addr(BAD_ADDR);
@@ -240,14 +238,14 @@
Sleep(5);
- TEST(bclose(fd) == 0);
+ TEST(bclose(fd), 0);
- TEST(WaitForSingleObject(h,100) == 0);
- TEST(GetExitCodeThread(h,(DWORD*) &r));
+ TEST(WaitForSingleObject(h,100), 0);
+ TEST(GetExitCodeThread(h,(DWORD*) &r),TRUE);
- TEST(r == 0);
+ TEST(r,0);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -261,11 +259,11 @@
int fd;
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL),0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd,0);
in.sin_family = AF_INET;
in.sin_addr.s_addr = inet_addr(BAD_ADDR);
@@ -277,40 +275,39 @@
r = bconnect(fd,(struct sockaddr*) &in, sizeof(in));
- TEST(r == -1);
- TEST(errno == EINPROGRESS);
+ TEST(r, -1);
+ TEST(errno, EINPROGRESS);
r = bconnect(fd,(struct sockaddr*) &in, sizeof(in));
- TEST(r == -1);
- TEST(errno == EALREADY);
+ TEST(r, -1);
+ TEST(errno, EALREADY);
bfcntl(fd,F_SETFL,O_BLOCK);
r = bconnect(fd,(struct sockaddr*) &in, sizeof(in));
- TEST(r == -1);
- TEST(errno == EALREADY);
+ TEST(r, -1);
+ TEST(errno, EALREADY);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd >= 0);
+ TEST(fd >= 0,TRUE);
google.sin_port = htons(80);
r = bconnect(fd,(struct sockaddr*) &google, sizeof(google));
- TEST(r == 0);
+ TEST(r,0);
r = bconnect(fd,(struct sockaddr*) &google, sizeof(google));
- TEST(r == -1);
- TEST(errno == EISCONN);
+ TEST(r,-1);
+ TEST(errno,EISCONN);
+ TEST(bsocket_shutdown(NULL),0);
- TEST(bsocket_shutdown(NULL) == 0);
-
return 0;
}
@@ -321,12 +318,12 @@
struct sockaddr_in in;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL),0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd >= 0);
- TEST(fd < MAX_BSOCKETS);
+ TEST(fd >= 0,TRUE);
+ TEST(fd < MAX_BSOCKETS,TRUE);
in.sin_family = AF_INET;
in.sin_addr.s_addr = inet_addr(BAD_ADDR);
@@ -335,11 +332,11 @@
errno = 0;
r = bconnect(fd,(struct sockaddr*) &in,sizeof(in));
- TEST( r == -1);
- TEST( errno == ETIMEDOUT);
+ TEST( r, -1);
+ TEST( errno, ETIMEDOUT);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -352,14 +349,14 @@
int f;
int i;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
for (i=0; i<MAX_BSOCKETS; i++) {
fd[i] = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd[i] >= 0);
- TEST(fd[i] < MAX_BSOCKETS);
+ TEST(fd[i] >= 0,TRUE);
+ TEST(fd[i] < MAX_BSOCKETS,TRUE);
}
@@ -367,31 +364,29 @@
f = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(f == -1);
- TEST(errno == EMFILE);
+ TEST(f,-1);
+ TEST(errno, EMFILE);
errno = 0;
- TEST(bclose(fd[0]) == 0);
- TEST(errno == 0);
+ TEST(bclose(fd[0]),0);
+ TEST(errno,0);
f = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(f != -1);
- TEST(errno == 0);
+ TEST(f != -1, TRUE);
+ TEST(errno, 0);
for (i=1; i<MAX_BSOCKETS; i++) {
-
- TEST(bclose(fd[i]) == 0);
- TEST(errno == 0);
-
+ TEST(bclose(fd[i]), 0);
+ TEST(errno, 0);
}
f = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(f != -1);
+ TEST(f != -1,TRUE);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -405,19 +400,17 @@
while (tests--) {
- TEST(bsocket_init(NULL) == 0 );
+ TEST(bsocket_init(NULL), 0 );
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd >= 0);
- TEST(fd < MAX_BSOCKETS);
+ TEST(fd >= 0,TRUE);
+ TEST(fd < MAX_BSOCKETS,TRUE);
- TEST(bclose(fd) == 0);
+ TEST(bclose(fd),0);
- TEST(bsocket_shutdown(NULL) == 0 );
-
-
+ TEST(bsocket_shutdown(NULL),0);
}
return 0;
@@ -430,9 +423,9 @@
list = list_new();
- TEST(list != NULL);
- TEST(list->head == NULL);
- TEST(list->tail == NULL);
+ TEST(list != NULL,TRUE);
+ TEST(list->head,NULL);
+ TEST(list->tail, NULL);
list_free(list);
@@ -449,21 +442,21 @@
list = list_new();
- TEST(list != NULL);
+ TEST(list != NULL,TRUE);
- TEST(list_enqueue((void*)d,list) != NULL);
+ TEST(list_enqueue((void*)d,list) != NULL,TRUE);
- TEST(list->head != NULL);
- TEST(list->tail != NULL);
- TEST(list->head == list->tail);
+ TEST(list->head != NULL,TRUE);
+ TEST(list->tail != NULL,TRUE);
+ TEST(list->head, list->tail);
r = list_dequeue(&res2,list);
- TEST(r == 0);
- TEST(res2 == d);
+ TEST(r, 0);
+ TEST(res2, d);
- TEST(list->head == NULL);
- TEST(list->tail == NULL);
+ TEST(list->head,NULL);
+ TEST(list->tail, NULL);
list_free(list);
@@ -491,21 +484,21 @@
r = rand();
v[i] = r;
- TEST(list_enqueue((void*) r,list) != NULL);
+ TEST(list_enqueue((void*) r,list) != NULL,TRUE);
}
for (i=0; i<num; i++) {
n = list_dequeue(&r,list);
- TEST(n == 0);
- TEST(r == v[i]);
+ TEST(n, 0);
+ TEST(r,v[i]);
}
n = list_dequeue(&r,list);
- TEST(n != 0);
+ TEST(n != 0,TRUE);
- TEST(list->head == NULL);
- TEST(list->tail == NULL);
+ TEST(list->head, NULL);
+ TEST(list->tail, NULL);
free(v);
list_free(list);
@@ -524,11 +517,6 @@
claimed++;
}
-
-// if (WaitForSingleObject(env->free_m,0) == WAIT_OBJECT_0) {
-// claimed++;
-// }
-
if (WaitForSingleObject(env->post_m,0) == WAIT_OBJECT_0) {
claimed++;
}
@@ -545,7 +533,6 @@
}
-//todo - should do test for each mutex, but this is good enough for now
int test_envmutex() {
HANDLE h;
@@ -555,7 +542,7 @@
int r;
int tests = 1000;
- TEST((bsocket_init(NULL) == 0));
+ TEST(bsocket_init(NULL), 0);
env = __GLOBAL_BSOCKET_ENV_;
@@ -566,14 +553,14 @@
/*check if any of the mutexes can be claimed (very bad if)*/
h = new_thread(test_envmutex_helper,env);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
r = WaitForSingleObject(h,1000);
- TEST(r == WAIT_OBJECT_0);
- TEST(GetExitCodeThread(h,(DWORD*)&r));
+ TEST(r, WAIT_OBJECT_0);
+ TEST(GetExitCodeThread(h,(DWORD*)&r),TRUE);
- TEST(r == 0);
+ TEST(r,0);
CloseHandle(h);
release(-1,AE_GLOBAL);
@@ -581,21 +568,21 @@
/*now make sure they were released properly*/
h = new_thread(test_envmutex_helper,env);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
r = WaitForSingleObject(h,1000);
- TEST(r == WAIT_OBJECT_0);
+ TEST(r, WAIT_OBJECT_0);
- TEST(GetExitCodeThread(h,(DWORD*)&r));
+ TEST(GetExitCodeThread(h,(DWORD*)&r),TRUE);
- TEST(r == 3);
+ TEST(r,3);
CloseHandle(h);
}
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -608,8 +595,8 @@
l = bsocket_list_get(env);
- SILENT_TEST(l == NULL);
- SILENT_TEST(errno == ECLOSED);
+ SILENT_TEST(l,NULL);
+ SILENT_TEST(errno, ECLOSED);
return 0;
}
@@ -624,21 +611,22 @@
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
env = __GLOBAL_BSOCKET_ENV_;
l = bsocket_list_get(env);
- TEST(l != NULL);
+ //come up with TESTf
+ TEST(l != NULL,TRUE);
h = new_thread(test_envmutexclose_helper,env);
Sleep(10);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
- TEST(WaitForSingleObject(h,100) == WAIT_OBJECT_0);
- TEST(GetExitCodeThread(h,(DWORD*) &r));
+ TEST(WaitForSingleObject(h,100) ,WAIT_OBJECT_0);
+ TEST(GetExitCodeThread(h,(DWORD*) &r),TRUE);
- TEST(r == 0);
+ TEST(r,0);
CloseHandle(h);
@@ -669,7 +657,6 @@
return count;
}
-//todo - a test for each mutex individually
int test_socketmutex() {
HANDLE h;
@@ -679,55 +666,52 @@
int r;
int fd;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
env = __GLOBAL_BSOCKET_ENV_;
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd,0);
errno = 0;
r = atomic(fd,AS_GLOBAL);
- TEST(r == 0);
+ TEST(r,0);
h = new_thread(test_socketmutex_helper,env->b[fd]);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
r = WaitForSingleObject(h,2000);
- TEST(r == WAIT_OBJECT_0);
+ TEST(r, WAIT_OBJECT_0);
GetExitCodeThread(h, (DWORD*) &r);
- TEST(r == 0);
+ TEST(r,0);
CloseHandle(h);
release(fd,AS_GLOBAL);
h = new_thread(test_socketmutex_helper,env->b[fd]);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
r = WaitForSingleObject(h,2000);
- TEST(r == WAIT_OBJECT_0);
+ TEST(r, WAIT_OBJECT_0);
GetExitCodeThread(h, (DWORD*) &r);
- TEST(atomic(fd,AS_GLOBAL) == 0);
+ TEST(atomic(fd,AS_GLOBAL), 0);
release(fd,AS_GLOBAL);
- TEST(r == 2);
+ TEST(r, 2);
CloseHandle(h);
- TEST(bclose(fd) == 0);
+ TEST(bclose(fd),0);
+ TEST(bsocket_shutdown(NULL),0);
-
- TEST(bsocket_shutdown(NULL) == 0);
-
return 0;
}
-
//attempt to grab a mutex for a socket that does not exist
int test_badsocket() {
@@ -735,17 +719,17 @@
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
env = __GLOBAL_BSOCKET_ENV_;
errno = 0;
r = atomic(0,AS_READ);
- TEST(r == -1);
- TEST(errno == EBADF);
+ TEST(r, -1);
+ TEST(errno, EBADF);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL),0);
return 0;
@@ -762,13 +746,11 @@
errno = 0;
r = atomic(fd,AS_GLOBAL);
- SILENT_TEST(r == -1);
- SILENT_TEST(errno == ECLOSED);
+ SILENT_TEST(r, -1);
+ SILENT_TEST(errno, ECLOSED);
-
return 0;
-
}
int test_socketmutexclose() {
@@ -779,11 +761,11 @@
int fd;
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd,0);
env = __GLOBAL_BSOCKET_ENV_;
@@ -793,19 +775,19 @@
Sleep(30);
- TEST(bclose(fd) == 0);
+ TEST(bclose(fd), 0);
r = WaitForSingleObject(h,10000);
- TEST(r == WAIT_OBJECT_0);
+ TEST(r, WAIT_OBJECT_0);
GetExitCodeThread(h,(DWORD*)&r);
- TEST(r == 0);
+ TEST(r,0);
CloseHandle(h);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL),0);
return 0;
@@ -822,12 +804,12 @@
rounds = 1000;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
env = __GLOBAL_BSOCKET_ENV_;
- TEST(fd == 0);
+ TEST(fd, 0);
while(rounds--) {
@@ -835,15 +817,15 @@
bsocket_release(fd,AS_GLOBAL,env);
- TEST(bsocket_list_get(env) != NULL);
+ TEST(bsocket_list_get(env) != NULL,TRUE);
bsocket_list_release(env);
}
- TEST(bclose(fd) == 0);
+ TEST(bclose(fd), 0);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
}
@@ -864,7 +846,7 @@
while (tests--) {
list = list_new();
- TEST(list != NULL);
+ TEST(list != NULL,TRUE);
remove_this_guy = ran(elements);
@@ -873,33 +855,29 @@
for (i=0; i<elements; i++) {
if (i == remove_this_guy) {
n = list_enqueue((void*) i,list);
- TEST(n != NULL);
+ TEST(n != NULL,TRUE);
} else {
- TEST(list_enqueue((void*) i,list) != NULL);
+ TEST(list_enqueue((void*) i,list) != NULL,TRUE);
}
}
- TEST(n != NULL);
+ TEST(n != NULL,TRUE);
list_node_detach(n);
for (i=0; i<elements; i++) {
if (i != remove_this_guy) {
-
r = list_dequeue(&num,list);
- TEST(r == 0);
-
- TEST(num == i);
-
+ TEST(r,0);
+ TEST(num,i);
}
-
}
r = list_dequeue(&num,list);
- TEST(r != 0);
+ TEST(r != 0,TRUE);
list_free(list);
@@ -930,7 +908,7 @@
wl = wl_new();
- TEST(wl != NULL);
+ TEST(wl != NULL,TRUE);
h = new_thread(test_waitclose_helper,wl);
@@ -938,11 +916,11 @@
wl_free(wl);
- TEST(WaitForSingleObject(h,INFINITE) == WAIT_OBJECT_0);
+ TEST(WaitForSingleObject(h,INFINITE), WAIT_OBJECT_0);
- TEST(GetExitCodeThread(h,(DWORD*) &r));
+ TEST(GetExitCodeThread(h,(DWORD*) &r),TRUE);
- TEST(r == ECLOSED);
+ TEST(r, ECLOSED);
return 0;
}
@@ -953,7 +931,7 @@
r = wait_one(wl);
- SILENT_TEST(r == 0);
+ SILENT_TEST(r, 0);
return 0;
}
@@ -972,24 +950,24 @@
while (tests--) {
- TEST(wl != NULL);
+ TEST(wl != NULL,TRUE);
h = new_thread(test_waitfail_helper, wl);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
Sleep(10);
r = wait_one(wl);
- TEST(r == -1);
- TEST(errno == EAGAIN);
+ TEST(r, -1);
+ TEST(errno, EAGAIN);
wl_activate(wl,0);
- TEST(WaitForSingleObject(h,INFINITE) == WAIT_OBJECT_0);
- TEST(GetExitCodeThread(h,(DWORD*) &r));
- TEST(r == 0);
+ TEST(WaitForSingleObject(h,INFINITE), WAIT_OBJECT_0);
+ TEST(GetExitCodeThread(h,(DWORD*) &r),TRUE);
+ TEST(r, 0);
wl_deactivate(wl);
@@ -1036,7 +1014,7 @@
for (i=0; i<items; i++) {
wl[i] = wl_new();
- TEST(wl[i] != NULL);
+ TEST(wl[i] != NULL,TRUE);
}
while (tests--) {
@@ -1045,7 +1023,7 @@
while ( (m = ran(items)) == n);
- TEST(m != n);
+ TEST(m != n,TRUE);
desired[0] = wl[n];
desired[1] = wl[m];
@@ -1054,28 +1032,19 @@
h = new_thread(test_waitobject_helper,desired[wake_this_guy]);
- TEST(WaitForSingleObject(h,INFINITE) == WAIT_OBJECT_0);
+ TEST(WaitForSingleObject(h,INFINITE), WAIT_OBJECT_0);
CloseHandle(h);
r = wait_many(desired,2);
-
-
- // printf("%d %d\n",r, wake_this_guy);
-
- TEST(r == wake_this_guy);
-
-
+ TEST(r, wake_this_guy);
wl_deactivate(desired[wake_this_guy]);
-
-
}
for (i=0; i<items; i++) {
wl_free(wl[i]);
}
-
return 0;
}
@@ -1107,22 +1076,16 @@
h = new_thread(test_simplewaitobject_helper,l);
- TEST(WaitForSingleObject(h,INFINITE) == WAIT_OBJECT_0);
+ TEST(WaitForSingleObject(h,INFINITE), WAIT_OBJECT_0);
CloseHandle(h);
while(tests--) {
-
- TEST(l != NULL);
-
+ TEST(l != NULL,TRUE);
r = wait_one(l);
-
- TEST(r == 0);
-
+ TEST(r,0);
}
- /*after this, make sure it blocks*/
-
wl_free(l);
return 0;
@@ -1149,12 +1112,11 @@
h = new_thread(test_errnosafe_helper, NULL);
- TEST(h != NULL);
- TEST(WaitForSingleObject(h,INFINITE) == WAIT_OBJECT_0);
-
+ TEST(h != NULL,TRUE);
+ TEST(WaitForSingleObject(h,INFINITE), WAIT_OBJECT_0);
CloseHandle(h);
- TEST(errno == 102);
+ TEST(errno,102);
return 0;
@@ -1196,32 +1158,32 @@
s = socket(AF_INET,SOCK_STREAM,0);
- TEST(s != INVALID_SOCKET);
+ TEST(s != INVALID_SOCKET,TRUE);
in.sin_family = AF_INET;
in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
in.sin_port = htons(0);
r = bind(s,(struct sockaddr*) &in,sizeof(struct sockaddr_in));
- TEST(r != SOCKET_ERROR);
+ TEST(r != SOCKET_ERROR,TRUE);
i = sizeof(struct sockaddr_in);
r = getsockname(s,(struct sockaddr*) &name,&i);
- TEST(r == 0);
+ TEST(r,0);
r = listen(s,10);
- TEST(r != SOCKET_ERROR);
+ TEST(r != SOCKET_ERROR,TRUE);
h = new_thread(test_maxoverlapped_server,(void*) s);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
for (i=0; i<MAX_BSOCKETS; i++) {
c[i] = WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
- TEST(c[i] != INVALID_SOCKET);
+ TEST(c[i] != INVALID_SOCKET,TRUE);
r = connect(c[i],(struct sockaddr*) &name,sizeof(struct sockaddr_in));
- TEST(r != SOCKET_ERROR);
+ TEST(r != SOCKET_ERROR,TRUE);
flags = 0;
@@ -1232,12 +1194,12 @@
r = WSARecv(c[i],&buf[i],1,(DWORD*) &recv, (DWORD*)&flags,&wo[i],test_maxoverlapped_callback);
- TEST(r == SOCKET_ERROR);
- TEST(WSAGetLastError() == WSA_IO_PENDING);
+ TEST(r, SOCKET_ERROR);
+ TEST(WSAGetLastError(), WSA_IO_PENDING);
}
for (i=0; i<MAX_BSOCKETS; i++) {
- TEST(closesocket(c[i]) == 0);
+ TEST(closesocket(c[i]), 0);
}
closesocket(s);
@@ -1250,109 +1212,108 @@
BFD_ZERO(&fd);
- TEST(fd.count == 0);
+ TEST(fd.count, 0);
// TEST(fd.hash[0] == -1);
BFD_SET(0,&fd);
- TEST(fd.count == 1);
- TEST(fd.list[0] == 0);
+ TEST(fd.count, 1);
+ TEST(fd.list[0] , 0);
- TEST(fd.hash[0] == 0);
+ TEST(fd.hash[0] , 0);
BFD_SET(1000,&fd);
- TEST(fd.count == 2);
- TEST(fd.list[0] == 0);
- TEST(fd.list[1] == 1000);
+ TEST(fd.count , 2);
+ TEST(fd.list[0] , 0);
+ TEST(fd.list[1] , 1000);
- TEST(fd.hash[0] == 0);
- TEST(fd.hash[1000] == 1);
+ TEST(fd.hash[0] , 0);
+ TEST(fd.hash[1000] , 1);
BFD_SET(2000,&fd);
- TEST(BFD_ISSET(0,&fd) );
- TEST(BFD_ISSET(1000,&fd));
- TEST(BFD_ISSET(2000,&fd));
+ TEST(BFD_ISSET(0,&fd),TRUE);
+ TEST(BFD_ISSET(1000,&fd),TRUE);
+ TEST(BFD_ISSET(2000,&fd),TRUE);
- TEST(fd.count == 3);
- TEST(fd.list[0] == 0);
- TEST(fd.list[1] == 1000);
- TEST(fd.list[2] == 2000);
+ TEST(fd.count, 3);
+ TEST(fd.list[0], 0);
+ TEST(fd.list[1] , 1000);
+ TEST(fd.list[2] , 2000);
- TEST(fd.hash[0] == 0);
- TEST(fd.hash[1000] == 1);
- TEST(fd.hash[2000] == 2);
+ TEST(fd.hash[0] , 0);
+ TEST(fd.hash[1000] , 1);
+ TEST(fd.hash[2000] , 2);
BFD_CLR(0,&fd);
- TEST(!BFD_ISSET(0,&fd));
- TEST(BFD_ISSET(1000,&fd));
- TEST(BFD_ISSET(2000,&fd));
+ TEST(BFD_ISSET(0,&fd),FALSE);
+ TEST(BFD_ISSET(1000,&fd),TRUE);
+ TEST(BFD_ISSET(2000,&fd),TRUE);
- TEST(fd.count == 2);
- TEST(fd.list[0] == 1000);
- TEST(fd.list[1] == 2000);
+ TEST(fd.count, 2);
+ TEST(fd.list[0] , 1000);
+ TEST(fd.list[1] , 2000);
//TEST(fd.hash[0] == -1);
- TEST(fd.hash[1000] == 0);
- TEST(fd.hash[2000] == 1);
+ TEST(fd.hash[1000] , 0);
+ TEST(fd.hash[2000] , 1);
BFD_SET(0,&fd);
- TEST(fd.count == 3);
- TEST(fd.list[0] == 1000);
- TEST(fd.list[1] == 2000);
- TEST(fd.list[2] == 0);
+ TEST(fd.count , 3);
+ TEST(fd.list[0] , 1000);
+ TEST(fd.list[1] , 2000);
+ TEST(fd.list[2] , 0);
- TEST(fd.hash[0] == 2);
- TEST(fd.hash[1000] == 0);
- TEST(fd.hash[2000] == 1);
+ TEST(fd.hash[0] , 2);
+ TEST(fd.hash[1000] , 0);
+ TEST(fd.hash[2000] , 1);
BFD_CLR(2000,&fd);
- TEST(BFD_ISSET(0,&fd));
- TEST(BFD_ISSET(1000,&fd));
- TEST(!BFD_ISSET(2000,&fd));
+ TEST(BFD_ISSET(0,&fd),TRUE);
+ TEST(BFD_ISSET(1000,&fd),TRUE);
+ TEST(BFD_ISSET(2000,&fd),FALSE);
- TEST(fd.count == 2);
- TEST(fd.list[0] == 1000);
- TEST(fd.list[1] == 0);
+ TEST(fd.count , 2);
+ TEST(fd.list[0] , 1000);
+ TEST(fd.list[1] , 0);
- TEST(fd.hash[0] == 1);
- TEST(fd.hash[1000] == 0);
+ TEST(fd.hash[0] , 1);
+ TEST(fd.hash[1000] , 0);
// TEST(fd.hash[2000] == -1);
BFD_SET(2000,&fd);
- TEST(fd.count == 3);
- TEST(fd.list[0] == 1000);
- TEST(fd.list[1] == 0);
- TEST(fd.list[2] == 2000);
+ TEST(fd.count , 3);
+ TEST(fd.list[0] , 1000);
+ TEST(fd.list[1] , 0);
+ TEST(fd.list[2] , 2000);
- TEST(fd.hash[0] == 1);
- TEST(fd.hash[1000] == 0);
- TEST(fd.hash[2000] == 2);
+ TEST(fd.hash[0] , 1);
+ TEST(fd.hash[1000] , 0);
+ TEST(fd.hash[2000] , 2);
BFD_CLR(2000,&fd);
- TEST(BFD_ISSET(0,&fd));
- TEST(BFD_ISSET(1000,&fd));
- TEST(!BFD_ISSET(2000,&fd));
+ TEST(BFD_ISSET(0,&fd),TRUE);
+ TEST(BFD_ISSET(1000,&fd),TRUE);
+ TEST(BFD_ISSET(2000,&fd),FALSE);
- TEST(fd.count == 2);
- TEST(fd.list[0] == 1000);
- TEST(fd.list[1] == 0);
+ TEST(fd.count , 2);
+ TEST(fd.list[0] , 1000);
+ TEST(fd.list[1] , 0);
- TEST(fd.hash[0] == 1);
- TEST(fd.hash[1000] == 0);
+ TEST(fd.hash[0] , 1);
+ TEST(fd.hash[1000] , 0);
//TEST(fd.hash[2000] == -1);
BFD_ZERO(&fd);
- TEST(fd.count == 0);
- TEST(!BFD_ISSET(0,&fd));
- TEST(!BFD_ISSET(1000,&fd));
- TEST(!BFD_ISSET(2000,&fd));
+ TEST(fd.count, 0);
+ TEST(BFD_ISSET(0,&fd),FALSE);
+ TEST(BFD_ISSET(1000,&fd),FALSE);
+ TEST(BFD_ISSET(2000,&fd),FALSE);
-
return 0;
}
@@ -1361,23 +1322,23 @@
int fd;
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd,0);
google.sin_port = htons(80);
r = bconnect(fd,(struct sockaddr*) &google,sizeof(google));
- TEST(r == 0);
+ TEST(r,0);
r = bclose(fd);
- TEST(r == 0);
+ TEST(r,0);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -1389,19 +1350,19 @@
int err;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL),0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd,0);
socket_set_err(fd,666,__GLOBAL_BSOCKET_ENV_);
socket_last_err(fd,&err,__GLOBAL_BSOCKET_ENV_);
- TEST(err == 666);
+ TEST(err, 666);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -1409,7 +1370,7 @@
int test_multi_connect() {
- const int clients = 300;
+ const int clients = 600;
bfd_set fds_out, fds_in;
struct sockaddr_in in;
@@ -1428,11 +1389,10 @@
int good;
s = loopback_server(&port);
+ TEST(s != 0, TRUE);
- TEST(s != 0);
+ TEST(bsocket_init(NULL), 0);
- TEST(bsocket_init(NULL) == 0);
-
in.sin_family = AF_INET;
in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
in.sin_port = port;
@@ -1443,19 +1403,19 @@
fd[i] = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd[i] >= 0);
+ TEST(fd[i] >= 0,TRUE);
- TEST(bfcntl(fd[i],F_SETFL,O_NONBLOCK) == 0);
+ TEST(bfcntl(fd[i],F_SETFL,O_NONBLOCK),0);
- TEST(fd[i] != -1);
+ TEST(fd[i] != -1,TRUE);
BFD_SET(fd[i],&fds_in);
}
for (i=0; i<clients; i++) {
r = bconnect(fd[i],(struct sockaddr*) &in, sizeof(in));
- TEST(r == -1);
- TEST(errno == EINPROGRESS);
+ TEST(r, -1);
+ TEST(errno, EINPROGRESS);
}
BFD_COPY(&fds_out,&fds_in);
@@ -1470,7 +1430,7 @@
z = bgetsockopt( fds_out.list[0],SOL_SOCKET,SO_ERROR,&err,&len);
- TEST(z == 0);
+ TEST(z, 0);
if (err == 0) {
good = TRUE;
@@ -1484,30 +1444,24 @@
}
//make sure at least one connection succeeded
- TEST(good);
+ TEST(good,TRUE);
+ TEST(counter, clients);
- TEST(counter == clients);
+ TEST(r, -1);
- TEST(r == -1);
+ TEST(fds_out.count, 0);
- TEST(fds_out.count == 0);
-
for (i=0; i<clients; i++) {
- TEST(bclose(fd[i]) == 0);
+ TEST(bclose(fd[i]), 0);
}
- //todo -- why does this fail
- //TEST(closesocket(s) == 0);
- closesocket(s);
+ TEST(closesocket(s), 0);
+ TEST(bsocket_shutdown(NULL), 0);
-
- TEST(bsocket_shutdown(NULL) == 0);
-
return 0;
}
-
int test_simple_write_helper() {
SOCKET s;
@@ -1517,21 +1471,19 @@
s = socket(AF_INET,SOCK_STREAM,0);
- SILENT_TEST(s != INVALID_SOCKET);
+ SILENT_TEST(s != INVALID_SOCKET,TRUE);
+ SILENT_TEST(bind(s,(struct sockaddr*) &localhost,sizeof(localhost)), 0);
+ SILENT_TEST(listen(s,1), 0);
- SILENT_TEST(bind(s,(struct sockaddr*) &localhost,sizeof(localhost)) == 0);
-
- SILENT_TEST(listen(s,1) == 0);
-
c = accept(s,NULL,0);
- SILENT_TEST(c != INVALID_SOCKET);
+ SILENT_TEST(c != INVALID_SOCKET,TRUE);
r = -1;
- SILENT_TEST(recv(c,(char*) &r,sizeof(r),0) == sizeof(r));
+ SILENT_TEST(recv(c,(char*) &r,sizeof(r),0), sizeof(r));
- SILENT_TEST(r == 666);
+ SILENT_TEST(r, 666);
closesocket(s);
closesocket(c);
@@ -1544,38 +1496,39 @@
HANDLE h;
+
int msg;
int fd;
int r;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
/*start server*/
-
h = new_thread(test_simple_write_helper,NULL);
Sleep(100);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd == 0);
+ TEST(fd, 0);
r = bconnect(fd, (struct sockaddr *) &localhost,sizeof(localhost));
- TEST(r == 0);
+ TEST(r, 0);
msg = 666;
r = bsend(fd,&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
- TEST(WaitForSingleObject(h,1000) == WAIT_OBJECT_0);
+ TEST(r, sizeof(msg));
- TEST(GetExitCodeThread(h,(DWORD*) &r));
+ TEST(WaitForSingleObject(h,1000), WAIT_OBJECT_0);
- TEST(r == 0);
+ TEST(GetExitCodeThread(h,(DWORD*) &r),TRUE);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(r, 0);
+ TEST(bsocket_shutdown(NULL), 0);
+
return 0;
}
@@ -1590,20 +1543,20 @@
s = socket(AF_INET,SOCK_STREAM,0);
msg = 666;
- SILENT_TEST(s != INVALID_SOCKET);
+ SILENT_TEST(s != INVALID_SOCKET,TRUE);
r = bind(s,(struct sockaddr*) &localhost,sizeof(localhost));
- SILENT_TEST(r == 0);
+ SILENT_TEST(r, 0);
r = listen(s,10);
- SILENT_TEST(r == 0);
+ SILENT_TEST(r, 0);
while (TRUE) {
c = accept(s,NULL,0);
- SILENT_TEST(c != INVALID_SOCKET);
+ SILENT_TEST(c != INVALID_SOCKET,TRUE);
r = send(c,(char*) &msg, sizeof(msg),0);
- SILENT_TEST(r == sizeof(msg));
+ SILENT_TEST(r, sizeof(msg));
closesocket(c);
@@ -1628,35 +1581,35 @@
while(mega_tests--) {
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
h = new_thread(test_simple_read_helper,NULL);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
tests = 50;
while (tests--) {
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd >= 0);
+ TEST(fd >= 0,TRUE);
r = bconnect(fd,(struct sockaddr*) &localhost,sizeof(localhost));
- TEST(r == 0);
+ TEST(r, 0);
r = brecv(fd,&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
- TEST(msg == 666);
+ TEST(r, sizeof(msg));
+ TEST(msg, 666);
r = brecv(fd,&msg,sizeof(msg),0);
- TEST(r == 0);
+ TEST(r, 0);
r = brecv(fd,&msg,sizeof(msg),0);
- TEST(r == 0);
+ TEST(r, 0);
- TEST(bclose(fd) == 0);
+ TEST(bclose(fd), 0);
}
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
CloseHandle(h);
}
@@ -1671,19 +1624,19 @@
int msg = 666;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
r = bsocketpair(AF_INET,SOCK_STREAM,0,s);
- TEST(r == 0);
+ TEST(r, 0);
r = bsend(s[0],&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
+ TEST(r, sizeof(msg));
r = brecv(s[1],&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
- TEST(msg == 666);
+ TEST(r, sizeof(msg));
+ TEST(msg, 666);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
}
@@ -1699,7 +1652,7 @@
int msg_count = 100;
int msg;
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
r = bsocketpair(AF_INET,SOCK_STREAM,0,s);
@@ -1707,44 +1660,44 @@
for (i=0; i<msg_count; i++) {
msg = msg_base+i;
r = bsend(s[0],&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
+ TEST(r, sizeof(msg));
}
for (i=0; i<msg_count; i++) {
msg = msg_base*2+i;
r = bsend(s[1],&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
+ TEST(r, sizeof(msg));
}
socket_exception(s[0],1, __GLOBAL_BSOCKET_ENV_);
for (i=0; i<msg_count; i++) {
r = brecv(s[1],&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
- TEST(msg == (msg_base+i));
+ TEST(r, sizeof(msg));
+ TEST(msg, (msg_base+i));
}
for (i=0; i<msg_count; i++) {
r = brecv(s[0],&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
- TEST(msg == (msg_base*2)+i);
+ TEST(r, sizeof(msg));
+ TEST(msg, (msg_base*2)+i);
}
r = brecv(s[0],&msg,sizeof(msg),0);
- TEST(r == 0);
+ TEST(r, 0);
r = brecv(s[1],&msg,sizeof(msg),0);
- TEST(r == 0);
+ TEST(r, 0);
r = bsend(s[0],&msg,sizeof(msg),0);
- TEST(r == -1);
- TEST(errno == ENOTCONN);
+ TEST(r, -1);
+ TEST(errno, ENOTCONN);
r = bsend(s[1],&msg,sizeof(msg),0);
- TEST(r == -1);
- TEST(errno == ENOTCONN);
+ TEST(r, -1);
+ TEST(errno, ENOTCONN);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
@@ -1755,10 +1708,6 @@
return -1;
}
-int test_complex_select() {
- return -1;
-}
-
int test_accept_helper(int num) {
struct sockaddr_in in;
@@ -1769,23 +1718,23 @@
memcpy(&in,&localhost,sizeof(in));
int fd = bsocket(AF_INET,SOCK_STREAM,0);
- SILENT_TEST(fd >= 0);
+ SILENT_TEST(fd >= 0,TRUE);
in.sin_port = htons(82);
r = bconnect(fd,(struct sockaddr*) &in, sizeof(in));
- SILENT_TEST(r == 0);
+ SILENT_TEST(r,0);
msg = 666;
r = bsend(fd,&msg,sizeof(msg),0);
- SILENT_TEST(r == sizeof(msg));
+ SILENT_TEST(r, sizeof(msg));
r = brecv(fd,&msg,sizeof(msg),0);
- SILENT_TEST(r == sizeof(msg));
- SILENT_TEST(msg == 667);
+ SILENT_TEST(r, sizeof(msg));
+ SILENT_TEST(msg, 667);
//todo -- SO_LINGER might raise an issue here
- SILENT_TEST(bclose(fd) == 0);
+ SILENT_TEST(bclose(fd), 0);
return 0;
@@ -1804,46 +1753,55 @@
struct sockaddr_in lh;
memcpy(&lh,&localhost,sizeof(lh));
- TEST(bsocket_init(NULL) == 0);
+ TEST(bsocket_init(NULL), 0);
fd = bsocket(AF_INET,SOCK_STREAM,0);
- TEST(fd >= 0);
+ TEST(fd >= 0,TRUE);
lh.sin_port = htons(82);
r = bbind(fd,(struct sockaddr*) &lh,sizeof(lh));
- TEST(r == 0);
+ TEST(r, 0);
r = blisten(fd,10);
- TEST(r == 0);
+ TEST(r, 0);
h = new_thread(test_accept_helper,NULL);
- TEST(h != NULL);
+ TEST(h != NULL,TRUE);
c = baccept(fd,NULL,0);
- TEST(c >= 0);
+ TEST(c >= 0,TRUE);
r = brecv(c,&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
- TEST(msg == 666);
+ TEST(r, sizeof(msg));
+ TEST(msg, 666);
msg = 667;
r = bsend(c,&msg,sizeof(msg),0);
- TEST(r == sizeof(msg));
+ TEST(r, sizeof(msg));
r = WaitForSingleObject(h, INFINITE);
- TEST(r == WAIT_OBJECT_0);
+ TEST(r, WAIT_OBJECT_0);
- TEST(GetExitCodeThread(h,(DWORD*)&r));
- TEST(r == 0);
+ TEST(GetExitCodeThread(h,(DWORD*)&r),TRUE);
+ TEST(r, 0);
CloseHandle(h);
- TEST(bsocket_shutdown(NULL) == 0);
+ TEST(bsocket_shutdown(NULL), 0);
return 0;
}
+
+int test_echo_server( ) {
+
+ TEST(TRUE,FALSE);
+
+ return 0;
+
+}
+
struct test_case tc[] =
{
@@ -1863,6 +1821,7 @@
{test_overflowsocket,NULL,"Testing if bsocket() fails after creating too many sockets."},
{test_simplewaitobject,NULL,"Testing simple usage of wait objects."},
{test_waitobject,NULL,"Test more complicated usage of wait objects."},
+ //todo -- this has blocked before
{test_waitfail,NULL,"Tests if a wait object fails when it should."},
{test_waitclose,NULL,"Test if a wait object behaves properly on closure."},
{test_maxoverlapped,NULL,"Test if we can have a sufficient number of outstanding operations."},
@@ -1883,6 +1842,7 @@
{test_socketpair_exception,NULL,"Test is socketpair exception behaves properly."},
//{test_verify_npp_usage,NULL,"Check that the NPP is not being used for large sends."},
{test_accept,NULL,"Test is baccept() behaves properly."},
+ {test_echo_server,NULL,"Test if an echo server made entirely with bsockets runs nicely."},
{NULL,NULL,NULL}
};
@@ -1891,7 +1851,6 @@
struct hostent *host;
- /*todo warn user about this putting strain on system*/
int i = 0;
int pass = 0;
@@ -1914,6 +1873,8 @@
if (argc > 1) {
i = atoi(argv[1]);
+ } else {
+ i = 0;
}
while (tc[i].fun != NULL) {
@@ -1939,7 +1900,7 @@
printf("\nTesting Report: %d passed, %d failed\n",pass,fail);
- return 0;
+ return fail;
}
Modified: bsockets/trunk/test.h
===================================================================
--- bsockets/trunk/test.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/test.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -1,9 +1,14 @@
#ifndef _TEST_H_
#define _TEST_H
-#define TEST(X) if (!((res = X))) { printf("Assertion failed: \"%s\" [errno:%d, w32err:%d line:%d]\n", #X,errno,(int) GetLastError(),__LINE__); fflush(stdout); return 1; }
-#define SILENT_TEST(X) if (!(X)) { return GetLastError(); }
+#define TEST(X,Y) res = (int) (X);\
+ result2 = (int) (Y);\
+ if (res != result2) {\
+ printf("Assertion failed: \"%s != %s [%d] \", %s = %d \n\t[errno:%d, w32err:%d line:%d]\n", #X,#Y,result2,#X,res,errno,(int) GetLastError(),__LINE__); fflush(stdout); return 1; \
+ }\
+#define SILENT_TEST(X,Y) if ( ((X) != (Y)) ) { return GetLastError(); }
+
#endif
Modified: bsockets/trunk/wait.c
===================================================================
--- bsockets/trunk/wait.c 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/wait.c 2006-08-30 08:34:56 UTC (rev 8305)
@@ -5,10 +5,6 @@
#include "wait.h"
#include "misc.h"
-//todo, come up with a way to recycle monitor objects so we dont have
-//keep allocating them for each wait operation
-
-//todo -- when activated with error code, each wait should return with that error code
int monitor_link(struct _wait_list *l, struct _monitor *m) {
int out;
@@ -19,25 +15,15 @@
ASSERT(m != NULL);
if (l->active) {
-
out = 1;
-
} else {
-
if (l->waiting == NULL) {
-
CHECK(list_enqueue(l, m->where) != NULL,0);
-
l->waiting = m;
-
-
} else {
-
errno = EAGAIN;
out = -1;
-
}
-
}
fail:
@@ -52,12 +38,9 @@
ASSERT(m != NULL);
while (list_dequeue(&l,m->where) == 0) {
-
ASSERT(l != NULL);
ASSERT(l->waiting == m);
-
l->waiting = NULL;
-
}
ASSERT(m->where->head == NULL);
@@ -65,7 +48,6 @@
}
-//todo -- this function should not require any time of atomicity
void monitor_wait(struct _monitor *m, int timeout) {
int r;
@@ -76,6 +58,7 @@
timeout = INFINITE;
}
+ ASSERT(ReleaseMutex(m->mutex));
r = WaitForSingleObject(m->event,timeout);
@@ -89,7 +72,6 @@
break;
-
case WAIT_TIMEOUT:
ASSERT(timeout != INFINITE);
m->who = NULL;
@@ -97,12 +79,10 @@
monitor_detach(m);
break;
-
default:
ASSERT(FALSE);
break;
-
}
ASSERT(m->where != NULL);
@@ -123,6 +103,10 @@
if (m->event != NULL)
ASSERT(CloseHandle(m->event));
+ if (m->mutex != NULL)
+ ASSERT(CloseHandle(m->mutex));
+
+
if (m->where != NULL)
list_free(m->where);
@@ -130,6 +114,33 @@
}
+//returns int so we can consider the case when event inits fail
+int monitor_init(struct _monitor *m) {
+
+ int out;
+
+ out = 0;
+
+ m->event = NULL;
+ m->mutex = NULL;
+ m->err = 0;
+
+ m->where = list_new();
+ CHECK(m->where != NULL,0);
+
+ m->event = CreateEvent(NULL,FALSE,FALSE,NULL);
+ CHECK_(m->event != NULL);
+
+ m->mutex = CreateMutex(NULL,TRUE,NULL);
+ CHECK_(m->mutex != NULL);
+
+ fail:
+
+ return out;
+
+
+}
+
struct _monitor *monitor_new() {
struct _monitor *m;
@@ -139,39 +150,31 @@
out = 0;
err = 0;
+
m = (struct _monitor*)
malloc(sizeof(struct _monitor));
CHECK(m != NULL,ENOMEM);
- m->where = list_new();
- CHECK(m->where != NULL,0);
+ CHECK(monitor_init(m) == 0,0);
- m->event = CreateEvent(NULL,FALSE,FALSE,NULL);
- CHECK_(m->event != NULL);
-
fail:
err = errno;
if (out == -1) {
-
if (m != NULL) {
- ASSERT(FALSE);
monitor_free(m);
m = NULL;
}
-
}
errno = err;
return m;
}
-//todo -- get rid of multiple release mutexes
-//this code is a nightmare, clean it up
int wait_many_timeout(struct _wait_list **d, int n, int timeout) {
- struct _monitor *m;
+ struct _monitor m;
ASSERT(d != NULL);
@@ -179,24 +182,18 @@
int out;
int r;
+ monitor_init(&m);
- m = monitor_new();
-
- CHECK(m != NULL,0);
-
-
for (i=0; i<n; i++) {
- r = monitor_link(d[i],m);
+ r = monitor_link(d[i],&m);
CHECK(r != -1,0);
if (r == 1) {
-
//we have found one which is open, no need to keep looking
- monitor_free(m);
+ monitor_detach(&m);
- //todo -- i hate this
return i;
} else {
@@ -205,22 +202,25 @@
}
- monitor_wait(m,timeout);
+ monitor_wait(&m,timeout);
+ ASSERT(m.who != NULL);
- //todo -- why was this here?
- //ASSERT(m->who != NULL);
out = -1;
- if (m->err) {
- errno = m->err;
-
+ if (m.err) {
+ errno = m.err;
CHECK(FALSE,0);
}
- //todo -- there must be a way to do this in constant time
+
+ /*while this looks ugly, it has to be done. if we are going to create a
+ lookup table, it's going to take just as much time to build as the loop
+ needs to run
+ i wish i didn't decide to implement select
+ select is the problem, not my coding skills*/
for(i=0; i<n; i++) {
- if (d[i] == m->who) {
+ if (d[i] == m.who) {
out = i;
i = n+1;
}
@@ -231,10 +231,6 @@
fail:
- if (m != NULL) {
- monitor_free(m);
- }
-
return out;
}
@@ -252,36 +248,17 @@
}
-/*
-void wl_open(struct _wait_list *wl) {
-
- ASSERT(wl != NULL);
-
- wl_activate(wl,0);
- wl->active = -1;
-
-}
-
-void wl_close(struct _wait_list *wl) {
-
- ASSERT(wl != NULL);
- //you are not allowed to call wl_close on a closed lists
- ASSERT(wl->waiting == NULL);
-
- wl->active = 0;
-
-}
-
-*/
-
void wl_activate(struct _wait_list *l, int code) {
struct _monitor *m;
+ int r;
ASSERT(l != NULL);
if ( (m = l->waiting) != NULL) {
+ r = WaitForSingleObject(m->mutex,INFINITE);
+ ASSERT(r == WAIT_OBJECT_0);
monitor_detach(m);
m->who = l;
@@ -291,10 +268,8 @@
}
-
l->active = TRUE;
-
}
void wl_deactivate(struct _wait_list *l) {
Modified: bsockets/trunk/wait.h
===================================================================
--- bsockets/trunk/wait.h 2006-08-29 22:06:37 UTC (rev 8304)
+++ bsockets/trunk/wait.h 2006-08-30 08:34:56 UTC (rev 8305)
@@ -10,9 +10,9 @@
int active;
+
};
-
struct _monitor {
/*whoever is waiting on this monitor is waiting for this event
@@ -20,6 +20,9 @@
HANDLE event;
+ HANDLE mutex;
+
+
/*nodes which are in lines in which we are currently waiting*/
struct _list *where;
@@ -32,8 +35,6 @@
};
-
-
struct _wait_list *wl_new();
void wl_free();
@@ -48,6 +49,4 @@
void wl_activate(struct _wait_list *, int);
void wl_deactivate(struct _wait_list *);
-//void wl_wakeall(struct _wait_list *, int);
-
#endif