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>
|
|
|
|
|
|
|
|
|
|
namespace brynet { namespace net { namespace detail {
|
|
|
|
|
|
|
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
2021-04-30 16:10:14 +02:00
|
|
|
|
class WakeupChannel final : public Channel, public brynet::base::NonCopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit WakeupChannel(HANDLE iocp)
|
|
|
|
|
: mIOCP(iocp),
|
|
|
|
|
mWakeupOvl(port::Win::OverlappedType::OverlappedRecv)
|
2020-11-24 15:41:18 +01:00
|
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
bool wakeup() BRYNET_NOEXCEPT
|
|
|
|
|
{
|
|
|
|
|
return PostQueuedCompletionStatus(mIOCP,
|
|
|
|
|
0,
|
|
|
|
|
reinterpret_cast<ULONG_PTR>(this),
|
|
|
|
|
&mWakeupOvl.base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void canRecv(bool) BRYNET_NOEXCEPT override
|
|
|
|
|
{
|
|
|
|
|
;
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
void canSend() BRYNET_NOEXCEPT override
|
|
|
|
|
{
|
|
|
|
|
;
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
void onClose() BRYNET_NOEXCEPT override
|
|
|
|
|
{
|
|
|
|
|
;
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
HANDLE mIOCP;
|
|
|
|
|
port::Win::OverlappedExt mWakeupOvl;
|
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
#elif defined BRYNET_PLATFORM_LINUX
|
2021-04-30 16:10:14 +02:00
|
|
|
|
class WakeupChannel final : public Channel, public brynet::base::NonCopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit WakeupChannel(BrynetSocketFD fd)
|
|
|
|
|
: mUniqueFd(fd)
|
2020-11-24 15:41:18 +01:00
|
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +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:
|
|
|
|
|
void canRecv(bool) override
|
|
|
|
|
{
|
|
|
|
|
char temp[1024 * 10];
|
|
|
|
|
while (true)
|
2020-11-24 15:41:18 +01:00
|
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
|
auto n = read(mUniqueFd.getFD(), temp, sizeof(temp));
|
|
|
|
|
if (n == -1 || static_cast<size_t>(n) < sizeof(temp))
|
2020-11-24 15:41:18 +01:00
|
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
|
break;
|
2020-11-24 15:41:18 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-30 16:10:14 +02:00
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
void canSend() override
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
void onClose() override
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
private:
|
|
|
|
|
UniqueFd mUniqueFd;
|
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
|
|
|
|
#elif defined BRYNET_PLATFORM_DARWIN
|
2021-04-30 16:10:14 +02:00
|
|
|
|
class WakeupChannel final : public Channel, public brynet::base::NonCopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit WakeupChannel(int kqueuefd, int ident)
|
|
|
|
|
: mKqueueFd(kqueuefd),
|
|
|
|
|
mUserEvent(ident)
|
2020-11-24 15:41:18 +01:00
|
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +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-04-30 16:10:14 +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:
|
|
|
|
|
void canRecv(bool) override
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
void canSend() override
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
void onClose() override
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
private:
|
|
|
|
|
int mKqueueFd;
|
|
|
|
|
int mUserEvent;
|
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
|
}}}// namespace brynet::net::detail
|