replace uint8_t with uint64_t for the send/close requests types / use named variable to index into the _fildes array

This commit is contained in:
Benjamin Sergeant 2019-03-14 15:03:57 -07:00
parent 9d245add9c
commit b5e7fb20b6
4 changed files with 25 additions and 17 deletions

View File

@ -31,6 +31,11 @@
namespace ix namespace ix
{ {
// File descriptor at index 0 in _fildes is the read end of the pipe
// File descriptor at index 1 in _fildes is the write end of the pipe
const int EventFd::kPipeReadIndex = 0;
const int EventFd::kPipeWriteIndex = 1;
EventFd::EventFd() EventFd::EventFd()
{ {
#ifdef __linux__ #ifdef __linux__
@ -38,12 +43,12 @@ namespace ix
_eventfd = eventfd(0, 0); _eventfd = eventfd(0, 0);
fcntl(_eventfd, F_SETFL, O_NONBLOCK); fcntl(_eventfd, F_SETFL, O_NONBLOCK);
#else #else
_fildes[0] = -1; _fildes[kPipeReadIndex] = -1;
_fildes[1] = -1; _fildes[kPipeWriteIndex] = -1;
pipe(_fildes); pipe(_fildes);
fcntl(_fildes[0], F_SETFL, O_NONBLOCK); fcntl(_fildes[kPipeReadIndex], F_SETFL, O_NONBLOCK);
fcntl(_fildes[1], F_SETFL, O_NONBLOCK); fcntl(_fildes[kPipeWriteIndex], F_SETFL, O_NONBLOCK);
#endif #endif
} }
@ -52,10 +57,10 @@ namespace ix
#ifdef __linux__ #ifdef __linux__
::close(_eventfd); ::close(_eventfd);
#else #else
::close(_fildes[0]); ::close(_fildes[kPipeReadIndex]);
::close(_fildes[1]); ::close(_fildes[kPipeWriteIndex]);
_fildes[0] = -1; _fildes[kPipeReadIndex] = -1;
_fildes[1] = -1; _fildes[kPipeWriteIndex] = -1;
#endif #endif
} }
@ -66,8 +71,7 @@ namespace ix
#if defined(__linux__) #if defined(__linux__)
fd = _eventfd; fd = _eventfd;
#else #else
// File descriptor at index 1 in _fildes is the write end of the pipe fd = _fildes[kPipeWriteIndex];
fd = _fildes[1];
#endif #endif
if (fd == -1) return false; if (fd == -1) return false;
@ -84,7 +88,7 @@ namespace ix
#if defined(__linux__) #if defined(__linux__)
fd = _eventfd; fd = _eventfd;
#else #else
fd = _fildes[0]; fd = _fildes[kPipeReadIndex];
#endif #endif
uint64_t value = 0; uint64_t value = 0;
::read(fd, &value, sizeof(value)); ::read(fd, &value, sizeof(value));
@ -111,7 +115,7 @@ namespace ix
#if defined(__linux__) #if defined(__linux__)
return _eventfd; return _eventfd;
#else #else
return _fildes[0]; return _fildes[kPipeReadIndex];
#endif #endif
} }
} }

View File

@ -28,6 +28,10 @@ namespace ix
// happens between a control thread and a background thread, which is // happens between a control thread and a background thread, which is
// blocked on select. // blocked on select.
int _fildes[2]; int _fildes[2];
// Used to identify the read/write idx
static const int kPipeReadIndex;
static const int kPipeWriteIndex;
#endif #endif
}; };
} }

View File

@ -23,8 +23,8 @@ namespace ix
{ {
const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default
const int Socket::kDefaultPollTimeout = kDefaultPollNoTimeout; const int Socket::kDefaultPollTimeout = kDefaultPollNoTimeout;
const uint8_t Socket::kSendRequest = 1; const uint64_t Socket::kSendRequest = 1;
const uint8_t Socket::kCloseRequest = 2; const uint64_t Socket::kCloseRequest = 2;
constexpr size_t Socket::kChunkSize; constexpr size_t Socket::kChunkSize;
Socket::Socket(int fd) : Socket::Socket(int fd) :
@ -86,7 +86,7 @@ namespace ix
} }
else if (eventfd != -1 && FD_ISSET(eventfd, &rfds)) else if (eventfd != -1 && FD_ISSET(eventfd, &rfds))
{ {
uint8_t value = _eventfd.read(); uint64_t value = _eventfd.read();
if (value == kSendRequest) if (value == kSendRequest)
{ {

View File

@ -76,8 +76,8 @@ namespace ix
static void cleanup(); // Required on Windows to cleanup WinSocket static void cleanup(); // Required on Windows to cleanup WinSocket
// Used as special codes for pipe communication // Used as special codes for pipe communication
static const uint8_t kSendRequest; static const uint64_t kSendRequest;
static const uint8_t kCloseRequest; static const uint64_t kCloseRequest;
protected: protected:
void closeSocket(int fd); void closeSocket(int fd);