2 * UCW Library -- Main Loop: Record I/O
4 * (c) 2011 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "ucw/mainloop.h"
29 rec_io_timer_expired(struct main_timer *tm)
31 struct main_rec_io *rio = tm->data;
32 timer_del(&rio->timer);
33 rio->notify_handler(rio, RIO_ERR_TIMEOUT);
37 rec_io_add(struct main_rec_io *rio, int fd)
41 rio->timer.handler = rec_io_timer_expired;
42 rio->timer.data = rio;
43 clist_init(&rio->idle_write_buffers);
44 clist_init(&rio->busy_write_buffers);
48 rec_io_del(struct main_rec_io *rio)
50 timer_del(&rio->timer);
55 DBG("RIO: Freeing read buffer");
61 while ((b = clist_remove_head(&rio->idle_write_buffers)) || (b = clist_remove_head(&rio->busy_write_buffers)))
63 DBG("RIO: Freeing write buffer");
69 rec_io_read_handler(struct main_file *fi)
71 struct main_rec_io *rio = (struct main_rec_io *) fi;
73 if (rio->read_rec_max && rio->read_avail >= rio->read_rec_max)
75 rec_io_stop_read(rio);
76 rio->notify_handler(rio, RIO_ERR_RECORD_TOO_LARGE);
81 uns rec_start_pos = rio->read_rec_start - rio->read_buf;
82 uns rec_end_pos = rec_start_pos + rio->read_avail;
83 uns free_space = rio->read_buf_size - rec_end_pos;
84 DBG("RIO READ: rec_start=%u avail=%u prev_avail=%u free=%u/%u",
85 rec_start_pos, rio->read_avail, rio->read_prev_avail,
86 free_space, rio->read_buf_size);
88 if (free_space <= rio->read_buf_size/8)
90 if (rec_start_pos && rec_start_pos >= rio->read_buf_size/2)
92 // Moving the partial record to the start of the buffer
93 DBG("RIO READ: Moving partial record to start");
94 memmove(rio->read_buf, rio->read_rec_start, rio->read_avail);
95 rio->read_rec_start = rio->read_buf;
99 DBG("RIO READ: Resizing buffer");
100 rio->read_buf_size *= 2;
101 rio->read_buf = xrealloc(rio->read_buf, rio->read_buf_size);
102 rio->read_rec_start = rio->read_buf + rec_start_pos;
107 int l = read(fi->fd, rio->read_buf + rec_end_pos, free_space);
108 DBG("RIO READ: Read %d bytes", l);
111 if (errno != EINTR && errno != EAGAIN)
113 DBG("RIO READ: Signalling error");
114 rec_io_stop_read(rio);
115 rio->notify_handler(rio, RIO_ERR_READ);
121 DBG("RIO READ: Signalling EOF");
122 rec_io_stop_read(rio);
123 rio->notify_handler(rio, RIO_EVENT_EOF);
126 rio->read_prev_avail = rio->read_avail;
127 rio->read_avail += l;
128 DBG("RIO READ: Available: %u bytes", rio->read_avail);
131 while (rio->read_running && (got = rio->read_handler(rio)))
133 DBG("RIO READ: Ate %u bytes", got);
134 rio->read_rec_start += got;
135 rio->read_avail -= got;
136 rio->read_prev_avail = 0;
137 if (!rio->read_avail)
139 DBG("RIO READ: Resetting buffer");
140 rio->read_rec_start = rio->read_buf;
144 DBG("RIO READ: Want more");
145 return (rio->read_running ? HOOK_RETRY : HOOK_IDLE);
149 rec_io_recalc_read(struct main_rec_io *rio)
151 uns flow = !rio->write_throttle_read || rio->write_watermark < rio->write_throttle_read;
152 uns run = rio->read_started && flow;
153 DBG("RIO: Recalc read (flow=%u, start=%u) -> %u", flow, rio->read_started, run);
154 if (run != rio->read_running)
160 if (!rio->read_buf_size)
161 rio->read_buf_size = 256;
162 rio->read_buf = xmalloc(rio->read_buf_size);
163 DBG("RIO: Created buffer (%u bytes)", rio->read_buf_size);
164 rio->read_rec_start = rio->read_buf;
166 rio->file.read_handler = rec_io_read_handler;
167 file_chg(&rio->file);
168 DBG("RIO: Reading started");
172 rio->file.read_handler = NULL;
173 file_chg(&rio->file);
174 DBG("RIO: Reading stopped");
176 rio->read_running = run;
181 rec_io_start_read(struct main_rec_io *rio)
183 ASSERT(clist_is_linked(&rio->file.n));
184 rio->read_started = 1;
185 rec_io_recalc_read(rio);
189 rec_io_stop_read(struct main_rec_io *rio)
191 ASSERT(clist_is_linked(&rio->file.n));
192 rio->read_started = 0;
193 rec_io_recalc_read(rio);
197 rec_io_stop_write(struct main_rec_io *rio)
199 DBG("RIO WRITE: Stopping write");
200 ASSERT(!rio->write_watermark);
201 rio->file.write_handler = NULL;
202 file_chg(&rio->file);
206 rec_io_write_handler(struct main_file *fi)
208 struct main_rec_io *rio = (struct main_rec_io *) fi;
209 struct rio_buffer *b = clist_head(&rio->busy_write_buffers);
212 rec_io_stop_write(rio);
216 int l = write(fi->fd, b->buf + b->written, b->full - b->written);
217 DBG("RIO WRITE: Written %d bytes", l);
220 if (errno != EINTR && errno != EAGAIN)
222 rec_io_stop_write(rio);
223 rio->notify_handler(rio, RIO_ERR_WRITE);
228 if (b->written == b->full)
230 DBG("RIO WRITE: Written full buffer");
232 clist_add_tail(&rio->idle_write_buffers, &b->n);
235 rio->write_watermark -= l;
236 int ret = HOOK_RETRY;
237 if (!rio->write_watermark)
240 rec_io_stop_write(rio);
242 rec_io_recalc_read(rio);
244 // Call the hook, but carefully, because it can delete the RIO structure
245 if (rio->notify_handler(rio, rio->write_watermark ? RIO_EVENT_PART_WRITTEN : RIO_EVENT_ALL_WRITTEN) == HOOK_IDLE)
250 static struct rio_buffer *
251 rec_io_get_buffer(struct main_rec_io *rio)
253 struct rio_buffer *b = clist_remove_tail(&rio->idle_write_buffers);
255 DBG("RIO WRITE: Recycled old buffer");
258 if (!rio->write_buf_size)
259 rio->write_buf_size = 1024;
260 b = xmalloc(sizeof(struct rio_buffer) + rio->write_buf_size);
261 DBG("RIO WRITE: Allocated new buffer");
263 b->full = b->written = 0;
268 rec_io_write(struct main_rec_io *rio, void *data, uns len)
271 ASSERT(clist_is_linked(&rio->file.n));
277 struct rio_buffer *b = clist_tail(&rio->busy_write_buffers);
278 if (!b || b->full >= rio->write_buf_size)
280 b = rec_io_get_buffer(rio);
281 clist_add_tail(&rio->busy_write_buffers, &b->n);
283 uns l = MIN(len, rio->write_buf_size - b->full);
284 memcpy(b->buf + b->full, bdata, l);
288 rio->write_watermark += l;
289 DBG("RIO WRITE: Buffered %u bytes of data (total %u)", l, rio->write_watermark);
290 rec_io_recalc_read(rio);
293 if (!rio->file.write_handler)
295 DBG("RIO WRITE: Starting write");
296 rio->file.write_handler = rec_io_write_handler;
297 file_chg(&rio->file);
302 rec_io_set_timeout(struct main_rec_io *rio, timestamp_t expires_delta)
304 DBG("RIO: Setting timeout %u", (uns) expires_delta);
306 timer_del(&rio->timer);
308 timer_add_rel(&rio->timer, expires_delta);
312 rec_io_parse_line(struct main_rec_io *rio)
314 for (uns i = rio->read_prev_avail; i < rio->read_avail; i++)
315 if (rio->read_rec_start[i] == '\n')
322 static uns rhand(struct main_rec_io *rio)
324 uns r = rec_io_parse_line(rio);
327 rio->read_rec_start[r-1] = 0;
328 printf("Read <%s>\n", rio->read_rec_start);
329 rec_io_set_timeout(rio, 10000);
330 rio->read_rec_start[r-1] = '\n';
331 rec_io_write(rio, rio->read_rec_start, r);
336 static int ehand(struct main_rec_io *rio, int cause)
338 if (cause < 0 || cause == RIO_EVENT_EOF)
340 msg(L_ERROR, "Error %d", cause);
347 msg(L_INFO, "Event %d", cause);
358 struct main_rec_io rio = {};
359 rio.read_buf_size = 4;
360 rio.read_handler = rhand;
361 rio.notify_handler = ehand;
362 // rio.read_rec_max = 40;
363 rio.write_buf_size = 4;
364 rio.write_throttle_read = 6;
366 rec_io_start_read(&rio);
367 rec_io_set_timeout(&rio, 10000);
372 msg(L_INFO, "Finished.");
374 if (clist_is_linked(&rio.file.n))