[or-cvs] r10877: Windows Bufferevent implementation mostly complete but not y (in libevent-urz/trunk: . loaders sample)

Urz at seul.org Urz at seul.org
Thu Jul 19 07:58:47 UTC 2007


Author: Urz
Date: 2007-07-19 03:58:47 -0400 (Thu, 19 Jul 2007)
New Revision: 10877

Added:
   libevent-urz/trunk/sample/cp-be-connector.c
   libevent-urz/trunk/sample/cp-be-listener.c
   libevent-urz/trunk/sample/crossplat-bufferevent-test.c
   libevent-urz/trunk/win_evbuffer.c
Modified:
   libevent-urz/trunk/Makefile.am
   libevent-urz/trunk/Makefile.in
   libevent-urz/trunk/evbuffer.c
   libevent-urz/trunk/event.h
   libevent-urz/trunk/loaders/IOCPloader.c
   libevent-urz/trunk/loaders/IOCPloader.h
   libevent-urz/trunk/sample/IOCPloader-test.c
   libevent-urz/trunk/sample/Makefile.am
   libevent-urz/trunk/sample/Makefile.in
Log:
Windows Bufferevent implementation mostly complete but not yet fully tested. libevent regression tests fail to compile badly on windows (missing netdb.h?) so I re-wrote sa_bufferevent to be platform independent (ongoing) and will use that to test with.
Todo: socket close function

Modified: libevent-urz/trunk/Makefile.am
===================================================================
--- libevent-urz/trunk/Makefile.am	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/Makefile.am	2007-07-19 07:58:47 UTC (rev 10877)
@@ -27,6 +27,10 @@
 SYS_LIBS = -lws2_32
 SYS_SRC = WIN32-Code/misc.c WIN32-Code/win32.c
 SYS_INCLUDES = -IWIN32-Code
+libevent_la_SOURCES = event.c buffer.c sa_evbuffer.c win_evbuffer.c log.c event_tagging.c \
+	http.c evhttp.h http-internal.h evdns.c evdns.h strlcpy.c \
+	strlcpy-internal.h strlcpy-internal.h \
+	$(SYS_SRC)
 
 else
 
@@ -34,13 +38,13 @@
 SYS_LIBS =
 SYS_SRC =
 SYS_INCLUDES =
-
-endif
-
 libevent_la_SOURCES = event.c buffer.c sa_evbuffer.c evbuffer.c log.c event_tagging.c \
 	http.c evhttp.h http-internal.h evdns.c evdns.h strlcpy.c \
 	strlcpy-internal.h strlcpy-internal.h \
 	$(SYS_SRC)
+
+endif
+
 libevent_la_LIBADD = @LTLIBOBJS@ $(SYS_LIBS)
 libevent_la_LDFLAGS = -release @VERSION@ -version-info 1:3:0
 libevent_la_CFLAGS = -g

Modified: libevent-urz/trunk/Makefile.in
===================================================================
--- libevent-urz/trunk/Makefile.in	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/Makefile.in	2007-07-19 07:58:47 UTC (rev 10877)
@@ -36,7 +36,6 @@
 PRE_UNINSTALL = :
 POST_UNINSTALL = :
 host_triplet = @host@
- at BUILD_WIN32_TRUE@am__append_1 = loaders/IOCPloader.h loaders/IOCPloader.c
 ACLOCAL = @ACLOCAL@
 AMDEP_FALSE = @AMDEP_FALSE@
 AMDEP_TRUE = @AMDEP_TRUE@
@@ -167,11 +166,17 @@
 @BUILD_WIN32_TRUE at SYS_INCLUDES = -IWIN32-Code
 @BUILD_WIN32_FALSE at SYS_INCLUDES = 
 
-libevent_la_SOURCES = event.c buffer.c sa_evbuffer.c evbuffer.c log.c event_tagging.c \
-	http.c evhttp.h http-internal.h evdns.c evdns.h strlcpy.c \
-	strlcpy-internal.h strlcpy-internal.h \
-	$(SYS_SRC)\
-$(am__append_1)
+ at BUILD_WIN32_TRUE@libevent_la_SOURCES = event.c buffer.c sa_evbuffer.c win_evbuffer.c log.c event_tagging.c \
+ at BUILD_WIN32_TRUE@	http.c evhttp.h http-internal.h evdns.c evdns.h strlcpy.c \
+ at BUILD_WIN32_TRUE@	strlcpy-internal.h strlcpy-internal.h \
+ at BUILD_WIN32_TRUE@	$(SYS_SRC)\
+ at BUILD_WIN32_TRUE@loaders/IOCPloader.h loaders/IOCPloader.c
+ at BUILD_WIN32_FALSE@libevent_la_SOURCES = event.c buffer.c sa_evbuffer.c evbuffer.c log.c event_tagging.c \
+ at BUILD_WIN32_FALSE@	http.c evhttp.h http-internal.h evdns.c evdns.h strlcpy.c \
+ at BUILD_WIN32_FALSE@	strlcpy-internal.h strlcpy-internal.h \
+ at BUILD_WIN32_FALSE@	$(SYS_SRC)
+
+
 libevent_la_LIBADD = @LTLIBOBJS@ $(SYS_LIBS)
 libevent_la_LDFLAGS = -release @VERSION@ -version-info 1:3:0
 libevent_la_CFLAGS = -g
