--------------------- PatchSet 5214 Date: 2002/10/04 09:47:41 Author: adri Branch: commloops Tag: (none) Log: Add in support for comm_fill - filling a storeiobuffer untested, and I need to whittle away the IOFCB function decl where StoreIOBuffer is already going to be defined. Members: src/comm.c:1.21.4.14->1.21.4.15 src/protos.h:1.49.4.10->1.49.4.11 Index: squid/src/comm.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/comm.c,v retrieving revision 1.21.4.14 retrieving revision 1.21.4.15 diff -u -r1.21.4.14 -r1.21.4.15 --- squid/src/comm.c 4 Oct 2002 08:25:03 -0000 1.21.4.14 +++ squid/src/comm.c 4 Oct 2002 09:47:42 -0000 1.21.4.15 @@ -1,6 +1,6 @@ /* - * $Id: comm.c,v 1.21.4.14 2002/10/04 08:25:03 adri Exp $ + * $Id: comm.c,v 1.21.4.15 2002/10/04 09:47:42 adri Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -34,6 +34,7 @@ */ #include "squid.h" +#include "StoreIOBuffer.h" #if defined(_SQUID_CYGWIN_) #include @@ -74,6 +75,8 @@ static int commRetryConnect(ConnectStateData * cs); CBDATA_TYPE(ConnectStateData); +typedef void IOFCB(int fd, StoreIOBuffer sb, size_t readlen, comm_err_t flag, int xerrno, void *data); + struct _fdc_t { int active; dlink_list CommCallbackList; @@ -89,13 +92,20 @@ IOACB *handler; void *handler_data; } accept; + struct { + StoreIOBuffer sb; + IOFCB *handler; + void *handler_data; + } fill; + }; typedef struct _fdc_t fdc_t; typedef enum { COMM_CB_READ = 1, COMM_CB_WRITE, - COMM_CB_ACCEPT + COMM_CB_ACCEPT, + COMM_CB_FILL } comm_callback_t; struct _CommCallbackData { @@ -109,6 +119,7 @@ union { IOCB *r_callback; IOACB *a_callback; + IOFCB *f_callback; } c; void *callback_data; comm_err_t errcode; @@ -116,6 +127,7 @@ int seqnum; struct sockaddr_in me; struct sockaddr_in pn; + StoreIOBuffer sb; }; typedef struct _CommCallbackData CommCallbackData; @@ -212,6 +224,38 @@ } +static void +comm_add_fill_callback(int fd, StoreIOBuffer sb, size_t retval, comm_err_t errcode, int xerrno, + IOFCB *callback, void *callback_data) +{ + CommCallbackData *cio; + + assert(fdc_table[fd].active == 1); + + /* Allocate a new struct */ + cio = memPoolAlloc(comm_callback_pool); + + /* Throw our data into it */ + cio->fd = fd; + cio->xerrno = xerrno; + cio->errcode = errcode; + cio->c.f_callback = callback; + cio->callback_data = callback_data; + cio->seqnum = CommCallbackSeqnum; + cio->type = COMM_CB_FILL; + cio->retval = retval; + cio->sb = sb; + + /* Add it to the end of the list */ + dlinkAddTail(cio, &(cio->h_node), &CommCallbackList); + + /* and add it to the end of the fd list */ + dlinkAddTail(cio, &(cio->fd_node), &(fdc_table[fd].CommCallbackList)); + +} + + + static void comm_call_io_callback(CommCallbackData *cio) @@ -228,6 +272,9 @@ cio->c.a_callback(cio->fd, cio->newfd, &cio->me, &cio->pn, cio->errcode, cio->xerrno, cio->callback_data); break; + case COMM_CB_FILL: + cio->c.f_callback(cio->fd, cio->sb, cio->retval, cio->errcode, + cio->xerrno, cio->callback_data); default: fatal("unknown comm io callback type!"); break; @@ -349,6 +396,51 @@ } +static void +comm_fill_read(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data) +{ + assert(fdc_table[fd].active == 1); + StoreIOBuffer sb; + + sb = fdc_table[fd].fill.sb; + if (flag != COMM_OK) { + /* Error! */ + comm_add_fill_callback(fd, sb, -1, flag, xerrno, fdc_table[fd].fill.handler, + fdc_table[fd].fill.handler_data); + fdc_table[fd].fill.handler = fdc_table[fd].fill.handler_data = NULL; + return; + } + /* flag is COMM_OK */ + /* We handle EOFs as read lengths of 0! Its eww, but its consistent */ + sb.offset += len; + assert(sb.offset < sb.length); + comm_add_fill_callback(fd, sb, len, COMM_OK, 0, fdc_table[fd].fill.handler, + fdc_table[fd].fill.handler_data); + fdc_table[fd].fill.handler = fdc_table[fd].fill.handler_data = NULL; +} + + +/* + * Try filling a StoreIOBuffer with some data, and call a callback when successful + */ +void +comm_fill(int fd, StoreIOBuffer sb, IOFCB *callback, void *data) +{ + assert(fdc_table[fd].fill.handler == NULL); + /* If we have data, schedule a callback immediately */ + if (sb.offset > 0) { + comm_add_fill_callback(fd, sb, sb.offset, COMM_OK, 0, callback, data); + return; + } + + /* If we don't have any data, record details and schedule a read */ + fdc_table[fd].fill.handler = callback; + fdc_table[fd].fill.handler_data = data; + fdc_table[fd].fill.sb = sb; + + comm_read(fd, sb.data, sb.length, comm_fill_read, NULL); +} + /* Older stuff */ Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/protos.h,v retrieving revision 1.49.4.10 retrieving revision 1.49.4.11 diff -u -r1.49.4.10 -r1.49.4.11 --- squid/src/protos.h 3 Oct 2002 12:36:39 -0000 1.49.4.10 +++ squid/src/protos.h 4 Oct 2002 09:47:41 -0000 1.49.4.11 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.49.4.10 2002/10/03 12:36:39 adri Exp $ + * $Id: protos.h,v 1.49.4.11 2002/10/04 09:47:41 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -155,6 +155,7 @@ extern int comm_existsiocallback(void); extern void comm_calliocallback(void); extern void comm_read(int fd, char *buf, int len, IOCB *handler, void *data); +extern void comm_fill(int fd, StoreIOBuffer sb, IOFCB *callback, void *data) extern void comm_accept(int fd, IOACB *handler, void *handler_data); extern int comm_listen(int fd);