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

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.18    ! frystyk     6: **     @(#) $Id: HTBufWrt.c,v 2.17 1998/01/04 16:53:54 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 */
                     15: #include "sysdep.h"
                     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;
                     62:     if (timer != me->timer) HTDebugBreak();
2.17      frystyk    63:     if (PROT_TRACE) HTTrace("Buffer...... Timeout flushing %p with timer %p\n", me, timer);
2.11      frystyk    64: 
                     65:     /*
                     66:     **  We ignore the return code here which we shouldn't!!!
                     67:     */
2.6       frystyk    68:     HTBufferWriter_flush(me);
2.11      frystyk    69: 
                     70:     /*
                     71:     **  Delete the timer
                     72:     */
                     73:     me->timer = NULL;
2.6       frystyk    74:     return HT_OK;
                     75: }
                     76: 
                     77: PRIVATE int HTBufferWriter_lazyFlush (HTOutputStream * me)
                     78: {
                     79:     HTNet * net;
                     80:     int delay;
                     81: 
2.18    ! frystyk    82:     if (me->read <= me->data) {
        !            83: #if 0
        !            84:        fprintf(stderr, "nothing to flush\n");
        !            85: #endif
2.6       frystyk    86:        return HT_OK;                   /* nothing to flush */
2.18    ! frystyk    87:     }
2.6       frystyk    88:     /*
                     89:     **  If we are allowed to delay the flush then set a timer with the
                     90:     **  delay descibed by our delay variable. If we can't delay then flush 
                     91:     **  right away.
                     92:     */
2.12      frystyk    93:     delay = HTHost_findWriteDelay(me->host, me->lastFlushTime, me->read - me->data);
2.6       frystyk    94: 
                     95:     /*
                     96:     ** Flush immediately
                     97:     */
2.11      frystyk    98:     if (!delay) {
                     99:        int status;
2.17      frystyk   100:        if (PROT_TRACE) HTTrace("Buffer...... Flushing %p\n", me);
2.11      frystyk   101:        if ((status = HTBufferWriter_flush(me)) && me->timer) {
                    102:            HTTimer_delete(me->timer);
                    103:            me->timer = NULL;
                    104:        }
                    105:        return status;
                    106:     }
2.6       frystyk   107: 
                    108:     /*
2.11      frystyk   109:     ** Set a timer and tell the host we've done the write if
                    110:     **  we have not already started a timer earlier.
2.6       frystyk   111:     */
2.11      frystyk   112:     if (!me->timer) {
2.14      frystyk   113:        ms_t exp = HTGetTimeInMillis() + delay;
2.11      frystyk   114:        net = HTHost_getWriteNet(me->host);
2.14      frystyk   115:        me->timer = HTTimer_new(NULL, FlushEvent, me, exp, NO);
2.11      frystyk   116:        HTHost_unregister(me->host, net, HTEvent_WRITE);
2.17      frystyk   117:        if (PROT_TRACE) HTTrace("Buffer...... Waiting %p\n", me);
2.11      frystyk   118:     }
2.6       frystyk   119:     return HT_OK;
                    120: }
                    121: 
2.1       frystyk   122: PRIVATE int HTBufferWriter_free (HTOutputStream * me)
                    123: {
2.11      frystyk   124:     return HTBufferWriter_lazyFlush(me);
2.1       frystyk   125: }
                    126: 
2.15      frystyk   127: PRIVATE BOOL HTBufferWriter_addBuffer(HTOutputStream * me, int addthis)
                    128: {
                    129:     if (me) {
2.16      frystyk   130:         me->allocated += (addthis - addthis%me->growby + (me->growby*me->expo));
2.17      frystyk   131:        me->expo *= 2;
2.16      frystyk   132:        if (PROT_TRACE) HTTrace("Buffer...... Increasing buffer to %d bytes\n", me->allocated);
2.15      frystyk   133:         if (me->data) {
                    134:             int size = me->read-me->data;
                    135:             if ((me->data = (char *) HT_REALLOC(me->data, me->allocated)) == NULL)
                    136:                 HT_OUTOFMEM("HTBufferWriter_addBuffer");
                    137:             me->read = me->data + size;
                    138:         } else {
                    139:             if ((me->data = (char *) HT_CALLOC(1, me->allocated)) == NULL)
                    140:                 HT_OUTOFMEM("HTBufferWriter_addBuffer");
                    141:             me->read = me->data;
                    142:         }
                    143:        return YES;
                    144:     }
                    145:     return NO;
                    146: }
                    147: 
2.1       frystyk   148: PRIVATE int HTBufferWriter_abort (HTOutputStream * me, HTList * e)
                    149: {
2.8       frystyk   150:     if (PROT_TRACE) HTTrace("Buffer...... ABORTING...\n");
2.1       frystyk   151:     if (me->target) (*me->target->isa->abort)(me->target, e);
2.8       frystyk   152:     if (me->timer) {
                    153:        HTTimer_delete(me->timer);
                    154:        me->timer = NULL;
                    155:     }
2.1       frystyk   156:     return HT_ERROR;
                    157: }
                    158: 
