Annotation of libwww/Library/src/HTBufWrt.c, revision 2.21
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.21 ! frystyk 6: ** @(#) $Id: HTBufWrt.c,v 2.20 1998/05/04 19:36:19 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: HTNet_addBytesWritten(net, size);
188: me->read = me->data;
189: } else {
190: return (status == HT_WOULD_BLOCK) ? HT_OK : HT_ERROR;
191: }
192: }
193: return HT_OK;
194: } else {
195:
196: /* Fill the existing buffer (if not already) and flush */
197: if (available) {
198: memcpy(me->read, buf, available);
199: buf += available;
200: len -= available;
201: me->read += available;
202: }
203: me->lastFlushTime = HTGetTimeInMillis();
204: status = PUTBLOCK(me->data, me->allocated);
205: if (status == HT_OK) {
206: HTNet_addBytesWritten(net, me->allocated);
207: me->read = me->data;
208: } else if (status == HT_WOULD_BLOCK) {
209: HTBufferWriter_addBuffer(me, len);
210: memcpy(me->read, buf, len);
211: me->read += len;
212: return HT_OK;
213: }
214: }
2.15 frystyk 215: }
2.1 frystyk 216: }
217:
218: /* Character handling
219: ** ------------------
220: */
221: PRIVATE int HTBufferWriter_put_character (HTOutputStream * me, char c)
222: {
223: return HTBufferWriter_write(me, &c, 1);
224: }
225:
226: /* String handling
227: ** ---------------
228: **
229: ** Strings must be smaller than this buffer size.
230: */
231: PRIVATE int HTBufferWriter_put_string (HTOutputStream * me, const char * s)
232: {
233: return HTBufferWriter_write(me, s, (int) strlen(s));
234: }
235: /*
236: ** The difference between the close and the free method is that we don't
237: ** close the connection in the free method - we only call the free method
238: ** of the target stream. That way, we can keep the output stream as long
239: ** as the channel itself.
240: */
241: PRIVATE int HTBufferWriter_close (HTOutputStream * me)
242: {
243: if (me) {
244: if (me->target) (*me->target->isa->close)(me->target);
245: HT_FREE(me->data);
246: HT_FREE(me);
247: }
248: return HT_OK;
249: }
250:
251: PRIVATE const HTOutputStreamClass HTBufferWriter =
252: {
2.6 frystyk 253: "BufferedSocketWriter",
254: HTBufferWriter_lazyFlush,
2.1 frystyk 255: HTBufferWriter_free,
256: HTBufferWriter_abort,
257: HTBufferWriter_put_character,
258: HTBufferWriter_put_string,
259: HTBufferWriter_write,
260: HTBufferWriter_close
261: };
262:
2.6 frystyk 263: PUBLIC HTOutputStream * HTBufferWriter_new (HTHost * host, HTChannel * ch,
2.1 frystyk 264: void * param, int bufsize)
265: {
2.6 frystyk 266: if (host && ch) {
2.1 frystyk 267: HTOutputStream * me = HTChannel_output(ch);
2.6 frystyk 268: if (!me) {
269: HTOutputStream * me;
2.16 frystyk 270: int tcpbufsize = 0;
271:
272: #if defined(HAVE_GETSOCKOPT) && defined(SO_SNDBUF)
273: /*
274: ** Get the TCP socket buffer size
275: */
276: {
277: SOCKET sockfd = HTChannel_socket(ch);
278: int size = sizeof(int);
279: int status = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
280: (void *) &tcpbufsize, &size);
281: if (status == -1) {
2.21 ! frystyk 282: if (STREAM_TRACE)
2.16 frystyk 283: HTTrace("Socket...... Could not get TCP send buffer size for socket %d\n", sockfd);
284: } else {
2.21 ! frystyk 285: if (STREAM_TRACE)
2.16 frystyk 286: HTTrace("Socket...... TCP send buffer size is %d for socket %d\n", tcpbufsize, sockfd);
287: }
288: }
289: #endif
2.17 frystyk 290: if (bufsize <= 0) bufsize = tcpbufsize ? tcpbufsize : OUTPUT_BUFFER_SIZE;
2.6 frystyk 291: if ((me = (HTOutputStream *) HT_CALLOC(1, sizeof(HTOutputStream)))==NULL ||
2.1 frystyk 292: (me->data = (char *) HT_MALLOC(bufsize)) == NULL)
293: HT_OUTOFMEM("HTBufferWriter_new");
294: me->isa = &HTBufferWriter;
295: me->read = me->data;
2.15 frystyk 296: me->allocated = bufsize;
297: me->growby = bufsize;
2.16 frystyk 298: me->expo = 1;
2.6 frystyk 299: me->target = HTWriter_new(host, ch, param, 0);
300: me->host = host;
2.15 frystyk 301: return me;
2.6 frystyk 302: }
2.1 frystyk 303: }
304: return NULL;
305: }
Webmaster