@@ -192,19 +197,30 @@
 
 @BUILD_WIN32_TRUE at libevent_la_DEPENDENCIES = @LTLIBOBJS@
 @BUILD_WIN32_FALSE at libevent_la_DEPENDENCIES = @LTLIBOBJS@
-am__libevent_la_SOURCES_DIST = event.c buffer.c sa_evbuffer.c evbuffer.c \
-	log.c event_tagging.c http.c evhttp.h http-internal.h evdns.c \
-	evdns.h strlcpy.c strlcpy-internal.h WIN32-Code/misc.c \
-	WIN32-Code/win32.c loaders/IOCPloader.h loaders/IOCPloader.c
+am__libevent_la_SOURCES_DIST = event.c buffer.c sa_evbuffer.c \
+	win_evbuffer.c log.c event_tagging.c http.c evhttp.h \
+	http-internal.h evdns.c evdns.h strlcpy.c strlcpy-internal.h \
+	WIN32-Code/misc.c WIN32-Code/win32.c loaders/IOCPloader.h \
+	loaders/IOCPloader.c evbuffer.c
 @BUILD_WIN32_TRUE at am__objects_1 = libevent_la-misc.lo \
 @BUILD_WIN32_TRUE@	libevent_la-win32.lo
 @BUILD_WIN32_FALSE at am__objects_1 =
- at BUILD_WIN32_TRUE@am__objects_2 = libevent_la-IOCPloader.lo
-am_libevent_la_OBJECTS = libevent_la-event.lo libevent_la-buffer.lo \
-	libevent_la-sa_evbuffer.lo libevent_la-evbuffer.lo \
-	libevent_la-log.lo libevent_la-event_tagging.lo \
-	libevent_la-http.lo libevent_la-evdns.lo libevent_la-strlcpy.lo \
-	$(am__objects_1) $(am__objects_2)
+ at BUILD_WIN32_TRUE@am_libevent_la_OBJECTS = libevent_la-event.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-buffer.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-sa_evbuffer.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-win_evbuffer.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-log.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-event_tagging.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-http.lo libevent_la-evdns.lo \
+ at BUILD_WIN32_TRUE@	libevent_la-strlcpy.lo $(am__objects_1) \
+ at BUILD_WIN32_TRUE@	libevent_la-IOCPloader.lo
+ at BUILD_WIN32_FALSE@am_libevent_la_OBJECTS = libevent_la-event.lo \
+ at BUILD_WIN32_FALSE@	libevent_la-buffer.lo \
+ at BUILD_WIN32_FALSE@	libevent_la-sa_evbuffer.lo \
+ at BUILD_WIN32_FALSE@	libevent_la-evbuffer.lo libevent_la-log.lo \
+ at BUILD_WIN32_FALSE@	libevent_la-event_tagging.lo \
+ at BUILD_WIN32_FALSE@	libevent_la-http.lo libevent_la-evdns.lo \
+ at BUILD_WIN32_FALSE@	libevent_la-strlcpy.lo $(am__objects_1)
 libevent_la_OBJECTS = $(am_libevent_la_OBJECTS)
 SCRIPTS = $(bin_SCRIPTS)
 
@@ -308,7 +324,7 @@
 libevent_la-event.lo: event.c
 libevent_la-buffer.lo: buffer.c
 libevent_la-sa_evbuffer.lo: sa_evbuffer.c
-libevent_la-evbuffer.lo: evbuffer.c
+libevent_la-win_evbuffer.lo: win_evbuffer.c
 libevent_la-log.lo: log.c
 libevent_la-event_tagging.lo: event_tagging.c
 libevent_la-http.lo: http.c
@@ -317,6 +333,7 @@
 libevent_la-misc.lo: WIN32-Code/misc.c
 libevent_la-win32.lo: WIN32-Code/win32.c
 libevent_la-IOCPloader.lo: loaders/IOCPloader.c
+libevent_la-evbuffer.lo: evbuffer.c
 libevent.la: $(libevent_la_OBJECTS) $(libevent_la_DEPENDENCIES) 
 	$(LINK) -rpath $(libdir) $(libevent_la_LDFLAGS) $(libevent_la_OBJECTS) $(libevent_la_LIBADD) $(LIBS)
 binSCRIPT_INSTALL = $(INSTALL_SCRIPT)
@@ -382,14 +399,14 @@
 libevent_la-sa_evbuffer.lo: sa_evbuffer.c
 	$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-sa_evbuffer.lo `test -f 'sa_evbuffer.c' || echo '$(srcdir)/'`sa_evbuffer.c
 
