mirror of https://github.com/aria2/aria2
Refactor event poll classes so that we don't have to allocate memory on query
I left Port and libuv classes, since I don't have them compiled. So they were updated minimally.pull/287/head
parent
f55c16c7ed
commit
596e5c6162
|
@ -132,12 +132,11 @@ void EpollEventPoll::poll(const struct timeval& tv)
|
||||||
// own timeout and ares may create new sockets or closes socket in
|
// own timeout and ares may create new sockets or closes socket in
|
||||||
// their API. So we call ares_process_fd for all ares_channel and
|
// their API. So we call ares_process_fd for all ares_channel and
|
||||||
// re-register their sockets.
|
// re-register their sockets.
|
||||||
for(KAsyncNameResolverEntrySet::iterator i =
|
for(auto& i : nameResolverEntries_) {
|
||||||
nameResolverEntries_.begin(), eoi = nameResolverEntries_.end();
|
auto& ent = i.second;
|
||||||
i != eoi; ++i) {
|
ent.processTimeout();
|
||||||
(*i)->processTimeout();
|
ent.removeSocketEvents(this);
|
||||||
(*i)->removeSocketEvents(this);
|
ent.addSocketEvents(this);
|
||||||
(*i)->addSocketEvents(this);
|
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
@ -168,36 +167,37 @@ int translateEvents(EventPoll::EventType events)
|
||||||
bool EpollEventPoll::addEvents(sock_t socket,
|
bool EpollEventPoll::addEvents(sock_t socket,
|
||||||
const EpollEventPoll::KEvent& event)
|
const EpollEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
auto socketEntry = std::make_shared<KSocketEntry>(socket);
|
auto i = socketEntries_.lower_bound(socket);
|
||||||
KSocketEntrySet::iterator i = socketEntries_.lower_bound(socketEntry);
|
|
||||||
int r = 0;
|
int r = 0;
|
||||||
int errNum = 0;
|
int errNum = 0;
|
||||||
if(i != socketEntries_.end() && *(*i) == *socketEntry) {
|
if(i != std::end(socketEntries_) && (*i).first == socket) {
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
|
|
||||||
event.addSelf(*i);
|
event.addSelf(&socketEntry);
|
||||||
|
|
||||||
struct epoll_event epEvent = (*i)->getEvents();
|
struct epoll_event epEvent = socketEntry.getEvents();
|
||||||
r = epoll_ctl(epfd_, EPOLL_CTL_MOD, (*i)->getSocket(), &epEvent);
|
r = epoll_ctl(epfd_, EPOLL_CTL_MOD, socketEntry.getSocket(), &epEvent);
|
||||||
if(r == -1) {
|
if(r == -1) {
|
||||||
// try EPOLL_CTL_ADD: There is a chance that previously socket X is
|
// try EPOLL_CTL_ADD: There is a chance that previously socket X is
|
||||||
// added to epoll, but it is closed and is not yet removed from
|
// added to epoll, but it is closed and is not yet removed from
|
||||||
// SocketEntries. In this case, EPOLL_CTL_MOD is failed with ENOENT.
|
// SocketEntries. In this case, EPOLL_CTL_MOD is failed with ENOENT.
|
||||||
|
|
||||||
r = epoll_ctl(epfd_, EPOLL_CTL_ADD, (*i)->getSocket(),
|
r = epoll_ctl(epfd_, EPOLL_CTL_ADD, socketEntry.getSocket(),
|
||||||
&epEvent);
|
&epEvent);
|
||||||
errNum = errno;
|
errNum = errno;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
socketEntries_.insert(i, socketEntry);
|
i = socketEntries_.insert(i, std::make_pair(socket, KSocketEntry(socket)));
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
if(socketEntries_.size() > epEventsSize_) {
|
if(socketEntries_.size() > epEventsSize_) {
|
||||||
epEventsSize_ *= 2;
|
epEventsSize_ *= 2;
|
||||||
epEvents_ = make_unique<struct epoll_event[]>(epEventsSize_);
|
epEvents_ = make_unique<struct epoll_event[]>(epEventsSize_);
|
||||||
}
|
}
|
||||||
|
|
||||||
event.addSelf(socketEntry);
|
event.addSelf(&socketEntry);
|
||||||
|
|
||||||
struct epoll_event epEvent = socketEntry->getEvents();
|
struct epoll_event epEvent = socketEntry.getEvents();
|
||||||
r = epoll_ctl(epfd_, EPOLL_CTL_ADD, socketEntry->getSocket(), &epEvent);
|
r = epoll_ctl(epfd_, EPOLL_CTL_ADD, socketEntry.getSocket(), &epEvent);
|
||||||
errNum = errno;
|
errNum = errno;
|
||||||
}
|
}
|
||||||
if(r == -1) {
|
if(r == -1) {
|
||||||
|
@ -228,41 +228,41 @@ bool EpollEventPoll::addEvents(sock_t socket, Command* command, int events,
|
||||||
bool EpollEventPoll::deleteEvents(sock_t socket,
|
bool EpollEventPoll::deleteEvents(sock_t socket,
|
||||||
const EpollEventPoll::KEvent& event)
|
const EpollEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
auto socketEntry = std::make_shared<KSocketEntry>(socket);
|
auto i = socketEntries_.find(socket);
|
||||||
KSocketEntrySet::iterator i = socketEntries_.find(socketEntry);
|
if(i == std::end(socketEntries_)) {
|
||||||
if(i == socketEntries_.end()) {
|
|
||||||
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
|
event.removeSelf(&socketEntry);
|
||||||
|
int r = 0;
|
||||||
|
int errNum = 0;
|
||||||
|
if(socketEntry.eventEmpty()) {
|
||||||
|
// In kernel before 2.6.9, epoll_ctl with EPOLL_CTL_DEL requires non-null
|
||||||
|
// pointer of epoll_event.
|
||||||
|
struct epoll_event ev = {0,{0}};
|
||||||
|
r = epoll_ctl(epfd_, EPOLL_CTL_DEL, socketEntry.getSocket(), &ev);
|
||||||
|
errNum = errno;
|
||||||
|
socketEntries_.erase(i);
|
||||||
} else {
|
} else {
|
||||||
event.removeSelf(*i);
|
// If socket is closed, then it seems it is automatically removed from
|
||||||
int r = 0;
|
// epoll, so following EPOLL_CTL_MOD may fail.
|
||||||
int errNum = 0;
|
struct epoll_event epEvent = socketEntry.getEvents();
|
||||||
if((*i)->eventEmpty()) {
|
r = epoll_ctl(epfd_, EPOLL_CTL_MOD, socketEntry.getSocket(), &epEvent);
|
||||||
// In kernel before 2.6.9, epoll_ctl with EPOLL_CTL_DEL requires non-null
|
errNum = errno;
|
||||||
// pointer of epoll_event.
|
|
||||||
struct epoll_event ev = {0,{0}};
|
|
||||||
r = epoll_ctl(epfd_, EPOLL_CTL_DEL, (*i)->getSocket(), &ev);
|
|
||||||
errNum = errno;
|
|
||||||
socketEntries_.erase(i);
|
|
||||||
} else {
|
|
||||||
// If socket is closed, then it seems it is automatically removed from
|
|
||||||
// epoll, so following EPOLL_CTL_MOD may fail.
|
|
||||||
struct epoll_event epEvent = (*i)->getEvents();
|
|
||||||
r = epoll_ctl(epfd_, EPOLL_CTL_MOD, (*i)->getSocket(), &epEvent);
|
|
||||||
errNum = errno;
|
|
||||||
if(r == -1) {
|
|
||||||
A2_LOG_DEBUG(fmt("Failed to delete socket event, but may be ignored:%s",
|
|
||||||
util::safeStrerror(errNum).c_str()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(r == -1) {
|
if(r == -1) {
|
||||||
A2_LOG_DEBUG(fmt("Failed to delete socket event:%s",
|
A2_LOG_DEBUG(fmt("Failed to delete socket event, but may be ignored:%s",
|
||||||
util::safeStrerror(errNum).c_str()));
|
util::safeStrerror(errNum).c_str()));
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(r == -1) {
|
||||||
|
A2_LOG_DEBUG(fmt("Failed to delete socket event:%s",
|
||||||
|
util::safeStrerror(errNum).c_str()));
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
|
@ -284,31 +284,31 @@ bool EpollEventPoll::deleteEvents(sock_t socket, Command* command,
|
||||||
bool EpollEventPoll::addNameResolver
|
bool EpollEventPoll::addNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
auto entry = std::make_shared<KAsyncNameResolverEntry>(resolver, command);
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
KAsyncNameResolverEntrySet::iterator itr =
|
auto itr = nameResolverEntries_.lower_bound(key);
|
||||||
nameResolverEntries_.lower_bound(entry);
|
|
||||||
if(itr != nameResolverEntries_.end() && *(*itr) == *entry) {
|
if(itr != std::end(nameResolverEntries_) && (*itr).first == key) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
nameResolverEntries_.insert(itr, entry);
|
|
||||||
entry->addSocketEvents(this);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itr = nameResolverEntries_.insert
|
||||||
|
(itr, std::make_pair(key, KAsyncNameResolverEntry(resolver, command)));
|
||||||
|
(*itr).second.addSocketEvents(this);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EpollEventPoll::deleteNameResolver
|
bool EpollEventPoll::deleteNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
auto entry = std::make_shared<KAsyncNameResolverEntry>(resolver, command);
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
KAsyncNameResolverEntrySet::iterator itr =
|
auto itr = nameResolverEntries_.find(key);
|
||||||
nameResolverEntries_.find(entry);
|
if(itr == std::end(nameResolverEntries_)) {
|
||||||
if(itr == nameResolverEntries_.end()) {
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
(*itr)->removeSocketEvents(this);
|
|
||||||
nameResolverEntries_.erase(itr);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(*itr).second.removeSocketEvents(this);
|
||||||
|
nameResolverEntries_.erase(itr);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
# include <sys/epoll.h>
|
# include <sys/epoll.h>
|
||||||
|
|
||||||
#include <set>
|
#include <map>
|
||||||
|
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
|
@ -64,19 +64,20 @@ private:
|
||||||
public:
|
public:
|
||||||
KSocketEntry(sock_t socket);
|
KSocketEntry(sock_t socket);
|
||||||
|
|
||||||
|
KSocketEntry(const KSocketEntry&) = delete;
|
||||||
|
KSocketEntry(KSocketEntry&&) = default;
|
||||||
|
|
||||||
struct epoll_event getEvents();
|
struct epoll_event getEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
friend int accumulateEvent(int events, const KEvent& event);
|
friend int accumulateEvent(int events, const KEvent& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::set<std::shared_ptr<KSocketEntry>,
|
typedef std::map<sock_t, KSocketEntry> KSocketEntrySet;
|
||||||
DerefLess<std::shared_ptr<KSocketEntry> > > KSocketEntrySet;
|
|
||||||
KSocketEntrySet socketEntries_;
|
KSocketEntrySet socketEntries_;
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
typedef std::set<std::shared_ptr<KAsyncNameResolverEntry>,
|
typedef std::map<std::pair<AsyncNameResolver*, Command*>,
|
||||||
DerefLess<std::shared_ptr<KAsyncNameResolverEntry> > >
|
KAsyncNameResolverEntry> KAsyncNameResolverEntrySet;
|
||||||
KAsyncNameResolverEntrySet;
|
|
||||||
KAsyncNameResolverEntrySet nameResolverEntries_;
|
KAsyncNameResolverEntrySet nameResolverEntries_;
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
|
19
src/Event.h
19
src/Event.h
|
@ -59,10 +59,9 @@ public:
|
||||||
|
|
||||||
virtual int getEvents() const = 0;
|
virtual int getEvents() const = 0;
|
||||||
|
|
||||||
virtual void addSelf(const std::shared_ptr<SocketEntry>& socketEntry) const = 0;
|
virtual void addSelf(SocketEntry* socketEntry) const = 0;
|
||||||
|
|
||||||
virtual void removeSelf
|
virtual void removeSelf(SocketEntry* socketEntry) const = 0;
|
||||||
(const std::shared_ptr<SocketEntry>& socketEntry) const = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename SocketEntry, typename EventPoll>
|
template<typename SocketEntry, typename EventPoll>
|
||||||
|
@ -124,12 +123,12 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void addSelf(const std::shared_ptr<SocketEntry>& socketEntry) const
|
virtual void addSelf(SocketEntry* socketEntry) const
|
||||||
{
|
{
|
||||||
socketEntry->addCommandEvent(*this);
|
socketEntry->addCommandEvent(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void removeSelf(const std::shared_ptr<SocketEntry>& socketEntry) const
|
virtual void removeSelf(SocketEntry* socketEntry) const
|
||||||
{
|
{
|
||||||
socketEntry->removeCommandEvent(*this);
|
socketEntry->removeCommandEvent(*this);
|
||||||
}
|
}
|
||||||
|
@ -179,12 +178,12 @@ public:
|
||||||
command_->setStatusActive();
|
command_->setStatusActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void addSelf(const std::shared_ptr<SocketEntry>& socketEntry) const
|
virtual void addSelf(SocketEntry* socketEntry) const
|
||||||
{
|
{
|
||||||
socketEntry->addADNSEvent(*this);
|
socketEntry->addADNSEvent(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void removeSelf(const std::shared_ptr<SocketEntry>& socketEntry) const
|
virtual void removeSelf(SocketEntry* socketEntry) const
|
||||||
{
|
{
|
||||||
socketEntry->removeADNSEvent(*this);
|
socketEntry->removeADNSEvent(*this);
|
||||||
}
|
}
|
||||||
|
@ -209,7 +208,8 @@ protected:
|
||||||
public:
|
public:
|
||||||
SocketEntry(sock_t socket):socket_(socket) {}
|
SocketEntry(sock_t socket):socket_(socket) {}
|
||||||
|
|
||||||
virtual ~SocketEntry() {}
|
SocketEntry(const SocketEntry&) = delete;
|
||||||
|
SocketEntry(SocketEntry&&) = default;
|
||||||
|
|
||||||
bool operator==(const SocketEntry& entry) const
|
bool operator==(const SocketEntry& entry) const
|
||||||
{
|
{
|
||||||
|
@ -318,6 +318,9 @@ public:
|
||||||
Command* command):
|
Command* command):
|
||||||
nameResolver_(std::move(nameResolver)), command_(command), socketsSize_(0) {}
|
nameResolver_(std::move(nameResolver)), command_(command), socketsSize_(0) {}
|
||||||
|
|
||||||
|
AsyncNameResolverEntry(const AsyncNameResolverEntry&) = delete;
|
||||||
|
AsyncNameResolverEntry(AsyncNameResolverEntry&&) = default;
|
||||||
|
|
||||||
bool operator==(const AsyncNameResolverEntry& entry)
|
bool operator==(const AsyncNameResolverEntry& entry)
|
||||||
{
|
{
|
||||||
return *nameResolver_ == *entry.nameResolver_ &&
|
return *nameResolver_ == *entry.nameResolver_ &&
|
||||||
|
|
|
@ -89,7 +89,7 @@ size_t KqueueEventPoll::KSocketEntry::getEvents
|
||||||
|
|
||||||
KqueueEventPoll::KqueueEventPoll()
|
KqueueEventPoll::KqueueEventPoll()
|
||||||
: kqEventsSize_(KQUEUE_EVENTS_MAX),
|
: kqEventsSize_(KQUEUE_EVENTS_MAX),
|
||||||
kqEvents_(new struct kevent[kqEventsSize_])
|
kqEvents_(make_unique<struct kevent[]>(kqEventsSize_))
|
||||||
{
|
{
|
||||||
kqfd_ = kqueue();
|
kqfd_ = kqueue();
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,6 @@ KqueueEventPoll::~KqueueEventPoll()
|
||||||
util::safeStrerror(errNum).c_str()));
|
util::safeStrerror(errNum).c_str()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete [] kqEvents_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KqueueEventPoll::good() const
|
bool KqueueEventPoll::good() const
|
||||||
|
@ -118,7 +117,8 @@ void KqueueEventPoll::poll(const struct timeval& tv)
|
||||||
{
|
{
|
||||||
struct timespec timeout = { tv.tv_sec, tv.tv_usec*1000 };
|
struct timespec timeout = { tv.tv_sec, tv.tv_usec*1000 };
|
||||||
int res;
|
int res;
|
||||||
while((res = kevent(kqfd_, kqEvents_, 0, kqEvents_, kqEventsSize_, &timeout))
|
while((res = kevent(kqfd_, kqEvents_.get(), 0, kqEvents_.get(),
|
||||||
|
kqEventsSize_, &timeout))
|
||||||
== -1 && errno == EINTR);
|
== -1 && errno == EINTR);
|
||||||
if(res > 0) {
|
if(res > 0) {
|
||||||
for(int i = 0; i < res; ++i) {
|
for(int i = 0; i < res; ++i) {
|
||||||
|
@ -142,9 +142,10 @@ void KqueueEventPoll::poll(const struct timeval& tv)
|
||||||
// their API. So we call ares_process_fd for all ares_channel and
|
// their API. So we call ares_process_fd for all ares_channel and
|
||||||
// re-register their sockets.
|
// re-register their sockets.
|
||||||
for(auto & r : nameResolverEntries_) {
|
for(auto & r : nameResolverEntries_) {
|
||||||
r->processTimeout();
|
auto& ent = r.second;
|
||||||
r->removeSocketEvents(this);
|
ent.processTimeout();
|
||||||
r->addSocketEvents(this);
|
ent.removeSocketEvents(this);
|
||||||
|
ent.addSocketEvents(this);
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
@ -169,24 +170,24 @@ int translateEvents(EventPoll::EventType events)
|
||||||
bool KqueueEventPoll::addEvents
|
bool KqueueEventPoll::addEvents
|
||||||
(sock_t socket, const KqueueEventPoll::KEvent& event)
|
(sock_t socket, const KqueueEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
|
auto i = socketEntries_.lower_bound(socket);
|
||||||
auto i = socketEntries_.lower_bound(socketEntry);
|
|
||||||
int r = 0;
|
int r = 0;
|
||||||
struct timespec zeroTimeout = { 0, 0 };
|
struct timespec zeroTimeout = { 0, 0 };
|
||||||
struct kevent changelist[2];
|
struct kevent changelist[2];
|
||||||
size_t n;
|
size_t n;
|
||||||
if(i != socketEntries_.end() && *(*i) == *socketEntry) {
|
if(i != std::end(socketEntries_) && (*i).first == socket) {
|
||||||
event.addSelf(*i);
|
auto& socketEntry = (*i).second;
|
||||||
n = (*i)->getEvents(changelist);
|
event.addSelf(&socketEntry);
|
||||||
|
n = socketEntry.getEvents(changelist);
|
||||||
} else {
|
} else {
|
||||||
socketEntries_.insert(i, socketEntry);
|
i = socketEntries_.insert(i, std::make_pair(socket, KSocketEntry(socket)));
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
if(socketEntries_.size() > kqEventsSize_) {
|
if(socketEntries_.size() > kqEventsSize_) {
|
||||||
kqEventsSize_ *= 2;
|
kqEventsSize_ *= 2;
|
||||||
delete [] kqEvents_;
|
kqEvents_ = make_unique<struct kevent[]>(kqEventsSize_);
|
||||||
kqEvents_ = new struct kevent[kqEventsSize_];
|
|
||||||
}
|
}
|
||||||
event.addSelf(socketEntry);
|
event.addSelf(&socketEntry);
|
||||||
n = socketEntry->getEvents(changelist);
|
n = socketEntry.getEvents(changelist);
|
||||||
}
|
}
|
||||||
r = kevent(kqfd_, changelist, n, changelist, 0, &zeroTimeout);
|
r = kevent(kqfd_, changelist, n, changelist, 0, &zeroTimeout);
|
||||||
int errNum = errno;
|
int errNum = errno;
|
||||||
|
@ -201,7 +202,7 @@ bool KqueueEventPoll::addEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KqueueEventPoll::addEvents(sock_t socket, Command* command,
|
bool KqueueEventPoll::addEvents(sock_t socket, Command* command,
|
||||||
EventPoll::EventType events)
|
EventPoll::EventType events)
|
||||||
{
|
{
|
||||||
int kqEvents = translateEvents(events);
|
int kqEvents = translateEvents(events);
|
||||||
return addEvents(socket, KCommandEvent(command, kqEvents));
|
return addEvents(socket, KCommandEvent(command, kqEvents));
|
||||||
|
@ -216,44 +217,44 @@ bool KqueueEventPoll::addEvents(sock_t socket, Command* command, int events,
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
bool KqueueEventPoll::deleteEvents(sock_t socket,
|
bool KqueueEventPoll::deleteEvents(sock_t socket,
|
||||||
const KqueueEventPoll::KEvent& event)
|
const KqueueEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
|
auto i = socketEntries_.find(socket);
|
||||||
auto i = socketEntries_.find(socketEntry);
|
if(i == std::end(socketEntries_)) {
|
||||||
if(i == socketEntries_.end()) {
|
|
||||||
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
|
event.removeSelf(&socketEntry);
|
||||||
|
int r = 0;
|
||||||
|
struct timespec zeroTimeout = { 0, 0 };
|
||||||
|
struct kevent changelist[2];
|
||||||
|
size_t n = socketEntry.getEvents(changelist);
|
||||||
|
r = kevent(kqfd_, changelist, n, changelist, 0, &zeroTimeout);
|
||||||
|
int errNum = errno;
|
||||||
|
if(socketEntry.eventEmpty()) {
|
||||||
|
socketEntries_.erase(i);
|
||||||
|
}
|
||||||
|
if(r == -1) {
|
||||||
|
A2_LOG_DEBUG(fmt("Failed to delete socket event:%s",
|
||||||
|
util::safeStrerror(errNum).c_str()));
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
event.removeSelf(*i);
|
return true;
|
||||||
int r = 0;
|
|
||||||
struct timespec zeroTimeout = { 0, 0 };
|
|
||||||
struct kevent changelist[2];
|
|
||||||
size_t n = (*i)->getEvents(changelist);
|
|
||||||
r = kevent(kqfd_, changelist, n, changelist, 0, &zeroTimeout);
|
|
||||||
int errNum = errno;
|
|
||||||
if((*i)->eventEmpty()) {
|
|
||||||
socketEntries_.erase(i);
|
|
||||||
}
|
|
||||||
if(r == -1) {
|
|
||||||
A2_LOG_DEBUG(fmt("Failed to delete socket event:%s",
|
|
||||||
util::safeStrerror(errNum).c_str()));
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
|
bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
|
||||||
const std::shared_ptr<AsyncNameResolver>& rs)
|
const std::shared_ptr<AsyncNameResolver>& rs)
|
||||||
{
|
{
|
||||||
return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
|
return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
|
bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
|
||||||
EventPoll::EventType events)
|
EventPoll::EventType events)
|
||||||
{
|
{
|
||||||
int kqEvents = translateEvents(events);
|
int kqEvents = translateEvents(events);
|
||||||
return deleteEvents(socket, KCommandEvent(command, kqEvents));
|
return deleteEvents(socket, KCommandEvent(command, kqEvents));
|
||||||
|
@ -263,16 +264,17 @@ bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
|
||||||
bool KqueueEventPoll::addNameResolver
|
bool KqueueEventPoll::addNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KAsyncNameResolverEntry> entry
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
(new KAsyncNameResolverEntry(resolver, command));
|
auto itr = nameResolverEntries_.lower_bound(key);
|
||||||
auto itr = nameResolverEntries_.find(entry);
|
|
||||||
if(itr == nameResolverEntries_.end()) {
|
if(itr != std::end(nameResolverEntries_) && (*itr).first == key) {
|
||||||
nameResolverEntries_.insert(entry);
|
|
||||||
entry->addSocketEvents(this);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itr = nameResolverEntries_.insert
|
||||||
|
(itr, std::make_pair(key, KAsyncNameResolverEntry(resolver, command)));
|
||||||
|
(*itr).second.addSocketEvents(this);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KqueueEventPoll::deleteNameResolver
|
bool KqueueEventPoll::deleteNameResolver
|
||||||
|
@ -280,14 +282,15 @@ bool KqueueEventPoll::deleteNameResolver
|
||||||
{
|
{
|
||||||
std::shared_ptr<KAsyncNameResolverEntry> entry
|
std::shared_ptr<KAsyncNameResolverEntry> entry
|
||||||
(new KAsyncNameResolverEntry(resolver, command));
|
(new KAsyncNameResolverEntry(resolver, command));
|
||||||
auto itr = nameResolverEntries_.find(entry);
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
if(itr == nameResolverEntries_.end()) {
|
auto itr = nameResolverEntries_.find(key);
|
||||||
|
if(itr == std::end(nameResolverEntries_)) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
(*itr)->removeSocketEvents(this);
|
|
||||||
nameResolverEntries_.erase(itr);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(*itr).second.removeSocketEvents(this);
|
||||||
|
nameResolverEntries_.erase(itr);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
#include <sys/event.h>
|
#include <sys/event.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#include <set>
|
#include <map>
|
||||||
|
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
|
@ -67,6 +67,9 @@ private:
|
||||||
public:
|
public:
|
||||||
KSocketEntry(sock_t socket);
|
KSocketEntry(sock_t socket);
|
||||||
|
|
||||||
|
KSocketEntry(const KSocketEntry&) = delete;
|
||||||
|
KSocketEntry(KSocketEntry&&) = default;
|
||||||
|
|
||||||
// eventlist should be at least size 2. This function returns the
|
// eventlist should be at least size 2. This function returns the
|
||||||
// number of filled struct kevent in eventlist.
|
// number of filled struct kevent in eventlist.
|
||||||
size_t getEvents(struct kevent* eventlist);
|
size_t getEvents(struct kevent* eventlist);
|
||||||
|
@ -75,13 +78,11 @@ private:
|
||||||
friend int accumulateEvent(int events, const KEvent& event);
|
friend int accumulateEvent(int events, const KEvent& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::set<std::shared_ptr<KSocketEntry>,
|
typedef std::map<sock_t, KSocketEntry> KSocketEntrySet;
|
||||||
DerefLess<std::shared_ptr<KSocketEntry> > > KSocketEntrySet;
|
|
||||||
KSocketEntrySet socketEntries_;
|
KSocketEntrySet socketEntries_;
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
typedef std::set<std::shared_ptr<KAsyncNameResolverEntry>,
|
typedef std::map<std::pair<AsyncNameResolver*, Command*>,
|
||||||
DerefLess<std::shared_ptr<KAsyncNameResolverEntry> > >
|
KAsyncNameResolverEntry> KAsyncNameResolverEntrySet;
|
||||||
KAsyncNameResolverEntrySet;
|
|
||||||
KAsyncNameResolverEntrySet nameResolverEntries_;
|
KAsyncNameResolverEntrySet nameResolverEntries_;
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
@ -89,7 +90,7 @@ private:
|
||||||
|
|
||||||
size_t kqEventsSize_;
|
size_t kqEventsSize_;
|
||||||
|
|
||||||
struct kevent* kqEvents_;
|
std::unique_ptr<struct kevent[]> kqEvents_;
|
||||||
|
|
||||||
static const size_t KQUEUE_EVENTS_MAX = 1024;
|
static const size_t KQUEUE_EVENTS_MAX = 1024;
|
||||||
|
|
||||||
|
|
|
@ -233,7 +233,7 @@ bool LibuvEventPoll::addEvents(sock_t socket,
|
||||||
auto i = socketEntries_.lower_bound(socketEntry);
|
auto i = socketEntries_.lower_bound(socketEntry);
|
||||||
|
|
||||||
if (i != socketEntries_.end() && **i == *socketEntry) {
|
if (i != socketEntries_.end() && **i == *socketEntry) {
|
||||||
event.addSelf(*i);
|
event.addSelf((*i).get());
|
||||||
auto poll = polls_.find(socket);
|
auto poll = polls_.find(socket);
|
||||||
if (poll == polls_.end()) {
|
if (poll == polls_.end()) {
|
||||||
throw std::logic_error("Invalid socket");
|
throw std::logic_error("Invalid socket");
|
||||||
|
@ -243,7 +243,7 @@ bool LibuvEventPoll::addEvents(sock_t socket,
|
||||||
}
|
}
|
||||||
|
|
||||||
socketEntries_.insert(i, socketEntry);
|
socketEntries_.insert(i, socketEntry);
|
||||||
event.addSelf(socketEntry);
|
event.addSelf(socketEntry.get());
|
||||||
auto poll = new KPoll(this, socketEntry.get(), socket);
|
auto poll = new KPoll(this, socketEntry.get(), socket);
|
||||||
polls_[socket] = poll;
|
polls_[socket] = poll;
|
||||||
poll->start();
|
poll->start();
|
||||||
|
@ -275,7 +275,7 @@ bool LibuvEventPoll::deleteEvents(sock_t socket,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
event.removeSelf(*i);
|
event.removeSelf((*i).get());
|
||||||
|
|
||||||
auto poll = polls_.find(socket);
|
auto poll = polls_.find(socket);
|
||||||
if (poll == polls_.end()) {
|
if (poll == polls_.end()) {
|
||||||
|
|
|
@ -78,35 +78,30 @@ struct pollfd PollEventPoll::KSocketEntry::getEvents()
|
||||||
|
|
||||||
PollEventPoll::PollEventPoll()
|
PollEventPoll::PollEventPoll()
|
||||||
: pollfdCapacity_(1024),
|
: pollfdCapacity_(1024),
|
||||||
pollfdNum_(0)
|
pollfdNum_(0),
|
||||||
{
|
pollfds_(make_unique<struct pollfd[]>(pollfdCapacity_))
|
||||||
pollfds_ = new struct pollfd[pollfdCapacity_];
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
PollEventPoll::~PollEventPoll()
|
PollEventPoll::~PollEventPoll()
|
||||||
{
|
{}
|
||||||
delete [] pollfds_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PollEventPoll::poll(const struct timeval& tv)
|
void PollEventPoll::poll(const struct timeval& tv)
|
||||||
{
|
{
|
||||||
// timeout is millisec
|
// timeout is millisec
|
||||||
int timeout = tv.tv_sec*1000+tv.tv_usec/1000;
|
int timeout = tv.tv_sec*1000+tv.tv_usec/1000;
|
||||||
int res;
|
int res;
|
||||||
while((res = ::poll(pollfds_, pollfdNum_, timeout)) == -1 &&
|
while((res = ::poll(pollfds_.get(), pollfdNum_, timeout)) == -1 &&
|
||||||
errno == EINTR);
|
errno == EINTR);
|
||||||
if(res > 0) {
|
if(res > 0) {
|
||||||
std::shared_ptr<KSocketEntry> se(new KSocketEntry(0));
|
for(auto first = pollfds_.get(), last = pollfds_.get() + pollfdNum_;
|
||||||
for(struct pollfd* first = pollfds_, *last = pollfds_+pollfdNum_;
|
|
||||||
first != last; ++first) {
|
first != last; ++first) {
|
||||||
if(first->revents) {
|
if(first->revents) {
|
||||||
se->setSocket(first->fd);
|
auto itr = socketEntries_.find(first->fd);
|
||||||
auto itr = socketEntries_.find(se);
|
if(itr == std::end(socketEntries_)) {
|
||||||
if(itr == socketEntries_.end()) {
|
|
||||||
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.",
|
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.",
|
||||||
first->fd));
|
first->fd));
|
||||||
} else {
|
} else {
|
||||||
(*itr)->processEvents(first->revents);
|
(*itr).second.processEvents(first->revents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,9 +115,10 @@ void PollEventPoll::poll(const struct timeval& tv)
|
||||||
// their API. So we call ares_process_fd for all ares_channel and
|
// their API. So we call ares_process_fd for all ares_channel and
|
||||||
// re-register their sockets.
|
// re-register their sockets.
|
||||||
for(auto& r : nameResolverEntries_) {
|
for(auto& r : nameResolverEntries_) {
|
||||||
r->processTimeout();
|
auto& ent = r.second;
|
||||||
r->removeSocketEvents(this);
|
ent.processTimeout();
|
||||||
r->addSocketEvents(this);
|
ent.removeSocketEvents(this);
|
||||||
|
ent.addSocketEvents(this);
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
@ -151,28 +147,29 @@ int PollEventPoll::translateEvents(EventPoll::EventType events)
|
||||||
bool PollEventPoll::addEvents
|
bool PollEventPoll::addEvents
|
||||||
(sock_t socket, const PollEventPoll::KEvent& event)
|
(sock_t socket, const PollEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
|
auto i = socketEntries_.lower_bound(socket);
|
||||||
auto i = socketEntries_.lower_bound(socketEntry);
|
if(i != std::end(socketEntries_) && (*i).first == socket) {
|
||||||
if(i != socketEntries_.end() && *(*i) == *socketEntry) {
|
auto& socketEntry = (*i).second;
|
||||||
event.addSelf(*i);
|
event.addSelf(&socketEntry);
|
||||||
for(struct pollfd* first = pollfds_, *last = pollfds_+pollfdNum_;
|
for(auto first = pollfds_.get(), last = pollfds_.get() + pollfdNum_;
|
||||||
first != last; ++first) {
|
first != last; ++first) {
|
||||||
if(first->fd == socket) {
|
if(first->fd == socket) {
|
||||||
*first = (*i)->getEvents();
|
*first = socketEntry.getEvents();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
socketEntries_.insert(i, socketEntry);
|
i = socketEntries_.insert(i, std::make_pair(socket, KSocketEntry(socket)));
|
||||||
event.addSelf(socketEntry);
|
auto& socketEntry = (*i).second;
|
||||||
|
event.addSelf(&socketEntry);
|
||||||
if(pollfdCapacity_ == pollfdNum_) {
|
if(pollfdCapacity_ == pollfdNum_) {
|
||||||
pollfdCapacity_ *= 2;
|
pollfdCapacity_ *= 2;
|
||||||
auto newPollfds = new struct pollfd[pollfdCapacity_];
|
auto newPollfds = make_unique<struct pollfd[]>(pollfdCapacity_);
|
||||||
memcpy(newPollfds, pollfds_, pollfdNum_*sizeof(struct pollfd));
|
memcpy(newPollfds.get(), pollfds_.get(),
|
||||||
delete [] pollfds_;
|
pollfdNum_ * sizeof(struct pollfd));
|
||||||
pollfds_ = newPollfds;
|
pollfds_ = std::move(newPollfds);
|
||||||
}
|
}
|
||||||
pollfds_[pollfdNum_] = socketEntry->getEvents();
|
pollfds_[pollfdNum_] = socketEntry.getEvents();
|
||||||
++pollfdNum_;
|
++pollfdNum_;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -197,30 +194,30 @@ bool PollEventPoll::addEvents
|
||||||
bool PollEventPoll::deleteEvents
|
bool PollEventPoll::deleteEvents
|
||||||
(sock_t socket, const PollEventPoll::KEvent& event)
|
(sock_t socket, const PollEventPoll::KEvent& event)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KSocketEntry> socketEntry(new KSocketEntry(socket));
|
auto i = socketEntries_.find(socket);
|
||||||
auto i = socketEntries_.find(socketEntry);
|
if(i == std::end(socketEntries_)) {
|
||||||
if(i == socketEntries_.end()) {
|
|
||||||
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
event.removeSelf(*i);
|
|
||||||
for(struct pollfd* first = pollfds_, *last = pollfds_+pollfdNum_;
|
|
||||||
first != last; ++first) {
|
|
||||||
if(first->fd == socket) {
|
|
||||||
if((*i)->eventEmpty()) {
|
|
||||||
if(pollfdNum_ >= 2) {
|
|
||||||
*first = *(last-1);
|
|
||||||
}
|
|
||||||
--pollfdNum_;
|
|
||||||
socketEntries_.erase(i);
|
|
||||||
} else {
|
|
||||||
*first = (*i)->getEvents();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
|
event.removeSelf(&socketEntry);
|
||||||
|
for(auto first = pollfds_.get(), last = pollfds_.get() + pollfdNum_;
|
||||||
|
first != last; ++first) {
|
||||||
|
if(first->fd == socket) {
|
||||||
|
if(socketEntry.eventEmpty()) {
|
||||||
|
if(pollfdNum_ >= 2) {
|
||||||
|
*first = *(last-1);
|
||||||
|
}
|
||||||
|
--pollfdNum_;
|
||||||
|
socketEntries_.erase(i);
|
||||||
|
} else {
|
||||||
|
*first = socketEntry.getEvents();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
|
@ -242,31 +239,31 @@ bool PollEventPoll::deleteEvents
|
||||||
bool PollEventPoll::addNameResolver
|
bool PollEventPoll::addNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KAsyncNameResolverEntry> entry
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
(new KAsyncNameResolverEntry(resolver, command));
|
auto itr = nameResolverEntries_.lower_bound(key);
|
||||||
auto itr = nameResolverEntries_.lower_bound(entry);
|
|
||||||
if(itr != nameResolverEntries_.end() && *(*itr) == *entry) {
|
if(itr != std::end(nameResolverEntries_) && (*itr).first == key) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
nameResolverEntries_.insert(itr, entry);
|
|
||||||
entry->addSocketEvents(this);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itr = nameResolverEntries_.insert
|
||||||
|
(itr, std::make_pair(key, KAsyncNameResolverEntry(resolver, command)));
|
||||||
|
(*itr).second.addSocketEvents(this);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PollEventPoll::deleteNameResolver
|
bool PollEventPoll::deleteNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<KAsyncNameResolverEntry> entry
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
(new KAsyncNameResolverEntry(resolver, command));
|
auto itr = nameResolverEntries_.find(key);
|
||||||
auto itr = nameResolverEntries_.find(entry);
|
if(itr == std::end(nameResolverEntries_)) {
|
||||||
if(itr == nameResolverEntries_.end()) {
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
(*itr)->removeSocketEvents(this);
|
|
||||||
nameResolverEntries_.erase(itr);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(*itr).second.removeSocketEvents(this);
|
||||||
|
nameResolverEntries_.erase(itr);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
# include <poll.h>
|
# include <poll.h>
|
||||||
|
|
||||||
#include <set>
|
#include <map>
|
||||||
|
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
|
@ -64,19 +64,20 @@ private:
|
||||||
public:
|
public:
|
||||||
KSocketEntry(sock_t socket);
|
KSocketEntry(sock_t socket);
|
||||||
|
|
||||||
|
KSocketEntry(const KSocketEntry&) = delete;
|
||||||
|
KSocketEntry(KSocketEntry&&) = default;
|
||||||
|
|
||||||
struct pollfd getEvents();
|
struct pollfd getEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
friend int accumulateEvent(int events, const KEvent& event);
|
friend int accumulateEvent(int events, const KEvent& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::set<std::shared_ptr<KSocketEntry>,
|
typedef std::map<sock_t, KSocketEntry> KSocketEntrySet;
|
||||||
DerefLess<std::shared_ptr<KSocketEntry> > > KSocketEntrySet;
|
|
||||||
KSocketEntrySet socketEntries_;
|
KSocketEntrySet socketEntries_;
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
typedef std::set<std::shared_ptr<KAsyncNameResolverEntry>,
|
typedef std::map<std::pair<AsyncNameResolver*, Command*>,
|
||||||
DerefLess<std::shared_ptr<KAsyncNameResolverEntry> > >
|
KAsyncNameResolverEntry> KAsyncNameResolverEntrySet;
|
||||||
KAsyncNameResolverEntrySet;
|
|
||||||
KAsyncNameResolverEntrySet nameResolverEntries_;
|
KAsyncNameResolverEntrySet nameResolverEntries_;
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ private:
|
||||||
// The number of valid struct pollfd in pollfds_.
|
// The number of valid struct pollfd in pollfds_.
|
||||||
int pollfdNum_;
|
int pollfdNum_;
|
||||||
|
|
||||||
struct pollfd* pollfds_;
|
std::unique_ptr<struct pollfd[]> pollfds_;
|
||||||
|
|
||||||
bool addEvents(sock_t socket, const KEvent& event);
|
bool addEvents(sock_t socket, const KEvent& event);
|
||||||
|
|
||||||
|
|
|
@ -179,7 +179,7 @@ bool PortEventPoll::addEvents(sock_t socket,
|
||||||
int r = 0;
|
int r = 0;
|
||||||
int errNum = 0;
|
int errNum = 0;
|
||||||
if(i != socketEntries_.end() && *(*i) == *socketEntry) {
|
if(i != socketEntries_.end() && *(*i) == *socketEntry) {
|
||||||
event.addSelf(*i);
|
event.addSelf((*i).get());
|
||||||
A2PortEvent pv = (*i)->getEvents();
|
A2PortEvent pv = (*i)->getEvents();
|
||||||
r = port_associate(port_, PORT_SOURCE_FD, (*i)->getSocket(),
|
r = port_associate(port_, PORT_SOURCE_FD, (*i)->getSocket(),
|
||||||
pv.events, pv.socketEntry);
|
pv.events, pv.socketEntry);
|
||||||
|
@ -191,7 +191,7 @@ bool PortEventPoll::addEvents(sock_t socket,
|
||||||
delete [] portEvents_;
|
delete [] portEvents_;
|
||||||
portEvents_ = new port_event_t[portEventsSize_];
|
portEvents_ = new port_event_t[portEventsSize_];
|
||||||
}
|
}
|
||||||
event.addSelf(socketEntry);
|
event.addSelf(socketEntry.get());
|
||||||
A2PortEvent pv = socketEntry->getEvents();
|
A2PortEvent pv = socketEntry->getEvents();
|
||||||
r = port_associate(port_, PORT_SOURCE_FD, socketEntry->getSocket(),
|
r = port_associate(port_, PORT_SOURCE_FD, socketEntry->getSocket(),
|
||||||
pv.events, pv.socketEntry);
|
pv.events, pv.socketEntry);
|
||||||
|
@ -230,7 +230,7 @@ bool PortEventPoll::deleteEvents(sock_t socket,
|
||||||
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
event.removeSelf(*i);
|
event.removeSelf((*i).get());
|
||||||
int r = 0;
|
int r = 0;
|
||||||
int errNum = 0;
|
int errNum = 0;
|
||||||
if((*i)->eventEmpty()) {
|
if((*i)->eventEmpty()) {
|
||||||
|
|
|
@ -177,8 +177,9 @@ void SelectEventPoll::poll(const struct timeval& tv)
|
||||||
#endif // __MINGW32__
|
#endif // __MINGW32__
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
for(auto& entry : nameResolverEntries_) {
|
for(auto& i : nameResolverEntries_) {
|
||||||
int fd = entry->getFds(&rfds, &wfds);
|
auto& entry = i.second;
|
||||||
|
int fd = entry.getFds(&rfds, &wfds);
|
||||||
// TODO force error if fd == 0
|
// TODO force error if fd == 0
|
||||||
if(fdmax_ < fd) {
|
if(fdmax_ < fd) {
|
||||||
fdmax_ = fd;
|
fdmax_ = fd;
|
||||||
|
@ -196,15 +197,16 @@ void SelectEventPoll::poll(const struct timeval& tv)
|
||||||
#endif // !__MINGW32__
|
#endif // !__MINGW32__
|
||||||
} while(retval == -1 && errno == EINTR);
|
} while(retval == -1 && errno == EINTR);
|
||||||
if(retval > 0) {
|
if(retval > 0) {
|
||||||
for(auto& e: socketEntries_) {
|
for(auto& i: socketEntries_) {
|
||||||
|
auto& e = i.second;
|
||||||
int events = 0;
|
int events = 0;
|
||||||
if(FD_ISSET(e->getSocket(), &rfds)) {
|
if(FD_ISSET(e.getSocket(), &rfds)) {
|
||||||
events |= EventPoll::EVENT_READ;
|
events |= EventPoll::EVENT_READ;
|
||||||
}
|
}
|
||||||
if(FD_ISSET(e->getSocket(), &wfds)) {
|
if(FD_ISSET(e.getSocket(), &wfds)) {
|
||||||
events |= EventPoll::EVENT_WRITE;
|
events |= EventPoll::EVENT_WRITE;
|
||||||
}
|
}
|
||||||
e->processEvents(events);
|
e.processEvents(events);
|
||||||
}
|
}
|
||||||
} else if(retval == -1) {
|
} else if(retval == -1) {
|
||||||
int errNum = errno;
|
int errNum = errno;
|
||||||
|
@ -212,8 +214,8 @@ void SelectEventPoll::poll(const struct timeval& tv)
|
||||||
}
|
}
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
for(auto & e: nameResolverEntries_) {
|
for(auto& i: nameResolverEntries_) {
|
||||||
e->process(&rfds, &wfds);
|
i.second.process(&rfds, &wfds);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
@ -240,8 +242,9 @@ void SelectEventPoll::updateFdSet()
|
||||||
#endif // !__MINGW32__
|
#endif // !__MINGW32__
|
||||||
FD_ZERO(&rfdset_);
|
FD_ZERO(&rfdset_);
|
||||||
FD_ZERO(&wfdset_);
|
FD_ZERO(&wfdset_);
|
||||||
for(auto& e: socketEntries_) {
|
for(auto& i: socketEntries_) {
|
||||||
sock_t fd = e->getSocket();
|
auto&e = i.second;
|
||||||
|
sock_t fd = e.getSocket();
|
||||||
#ifndef __MINGW32__
|
#ifndef __MINGW32__
|
||||||
if(fd < 0 || FD_SETSIZE <= fd) {
|
if(fd < 0 || FD_SETSIZE <= fd) {
|
||||||
A2_LOG_WARN("Detected file descriptor >= FD_SETSIZE or < 0. "
|
A2_LOG_WARN("Detected file descriptor >= FD_SETSIZE or < 0. "
|
||||||
|
@ -249,7 +252,7 @@ void SelectEventPoll::updateFdSet()
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
#endif // !__MINGW32__
|
#endif // !__MINGW32__
|
||||||
int events = e->getEvents();
|
int events = e.getEvents();
|
||||||
if(events&EventPoll::EVENT_READ) {
|
if(events&EventPoll::EVENT_READ) {
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
checkFdCountMingw(rfdset_);
|
checkFdCountMingw(rfdset_);
|
||||||
|
@ -271,13 +274,12 @@ void SelectEventPoll::updateFdSet()
|
||||||
bool SelectEventPoll::addEvents(sock_t socket, Command* command,
|
bool SelectEventPoll::addEvents(sock_t socket, Command* command,
|
||||||
EventPoll::EventType events)
|
EventPoll::EventType events)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SocketEntry> socketEntry(new SocketEntry(socket));
|
auto i = socketEntries_.lower_bound(socket);
|
||||||
auto i = socketEntries_.lower_bound(socketEntry);
|
if(i != std::end(socketEntries_) && (*i).first == socket) {
|
||||||
if(i != socketEntries_.end() && *(*i) == *socketEntry) {
|
(*i).second.addCommandEvent(command, events);
|
||||||
(*i)->addCommandEvent(command, events);
|
|
||||||
} else {
|
} else {
|
||||||
socketEntries_.insert(i, socketEntry);
|
i = socketEntries_.insert(i, std::make_pair(socket, SocketEntry(socket)));
|
||||||
socketEntry->addCommandEvent(command, events);
|
(*i).second.addCommandEvent(command, events);
|
||||||
}
|
}
|
||||||
updateFdSet();
|
updateFdSet();
|
||||||
return true;
|
return true;
|
||||||
|
@ -286,42 +288,42 @@ bool SelectEventPoll::addEvents(sock_t socket, Command* command,
|
||||||
bool SelectEventPoll::deleteEvents(sock_t socket, Command* command,
|
bool SelectEventPoll::deleteEvents(sock_t socket, Command* command,
|
||||||
EventPoll::EventType events)
|
EventPoll::EventType events)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SocketEntry> socketEntry(new SocketEntry(socket));
|
auto i = socketEntries_.find(socket);
|
||||||
auto i = socketEntries_.find(socketEntry);
|
if(i == std::end(socketEntries_)) {
|
||||||
if(i == socketEntries_.end()) {
|
|
||||||
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
(*i)->removeCommandEvent(command, events);
|
|
||||||
if((*i)->eventEmpty()) {
|
|
||||||
socketEntries_.erase(i);
|
|
||||||
}
|
|
||||||
updateFdSet();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto& socketEntry = (*i).second;
|
||||||
|
socketEntry.removeCommandEvent(command, events);
|
||||||
|
if(socketEntry.eventEmpty()) {
|
||||||
|
socketEntries_.erase(i);
|
||||||
|
}
|
||||||
|
updateFdSet();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
bool SelectEventPoll::addNameResolver
|
bool SelectEventPoll::addNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<AsyncNameResolverEntry> entry
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
(new AsyncNameResolverEntry(resolver, command));
|
auto itr = nameResolverEntries_.lower_bound(key);
|
||||||
auto itr = nameResolverEntries_.lower_bound(entry);
|
if(itr != std::end(nameResolverEntries_) && (*itr).first == key) {
|
||||||
if(itr != nameResolverEntries_.end() && *(*itr) == *entry) {
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
nameResolverEntries_.insert(itr, entry);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nameResolverEntries_.insert
|
||||||
|
(itr, std::make_pair(key, AsyncNameResolverEntry(resolver, command)));
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SelectEventPoll::deleteNameResolver
|
bool SelectEventPoll::deleteNameResolver
|
||||||
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
||||||
{
|
{
|
||||||
std::shared_ptr<AsyncNameResolverEntry> entry
|
auto key = std::make_pair(resolver.get(), command);
|
||||||
(new AsyncNameResolverEntry(resolver, command));
|
return nameResolverEntries_.erase(key) == 1;
|
||||||
return nameResolverEntries_.erase(entry) == 1;
|
|
||||||
}
|
}
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
#include "EventPoll.h"
|
#include "EventPoll.h"
|
||||||
|
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <set>
|
#include <map>
|
||||||
|
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
|
@ -100,6 +100,9 @@ private:
|
||||||
public:
|
public:
|
||||||
SocketEntry(sock_t socket);
|
SocketEntry(sock_t socket);
|
||||||
|
|
||||||
|
SocketEntry(const SocketEntry&) = delete;
|
||||||
|
SocketEntry(SocketEntry&&) = default;
|
||||||
|
|
||||||
bool operator==(const SocketEntry& entry) const
|
bool operator==(const SocketEntry& entry) const
|
||||||
{
|
{
|
||||||
return socket_ == entry.socket_;
|
return socket_ == entry.socket_;
|
||||||
|
@ -141,6 +144,9 @@ private:
|
||||||
AsyncNameResolverEntry(const std::shared_ptr<AsyncNameResolver>& nameResolver,
|
AsyncNameResolverEntry(const std::shared_ptr<AsyncNameResolver>& nameResolver,
|
||||||
Command* command);
|
Command* command);
|
||||||
|
|
||||||
|
AsyncNameResolverEntry(const AsyncNameResolverEntry&) = delete;
|
||||||
|
AsyncNameResolverEntry(AsyncNameResolverEntry&&) = default;
|
||||||
|
|
||||||
bool operator==(const AsyncNameResolverEntry& entry)
|
bool operator==(const AsyncNameResolverEntry& entry)
|
||||||
{
|
{
|
||||||
return *nameResolver_ == *entry.nameResolver_ &&
|
return *nameResolver_ == *entry.nameResolver_ &&
|
||||||
|
@ -164,13 +170,11 @@ private:
|
||||||
fd_set wfdset_;
|
fd_set wfdset_;
|
||||||
sock_t fdmax_;
|
sock_t fdmax_;
|
||||||
|
|
||||||
typedef std::set<std::shared_ptr<SocketEntry>,
|
typedef std::map<sock_t, SocketEntry> SocketEntrySet;
|
||||||
DerefLess<std::shared_ptr<SocketEntry> > > SocketEntrySet;
|
|
||||||
SocketEntrySet socketEntries_;
|
SocketEntrySet socketEntries_;
|
||||||
#ifdef ENABLE_ASYNC_DNS
|
#ifdef ENABLE_ASYNC_DNS
|
||||||
typedef std::set<std::shared_ptr<AsyncNameResolverEntry>,
|
typedef std::map<std::pair<AsyncNameResolver*, Command*>,
|
||||||
DerefLess<std::shared_ptr<AsyncNameResolverEntry> > >
|
AsyncNameResolverEntry> AsyncNameResolverEntrySet;
|
||||||
AsyncNameResolverEntrySet;
|
|
||||||
AsyncNameResolverEntrySet nameResolverEntries_;
|
AsyncNameResolverEntrySet nameResolverEntries_;
|
||||||
#endif // ENABLE_ASYNC_DNS
|
#endif // ENABLE_ASYNC_DNS
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue