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

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.9     ! eric        6: **     @(#) $Id: HTBufWrt.c,v 2.8 1996/12/07 15:55:43 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.1       frystyk    27:     int                                size;                         /* Buffer size */
2.3       frystyk    28:     int                                bb;
2.1       frystyk    29:     char *                     block;
                     30:     char *                     read;                  /* Position in 'data' */
                     31:     char *                     data;                              /* buffer */
2.6       frystyk    32: 
                     33:     BOOL                       delaying;
2.7       frystyk    34:     ms_t                       lastFlushTime;  /* polar coordinates of the moon */
2.6       frystyk    35:     HTTimer *                  timer;
2.1       frystyk    36: };
                     37: 
                     38: #define PUTBLOCK(b,l) (*me->target->isa->put_block)(me->target,(b),(l))
                     39: 
                     40: /* ------------------------------------------------------------------------- */
                     41: 
                     42: PRIVATE int HTBufferWriter_flush (HTOutputStream * me)
                     43: {
                     44:     int status = HT_OK;
2.6       frystyk    45:     if (me == NULL) return HT_ERROR;
2.1       frystyk    46:     if (me->read > me->data) {
                     47:        if ((status = PUTBLOCK(me->data, me->read - me->data))==HT_WOULD_BLOCK)
                     48:            return HT_WOULD_BLOCK;
2.6       frystyk    49:        me->lastFlushTime = HTGetTimeInMillis();
2.1       frystyk    50:        me->read = me->data;
                     51:        me->block = NULL;
                     52:     }
2.6       frystyk    53:     if (me->timer) {
                     54:        HTTimer_delete(me->timer);
                     55:        me->timer = NULL;
                     56:     }
2.1       frystyk    57:     return status;
                     58: }
                     59: 
2.9     ! eric       60: PRIVATE int FlushEvent (HTTimer * timer, void * param, HTEventType type)
2.6       frystyk    61: {
                     62:     HTOutputStream * me = (HTOutputStream *) param;
                     63:     if (timer != me->timer) HTDebugBreak();
                     64:     HTBufferWriter_flush(me);
                     65:     return HT_OK;
                     66: }
                     67: 
                     68: PRIVATE int HTBufferWriter_lazyFlush (HTOutputStream * me)
                     69: {
                     70:     HTNet * net;
                     71:     int delay;
                     72: 
                     73:     if (me->read <= me->data)
                     74:        return HT_OK;                   /* nothing to flush */
                     75:     /*
                     76:     **  If we are allowed to delay the flush then set a timer with the
                     77:     **  delay descibed by our delay variable. If we can't delay then flush 
                     78:     **  right away.
                     79:     */
                     80:     delay = HTHost_writeDelay(me->host, me->lastFlushTime, me->read - me->data);
                     81: 
                     82:     /*
                     83:     ** Flush immediately
                     84:     */
                     85:     if (!delay)
                     86:        return HTBufferWriter_flush(me);
                     87: 
                     88:     /*
                     89:     ** Delayed flush
                     90:     */
                     91:     if (me->timer)                     /* already queued to flush */
                     92:        return HT_OK;
                     93: 
                     94:     /*
                     95:     ** Set a timer and tell the host we've done the write
                     96:     */
                     97:     net = HTHost_getWriteNet(me->host);
                     98:     me->timer = HTTimer_new(NULL, FlushEvent, me, delay, YES);
                     99:     HTHost_unregister(me->host, net, HTEvent_WRITE);
                    100:     if (PROT_TRACE) HTTrace("Buffer...... Waiting...\n");
                    101:     return HT_OK;
                    102: }
                    103: 
2.1       frystyk   104: PRIVATE int HTBufferWriter_free (HTOutputStream * me)
                    105: {
                    106:     return HTBufferWriter_flush(me);
                    107: }
                    108: 
                    109: PRIVATE int HTBufferWriter_abort (HTOutputStream * me, HTList * e)
                    110: {
2.8       frystyk   111:     if (PROT_TRACE) HTTrace("Buffer...... ABORTING...\n");
2.1       frystyk   112:     if (me->target) (*me->target->isa->abort)(me->target, e);
2.8       frystyk   113:     if (me->timer) {
                    114:        HTTimer_delete(me->timer);
                    115:        me->timer = NULL;
                    116:     }
2.1       frystyk   117:     return HT_ERROR;
                    118: }
                    119: 
