Annotation of libwww/Library/src/HTBufWrt.c, revision 2.22

2.1       frystyk     1: /*
                      2: **     BUFFERED TRANSPORT WRITER STREAM
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.22    ! frystyk     6: **     @(#) $Id: HTBufWrt.c,v 2.21 1998/05/19 16:49:22 frystyk Exp $
2.1       frystyk     7: **
                      8: **     A buffered output stream. This stream lets you write characters to a
                      9: **     stream without causing a write every time.  The data is first written
                     10: **     into a buffer. Data is written to the actual stream only when the
                     11: **     buffer is full, or when the stream is flushed.
                     12: */
                     13: 
                     14: /* Library include files */
2.20      frystyk    15: #include "wwwsys.h"
2.1       frystyk    16: #include "WWWUtil.h"
                     17: #include "WWWCore.h"
                     18: #include "HTNetMan.h"
                     19: #include "HTWriter.h"
2.6       frystyk    20: #include "HTTimer.h"
2.1       frystyk    21: #include "HTBufWrt.h"                                   /* Implemented here */
                     22: 
                     23: struct _HTOutputStream {
                     24:     const HTOutputStreamClass *        isa;
                     25:     HTOutputStream *           target;          /* Target for outgoing data */
2.6       frystyk    26:     HTHost *                   host;
2.15      frystyk    27: 
                     28:     int                                allocated;          /* Allocated Buffer size */
                     29:     int                         growby;
2.16      frystyk    30:     int                                expo;
2.15      frystyk    31: 
2.1       frystyk    32:     char *                     read;                  /* Position in 'data' */
                     33:     char *                     data;                              /* buffer */
2.6       frystyk    34: 
2.7       frystyk    35:     ms_t                       lastFlushTime;  /* polar coordinates of the moon */
2.6       frystyk    36:     HTTimer *                  timer;
2.1       frystyk    37: };
                     38: 
                     39: #define PUTBLOCK(b,l) (*me->target->isa->put_block)(me->target,(b),(l))
                     40: 
                     41: /* ------------------------------------------------------------------------- */
                     42: 
2.11      frystyk    43: /*
                     44: **  This function is only called from either FlushEvent or HTBufferWriter_lazyFlush
                     45: **  which means that only the host object or timeout can cause a flush
                     46: */
2.1       frystyk    47: PRIVATE int HTBufferWriter_flush (HTOutputStream * me)
                     48: {
2.17      frystyk    49:     int status = HT_OK;
2.11      frystyk    50:     if (me && me->read > me->data) {
2.16      frystyk    51:        me->lastFlushTime = HTGetTimeInMillis();
                     52:         if ((status = PUTBLOCK(me->data, me->read - me->data))==HT_WOULD_BLOCK)
2.1       frystyk    53:            return HT_WOULD_BLOCK;
                     54:        me->read = me->data;
                     55:     }
                     56:     return status;
                     57: }
                     58: 
2.9       eric       59: PRIVATE int FlushEvent (HTTimer * timer, void * param, HTEventType type)
2.6       frystyk    60: {
                     61:     HTOutputStream * me = (HTOutputStream *) param;
2.19      frystyk    62:     if (timer != me->timer)
                     63:        HTDebugBreak(__FILE__, __LINE__, "Buffer Writer timer %p not in sync\n", timer);
2.17      frystyk    64:     if (PROT_TRACE) HTTrace("Buffer...... Timeout flushing %p with timer %p\n", me, timer);
2.11      frystyk    65: 
                     66:     /*
                     67:     **  We ignore the return code here which we shouldn't!!!
                     68:     */
2.6       frystyk    69:     HTBufferWriter_flush(me);
2.11      frystyk    70: 
                     71:     /*
                     72:     **  Delete the timer
                     73:     */
                     74:     me->timer = NULL;
2.6       frystyk    75:     return HT_OK;
                     76: }
                     77: 
                     78: PRIVATE int HTBufferWriter_lazyFlush (HTOutputStream * me)
                     79: {
                     80:     HTNet * net;
                     81:     int delay;
                     82: 
2.18      frystyk    83:     if (me->read <= me->data) {
2.6       frystyk    84:        return HT_OK;                   /* nothing to flush */
2.18      frystyk    85:     }
2.6       frystyk    86:     /*
                     87:     **  If we are allowed to delay the flush then set a timer with the
                     88:     **  delay descibed by our delay variable. If we can't delay then flush 
                     89:     **  right away.
                     90:     */
2.12      frystyk    91:     delay = HTHost_findWriteDelay(me->host, me->lastFlushTime, me->read - me->data);
2.6       frystyk    92: 
                     93:     /*
                     94:     ** Flush immediately
                     95:     */
2.11      frystyk    96:     if (!delay) {
                     97:        int status;
2.21      frystyk    98:        if (STREAM_TRACE) HTTrace("Buffer...... Flushing %p\n", me);
2.11      frystyk    99:        if ((status = HTBufferWriter_flush(me)) && me->timer) {
                    100:            HTTimer_delete(me->timer);
                    101:            me->timer = NULL;
                    102:        }
                    103:        return status;
                    104:     }
2.6       frystyk   105: 
                    106:     /*
2.11      frystyk   107:     ** Set a timer and tell the host we've done the write if
2.21      frystyk   108:     **  we have not already started a timer earlier. If a timer
                    109:     **  does already exist then make sure that it hasn't expired.
                    110:     **  This can be the case if we have a really slow client that
                    111:     **  can't parse the data fast enough.
2.6       frystyk   112:     */
2.11      frystyk   113:     if (!me->timer) {
                    114:        net = HTHost_getWriteNet(me->host);
2.21      frystyk   115:        me->timer = HTTimer_new(NULL, FlushEvent, me, delay, YES, NO);
2.11      frystyk   116:        HTHost_unregister(me->host, net, HTEvent_WRITE);
2.21      frystyk   117:        if (STREAM_TRACE) HTTrace("Buffer...... Waiting %dms on %p\n", delay, me);
                    118:     } else {
                    119:        if (HTTimer_hasTimerExpired(me->timer)) {
                    120:            if (STREAM_TRACE)
                    121:                HTTrace("Buffer...... Dispatching old timer %p\n", me->timer);
                    122:            HTTimer_dispatch(me->timer);
                    123:            me->timer = NULL;
                    124:        } else {
                    125:            if (STREAM_TRACE)
                    126:                HTTrace("Buffer...... Waiting on unexpired timer %p\n", me->timer);
                    127:        }
2.11      frystyk   128:     }
2.6       frystyk   129:     return HT_OK;
                    130: }
                    131: 
