event/PollGroup: ReadEvents() returns PollResult
This commit is contained in:
parent
0ecc3394c3
commit
1473d8474f
@ -257,8 +257,8 @@ EventLoop::Run() noexcept
|
|||||||
|
|
||||||
/* wait for new event */
|
/* wait for new event */
|
||||||
|
|
||||||
PollResult poll_result;
|
const auto poll_result =
|
||||||
poll_group.ReadEvents(poll_result, ExportTimeoutMS(timeout));
|
poll_group.ReadEvents(ExportTimeoutMS(timeout));
|
||||||
|
|
||||||
ready_sockets.clear();
|
ready_sockets.clear();
|
||||||
for (size_t i = 0; i < poll_result.GetSize(); ++i) {
|
for (size_t i = 0; i < poll_result.GetSize(); ++i) {
|
||||||
|
@ -25,19 +25,16 @@
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
#include "PollGroupWinSelect.hxx"
|
#include "PollGroupWinSelect.hxx"
|
||||||
typedef PollResultGeneric PollResult;
|
|
||||||
typedef PollGroupWinSelect PollGroup;
|
typedef PollGroupWinSelect PollGroup;
|
||||||
|
|
||||||
#elif defined(USE_EPOLL)
|
#elif defined(USE_EPOLL)
|
||||||
|
|
||||||
#include "PollGroupEpoll.hxx"
|
#include "PollGroupEpoll.hxx"
|
||||||
typedef PollResultEpoll PollResult;
|
|
||||||
typedef PollGroupEpoll PollGroup;
|
typedef PollGroupEpoll PollGroup;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include "PollGroupPoll.hxx"
|
#include "PollGroupPoll.hxx"
|
||||||
typedef PollResultGeneric PollResult;
|
|
||||||
typedef PollGroupPoll PollGroup;
|
typedef PollGroupPoll PollGroup;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -61,10 +61,12 @@ public:
|
|||||||
|
|
||||||
PollGroupEpoll() = default;
|
PollGroupEpoll() = default;
|
||||||
|
|
||||||
void ReadEvents(PollResultEpoll &result, int timeout_ms) noexcept {
|
auto ReadEvents(int timeout_ms) noexcept {
|
||||||
|
PollResultEpoll result;
|
||||||
int ret = epoll.Wait(result.events.data(), result.events.size(),
|
int ret = epoll.Wait(result.events.data(), result.events.size(),
|
||||||
timeout_ms);
|
timeout_ms);
|
||||||
result.n_events = std::max(0, ret);
|
result.n_events = std::max(0, ret);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Add(int fd, unsigned events, void *obj) noexcept {
|
bool Add(int fd, unsigned events, void *obj) noexcept {
|
||||||
|
@ -71,12 +71,13 @@ PollGroupPoll::Remove(int fd) noexcept
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
PollResultGeneric
|
||||||
PollGroupPoll::ReadEvents(PollResultGeneric &result, int timeout_ms) noexcept
|
PollGroupPoll::ReadEvents(int timeout_ms) noexcept
|
||||||
{
|
{
|
||||||
int n = poll(poll_events.empty() ? nullptr : &poll_events[0],
|
int n = poll(poll_events.empty() ? nullptr : &poll_events[0],
|
||||||
poll_events.size(), timeout_ms);
|
poll_events.size(), timeout_ms);
|
||||||
|
|
||||||
|
PollResultGeneric result;
|
||||||
for (size_t i = 0; n > 0 && i < poll_events.size(); ++i) {
|
for (size_t i = 0; n > 0 && i < poll_events.size(); ++i) {
|
||||||
const auto &e = poll_events[i];
|
const auto &e = poll_events[i];
|
||||||
if (e.revents != 0) {
|
if (e.revents != 0) {
|
||||||
@ -84,4 +85,6 @@ PollGroupPoll::ReadEvents(PollResultGeneric &result, int timeout_ms) noexcept
|
|||||||
--n;
|
--n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ public:
|
|||||||
PollGroupPoll() noexcept;
|
PollGroupPoll() noexcept;
|
||||||
~PollGroupPoll() noexcept;
|
~PollGroupPoll() noexcept;
|
||||||
|
|
||||||
void ReadEvents(PollResultGeneric &result, int timeout_ms) noexcept;
|
PollResultGeneric ReadEvents(int timeout_ms) noexcept;
|
||||||
bool Add(int fd, unsigned events, void *obj) noexcept;
|
bool Add(int fd, unsigned events, void *obj) noexcept;
|
||||||
bool Modify(int fd, unsigned events, void *obj) noexcept;
|
bool Modify(int fd, unsigned events, void *obj) noexcept;
|
||||||
bool Remove(int fd) noexcept;
|
bool Remove(int fd) noexcept;
|
||||||
|
@ -115,9 +115,8 @@ PollGroupWinSelect::Remove(SOCKET fd) noexcept
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
PollResultGeneric
|
||||||
PollGroupWinSelect::ReadEvents(PollResultGeneric &result,
|
PollGroupWinSelect::ReadEvents(int timeout_ms) noexcept
|
||||||
int timeout_ms) noexcept
|
|
||||||
{
|
{
|
||||||
bool use_sleep = event_set[EVENT_READ].IsEmpty() &&
|
bool use_sleep = event_set[EVENT_READ].IsEmpty() &&
|
||||||
event_set[EVENT_WRITE].IsEmpty();
|
event_set[EVENT_WRITE].IsEmpty();
|
||||||
@ -143,8 +142,9 @@ PollGroupWinSelect::ReadEvents(PollResultGeneric &result,
|
|||||||
except_set.GetPtr(),
|
except_set.GetPtr(),
|
||||||
timeout_ms < 0 ? nullptr : &tv);
|
timeout_ms < 0 ? nullptr : &tv);
|
||||||
|
|
||||||
|
PollResultGeneric result;
|
||||||
if (ret == 0 || ret == SOCKET_ERROR)
|
if (ret == 0 || ret == SOCKET_ERROR)
|
||||||
return;
|
return result;
|
||||||
|
|
||||||
for (const auto i : read_set)
|
for (const auto i : read_set)
|
||||||
items[i].events |= READ;
|
items[i].events |= READ;
|
||||||
@ -160,4 +160,6 @@ PollGroupWinSelect::ReadEvents(PollResultGeneric &result,
|
|||||||
result.Add(i.second.events, i.second.obj);
|
result.Add(i.second.events, i.second.obj);
|
||||||
i.second.events = 0;
|
i.second.events = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ public:
|
|||||||
PollGroupWinSelect() noexcept;
|
PollGroupWinSelect() noexcept;
|
||||||
~PollGroupWinSelect() noexcept;
|
~PollGroupWinSelect() noexcept;
|
||||||
|
|
||||||
void ReadEvents(PollResultGeneric &result, int timeout_ms) noexcept;
|
PollResultGeneric ReadEvents(int timeout_ms) noexcept;
|
||||||
bool Add(SOCKET fd, unsigned events, void *obj) noexcept;
|
bool Add(SOCKET fd, unsigned events, void *obj) noexcept;
|
||||||
bool Modify(SOCKET fd, unsigned events, void *obj) noexcept;
|
bool Modify(SOCKET fd, unsigned events, void *obj) noexcept;
|
||||||
bool Remove(SOCKET fd) noexcept;
|
bool Remove(SOCKET fd) noexcept;
|
||||||
|
Loading…
Reference in New Issue
Block a user