2.6       frystyk   120: PRIVATE int HTBufferWriter_write (HTOutputStream * me, const char * buf, int len)
2.1       frystyk   121: {
2.6       frystyk   122:     HTNet * net = HTHost_getWriteNet(me->host);
2.4       frystyk   123:     long total = len;
2.1       frystyk   124:     int status;
2.4       frystyk   125: 
2.3       frystyk   126:     if (me->bb > 0) {
2.1       frystyk   127:        len -= (me->block - buf);
2.3       frystyk   128:        if ((status = PUTBLOCK(me->block, me->bb)) != HT_OK) return status;
2.6       frystyk   129:        me->lastFlushTime = HTGetTimeInMillis();
2.3       frystyk   130:        me->block += me->bb;
                    131:        len -= me->bb;
                    132:        me->bb = 0;
                    133:     } else {
                    134:        int available = me->data + me->size - me->read;
                    135: 
                    136:        /* Still room in buffer */
                    137:        if (len <= available) {
                    138:            memcpy(me->read, buf, len);
                    139:            me->read += len;
                    140:            return HT_OK;
                    141:        }
                    142: 
                    143:        /* If already data in buffer then fill it and flush */
                    144:        if (me->read > me->data) {
                    145:            memcpy(me->read, buf, available);
                    146:            me->block = (char *) buf+available;
2.6       frystyk   147:            if (me->timer && me->delaying) {
                    148:                HTTimer_delete(me->timer);
                    149:                me->timer=NULL;
                    150:                me->delaying = NO;
                    151:            }
2.4       frystyk   152:            if ((status = PUTBLOCK(me->data, me->size))!=HT_OK) return status;
2.6       frystyk   153:            me->lastFlushTime = HTGetTimeInMillis();
2.3       frystyk   154:        }
                    155: 
                    156:        /* If more data then write n times buffer size */
                    157:        if (!me->block)
                    158:            me->block = (char *) buf;
                    159:        else {
                    160:            len -= (me->block - buf);
                    161:        }
                    162:        me->bb = len - len%me->size;
2.6       frystyk   163:        if (me->bb) {
                    164:            if ((status = PUTBLOCK(me->block, me->bb)) != HT_OK) return status;
                    165:            me->lastFlushTime = HTGetTimeInMillis();
                    166:        }
2.3       frystyk   167:        me->block += me->bb;
                    168:        len -= me->bb;
                    169:        me->bb = 0;
2.1       frystyk   170:     }
                    171: 
                    172:     /* If data is not aligned then save the rest in our buffer */
                    173:     if (len > 0) {
                    174:        memcpy(me->data, me->block, len);
                    175:        me->read = me->data + len;
                    176:     } else
                    177:        me->read = me->data;
                    178:     me->block = NULL;
2.6       frystyk   179:     HTNet_addBytesWritten(net, total);
2.1       frystyk   180:     return HT_OK;
                    181: }
                    182: 
                    183: /*     Character handling
                    184: **     ------------------
                    185: */
                    186: PRIVATE int HTBufferWriter_put_character (HTOutputStream * me, char c)
                    187: {
                    188:     return HTBufferWriter_write(me, &c, 1);
                    189: }
                    190: 
                    191: /*     String handling
                    192: **     ---------------
                    193: **
                    194: **     Strings must be smaller than this buffer size.
                    195: */
                    196: PRIVATE int HTBufferWriter_put_string (HTOutputStream * me, const char * s)
                    197: {
                    198:     return HTBufferWriter_write(me, s, (int) strlen(s));
                    199: }
                    200: /*
                    201: **     The difference between the close and the free method is that we don't
                    202: **     close the connection in the free method - we only call the free method
                    203: **     of the target stream. That way, we can keep the output stream as long 
                    204: **     as the channel itself.
                    205: */
                    206: PRIVATE int HTBufferWriter_close (HTOutputStream * me)
                    207: {
                    208:     if (me) {
                    209:        HTBufferWriter_flush(me);
                    210:        if (me->target) (*me->target->isa->close)(me->target);
                    211:        HT_FREE(me->data);
                    212:        HT_FREE(me);
                    213:     }
                    214:     return HT_OK;
                    215: }
                    216: 
                    217: PRIVATE const HTOutputStreamClass HTBufferWriter =
                    218: {              
2.6       frystyk   219:     "BufferedSocketWriter",
                    220:     HTBufferWriter_lazyFlush,
2.1       frystyk   221:     HTBufferWriter_free,
                    222:     HTBufferWriter_abort,
                    223:     HTBufferWriter_put_character,
                    224:     HTBufferWriter_put_string,
                    225:     HTBufferWriter_write,
                    226:     HTBufferWriter_close
                    227: }; 
                    228: 
2.6       frystyk   229: PUBLIC HTOutputStream * HTBufferWriter_new (HTHost * host, HTChannel * ch,
2.1       frystyk   230:                                            void * param, int bufsize)
                    231: {
2.6       frystyk   232:     if (host && ch) {
2.1       frystyk   233:        HTOutputStream * me = HTChannel_output(ch);
2.6       frystyk   234:        if (!me) {
                    235:            HTOutputStream * me;
2.1       frystyk   236:            if (bufsize <= 0) bufsize = OUTPUT_BUFFER_SIZE;
2.6       frystyk   237:            if ((me = (HTOutputStream *) HT_CALLOC(1, sizeof(HTOutputStream)))==NULL ||
2.1       frystyk   238:                (me->data = (char *) HT_MALLOC(bufsize)) == NULL)
                    239:                HT_OUTOFMEM("HTBufferWriter_new");
                    240:            me->isa = &HTBufferWriter;
                    241:            me->read = me->data;
                    242:            me->size = bufsize;
2.6       frystyk   243:            me->target = HTWriter_new(host, ch, param, 0);
                    244:            me->host = host;
                    245:            return me;
                    246:        }
2.1       frystyk   247:     }
                    248:     return NULL;
                    249: }

Webmaster