2.1       frystyk   132: PRIVATE int HTBufferWriter_free (HTOutputStream * me)
                    133: {
2.11      frystyk   134:     return HTBufferWriter_lazyFlush(me);
2.1       frystyk   135: }
                    136: 
2.15      frystyk   137: PRIVATE BOOL HTBufferWriter_addBuffer(HTOutputStream * me, int addthis)
                    138: {
                    139:     if (me) {
2.16      frystyk   140:         me->allocated += (addthis - addthis%me->growby + (me->growby*me->expo));
2.17      frystyk   141:        me->expo *= 2;
2.21      frystyk   142:        if (STREAM_TRACE) HTTrace("Buffer...... Increasing buffer to %d bytes\n", me->allocated);
2.15      frystyk   143:         if (me->data) {
                    144:             int size = me->read-me->data;
                    145:             if ((me->data = (char *) HT_REALLOC(me->data, me->allocated)) == NULL)
                    146:                 HT_OUTOFMEM("HTBufferWriter_addBuffer");
                    147:             me->read = me->data + size;
                    148:         } else {
                    149:             if ((me->data = (char *) HT_CALLOC(1, me->allocated)) == NULL)
                    150:                 HT_OUTOFMEM("HTBufferWriter_addBuffer");
                    151:             me->read = me->data;
                    152:         }
                    153:        return YES;
                    154:     }
                    155:     return NO;
                    156: }
                    157: 
2.1       frystyk   158: PRIVATE int HTBufferWriter_abort (HTOutputStream * me, HTList * e)
                    159: {
2.21      frystyk   160:     if (STREAM_TRACE) HTTrace("Buffer...... ABORTING...\n");
2.1       frystyk   161:     if (me->target) (*me->target->isa->abort)(me->target, e);
2.8       frystyk   162:     if (me->timer) {
                    163:        HTTimer_delete(me->timer);
                    164:        me->timer = NULL;
                    165:     }
2.1       frystyk   166:     return HT_ERROR;
                    167: }
                    168: 
