---------------------
PatchSet 1651 
Date: 2001/02/25 01:15:10
Author: hno
Branch: eventio
Tag: (none) 
Log:
Starting to debug ncomm implementation

Members: 
	doc/Programming-Guide/prog-guide.sgml:1.9.8.6->1.9.8.7 
	src/MemBuf.c:1.4.12.1->1.4.12.2 
	src/cbdata.c:1.9.8.2->1.9.8.3 
	src/ncomm.c:1.1.2.6->1.1.2.7 
	src/ncomm_poll.c:1.1.2.2->1.1.2.3 
	src/ncomm_test.c:1.1.2.1->1.1.2.2 
	src/protos.h:1.18.8.7->1.18.8.8 
	src/typedefs.h:1.15.8.5->1.15.8.6 

Index: squid/doc/Programming-Guide/prog-guide.sgml
===================================================================
RCS file: /cvsroot/squid-sf//squid/doc/Programming-Guide/prog-guide.sgml,v
retrieving revision 1.9.8.6
retrieving revision 1.9.8.7
diff -u -r1.9.8.6 -r1.9.8.7
--- squid/doc/Programming-Guide/prog-guide.sgml	24 Feb 2001 19:25:43 -0000	1.9.8.6
+++ squid/doc/Programming-Guide/prog-guide.sgml	25 Feb 2001 01:15:10 -0000	1.9.8.7
@@ -2,7 +2,7 @@
 <article>
 <title>Squid Programmers Guide</title>
 <author>Duane Wessels, Squid Developers
-<date>$Id: prog-guide.sgml,v 1.9.8.6 2001/02/24 19:25:43 hno Exp $</date>
+<date>$Id: prog-guide.sgml,v 1.9.8.7 2001/02/25 01:15:10 hno Exp $</date>
 
 <abstract>
 Squid is a WWW Cache application developed by the National Laboratory
@@ -3213,7 +3213,7 @@
 
 <p><verb>
 	typedef void