-libevent_la-evbuffer.o: evbuffer.c
-	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-evbuffer.o `test -f 'evbuffer.c' || echo '$(srcdir)/'`evbuffer.c
+libevent_la-win_evbuffer.o: win_evbuffer.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-win_evbuffer.o `test -f 'win_evbuffer.c' || echo '$(srcdir)/'`win_evbuffer.c
 
-libevent_la-evbuffer.obj: evbuffer.c
-	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-evbuffer.obj `if test -f 'evbuffer.c'; then $(CYGPATH_W) 'evbuffer.c'; else $(CYGPATH_W) '$(srcdir)/evbuffer.c'`
+libevent_la-win_evbuffer.obj: win_evbuffer.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-win_evbuffer.obj `if test -f 'win_evbuffer.c'; then $(CYGPATH_W) 'win_evbuffer.c'; else $(CYGPATH_W) '$(srcdir)/win_evbuffer.c'`
 
-libevent_la-evbuffer.lo: evbuffer.c
-	$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-evbuffer.lo `test -f 'evbuffer.c' || echo '$(srcdir)/'`evbuffer.c
+libevent_la-win_evbuffer.lo: win_evbuffer.c
+	$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-win_evbuffer.lo `test -f 'win_evbuffer.c' || echo '$(srcdir)/'`win_evbuffer.c
 
 libevent_la-log.o: log.c
 	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-log.o `test -f 'log.c' || echo '$(srcdir)/'`log.c
@@ -463,6 +480,15 @@
 libevent_la-IOCPloader.lo: loaders/IOCPloader.c
 	$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-IOCPloader.lo `test -f 'loaders/IOCPloader.c' || echo '$(srcdir)/'`loaders/IOCPloader.c
 
+libevent_la-evbuffer.o: evbuffer.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-evbuffer.o `test -f 'evbuffer.c' || echo '$(srcdir)/'`evbuffer.c
+
+libevent_la-evbuffer.obj: evbuffer.c
+	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-evbuffer.obj `if test -f 'evbuffer.c'; then $(CYGPATH_W) 'evbuffer.c'; else $(CYGPATH_W) '$(srcdir)/evbuffer.c'`
+
+libevent_la-evbuffer.lo: evbuffer.c
+	$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libevent_la_CFLAGS) $(CFLAGS) -c -o libevent_la-evbuffer.lo `test -f 'evbuffer.c' || echo '$(srcdir)/'`evbuffer.c
+
 mostlyclean-libtool:
 	-rm -f *.lo
 

Modified: libevent-urz/trunk/evbuffer.c
===================================================================
--- libevent-urz/trunk/evbuffer.c	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/evbuffer.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -25,7 +25,8 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <sys/types.h>
+#include <sys/types.h>
+#include <sys/socket.h>
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -50,6 +51,30 @@
 void bufferevent_setwatermark(struct bufferevent *, short, size_t, size_t);
 void bufferevent_read_pressure_cb(struct evbuffer *, size_t, size_t, void *);
 
+//socket
+int bufev_socket_new(void) {
+    return socket(AF_INET, SOCK_STREAM, 0);
+}
+// connect
+int bufev_socket_connect(struct bufferevent *bufev, struct sockaddr_in *addr) {
+    return connect(bufev->fd, (struct sockaddr*)addr, sizeof(addr));
+}
+// accept
+struct bufferevent *bufev_socket_accept(int listen_sock, evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg) {
+    int returned;
+    returned = accept(listen_sock, NULL, NULL);
+    
+    return bufferevent_new(returned, readcb, writecb, errorcb, cbarg);
+}
+// listen
+int bufev_socket_listen(int sock, int backlog) {
+    return listen(sock, backlog);
+}
+// bind
+int bufev_socket_bind(int sock, struct sockaddr_in *bind_to) {
+    return bind(sock, (struct sockaddr*) bind_to, sizeof(bind_to));
+}
+
 static int
 bufferevent_add(struct event *ev, int timeout)
 {

Modified: libevent-urz/trunk/event.h
===================================================================
--- libevent-urz/trunk/event.h	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/event.h	2007-07-19 07:58:47 UTC (rev 10877)
@@ -34,15 +34,19 @@
 #include "config.h"
 #include "compat/sys/queue.h"
 #include <stdarg.h>
+#include <sys/types.h>
 
 #ifdef WIN32
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <process.h>
+#include <Winsock2.h>
 /* #include "compat/sys/_time.h" */
 #undef WIN32_LEAN_AND_MEAN
 typedef unsigned char u_char;
 typedef unsigned short u_short;
+#else
+#include <sys/socket.h>
 #endif
 
 #define EVLIST_TIMEOUT	0x01
@@ -272,6 +276,17 @@
 	short enabled;	/* events that are currently enabled */
 };
 
