Annotation of libwww/Library/src/HTBufWrt.c, revision 2.24
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.24 ! frystyk 6: ** @(#) $Id: HTBufWrt.c,v 2.23 1998/07/17 17:05:03 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: */
2.24 ! frystyk 74: HTTimer_delete(me->timer);
2.11 frystyk 75: me->timer = NULL;
2.6 frystyk 76: return HT_OK;
77: }
78:
79: PRIVATE int HTBufferWriter_lazyFlush (HTOutputStream * me)
80: {
81: HTNet * net;
82: int delay;
83:
2.18 frystyk 84: if (me->read <= me->data) {
2.6 frystyk 85: return HT_OK; /* nothing to flush */
2.18 frystyk 86: }
2.6 frystyk 87: /*
88: ** If we are allowed to delay the flush then set a timer with the
89: ** delay descibed by our delay variable. If we can't delay then flush
90: ** right away.
91: */
2.12 frystyk 92: delay = HTHost_findWriteDelay(me->host, me->lastFlushTime, me->read - me->data);
2.6 frystyk 93:
94: /*
95: ** Flush immediately
96: */
2.11 frystyk 97: if (!delay) {
98: int status;
2.21 frystyk 99: if (STREAM_TRACE) HTTrace("Buffer...... Flushing %p\n", me);
2.11 frystyk 100: if ((status = HTBufferWriter_flush(me)) && me->timer) {
101: HTTimer_delete(me->timer);
102: me->timer = NULL;
103: }
104: return status;
105: }
2.6 frystyk 106:
107: /*
2.11 frystyk 108: ** Set a timer and tell the host we've done the write if
2.21 frystyk 109: ** we have not already started a timer earlier. If a timer
110: ** does already exist then make sure that it hasn't expired.
111: ** This can be the case if we have a really slow client that
112: ** can't parse the data fast enough.
2.6 frystyk 113: */
2.11 frystyk 114: if (!me->timer) {
115: net = HTHost_getWriteNet(me->host);
2.21 frystyk 116: me->timer = HTTimer_new(NULL, FlushEvent, me, delay, YES, NO);
2.11 frystyk 117: HTHost_unregister(me->host, net, HTEvent_WRITE);
2.21 frystyk 118: if (STREAM_TRACE) HTTrace("Buffer...... Waiting %dms on %p\n", delay, me);
119: } else {
120: if (HTTimer_hasTimerExpired(me->timer)) {
121: if (STREAM_TRACE)
122: HTTrace("Buffer...... Dispatching old timer %p\n", me->timer);
123: HTTimer_dispatch(me->timer);
124: me->timer = NULL;
125: } else {
126: if (STREAM_TRACE)
127: HTTrace("Buffer...... Waiting on unexpired timer %p\n", me->timer);
128: }
2.11 frystyk 129: }
2.6 frystyk 130: return HT_OK;
131: }
132:
2.1 frystyk 133: PRIVATE int HTBufferWriter_free (HTOutputStream * me)
134: {
2.11 frystyk 135: return HTBufferWriter_lazyFlush(me);
2.1 frystyk 136: }
137:
2.15 frystyk 138: PRIVATE BOOL HTBufferWriter_addBuffer(HTOutputStream * me, int addthis)
139: {
140: if (me) {
2.16 frystyk 141: me->allocated += (addthis - addthis%me->growby + (me->growby*me->expo));
2.17 frystyk 142: me->expo *= 2;
2.21 frystyk 143: if (STREAM_TRACE) HTTrace("Buffer...... Increasing buffer to %d bytes\n", me->allocated);
2.15 frystyk 144: if (me->data) {
145: int size = me->read-me->data;
146: if ((me->data = (char *) HT_REALLOC(me->data, me->allocated)) == NULL)
147: HT_OUTOFMEM("HTBufferWriter_addBuffer");
148: me->read = me->data + size;
149: } else {
150: if ((me->data = (char *) HT_CALLOC(1, me->allocated)) == NULL)
151: HT_OUTOFMEM("HTBufferWriter_addBuffer");
152: me->read = me->data;
153: }
154: return YES;
155: }
156: return NO;
157: }
158:
2.1 frystyk 159: PRIVATE int HTBufferWriter_abort (HTOutputStream * me, HTList * e)
160: {
2.21 frystyk 161: if (STREAM_TRACE) HTTrace("Buffer...... ABORTING...\n");
2.1 frystyk 162: if (me->target) (*me->target->isa->abort)(me->target, e);
2.8 frystyk 163: if (me->timer) {
164: HTTimer_delete(me->timer);
165: me->timer = NULL;
166: }
2.1 frystyk 167: return HT_ERROR;
168: }
169:
2.6 frystyk 170: PRIVATE int HTBufferWriter_write (HTOutputStream * me, const char * buf, int len)
2.1 frystyk 171: {
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