[or-cvs] r10981: Initial (untested) implementations of close and close&free. (in libevent-urz/trunk: . sample)

Urz at seul.org Urz at seul.org
Mon Jul 30 02:32:23 UTC 2007


Author: Urz
Date: 2007-07-29 22:32:22 -0400 (Sun, 29 Jul 2007)
New Revision: 10981

Modified:
   libevent-urz/trunk/evbuffer.c
   libevent-urz/trunk/event.h
   libevent-urz/trunk/sa_evbuffer.c
   libevent-urz/trunk/sample/crossplat-bufferevent-test.c
   libevent-urz/trunk/win_evbuffer.c
Log:
Initial (untested) implementations of close and close&free.

Modified: libevent-urz/trunk/evbuffer.c
===================================================================
--- libevent-urz/trunk/evbuffer.c	2007-07-30 01:32:12 UTC (rev 10980)
+++ libevent-urz/trunk/evbuffer.c	2007-07-30 02:32:22 UTC (rev 10981)
@@ -65,8 +65,7 @@
     returned = accept(listen_sock, NULL, NULL);
     
     if(returned < 0) {
-        printf("Accept failed : %d");
-        perror(NULL);
+        perror("Accept failed : ");
         return NULL;
     }
 
@@ -81,6 +80,10 @@
     return bind(sock, (struct sockaddr*) bind_to, sizeof(struct sockaddr_in));
 }
 
+int bufev_socket_close(int sock) {
+    return close(sock);
+}
+
 static int
 bufferevent_add(struct event *ev, int timeout)
 {
@@ -313,6 +316,11 @@
 	free(bufev);
 }
 
+void bufferevent_close_and_free(struct bufferevent *bufev) {
+    bufev_socket_close(bufev->fd);
+    bufferevent_free(bufev);
+}
+
 /*
  * Returns 0 on success;
  *        -1 on failure.

Modified: libevent-urz/trunk/event.h
===================================================================
--- libevent-urz/trunk/event.h	2007-07-30 01:32:12 UTC (rev 10980)
+++ libevent-urz/trunk/event.h	2007-07-30 02:32:22 UTC (rev 10981)
@@ -305,6 +305,9 @@
  * int bufev_socket_bind(int sock, struct sockaddr_in *bind_to);
  * Binds a socket (created with bufev_socket_new()) to a particular internet address.
  * returns 0 on success, -1 on failure.
+ *
+ * int bufev_socket_close(int sock);
+ * Closes a socket (created with bufev_socket_new()). Returns 0 on success, -1 on error.
  */
 
 //socket
@@ -317,6 +320,8 @@
 int bufev_socket_listen(int sock, int backlog);
 // bind
 int bufev_socket_bind(int sock, struct sockaddr_in *bind_to);
+// close
+int bufev_socket_close(int sock);
 
 /* 
  * Note: bufferevent_new must only be called with *connected* bufev_sockets.
@@ -328,6 +333,7 @@
 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
 void bufferevent_free(struct bufferevent *bufev);
+void bufferevent_close_and_free(struct bufferevent *bufev);
 int bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);

Modified: libevent-urz/trunk/sa_evbuffer.c
===================================================================
--- libevent-urz/trunk/sa_evbuffer.c	2007-07-30 01:32:12 UTC (rev 10980)
+++ libevent-urz/trunk/sa_evbuffer.c	2007-07-30 02:32:22 UTC (rev 10981)
@@ -250,7 +250,8 @@
      */
 
 	/* Invoke the user callback - must always be called last */
-	if (bufev->readcb != NULL) {
+	if (bufev->readcb != NULL &&
+            (bufev->enabled & EV_READ)) {
 		bufev->del_read_event_set = 1;
         notify();
     }
@@ -298,7 +299,8 @@
 	 * low watermark.
 	 */
 	if (bufev->writecb != NULL &&
-	    EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low) {
+	    EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low &&
+        (bufev->enabled & EV_WRITE)) {
         bufev->del_write_event_set = 1;
         notify();
     }

Modified: libevent-urz/trunk/sample/crossplat-bufferevent-test.c
===================================================================
--- libevent-urz/trunk/sample/crossplat-bufferevent-test.c	2007-07-30 01:32:12 UTC (rev 10980)
+++ libevent-urz/trunk/sample/crossplat-bufferevent-test.c	2007-07-30 02:32:22 UTC (rev 10981)
@@ -178,7 +178,6 @@
     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.
     bufferevent_enable(lsabe, EV_READ);
     bufferevent_enable(lsabe, EV_WRITE);
 

Modified: libevent-urz/trunk/win_evbuffer.c
===================================================================
--- libevent-urz/trunk/win_evbuffer.c	2007-07-30 01:32:12 UTC (rev 10980)
+++ libevent-urz/trunk/win_evbuffer.c	2007-07-30 02:32:22 UTC (rev 10981)
@@ -58,6 +58,14 @@
     }
     return ret;
 }
+// close
+int bufev_socket_close(int sock) {
+    if(close((SOCKET) sock) == SOCKET_ERROR) {
+        return -1;
+    } else {
+        return 0;
+    }
+}
 
 struct bufferevent *bufferevent_new(int fd,
     evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg) {
@@ -98,6 +106,11 @@
     sa_bufferevent_free((struct sa_bufferevent *) bufev);
 }
 
+void bufferevent_close_and_free(struct bufferevent *bufev) {
+    bufev_socket_close((int) bufev->connection_handle);
+    bufferevent_free(bufev);
+}
+
 int bufferevent_write(struct bufferevent *bufev, void *data, size_t size) {
     return sa_bufferevent_write((struct sa_bufferevent *) bufev, data, size);
 }



More information about the tor-commits mailing list