From b5e7fb20b6618b64a2d1b177fa39a8865bd560f8 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Thu, 14 Mar 2019 15:03:57 -0700 Subject: [PATCH] replace uint8_t with uint64_t for the send/close requests types / use named variable to index into the _fildes array --- ixwebsocket/IXEventFd.cpp | 28 ++++++++++++++++------------ ixwebsocket/IXEventFd.h | 4 ++++ ixwebsocket/IXSocket.cpp | 6 +++--- ixwebsocket/IXSocket.h | 4 ++-- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ixwebsocket/IXEventFd.cpp b/ixwebsocket/IXEventFd.cpp index 658ba51b..1284afdd 100644 --- a/ixwebsocket/IXEventFd.cpp +++ b/ixwebsocket/IXEventFd.cpp @@ -31,6 +31,11 @@ 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() { #ifdef __linux__ @@ -38,12 +43,12 @@ namespace ix _eventfd = eventfd(0, 0); fcntl(_eventfd, F_SETFL, O_NONBLOCK); #else - _fildes[0] = -1; - _fildes[1] = -1; + _fildes[kPipeReadIndex] = -1; + _fildes[kPipeWriteIndex] = -1; pipe(_fildes); - fcntl(_fildes[0], F_SETFL, O_NONBLOCK); - fcntl(_fildes[1], F_SETFL, O_NONBLOCK); + fcntl(_fildes[kPipeReadIndex], F_SETFL, O_NONBLOCK); + fcntl(_fildes[kPipeWriteIndex], F_SETFL, O_NONBLOCK); #endif } @@ -52,10 +57,10 @@ namespace ix #ifdef __linux__ ::close(_eventfd); #else - ::close(_fildes[0]); - ::close(_fildes[1]); - _fildes[0] = -1; - _fildes[1] = -1; + ::close(_fildes[kPipeReadIndex]); + ::close(_fildes[kPipeWriteIndex]); + _fildes[kPipeReadIndex] = -1; + _fildes[kPipeWriteIndex] = -1; #endif } @@ -66,8 +71,7 @@ namespace ix #if defined(__linux__) fd = _eventfd; #else - // File descriptor at index 1 in _fildes is the write end of the pipe - fd = _fildes[1]; + fd = _fildes[kPipeWriteIndex]; #endif if (fd == -1) return false; @@ -84,7 +88,7 @@ namespace ix #if defined(__linux__) fd = _eventfd; #else - fd = _fildes[0]; + fd = _fildes[kPipeReadIndex]; #endif uint64_t value = 0; ::read(fd, &value, sizeof(value)); @@ -111,7 +115,7 @@ namespace ix #if defined(__linux__) return _eventfd; #else - return _fildes[0]; + return _fildes[kPipeReadIndex]; #endif } } diff --git a/ixwebsocket/IXEventFd.h b/ixwebsocket/IXEventFd.h index 56db41b2..231be228 100644 --- a/ixwebsocket/IXEventFd.h +++ b/ixwebsocket/IXEventFd.h @@ -28,6 +28,10 @@ namespace ix // happens between a control thread and a background thread, which is // blocked on select. int _fildes[2]; + + // Used to identify the read/write idx + static const int kPipeReadIndex; + static const int kPipeWriteIndex; #endif }; } diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index 94219e6e..cd9155dd 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -23,8 +23,8 @@ namespace ix { const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default const int Socket::kDefaultPollTimeout = kDefaultPollNoTimeout; - const uint8_t Socket::kSendRequest = 1; - const uint8_t Socket::kCloseRequest = 2; + const uint64_t Socket::kSendRequest = 1; + const uint64_t Socket::kCloseRequest = 2; constexpr size_t Socket::kChunkSize; Socket::Socket(int fd) : @@ -86,7 +86,7 @@ namespace ix } else if (eventfd != -1 && FD_ISSET(eventfd, &rfds)) { - uint8_t value = _eventfd.read(); + uint64_t value = _eventfd.read(); if (value == kSendRequest) { diff --git a/ixwebsocket/IXSocket.h b/ixwebsocket/IXSocket.h index 88f8725a..c336390e 100644 --- a/ixwebsocket/IXSocket.h +++ b/ixwebsocket/IXSocket.h @@ -76,8 +76,8 @@ namespace ix static void cleanup(); // Required on Windows to cleanup WinSocket // Used as special codes for pipe communication - static const uint8_t kSendRequest; - static const uint8_t kCloseRequest; + static const uint64_t kSendRequest; + static const uint64_t kCloseRequest; protected: void closeSocket(int fd);