-	COMMNEWCB(filehandle *fh, struct sockaddr *local,
+	COMMNEWCB(filehandle *fh, int error, struct sockaddr *local,
 		  struct sockaddr *peer, void *cbdata);
 </verb>
 
@@ -3221,6 +3221,9 @@
 	Called when a new filehandle is created (ncomm_listen / ncomm_accept
 	/ ncomm_connect)
 
+<p>
+	On error, error is set to the appropriate errno.
+
 <sect2>COMMIOCB
 
 <p><verb>
Index: squid/src/MemBuf.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/MemBuf.c,v
retrieving revision 1.4.12.1
retrieving revision 1.4.12.2
diff -u -r1.4.12.1 -r1.4.12.2
--- squid/src/MemBuf.c	24 Feb 2001 12:52:42 -0000	1.4.12.1
+++ squid/src/MemBuf.c	25 Feb 2001 01:15:32 -0000	1.4.12.2
@@ -1,6 +1,6 @@
 
 /*
- * $Id: MemBuf.c,v 1.4.12.1 2001/02/24 12:52:42 hno Exp $
+ * $Id: MemBuf.c,v 1.4.12.2 2001/02/25 01:15:32 hno Exp $
  *
  * DEBUG: section 59    auto-growing Memory Buffer with printf
  * AUTHOR: Alex Rousskov
@@ -375,9 +375,11 @@
 	}
     }
 
-    /* copy old buffer */
-    xmemcpy(*mb->bufp, *old_bufp, mb->size);
-    cbdataUnreference(old_bufp);
+    if (old_bufp) {
+	/* copy old buffer */
+	xmemcpy(*mb->bufp, *old_bufp, mb->size);
+	cbdataUnreference(old_bufp);
+    }
 
     /* init tail, just in case */
     memset(*mb->bufp + mb->size, 0, new_cap - mb->size);
Index: squid/src/cbdata.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/cbdata.c,v
retrieving revision 1.9.8.2
retrieving revision 1.9.8.3
diff -u -r1.9.8.2 -r1.9.8.3
--- squid/src/cbdata.c	24 Feb 2001 23:48:43 -0000	1.9.8.2
+++ squid/src/cbdata.c	25 Feb 2001 01:15:32 -0000	1.9.8.3
@@ -1,6 +1,6 @@
 
 /*
- * $Id: cbdata.c,v 1.9.8.2 2001/02/24 23:48:43 hno Exp $
+ * $Id: cbdata.c,v 1.9.8.3 2001/02/25 01:15:32 hno Exp $
  *
  * DEBUG: section 45    Callback Data Registry
  * ORIGINAL AUTHOR: Duane Wessels
@@ -175,8 +175,9 @@
 {
     cbdata *c;
     FREE *free_func;
+    if (!p)
+	return;
     debug(45, 3) ("cbdataFree: %p\n", p);
-    assert(p);
     c = (cbdata *) (((char *) p) - OFFSET_OF(cbdata, data));
     assert(c->y == c);
     c->valid = 0;
Index: squid/src/ncomm.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm.c,v
retrieving revision 1.1.2.6
retrieving revision 1.1.2.7
diff -u -r1.1.2.6 -r1.1.2.7
--- squid/src/ncomm.c	24 Feb 2001 23:48:43 -0000	1.1.2.6
+++ squid/src/ncomm.c	25 Feb 2001 01:15:32 -0000	1.1.2.7
@@ -48,12 +48,12 @@
 IOBuf *
 IOBufAlloc(size_t size)
 {
-    IOBuf *buf;
+    IOBuf *buf, *bufp;
     buf = cbdataAlloc(IOBuf);
     memBufInit(buf, size, size);
-    cbdataLock(buf);
+    bufp = cbdataReference(buf);
     cbdataFree(buf);
-    return buf;
+    return bufp;
 }
 
 IOBuf *
@@ -70,7 +70,7 @@
     IOBuf *buf;
     buf = cbdataAlloc(IOBuf);
     *buf = *mb;
-    cbdataLock(buf->bufp);
+    mb->bufp = NULL;
     return buf;
 }
 static int
@@ -143,13 +143,17 @@
     comm_callback_entry_t *cb;
     while((cb = dlinkGetFirst(&callbacklist)) != NULL) {
 	int valid = cbdataValid(cb->cbdata);
+	filehandle *fh = cb->fh;
 	switch(cb->type) {
 	case COMMCB_io:
 	    /* NOT YET DONE */
 	    if (valid)
-		cb->cb.io.callback(cb->fh, cb->cb.io.buf, cb->cb.io.offset, cb->cb.io.len, cb->error, cb->cbdata);
+		cb->cb.io.callback(fh, cb->cb.io.buf, cb->cb.io.offset, cb->cb.io.len, cb->error, cb->cbdata);
+	    cbdataUnreference(cb->cb.io.buf);
 	    break;
 	case COMMCB_new:
+	    if (valid)
+		cb->cb.new.callback(fh, cb->error, &fh->local, &fh->peer, cb->cbdata);
 	    /* NOT YET DONE */
 	    break;
 	case COMMCB_close:
@@ -220,6 +224,9 @@
     fh_read_t *fhr = &fh->read;
     int done;
 
+    if (size > 65536)
+	size = 65536; /* Do not overflow buffer in comm_do_read */
+
     /* Read in some data */
     fhr->size = size;
     fhr->callback = callback;
@@ -320,27 +327,14 @@
 {
     int flags;
     int dummy = 0;
-#ifdef _SQUID_CYGWIN_
-    int nonblocking = TRUE;
-    if (fd_table[fd].type != FD_PIPE) {
-	if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
-	    debug(50, 0) ("commSetNonBlocking: FD %d: %s %D\n", fd, xstrerror(), fd_table[fd].type);
-	    return COMM_ERROR;
-	}
-    } else {
-#endif
-	if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
-	    debug(50, 0) ("FD %d: fcntl F_GETFL: %s\n", fd, xstrerror());
-	    return COMM_ERROR;
-	}
-	if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
-	    debug(50, 0) ("commSetNonBlocking: FD %d: %s\n", fd, xstrerror());
-	    return COMM_ERROR;
-	}
-#ifdef _SQUID_CYGWIN_
+    if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
+	debug(50, 0) ("FD %d: fcntl F_GETFL: %s\n", fd, xstrerror());
+	return COMM_ERROR;
+    }
+    if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
+	debug(50, 0) ("commSetNonBlocking: FD %d: %s\n", fd, xstrerror());
+	return COMM_ERROR;
     }
-#endif
-    fd_table[fd].flags.nonblocking = 1;
     return 0;
 }
 