+//socket
+int bufev_socket_new(void);
+// connect
+int bufev_socket_connect(struct bufferevent *bufev, struct sockaddr_in *addr);
+// accept
+struct bufferevent *bufev_socket_accept(int listen_sock, evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
+// listen
+int bufev_socket_listen(int sock, int backlog);
+// bind
+int bufev_socket_bind(int sock, struct sockaddr_in *bind_to);
+
 struct bufferevent *bufferevent_new(int fd,
     evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
@@ -318,7 +333,9 @@
     u_char del_read_event_set;
     u_char del_write_event_set;
     
-    LIST_ENTRY(sa_bufferevent) list_elem;	
+    LIST_ENTRY(sa_bufferevent) list_elem;
+    
+    void *connection_handle;
 };
 
 struct sa_bufferevent *sa_bufferevent_new(evsabuffercb readcb, 

Modified: libevent-urz/trunk/loaders/IOCPloader.c
===================================================================
--- libevent-urz/trunk/loaders/IOCPloader.c	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/loaders/IOCPloader.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -20,7 +20,6 @@
 mutex listLock;
 /*
  * Status :
- * Code should be functionally complete but untested and undocumented.
  * Code compiles, although it has the following warnings I do not understand:
  * loaders/IOCPloader.c: In function `iocp_worker_thread':
  * loaders/IOCPloader.c:209: warning: dereferencing type-punned pointer will break strict-aliasing rules
@@ -36,7 +35,7 @@
     printf("%x ]\n", (int) data[len-1]);
 }
  
-int IOCPloader_bind(SOCKET *s, struct sa_bufferevent * bufevent) {
+int IOCPloader_bind(SOCKET s, struct sa_bufferevent * bufevent) {
     DWORD myListElem;
     DWORD listSearch;
     char found = 0;
@@ -101,7 +100,7 @@
     connList[myListElem].inUse = 1;
     
     // Despite the name, this call associates the socket with the I/O Completion port
-    IOCP = CreateIoCompletionPort((HANDLE) *(connList[myListElem].sock), IOCP, (ULONG_PTR) myListElem, 0);
+    IOCP = CreateIoCompletionPort((HANDLE) connList[myListElem].sock, IOCP, (ULONG_PTR) myListElem, 0);
     ResetRead(&connList[myListElem]);
     
     ev_unlock(connList[myListElem].lock);
@@ -192,7 +191,7 @@
                 //printf("Presend buf: ");
                 //printhex(connList[listpos].sendbuf->buf, unloaded);
                 sendRet = WSASend(
-                    *(connList[listpos].sock),
+                    connList[listpos].sock,
                     // Socket to send on
                     connList[listpos].sendbuf,
                     // 'array' of WSA buffers to use
@@ -256,6 +255,8 @@
             );
         if(!GQCSRet) {
             printf("GetQueuedCompletionStatus error %d\n", (int) GetLastError());
+            Sleep(10000);
+            exit(0);
             continue;
         }
         
@@ -291,7 +292,7 @@
     UINT WSARecvRet;
     ULONG WSARecvFlags = 0;
     WSARecvRet = WSARecv(
-        *(Conn->sock),
+        Conn->sock,
         // The socket to recieve from
         Conn->recvbuf,
         // Pointer to an 'array' of WSABUFs.

Modified: libevent-urz/trunk/loaders/IOCPloader.h
===================================================================
--- libevent-urz/trunk/loaders/IOCPloader.h	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/loaders/IOCPloader.h	2007-07-19 07:58:47 UTC (rev 10877)
@@ -8,7 +8,7 @@
 void IOCPLoaderInit(void);
 DWORD WINAPI iocp_worker_thread(LPVOID);
 DWORD WINAPI iocp_writer_thread(LPVOID);
-int IOCPloader_bind(SOCKET *, struct sa_bufferevent *);
+int IOCPloader_bind(SOCKET, struct sa_bufferevent *);
 void printhex(char *data, size_t len);
 
 #define SUGGESTED_BUF_SIZE 4096
@@ -30,7 +30,7 @@
     struct sa_bufferevent *localbuf;
     WSABUF *recvbuf;
     WSABUF *sendbuf;
-    SOCKET *sock;
+    SOCKET sock;
     OLOPERATION recvol;
     OLOPERATION sendol;
     mutex lock;

Modified: libevent-urz/trunk/sample/IOCPloader-test.c
===================================================================
--- libevent-urz/trunk/sample/IOCPloader-test.c	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/sample/IOCPloader-test.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -171,7 +171,7 @@
     IOCPLoaderInit();
     // If we start this up before all the accepting is done it may interfere.
 
-    bindingHandle = IOCPloader_bind(&Accept, lsabe);
+    bindingHandle = IOCPloader_bind(Accept, lsabe);
     printf("IOCPLoader Bound (%d)\n", bindingHandle);
     
     gen_pattern_a(buf, BUF_SIZE);
@@ -253,7 +253,7 @@
     
     printf("Connected.\n");
     
-    bindingHandle = IOCPloader_bind(&Connect, csabe);
+    bindingHandle = IOCPloader_bind(Connect, csabe);
     
     printf("IOCPLoader Bound (%d)\n", bindingHandle);
     

Modified: libevent-urz/trunk/sample/Makefile.am
===================================================================
--- libevent-urz/trunk/sample/Makefile.am	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/sample/Makefile.am	2007-07-19 07:58:47 UTC (rev 10877)
@@ -5,15 +5,20 @@
 CFLAGS = -I../compat -g
 
 #noinst_PROGRAMS = event-test time-test signal-test
-noinst_PROGRAMS = time-test signal-test sa_evbuffer-test IOCPlistener IOCPconnector
+noinst_PROGRAMS = time-test signal-test sa_evbuffer-test IOCPlistener IOCPconnector cp-be-listener cp-be-connector
 
 event_test_sources = event-test.c
 time_test_sources = time-test.c
 signal_test_sources = signal-test.c
 sa_evbuffer_test_sources = sa_evbuffer-test.c
-IOCPlistener_sources = IOCPloader-test.c IOCPlistener.c
-IOCPconnector_sources = IOCPloader-test.c IOCPconnector.c
+IOCPlistener_sources = crossplat-bufferevent-test.c IOCPlistener.c
+IOCPconnector_sources = crossplat-bufferevent-test.c IOCPconnector.c
+cp-be-listener_sources = crossplat-bufferevent-test.c cp-be-listener.c
+cp-be-connector_sources = crossplat-bufferevent-test.c cp-be-connector.c
 
+cp-be-listener_CFLAGS = -g
+cp-be-connector_CFLAGS = -g
+
 IOCPlistener_CFLAGS = -g
 IOCPconnector_CFLAGS = -g
 

Modified: libevent-urz/trunk/sample/Makefile.in
===================================================================
--- libevent-urz/trunk/sample/Makefile.in	2007-07-19 07:46:11 UTC (rev 10876)
+++ libevent-urz/trunk/sample/Makefile.in	2007-07-19 07:58:47 UTC (rev 10877)
@@ -138,15 +138,20 @@
 CPPFPLAGS = -I.. 
 
 #noinst_PROGRAMS = event-test time-test signal-test
-noinst_PROGRAMS = time-test signal-test sa_evbuffer-test IOCPlistener IOCPconnector
+noinst_PROGRAMS = time-test signal-test sa_evbuffer-test IOCPlistener IOCPconnector cp-be-listener cp-be-connector
 
 event_test_sources = event-test.c
 time_test_sources = time-test.c
 signal_test_sources = signal-test.c
 sa_evbuffer_test_sources = sa_evbuffer-test.c
-IOCPlistener_sources = IOCPloader-test.c IOCPlistener.c
-IOCPconnector_sources = IOCPloader-test.c IOCPconnector.c
+IOCPlistener_sources = crossplat-bufferevent-test.c IOCPlistener.c
+IOCPconnector_sources = crossplat-bufferevent-test.c IOCPconnector.c
+cp-be-listener_sources = crossplat-bufferevent-test.c cp-be-listener.c
+cp-be-connector_sources = crossplat-bufferevent-test.c cp-be-connector.c
 
+cp-be-listener_CFLAGS = -g
+cp-be-connector_CFLAGS = -g
+
 IOCPlistener_CFLAGS = -g
 IOCPconnector_CFLAGS = -g
 
@@ -157,7 +162,8 @@
 CONFIG_CLEAN_FILES =
 noinst_PROGRAMS = time-test$(EXEEXT) signal-test$(EXEEXT) \
 	sa_evbuffer-test$(EXEEXT) IOCPlistener$(EXEEXT) \
-	IOCPconnector$(EXEEXT)
+	IOCPconnector$(EXEEXT) cp-be-listener$(EXEEXT) \
+	cp-be-connector$(EXEEXT)
 PROGRAMS = $(noinst_PROGRAMS)
 
 IOCPconnector_SOURCES = IOCPconnector.c
@@ -170,6 +176,16 @@
 IOCPlistener_LDADD = $(LDADD)
 IOCPlistener_DEPENDENCIES = ../libevent.la
 IOCPlistener_LDFLAGS =
+cp_be_connector_SOURCES = cp-be-connector.c
+cp_be_connector_OBJECTS = cp-be-connector.$(OBJEXT)
+cp_be_connector_LDADD = $(LDADD)
+cp_be_connector_DEPENDENCIES = ../libevent.la
+cp_be_connector_LDFLAGS =
+cp_be_listener_SOURCES = cp-be-listener.c
+cp_be_listener_OBJECTS = cp-be-listener.$(OBJEXT)
+cp_be_listener_LDADD = $(LDADD)
+cp_be_listener_DEPENDENCIES = ../libevent.la
+cp_be_listener_LDFLAGS =
 sa_evbuffer_test_SOURCES = sa_evbuffer-test.c
 sa_evbuffer_test_OBJECTS = sa_evbuffer-test.$(OBJEXT)
 sa_evbuffer_test_LDADD = $(LDADD)
@@ -196,10 +212,10 @@
 CCLD = $(CC)
 LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
 	$(AM_LDFLAGS) $(LDFLAGS) -o $@
-DIST_SOURCES = IOCPconnector.c IOCPlistener.c sa_evbuffer-test.c \
-	signal-test.c time-test.c
+DIST_SOURCES = IOCPconnector.c IOCPlistener.c cp-be-connector.c \
+	cp-be-listener.c sa_evbuffer-test.c signal-test.c time-test.c
 DIST_COMMON = Makefile.am Makefile.in
-SOURCES = IOCPconnector.c IOCPlistener.c sa_evbuffer-test.c signal-test.c time-test.c
+SOURCES = IOCPconnector.c IOCPlistener.c cp-be-connector.c cp-be-listener.c sa_evbuffer-test.c signal-test.c time-test.c
 
 all: all-am
 
@@ -225,6 +241,12 @@
 IOCPlistener$(EXEEXT): $(IOCPlistener_OBJECTS) $(IOCPlistener_DEPENDENCIES) 
 	@rm -f IOCPlistener$(EXEEXT)
 	$(LINK) $(IOCPlistener_LDFLAGS) $(IOCPlistener_OBJECTS) $(IOCPlistener_LDADD) $(LIBS)
+cp-be-connector$(EXEEXT): $(cp_be_connector_OBJECTS) $(cp_be_connector_DEPENDENCIES) 
+	@rm -f cp-be-connector$(EXEEXT)
+	$(LINK) $(cp_be_connector_LDFLAGS) $(cp_be_connector_OBJECTS) $(cp_be_connector_LDADD) $(LIBS)
+cp-be-listener$(EXEEXT): $(cp_be_listener_OBJECTS) $(cp_be_listener_DEPENDENCIES) 
+	@rm -f cp-be-listener$(EXEEXT)
+	$(LINK) $(cp_be_listener_LDFLAGS) $(cp_be_listener_OBJECTS) $(cp_be_listener_LDADD) $(LIBS)
 sa_evbuffer-test$(EXEEXT): $(sa_evbuffer_test_OBJECTS) $(sa_evbuffer_test_DEPENDENCIES) 
 	@rm -f sa_evbuffer-test$(EXEEXT)
 	$(LINK) $(sa_evbuffer_test_LDFLAGS) $(sa_evbuffer_test_OBJECTS) $(sa_evbuffer_test_LDADD) $(LIBS)

Added: libevent-urz/trunk/sample/cp-be-connector.c
===================================================================
--- libevent-urz/trunk/sample/cp-be-connector.c	                        (rev 0)
+++ libevent-urz/trunk/sample/cp-be-connector.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -0,0 +1,6 @@
+#include "crossplat-bufferevent-test.c"
+
+int main() {
+    connector();
+    return 0;
+}
\ No newline at end of file

Added: libevent-urz/trunk/sample/cp-be-listener.c
===================================================================
--- libevent-urz/trunk/sample/cp-be-listener.c	                        (rev 0)
+++ libevent-urz/trunk/sample/cp-be-listener.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -0,0 +1,6 @@
+#include "crossplat-bufferevent-test.c"
+
+int main() {
+    listener();
+    return 0;
+}
\ No newline at end of file

Added: libevent-urz/trunk/sample/crossplat-bufferevent-test.c
===================================================================
--- libevent-urz/trunk/sample/crossplat-bufferevent-test.c	                        (rev 0)
+++ libevent-urz/trunk/sample/crossplat-bufferevent-test.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -0,0 +1,236 @@
+/*
+ * Copyright (c) 2007 Christian King <urzumph at gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+ 
+ /*
+  * On Windows and all kinds of lame:
+  * Windows has *no* fork, or anything which emulates fork.
+  * The closest is threading, or "CreateProcess" which is like fork() + exec()
+  * Thus, this file contains all code for both processes and is included from
+  * each processes' main c file.
+  */
+
+#include <Winsock2.h>
+#include "event.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <Windows.h>
+#include <unistd.h>
+
+#define BUF_SIZE 1000
+#define LISTEN_PORT 1025
+
+void doexit(int val) {
+    char data;
+    fread(&data, 1, 1, stdin);
+    exit(val);
+}
+
+void gen_pattern_a(char *buf, size_t len) {
+    size_t upto;
+    static char cycle = 0;
+    for(upto = 0; upto < len; upto++) {
+        buf[upto] = cycle++;
+    }
+}
+
+int check_pattern_a(char *buf, size_t len) {
+    size_t upto;
+    static char cycle = 0;
+    for(upto = 0; upto < len; upto++) {
+        if(buf[upto] != cycle++) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
+void gen_pattern_b(char *buf, size_t len) {
+    size_t upto;
+    static char cycle = 255;
+    for(upto = 0; upto < len; upto++) {
+        buf[upto] = cycle--;
+    }
+}
+
+int check_pattern_b(char *buf, size_t len) {
+    size_t upto;
+    static char cycle = 255;
+    for(upto = 0; upto < len; upto++) {
+        if(buf[upto] != cycle--) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
+// check incoming data
+void listener_on_read(struct bufferevent *sabe, void *isnull) {
+    char buf[BUF_SIZE+1];
+    size_t len_read;
+    
+    do {
+        len_read = bufferevent_read(sabe, buf, BUF_SIZE);
+    
+        if(!check_pattern_b(buf, len_read)) {
+            buf[BUF_SIZE] = '\0';
+            printf("Recieved buffer failed pattern check b: recieved ");
+            printhex(buf, BUF_SIZE);
+            doexit(0);
+        }
+    } while (len_read != 0);
+    
+    printf("R");
+}
+
+// when outgoing data is sent, queue up more.
+void listener_on_write(struct bufferevent *sabe, void *isnull) {
+    char buf[BUF_SIZE+1];
+    size_t len_read;
+    
+    gen_pattern_a(buf, BUF_SIZE);
+    //printf("Sending buffer: ");
+    //printhex(buf, BUF_SIZE);
+    bufferevent_write(sabe, buf, BUF_SIZE);
+    
+    printf("W");
+}
+
+void listener() {
+    WSADATA wsaData;
+    struct bufferevent* lsabe;
+    int Listen;
+    struct sockaddr_in listenAddr;
+    int Accept;
+    int bindingHandle = -1;
+    char buf[BUF_SIZE];
+    int error;
+    int AcceptRet;
+    
+    DWORD zero;
+    
+    WSAStartup(MAKEWORD( 2, 2 ), &wsaData);
+    event_init();
+    
+    printf("Creating Listen Socket\n");
+    Listen = bufev_socket_new();
+    //lsabe = bufferevent_new(bufev_socket_new(), listener_on_read, listener_on_write, NULL, NULL);
+    printf("Listen = %d\n", (int) Listen);
+    
+    listenAddr.sin_family = AF_INET;
+    listenAddr.sin_port = htons(LISTEN_PORT);
+    listenAddr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+    printf("Binding Socket\n");
+    bufev_socket_bind(Listen, &listenAddr);
+    printf("Listening...\n");
+    bufev_socket_listen(Listen, 1);
+    
+    printf("Accepting...\n");
+
+    lsabe = bufev_socket_accept(Listen, listener_on_read, listener_on_write, NULL, NULL);
+    // accept() and overlapped IO do not play well together.
+    
+    printf("Client Accepted...\n");
+    
+    gen_pattern_a(buf, BUF_SIZE);
+    printf("Generating Initial Pattern\n");
+    //printf("Sending buffer: ");
+    //printhex(buf, BUF_SIZE);
+    
+    bufferevent_write(lsabe, buf, BUF_SIZE);
+    printf("Writing to buffer\n");
+    
+    printf("Starting Dispatch\n");
+    error = event_dispatch();
+    printf("Event dispach returned %d - error?\n", error);
+}
+
+void connector_on_read(struct bufferevent *sabe, void *isnull) {
+    char buf[BUF_SIZE+1];
+    size_t len_read;
+    
+    do {
+        len_read = bufferevent_read(sabe, buf, BUF_SIZE);
+    
+        if(!check_pattern_a(buf, len_read)) {
+            buf[BUF_SIZE] = '\0';
+            printf("Recieved buffer failed pattern check a: recieved ");
+            printhex(buf, BUF_SIZE);
+            doexit(0);
+        }
+    } while (len_read != 0);
+    
+    gen_pattern_b(buf, BUF_SIZE);
+    //printf("Sending buffer: ");
+    //printhex(buf, BUF_SIZE);
+    bufferevent_write(sabe, buf, BUF_SIZE);
+    
+    printf(".");
+}
+    
+
+void connector() {
+    WSADATA wsaData;
+    struct bufferevent* csabe;
+    struct sockaddr_in socketAddr;
+    struct hostent* hostp;
+    struct in_addr* ipAddress;
+    int Connect;
+    int bindingHandle = -1;
+    int error;
+    int conret;
+    
+    WSAStartup(MAKEWORD( 2, 2 ), &wsaData);
+    event_init();
+    
+    //csabe = sa_bufferevent_new(connector_on_read, NULL, NULL, NULL);
+    
+    hostp = gethostbyname("localhost");
+    // It may be depriciated, but it's a lot easier to use than getaddrinfo
+    ipAddress = (struct in_addr*)hostp->h_addr_list[0];
+
+    socketAddr.sin_family = AF_INET;
+    socketAddr.sin_port = htons(LISTEN_PORT);
+    socketAddr.sin_addr.s_addr = ipAddress->s_addr;
+
+    printf("Creating Socket\n");
+    Connect = bufev_socket_new();
+    
+    printf("Connect = %d\n", (int) Connect);
+    
+    printf("Creating Bufferevent\n");
+    csabe = bufferevent_new(Connect, connector_on_read, NULL, NULL, NULL);
+
+    printf("Connecting...\n");
+    bufev_socket_connect(csabe, &socketAddr);
+    
+    printf("Connected.\n");
+    
+    printf("Starting Event Dispatch\n");
+    error = event_dispatch();
+    printf("Event dispach returned %d - error?\n", error);
+}

Added: libevent-urz/trunk/win_evbuffer.c
===================================================================
--- libevent-urz/trunk/win_evbuffer.c	                        (rev 0)
+++ libevent-urz/trunk/win_evbuffer.c	2007-07-19 07:58:47 UTC (rev 10877)
@@ -0,0 +1,114 @@
+#include <sys/types.h>
+#include "event.h"
+#include <stdio.h>
+#include "loaders/IOCPloader.h"
+
+static char IOCPinit = 0;
+
+/* Warning: Here be some nasty typecasting
+ * OK so basically the sa_bufferevent and bufferevent structs
+ * are compatible. Thus, we can use sa_bufferevent_new and cast
+ * the return to a bufferevent, as well as casting the callbacks
+ * to the appropriate type.
+ */
+
+//socket
+int bufev_socket_new(void) {
+    SOCKET ret;
+    ret = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
+    if(ret == INVALID_SOCKET) {
+        printf("Get Socket failed\n");
+        return -1;
+    }
+    return (int) ret;
+}
+// connect
+int bufev_socket_connect(struct bufferevent *bufev, struct sockaddr_in *addr) {
+    int ret;
+    printf("Connecting with %d\n", (SOCKET)(((struct sa_bufferevent *)bufev)->connection_handle));
+    ret = connect((SOCKET)(((struct sa_bufferevent *)bufev)->connection_handle), (struct sockaddr*)addr, sizeof(struct sockaddr_in));
+    if(ret == SOCKET_ERROR) {
+        printf("Connecting failed with error %d\n", (int) GetLastError());
+        return -1;
+    }
+    return ret;
+}
+// accept
+struct bufferevent *bufev_socket_accept(int listen_sock, evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg) {
+    SOCKET returned;
+    printf("WSAAccept Starting\n");
+    returned = WSAAccept(listen_sock, NULL, NULL, NULL, 0);
+    if(returned == INVALID_SOCKET) {
+        return NULL;
+    }
+    printf("Accept Creating new Bufferevent with %d\n", (int) returned);
+    return bufferevent_new((int)returned, readcb, writecb, errorcb, cbarg);
+}
+// listen
+int bufev_socket_listen(int sock, int backlog) {
+    printf("Listen given sock: %d\n", sock);
+    return listen((SOCKET)sock, backlog);
+}
+// bind
+int bufev_socket_bind(int sock, struct sockaddr_in *bind_to) {
+    int ret;
+    ret = bind((SOCKET) sock, (struct sockaddr*) bind_to, sizeof(struct sockaddr_in));
+    if(ret == SOCKET_ERROR) {
+        return -1;
+    }
+    return ret;
+}
+
+struct bufferevent *bufferevent_new(int fd,
+    evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg) {
+    
+    struct sa_bufferevent *new;
+    
+    if(!IOCPinit) {
+        IOCPLoaderInit();
+        IOCPinit = 1;
+    }
+    
+    new = sa_bufferevent_new((evsabuffercb) readcb,(evsabuffercb) writecb,(evsaerrorcb) errorcb, cbarg);
+    new->connection_handle = (void *) fd;
+    
+    IOCPloader_bind((SOCKET) fd, new);
+    
+    return (struct bufferevent *) new;
+}
+
+int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev) {
+    // As per nickm's request
+}
+
+int bufferevent_priority_set(struct bufferevent *bufev, int pri) {
+    // As per nickm's request
+}
+
+void bufferevent_free(struct bufferevent *bufev) {
+    sa_bufferevent_free((struct sa_bufferevent *) bufev);
+}
+
+int bufferevent_write(struct bufferevent *bufev, void *data, size_t size) {
+    return sa_bufferevent_write((struct sa_bufferevent *) bufev, data, size);
+}
+
+int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf) {
+    return sa_bufferevent_write_buffer((struct sa_bufferevent *) bufev, buf);
+}
+
+size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size) {
+    return sa_bufferevent_read((struct sa_bufferevent *) bufev, data, size);
+}
+
+int bufferevent_enable(struct bufferevent *bufev, short event) {
+    return sa_bufferevent_enable((struct sa_bufferevent *) bufev, event);
+}
+
+int bufferevent_disable(struct bufferevent *bufev, short event) {
+    return sa_bufferevent_disable((struct sa_bufferevent *) bufev, event);
+}
+
+void bufferevent_settimeout(struct bufferevent *bufev, int timeout_read, int timeout_write) {
+    sa_bufferevent_settimeout((struct sa_bufferevent *) bufev, timeout_read, timeout_write);
+}



More information about the tor-commits mailing list