2.6       frystyk   169: PRIVATE int HTBufferWriter_write (HTOutputStream * me, const char * buf, int len)
2.1       frystyk   170: {
2.6       frystyk   171:     HTNet * net = HTHost_getWriteNet(me->host);
2.1       frystyk   172:     int status;
2.16      frystyk   173:     while (1) {
                    174:        int available = me->data + me->allocated - me->read;
                    175: 
                    176:        /* If we have enough buffer space */
                    177:        if (len <= available) {
                    178:            int size = 0;
                    179:            memcpy(me->read, buf, len);
                    180:            me->read += len;
2.15      frystyk   181:         
2.16      frystyk   182:            /* If we have accumulated enough data then flush */
                    183:            if ((size = me->read - me->data) > me->growby) {
                    184:                me->lastFlushTime = HTGetTimeInMillis();
                    185:                status = PUTBLOCK(me->data, size);
                    186:                if (status == HT_OK) {
                    187:                    me->read = me->data;
                    188:                } else {
                    189:                    return (status == HT_WOULD_BLOCK) ? HT_OK : HT_ERROR;
                    190:                }
                    191:            }
                    192:            return HT_OK;
                    193:        } else {
                    194: 
                    195:            /* Fill the existing buffer (if not already) and flush */
                    196:            if (available) {
                    197:                memcpy(me->read, buf, available);
                    198:                buf += available;
                    199:                len -= available;
                    200:                me->read += available;
                    201:            }
                    202:            me->lastFlushTime = HTGetTimeInMillis();
                    203:            status = PUTBLOCK(me->data, me->allocated);
                    204:            if (status == HT_OK) {
                    205:                me->read = me->data;
                    206:            } else if (status == HT_WOULD_BLOCK) {
                    207:                HTBufferWriter_addBuffer(me, len);
                    208:                memcpy(me->read, buf, len);
                    209:                me->read += len;
                    210:                return HT_OK;
                    211:            }
                    212:        }
2.15      frystyk   213:     }
2.1       frystyk   214: }
                    215: 
                    216: /*     Character handling
                    217: **     ------------------
                    218: */
                    219: PRIVATE int HTBufferWriter_put_character (HTOutputStream * me, char c)
                    220: {
                    221:     return HTBufferWriter_write(me, &c, 1);
                    222: }
                    223: 
                    224: /*     String handling
                    225: **     ---------------
                    226: **
                    227: **     Strings must be smaller than this buffer size.
                    228: */
                    229: PRIVATE int HTBufferWriter_put_string (HTOutputStream * me, const char * s)
                    230: {
                    231:     return HTBufferWriter_write(me, s, (int) strlen(s));
                    232: }
                    233: /*
                    234: **     The difference between the close and the free method is that we don't
                    235: **     close the connection in the free method - we only call the free method
                    236: **     of the target stream. That way, we can keep the output stream as long 
                    237: **     as the channel itself.
                    238: */
                    239: PRIVATE int HTBufferWriter_close (HTOutputStream * me)
                    240: {
                    241:     if (me) {
                    242:        if (me->target) (*me->target->isa->close)(me->target);
                    243:        HT_FREE(me->data);
                    244:        HT_FREE(me);
                    245:     }
                    246:     return HT_OK;
                    247: }
                    248: 
                    249: PRIVATE const HTOutputStreamClass HTBufferWriter =
                    250: {              
2.6       frystyk   251:     "BufferedSocketWriter",
                    252:     HTBufferWriter_lazyFlush,
2.1       frystyk   253:     HTBufferWriter_free,
                    254:     HTBufferWriter_abort,
                    255:     HTBufferWriter_put_character,
                    256:     HTBufferWriter_put_string,
                    257:     HTBufferWriter_write,
                    258:     HTBufferWriter_close
                    259: }; 
                    260: 
2.6       frystyk   261: PUBLIC HTOutputStream * HTBufferWriter_new (HTHost * host, HTChannel * ch,
2.1       frystyk   262:                                            void * param, int bufsize)
                    263: {
2.6       frystyk   264:     if (host && ch) {
2.1       frystyk   265:        HTOutputStream * me = HTChannel_output(ch);
2.6       frystyk   266:        if (!me) {
                    267:            HTOutputStream * me;
2.16      frystyk   268:            int tcpbufsize = 0;
                    269: 
                    270: #if defined(HAVE_GETSOCKOPT) && defined(SO_SNDBUF)
                    271:            /*
                    272:            ** Get the TCP socket buffer size
                    273:            */
                    274:            {
                    275:                SOCKET sockfd = HTChannel_socket(ch);
                    276:                int size = sizeof(int);
                    277:                int status = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
                    278:                                        (void *) &tcpbufsize, &size);
                    279:                if (status == -1) {
2.21      frystyk   280:                    if (STREAM_TRACE)
2.16      frystyk   281:                        HTTrace("Socket...... Could not get TCP send buffer size for socket %d\n", sockfd);
                    282:                } else {
2.21      frystyk   283:                    if (STREAM_TRACE)               
2.16      frystyk   284:                        HTTrace("Socket...... TCP send buffer size is %d for socket %d\n", tcpbufsize, sockfd);
                    285:                }               
                    286:            }
                    287: #endif
2.17      frystyk   288:            if (bufsize <= 0) bufsize = tcpbufsize ? tcpbufsize : OUTPUT_BUFFER_SIZE;
2.6       frystyk   289:            if ((me = (HTOutputStream *) HT_CALLOC(1, sizeof(HTOutputStream)))==NULL ||
2.1       frystyk   290:                (me->data = (char *) HT_MALLOC(bufsize)) == NULL)
                    291:                HT_OUTOFMEM("HTBufferWriter_new");
                    292:            me->isa = &HTBufferWriter;
                    293:            me->read = me->data;
2.15      frystyk   294:            me->allocated = bufsize;
                    295:             me->growby = bufsize;
2.16      frystyk   296:            me->expo = 1;
2.6       frystyk   297:            me->target = HTWriter_new(host, ch, param, 0);
                    298:            me->host = host;
2.15      frystyk   299:            return me;
2.6       frystyk   300:        }
2.1       frystyk   301:     }
                    302:     return NULL;
                    303: }

Webmaster