2.6       frystyk   159: PRIVATE int HTBufferWriter_write (HTOutputStream * me, const char * buf, int len)
2.1       frystyk   160: {
2.6       frystyk   161:     HTNet * net = HTHost_getWriteNet(me->host);
2.1       frystyk   162:     int status;
2.16      frystyk   163:     while (1) {
                    164:        int available = me->data + me->allocated - me->read;
                    165: 
                    166:        /* If we have enough buffer space */
                    167:        if (len <= available) {
                    168:            int size = 0;
                    169:            memcpy(me->read, buf, len);
                    170:            me->read += len;
2.15      frystyk   171:         
2.16      frystyk   172:            /* If we have accumulated enough data then flush */
                    173:            if ((size = me->read - me->data) > me->growby) {
                    174:                me->lastFlushTime = HTGetTimeInMillis();
                    175:                status = PUTBLOCK(me->data, size);
                    176:                if (status == HT_OK) {
                    177:                    HTNet_addBytesWritten(net, size);
                    178:                    me->read = me->data;
                    179:                } else {
                    180:                    return (status == HT_WOULD_BLOCK) ? HT_OK : HT_ERROR;
                    181:                }
                    182:            }
                    183:            return HT_OK;
                    184:        } else {
                    185: 
                    186:            /* Fill the existing buffer (if not already) and flush */
                    187:            if (available) {
                    188:                memcpy(me->read, buf, available);
                    189:                buf += available;
                    190:                len -= available;
                    191:                me->read += available;
                    192:            }
                    193:            me->lastFlushTime = HTGetTimeInMillis();
                    194:            status = PUTBLOCK(me->data, me->allocated);
                    195:            if (status == HT_OK) {
                    196:                HTNet_addBytesWritten(net, me->allocated);
                    197:                me->read = me->data;
                    198:            } else if (status == HT_WOULD_BLOCK) {
                    199:                HTBufferWriter_addBuffer(me, len);
                    200:                memcpy(me->read, buf, len);
                    201:                me->read += len;
                    202:                return HT_OK;
                    203:            }
                    204:        }
2.15      frystyk   205:     }
2.1       frystyk   206: }
                    207: 
                    208: /*     Character handling
                    209: **     ------------------
                    210: */
                    211: PRIVATE int HTBufferWriter_put_character (HTOutputStream * me, char c)
                    212: {
                    213:     return HTBufferWriter_write(me, &c, 1);
                    214: }
                    215: 
                    216: /*     String handling
                    217: **     ---------------
                    218: **
                    219: **     Strings must be smaller than this buffer size.
                    220: */
                    221: PRIVATE int HTBufferWriter_put_string (HTOutputStream * me, const char * s)
                    222: {
                    223:     return HTBufferWriter_write(me, s, (int) strlen(s));
                    224: }
                    225: /*
                    226: **     The difference between the close and the free method is that we don't
                    227: **     close the connection in the free method - we only call the free method
                    228: **     of the target stream. That way, we can keep the output stream as long 
                    229: **     as the channel itself.
                    230: */
                    231: PRIVATE int HTBufferWriter_close (HTOutputStream * me)
                    232: {
                    233:     if (me) {
                    234:        if (me->target) (*me->target->isa->close)(me->target);
                    235:        HT_FREE(me->data);
                    236:        HT_FREE(me);
                    237:     }
                    238:     return HT_OK;
                    239: }
                    240: 
                    241: PRIVATE const HTOutputStreamClass HTBufferWriter =
                    242: {              
2.6       frystyk   243:     "BufferedSocketWriter",
                    244:     HTBufferWriter_lazyFlush,
2.1       frystyk   245:     HTBufferWriter_free,
                    246:     HTBufferWriter_abort,
                    247:     HTBufferWriter_put_character,
                    248:     HTBufferWriter_put_string,
                    249:     HTBufferWriter_write,
                    250:     HTBufferWriter_close
                    251: }; 
                    252: 
2.6       frystyk   253: PUBLIC HTOutputStream * HTBufferWriter_new (HTHost * host, HTChannel * ch,
2.1       frystyk   254:                                            void * param, int bufsize)
                    255: {
2.6       frystyk   256:     if (host && ch) {
2.1       frystyk   257:        HTOutputStream * me = HTChannel_output(ch);
2.6       frystyk   258:        if (!me) {
                    259:            HTOutputStream * me;
2.16      frystyk   260:            int tcpbufsize = 0;
                    261: 
                    262: #if defined(HAVE_GETSOCKOPT) && defined(SO_SNDBUF)
                    263:            /*
                    264:            ** Get the TCP socket buffer size
                    265:            */
                    266:            {
                    267:                SOCKET sockfd = HTChannel_socket(ch);
                    268:                int size = sizeof(int);
                    269:                int status = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
                    270:                                        (void *) &tcpbufsize, &size);
                    271:                if (status == -1) {
                    272:                    if (PROT_TRACE)
                    273:                        HTTrace("Socket...... Could not get TCP send buffer size for socket %d\n", sockfd);
                    274:                } else {
                    275:                    if (PROT_TRACE)                 
                    276:                        HTTrace("Socket...... TCP send buffer size is %d for socket %d\n", tcpbufsize, sockfd);
                    277:                }               
                    278:            }
                    279: #endif
2.17      frystyk   280:            if (bufsize <= 0) bufsize = tcpbufsize ? tcpbufsize : OUTPUT_BUFFER_SIZE;
2.6       frystyk   281:            if ((me = (HTOutputStream *) HT_CALLOC(1, sizeof(HTOutputStream)))==NULL ||
2.1       frystyk   282:                (me->data = (char *) HT_MALLOC(bufsize)) == NULL)
                    283:                HT_OUTOFMEM("HTBufferWriter_new");
                    284:            me->isa = &HTBufferWriter;
                    285:            me->read = me->data;
2.15      frystyk   286:            me->allocated = bufsize;
                    287:             me->growby = bufsize;
2.16      frystyk   288:            me->expo = 1;
2.6       frystyk   289:            me->target = HTWriter_new(host, ch, param, 0);
                    290:            me->host = host;
2.15      frystyk   291:            return me;
2.6       frystyk   292:        }
2.1       frystyk   293:     }
                    294:     return NULL;
                    295: }

Webmaster