2020-11-24 15:41:18 +01:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <brynet/base/NonCopyable.hpp>
|
|
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
|
|
|
|
#include <brynet/net/port/Win.hpp>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <brynet/net/Channel.hpp>
|
|
|
|
|
#include <brynet/net/Socket.hpp>
|
|
|
|
|
|
|
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
2021-05-14 17:49:39 +02:00
|
|
|
|
class WakeupChannel final : public Channel, public NonCopyable {
|
2021-04-30 16:10:14 +02:00
|
|
|
|
public:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
explicit WakeupChannel(HANDLE iocp) :
|
|
|
|
|
mIOCP(iocp),
|
|
|
|
|
mWakeupOvl(port::Win::OverlappedType::OverlappedRecv) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wakeup() BRYNET_NOEXCEPT {
|
|
|
|
|
return PostQueuedCompletionStatus(mIOCP,
|
|
|
|
|
0,
|
|
|
|
|
reinterpret_cast<ULONG_PTR>(this),
|
|
|
|
|
&mWakeupOvl.base);
|
|
|
|
|
}
|
2021-04-30 16:10:14 +02:00
|
|
|
|
|
|
|
|
|
private:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
void canRecv(bool) BRYNET_NOEXCEPT override {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void canSend() BRYNET_NOEXCEPT override {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onClose() BRYNET_NOEXCEPT override {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HANDLE mIOCP;
|
|
|
|
|
port::Win::OverlappedExt mWakeupOvl;
|
2021-04-30 16:10:14 +02:00
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
#elif defined BRYNET_PLATFORM_LINUX
|
2021-05-14 17:49:39 +02:00
|
|
|
|
class WakeupChannel final : public Channel, public NonCopyable {
|
2021-04-30 16:10:14 +02:00
|
|
|
|
public:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
explicit WakeupChannel(BrynetSocketFD fd) :
|
|
|
|
|
mUniqueFd(fd) {
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-05-14 17:16:45 +02:00
|
|
|
|
bool wakeup() {
|
|
|
|
|
uint64_t one = 1;
|
|
|
|
|
return write(mUniqueFd.getFD(), &one, sizeof one) > 0;
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
private:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
void canRecv(bool) override {
|
|
|
|
|
char temp[1024 * 10];
|
|
|
|
|
while (true) {
|
|
|
|
|
auto n = read(mUniqueFd.getFD(), temp, sizeof(temp));
|
|
|
|
|
if (n == -1 || static_cast<size_t>(n) < sizeof(temp)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void canSend() override {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onClose() override {
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
private:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
UniqueFd mUniqueFd;
|
2021-04-30 16:10:14 +02:00
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
|
|
|
|
#elif defined BRYNET_PLATFORM_DARWIN
|
2021-05-14 17:49:39 +02:00
|
|
|
|
class WakeupChannel final : public Channel, public NonCopyable {
|
2021-04-30 16:10:14 +02:00
|
|
|
|
public:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
explicit WakeupChannel(int kqueuefd, int ident) :
|
|
|
|
|
mKqueueFd(kqueuefd),
|
|
|
|
|
mUserEvent(ident) {
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-05-14 17:16:45 +02:00
|
|
|
|
bool wakeup() {
|
|
|
|
|
struct kevent ev;
|
|
|
|
|
EV_SET(&ev, mUserEvent, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-05-14 17:16:45 +02:00
|
|
|
|
struct timespec timeout = { 0, 0 };
|
|
|
|
|
return kevent(mKqueueFd, &ev, 1, NULL, 0, &timeout) == 0;
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
private:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
void canRecv(bool) override {
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-05-14 17:16:45 +02:00
|
|
|
|
void canSend() override {
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-05-14 17:16:45 +02:00
|
|
|
|
void onClose() override {
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
private:
|
2021-05-14 17:16:45 +02:00
|
|
|
|
int mKqueueFd;
|
|
|
|
|
int mUserEvent;
|
2021-04-30 16:10:14 +02:00
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
#endif
|
|
|
|
|
|