Buffer: Convert API to std::chrono

This commit is contained in:
Peter Keresztes Schmidt 2021-06-13 17:18:28 +02:00
parent 707975e567
commit c823b9c00e
2 changed files with 7 additions and 3 deletions

View File

@ -74,11 +74,13 @@ int Buffer::read_into(int sd, unsigned int bytes) {
return bytes_read; return bytes_read;
} }
int Buffer::read_into(int sd, unsigned int bytes, struct timeval timeout) { int Buffer::read_into(int sd, unsigned int bytes, Microseconds timeout) {
fd_set set; fd_set set;
FD_ZERO(&set); /* clear the set */ FD_ZERO(&set); /* clear the set */
FD_SET(sd, &set); /* add our file descriptor to the set */ FD_SET(sd, &set); /* add our file descriptor to the set */
int rv = select(sd + 1, &set, NULL, NULL, &timeout); timeval timeout_tv = zm::chrono::duration_cast<timeval>(timeout);
int rv = select(sd + 1, &set, nullptr, nullptr, &timeout_tv);
if (rv == -1) { if (rv == -1) {
Error("Error %d %s from select", errno, strerror(errno)); Error("Error %d %s from select", errno, strerror(errno));
return rv; return rv;
@ -86,5 +88,6 @@ int Buffer::read_into(int sd, unsigned int bytes, struct timeval timeout) {
Debug(1, "timeout"); /* a timeout occured */ Debug(1, "timeout"); /* a timeout occured */
return 0; return 0;
} }
return read_into(sd, bytes); return read_into(sd, bytes);
} }

View File

@ -21,6 +21,7 @@
#define ZM_BUFFER_H #define ZM_BUFFER_H
#include "zm_logger.h" #include "zm_logger.h"
#include "zm_time.h"
#include <cstring> #include <cstring>
class Buffer { class Buffer {
@ -187,7 +188,7 @@ class Buffer {
return static_cast<int>(mSize); return static_cast<int>(mSize);
} }
int read_into(int sd, unsigned int bytes); int read_into(int sd, unsigned int bytes);
int read_into(int sd, unsigned int bytes, struct timeval timeout); int read_into(int sd, unsigned int bytes, Microseconds timeout);
}; };
#endif // ZM_BUFFER_H #endif // ZM_BUFFER_H