42#include "utilities/qio.h"
44#define MAX_IOSEND_SIZE (32 * 1024)
63 fds[0].events = POLLIN;
65 int pollret = poll(fds, 1, timeoutms);
69 }
else if (pollret < 0) {
73 if (fds[0].revents & POLLIN)
91 fds[0].events = POLLOUT;
93 int pollret = poll(fds, 1, timeoutms);
97 }
else if (pollret < 0) {
101 if (fds[0].revents & POLLOUT)
118ssize_t
qio_read(
int fd,
void *buf,
size_t nbytes,
int timeoutms) {
123 while (total < nbytes) {
127 ssize_t rsize = read(fd, buf + total, nbytes - total);
129 if (errno == EAGAIN || errno == EINPROGRESS) {
141 else if (errno == ETIMEDOUT)
158ssize_t
qio_write(
int fd,
const void *buf,
size_t nbytes,
int timeoutms) {
163 while (total < nbytes) {
166 ssize_t wsize = write(fd, buf + total, nbytes - total);
168 if (errno == EAGAIN || errno == EINPROGRESS) {
180 else if (errno == ETIMEDOUT)
198off_t
qio_send(
int outfd,
int infd, off_t nbytes,
int timeoutms) {
202 unsigned char buf[MAX_IOSEND_SIZE];
205 while (total < nbytes) {
207 if (nbytes - total <=
sizeof(buf))
208 chunksize = nbytes - total;
210 chunksize =
sizeof(buf);
213 ssize_t rsize =
qio_read(infd, buf, chunksize, timeoutms);
214 DEBUG(
"read %zd", rsize);
219 ssize_t wsize =
qio_write(outfd, buf, rsize, timeoutms);
220 DEBUG(
"write %zd", wsize);
225 if (rsize != wsize) {
226 DEBUG(
"size mismatch. read:%zd, write:%zd", rsize, wsize);
233 else if (errno == ETIMEDOUT)
255ssize_t
qio_gets(
int fd,
char *buf,
size_t bufsize,
int timeoutms) {
261 for (ptr = buf; readcnt < (bufsize - 1); ptr++) {
262 ssize_t rsize =
qio_read(fd, ptr, 1, timeoutms);
264 if (errno == EAGAIN || errno == EINPROGRESS) {
275 else if (*ptr ==
'\n')
283 else if (errno == ETIMEDOUT)
299ssize_t
qio_puts(
int fd,
const char *str,
int timeoutms) {
300 size_t strsize = strlen(str);
301 char *newstr = (
char *) malloc(strsize + 1 + 1);
304 strncpy(newstr, str, strsize);
305 newstr[strsize] =
'\n';
306 newstr[strsize + 1] =
'\0';
307 ssize_t ret =
qio_write(fd, newstr, strsize + 1, timeoutms);
323ssize_t
qio_printf(
int fd,
int timeoutms,
const char *format, ...) {
325 DYNAMIC_VSPRINTF(buf, format);
329 ssize_t ret =
qio_write(fd, buf, strlen(buf), timeoutms);
ssize_t qio_write(int fd, const void *buf, size_t nbytes, int timeoutms)
Write to a file descriptor.
ssize_t qio_puts(int fd, const char *str, int timeoutms)
Write a string and a trailing newline to a file descriptor.
int qio_wait_readable(int fd, int timeoutms)
Wait until a file descriptor becomes readable.
ssize_t qio_read(int fd, void *buf, size_t nbytes, int timeoutms)
Read from a file descriptor.
off_t qio_send(int outfd, int infd, off_t nbytes, int timeoutms)
Transfer data between file descriptors.
ssize_t qio_gets(int fd, char *buf, size_t bufsize, int timeoutms)
Read a line from a file descriptor into the buffer until a terminating newline or EOF.
int qio_wait_writable(int fd, int timeoutms)
Wait until a file descriptor is ready for writing.
ssize_t qio_printf(int fd, int timeoutms, const char *format,...)
Write formatted output to a file descriptor.