-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
666e46d
commit af81ef9
Showing
9 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "kqueue.hpp" | ||
#include "utils/const.hpp" | ||
#include <sys/event.h> | ||
|
||
namespace cppnet { | ||
|
||
KQueue::KQueue() : IOMultiplexingBase() {} | ||
|
||
KQueue::~KQueue() { Close(); } | ||
|
||
int KQueue::Init() { | ||
kq_fd_ = kqueue(); | ||
if (kq_fd_.status() == Socket::kInit) { | ||
err_msg_ = "[syserr]:" + std::string(strerror(errno)); | ||
return kSysErr; | ||
} | ||
return kSuccess; | ||
} | ||
|
||
int KQueue::MonitorSoc(const Socket &fd) { | ||
struct kevent ke; | ||
EV_SET(&ke, fd.fd(), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, nullptr); | ||
if (kevent(kq_fd_.fd(), &ke, 1, nullptr, 0, nullptr) == -1) { | ||
err_msg_ = "[syserr]:" + std::string(strerror(errno)); | ||
return kSysErr; | ||
} | ||
return kSuccess; | ||
} | ||
|
||
int KQueue::RemoveSoc(const Socket &fd) { | ||
struct kevent ke; | ||
EV_SET(&ke, fd.fd(), EVFILT_READ, EV_DELETE, 0, 0, nullptr); | ||
if (kevent(kq_fd_.fd(), &ke, 1, nullptr, 0, nullptr) == -1) { | ||
err_msg_ = "[syserr]:" + std::string(strerror(errno)); | ||
return kSysErr; | ||
} | ||
return kSuccess; | ||
} | ||
|
||
int KQueue::Loop(NotifyCallBack callback) { | ||
while (loop_flag_) { | ||
struct kevent evs[1024]; | ||
int nfds = kevent(kq_fd_.fd(), nullptr, 0, evs, 1024, nullptr); | ||
if (nfds < 0) { | ||
if (errno == EINTR) { | ||
} | ||
err_msg_ = "[syserr]:" + std::string(strerror(errno)); | ||
return kSysErr; | ||
} | ||
for (int i = 0; i < nfds; ++i) { | ||
if (evs[i].filter == EVFILT_READ) { | ||
if (callback != nullptr) { | ||
callback(*this, evs[i].ident); | ||
} | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
void KQueue::Close() { kq_fd_.Close(); } | ||
|
||
} // namespace cppnet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
#include "io_multiplexing_base.hpp" | ||
|
||
namespace cppnet { | ||
|
||
class KQueue : public IOMultiplexingBase { | ||
public: | ||
KQueue(); | ||
virtual ~KQueue(); | ||
int Init() override; | ||
int MonitorSoc(const Socket &fd) override; | ||
int RemoveSoc(const Socket &fd) override; | ||
int Loop(NotifyCallBack callback) override; | ||
void Close() override; | ||
|
||
private: | ||
Socket kq_fd_; | ||
|
||
}; | ||
|
||
} // namespace cppnet | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters