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)
159ssize_t
qio_write(
int fd,
const void *buf,
size_t nbytes,
int timeoutms) {
164 while (total < nbytes) {
167 ssize_t wsize = write(fd, buf + total, nbytes - total);
169 if (errno == EAGAIN || errno == EINPROGRESS) {
181 else if (errno == ETIMEDOUT)
199off_t
qio_send(
int outfd,
int infd, off_t nbytes,
int timeoutms) {
203 unsigned char buf[MAX_IOSEND_SIZE];
206 while (total < nbytes) {
208 if (nbytes - total <=
sizeof(buf))
209 chunksize = nbytes - total;
211 chunksize =
sizeof(buf);
214 ssize_t rsize =
qio_read(infd, buf, chunksize, timeoutms);
215 DEBUG(
"read %zd", rsize);
220 ssize_t wsize =
qio_write(outfd, buf, rsize, timeoutms);
221 DEBUG(
"write %zd", wsize);
226 if (rsize != wsize) {
227 DEBUG(
"size mismatch. read:%zd, write:%zd", rsize, wsize);
234 else if (errno == ETIMEDOUT)
257ssize_t
qio_gets(
int fd,
char *buf,
size_t bufsize,
int timeoutms) {
263 for (ptr = buf; readcnt < (bufsize - 1); ptr++) {
264 ssize_t rsize =
qio_read(fd, ptr, 1, timeoutms);
266 if (errno == EAGAIN || errno == EINPROGRESS) {
277 else if (*ptr ==
'\n')
285 else if (errno == ETIMEDOUT)
301ssize_t
qio_puts(
int fd,
const char *str,
int timeoutms) {
302 size_t strsize = strlen(str);
303 char *newstr = (
char *) malloc(strsize + 1 + 1);
306 strncpy(newstr, str, strsize);
307 newstr[strsize] =
'\n';
308 newstr[strsize + 1] =
'\0';
309 ssize_t ret =
qio_write(fd, newstr, strsize + 1, timeoutms);
325ssize_t
qio_printf(
int fd,
int timeoutms,
const char *format, ...) {
327 DYNAMIC_VSPRINTF(buf, format);
331 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)
Writes the string and a trailing newline to file descriptor.
int qio_wait_readable(int fd, int timeoutms)
Test & wait until the file descriptor has readable data.
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 pointed to until either a terminating newline or E...
int qio_wait_writable(int fd, int timeoutms)
Test & wait until the file descriptor is ready for writing.
ssize_t qio_printf(int fd, int timeoutms, const char *format,...)
Formatted output to a file descriptor.