2 * UCW Library -- Main Loop: Block I/O
4 * (c) 2004--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>
22 block_io_timer_expired(struct main_timer *tm)
24 struct main_block_io *bio = tm->data;
25 timer_del(&bio->timer);
26 if (bio->error_handler)
27 bio->error_handler(bio, BIO_ERR_TIMEOUT);
31 block_io_add(struct main_block_io *bio, int fd)
35 bio->timer.handler = block_io_timer_expired;
36 bio->timer.data = bio;
40 block_io_del(struct main_block_io *bio)
42 timer_del(&bio->timer);
47 block_io_read_handler(struct main_file *fi)
49 struct main_block_io *bio = (struct main_block_io *) fi;
51 while (bio->rpos < bio->rlen)
53 int l = read(fi->fd, bio->rbuf + bio->rpos, bio->rlen - bio->rpos);
54 DBG("BIO: FD %d: read %d", fi->fd, l);
57 if (errno != EINTR && errno != EAGAIN && bio->error_handler)
58 bio->error_handler(bio, BIO_ERR_READ);
65 DBG("BIO: FD %d done read %d of %d", fi->fd, bio->rpos, bio->rlen);
66 fi->read_handler = NULL;
73 block_io_write_handler(struct main_file *fi)
75 struct main_block_io *bio = (struct main_block_io *) fi;
77 while (bio->wpos < bio->wlen)
79 int l = write(fi->fd, bio->wbuf + bio->wpos, bio->wlen - bio->wpos);
80 DBG("BIO: FD %d: write %d", fi->fd, l);
83 if (errno != EINTR && errno != EAGAIN && bio->error_handler)
84 bio->error_handler(bio, BIO_ERR_WRITE);
89 DBG("BIO: FD %d done write %d", fi->fd, bio->wpos);
90 fi->write_handler = NULL;
97 block_io_read(struct main_block_io *bio, void *buf, uint len)
99 ASSERT(bio->file.n.next);
102 bio->file.read_handler = block_io_read_handler;
109 bio->file.read_handler = NULL;
111 bio->rpos = bio->rlen = 0;
113 file_chg(&bio->file);
117 block_io_write(struct main_block_io *bio, void *buf, uint len)
119 ASSERT(bio->file.n.next);
122 bio->file.write_handler = block_io_write_handler;
129 bio->file.write_handler = NULL;
131 bio->wpos = bio->wlen = 0;
133 file_chg(&bio->file);
137 block_io_set_timeout(struct main_block_io *bio, timestamp_t expires_delta)
140 timer_del(&bio->timer);
142 timer_add_rel(&bio->timer, expires_delta);