@@ -403,11 +397,13 @@
 {
     filehandle *fh;
     int fd, rc;
+    int on = 1;
 
     fd = socket(where->sa_family, sock_type, proto);
     statCounter.syscalls.sock.sockets++;
     if (fd < 0)
 	return NULL;
+    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));
     rc = bind(fd, where, addrsize);
     statCounter.syscalls.sock.binds++;
     if (rc < 0) {
Index: squid/src/ncomm_poll.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm_poll.c,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- squid/src/ncomm_poll.c	24 Feb 2001 23:48:43 -0000	1.1.2.2
+++ squid/src/ncomm_poll.c	25 Feb 2001 01:15:32 -0000	1.1.2.3
@@ -14,6 +14,7 @@
 {
     if (!pollfds[poll_index].events) {
 	pollfh[poll_index]->eventdata = NULL;
+	cbdataUnreference(pollfh[poll_index]);
 	pollfds[poll_index].fd = -1;
 	if (poll_index < pollfirstfree)
 	    pollfirstfree = poll_index;
@@ -32,6 +33,7 @@
 
     if (done) {
 	pollfds[poll_index].events &= ~POLLIN;
+	fh->read_handler = NULL;
 	cleanup_poll(fd, (unsigned int)poll_index);
     }
 }
@@ -44,6 +46,7 @@
 
     if (done) {
 	pollfds[poll_index].events &= ~POLLOUT;
+	fh->write_handler = NULL;
 	cleanup_poll(fd, (unsigned int)poll_index);
     }
 }
@@ -56,7 +59,7 @@
     struct pollfd *pollfd;
     int fds = poll(pollfds+1 /* skip position 0 */, pollnfds, timeout);
     int callback_limit = CALLBACK_MAGIC;
-    for (poll_index=0, pollfd = pollfds; poll_index < (int)pollnfds && fds > 0; poll_index++, pollfd++) {
+    for (poll_index=1, pollfd = pollfds+1; poll_index <= (int)pollnfds && fds > 0; poll_index++, pollfd++) {
 	short revents = pollfd->revents;
 	if(revents) {
 	    int fd = pollfd->fd;
@@ -112,7 +115,7 @@
 	pollfirstfree = poll_index + 1;
 	pollfd = &pollfds[poll_index];
 	pollfd->fd = fd;
-	pollfh[poll_index] = fh;
+	pollfh[poll_index] = cbdataReference(fh);
 	fh->eventdata = pollfd;
     }
     assert(pollfd->fd == fd);
Index: squid/src/ncomm_test.c
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/Attic/ncomm_test.c,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- squid/src/ncomm_test.c	24 Feb 2001 23:48:43 -0000	1.1.2.1
+++ squid/src/ncomm_test.c	25 Feb 2001 01:15:32 -0000	1.1.2.2
@@ -11,7 +11,34 @@
     abort();
 }
 
+void
+got_data(filehandle *fh, IOBuf *buf, int offset, int len, int error, void *data)
+{
+    printf("Got %d bytes\n", len);
+    ncomm_read(fh, 65536, got_data, NULL);
+}
+void
+new_connection(filehandle *fh, int error, struct sockaddr *local, struct sockaddr *remote, void *cbdata)
+{
+    printf("New connection!\n");
+    ncomm_read(fh, 65536, got_data, NULL);
+}
+
 int main(int argc, char **argv)
 {
+    struct sockaddr_in me;
+    filehandle *lfh;
+    cbdataInit();
+    memInitModule();
+    memBuf_module_init();
+    ncomm_module_init();
+
+    memset(&me, 0, sizeof(me));
+    me.sin_family = AF_INET;
+    me.sin_port = htons(45678);
+    lfh = ncomm_listen(SOCK_STREAM, 0, (struct sockaddr *)&me, sizeof(me), 8192, new_connection, NULL);
+    while(1)
+	ncomm_handle_events(1000);
+
     return 0;
 }
Index: squid/src/protos.h
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/protos.h,v
retrieving revision 1.18.8.7
retrieving revision 1.18.8.8
diff -u -r1.18.8.7 -r1.18.8.8
--- squid/src/protos.h	24 Feb 2001 23:48:43 -0000	1.18.8.7
+++ squid/src/protos.h	25 Feb 2001 01:15:32 -0000	1.18.8.8
@@ -1,6 +1,6 @@
 
 /*
- * $Id: protos.h,v 1.18.8.7 2001/02/24 23:48:43 hno Exp $
+ * $Id: protos.h,v 1.18.8.8 2001/02/25 01:15:32 hno Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -1304,6 +1304,7 @@
  */
 void ncomm_module_init(void);
 void ncomm_module_shutdown(void);
+void ncomm_handle_events(int timeout);
 int ncomm_close(filehandle *fh);
 int ncomm_abort(filehandle *fh);
 filehandle * ncomm_listen(int sock_type, int proto, struct sockaddr *where, int addrsize, int backlog, COMMNEWCB *callback, void *cbdata);
Index: squid/src/typedefs.h
===================================================================
RCS file: /cvsroot/squid-sf//squid/src/typedefs.h,v
retrieving revision 1.15.8.5
retrieving revision 1.15.8.6
diff -u -r1.15.8.5 -r1.15.8.6
--- squid/src/typedefs.h	24 Feb 2001 23:48:43 -0000	1.15.8.5
+++ squid/src/typedefs.h	25 Feb 2001 01:15:32 -0000	1.15.8.6
@@ -1,6 +1,6 @@
 
 /*
- * $Id: typedefs.h,v 1.15.8.5 2001/02/24 23:48:43 hno Exp $
+ * $Id: typedefs.h,v 1.15.8.6 2001/02/25 01:15:32 hno Exp $
  *
  *
  * SQUID Web Proxy Cache          http://www.squid-cache.org/
@@ -357,7 +357,7 @@
  * ncomm.c 
  */
 typedef MemBuf IOBuf;
-typedef void COMMNEWCB(filehandle *fh, struct sockaddr *local, struct sockaddr *peer, void *cbdata);
+typedef void COMMNEWCB(filehandle *fh, int error, struct sockaddr *local, struct sockaddr *peer, void *cbdata);
 typedef void COMMIOCB(filehandle *fh, IOBuf *buf, int offset, int size, int error, void *cbdata);
 typedef void COMMCLOSECB(filehandle *fh, void *cbdata);
 
