24 #include "common_priv.h"
27 #include <sys/select.h>
56 if (set) { maxfd = std::max(maxfd, fd); }
63 tv.tv_sec = (
int)timeout;
64 tv.tv_usec = (
int)((timeout - tv.tv_sec) * 1e6);
67 int ret = select(maxfd + 1, &rfd, &wfd, 0, ptv);
79 if (FD_ISSET(fd, &rfd)) {
82 if (FD_ISSET(fd, &wfd)) {
92 : fd(-1), readable(false), writeable(true), eof(false)
97 : fd(filed), readable(false), writeable(true), eof(false)
102 if (
fd != -1) { close(
fd); }
108 return Poll(&fh, &fh + 1, timeout);
116 int flags = fcntl(fd, F_GETFL, 0);
119 flags &= ~O_NONBLOCK;
123 if (fcntl(fd, F_SETFL, flags) != 0) {
133 int flags = fcntl(fd, F_GETFL, 0);
135 return !(flags & O_NONBLOCK);
149 if (close(
fd) != 0) {
163 if (
eof ||
fd == -1) {
return 0; }
166 unsigned bytesread = 0;
167 int num = read(filed, ptr, len);
179 }
else if (num == 0 && len != 0) {
184 if (
unsigned(num) < len) {
Readable(
false); }
194 if (
eof ||
fd == -1) {
return 0; }
198 for (
int i = 0; i < iovcnt; ++i) {
199 len += iov[i].iov_len;
201 unsigned bytesread = 0;
202 int num = readv(filed, iov, iovcnt);
214 }
else if (num == 0 && len != 0) {
219 if (
unsigned(num) < len) {
Readable(
false); }
229 if (
fd == -1) {
return 0; }
232 unsigned written = 0;
233 int num = write(filed, ptr, len);
255 if (
unsigned(num) < len) {
Writeable(
false); }
265 if (
fd == -1) {
return 0; }
268 unsigned written = 0;
269 int num = writev(filed, iov, iovcnt);
284 for (
int i = 0; i < iovcnt; ++i) {
285 len += iov[i].iov_len;
287 if (
unsigned(num) < len) {
Writeable(
false); }
294 if (fsync(
FD()) != 0) {
void Flush()
Tell the OS to flush any buffers it has. May not be supported for all file types. ...
bool IsBlocking() const
Test if the current file is in blocking or non blocking mode.
virtual void OnWriteable()
Called by Poll when it detects that the file is writeable.
bool Writeable(bool w)
Set that this file is currently writeable or not.
static int Poll(IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
poll a list of FileHandles for any activity and call the appropriate On method.
void SetBlocking(bool blocking)
Manipulate how the current file handles blocking.
void Close()
Close the file and reset the internal state.
virtual ~FileHandle()
Close the file descriptor. Use Reset if one wants to not close the file descriptor.
unsigned Writev(const iovec *iov, int iovcnt)
scatter gather io version of Write
A class to make it easy to deal with file descriptors.
A reference to an iterator.
FileHandle()
Construct a closed FileHandle.
bool Writeable() const
Gives the current writability status of the file.
void Reset()
Clear all internal state including the file descriptor! WARNING does not close the file! ...
bool Readable(bool r)
Set that the file is currently readable or not.
bool Readable() const
Gives the current readability status of the file.
unsigned Readv(const iovec *iov, int iovcnt)
scatter gather io version of Read
unsigned Read(void *ptr, unsigned len)
Read data from the file descriptor.
Generic file handle could be a file, or a socket or a device.
virtual void OnReadable()
Called by Poll when it detects that the file is readable.
unsigned Write(const void *ptr, unsigned len)
Write data to the file descriptor.