Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ tests/test_runner

# Docker / local secrets
.env

.claude/workflow/tasks/*
2 changes: 2 additions & 0 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Server
void handleClientOutput(int fd);
void handleMessage(Client *client, const std::string &raw);
void checkTimeouts();
void updateEpollInterest(Client *client);
bool dispatchExtensionFd(int fd, uint32_t events);

/* ─── Command dispatch ─── */
Expand Down Expand Up @@ -125,6 +126,7 @@ class Server
** stays below in run() (src/Server.cpp). */
libcpp98::Reactor _reactor;
std::map<int, Client *> _clients;
std::map<int, uint32_t> _epollMask;
std::map<std::string, Channel *> _channels;
std::vector<IServerExtension *> _extensions;
time_t _lastPingCheck;
Expand Down
19 changes: 18 additions & 1 deletion src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ void Server::run()
time_t now = std::time(NULL);
for (size_t i = 0; i < _extensions.size(); ++i)
_extensions[i]->onTick(*this, now);

for (std::map<int, Client *>::iterator it = _clients.begin();
it != _clients.end(); ++it)
updateEpollInterest(it->second);
}
}

Expand Down Expand Up @@ -264,7 +268,8 @@ void Server::acceptClient()
}
_clients[clientFd] = client;

addToEpoll(clientFd, EPOLLIN | EPOLLOUT);
addToEpoll(clientFd, EPOLLIN);
_epollMask[clientFd] = EPOLLIN;

Log::info("new connection from " + hostname
+ " (fd " + libcpp::str::to_string(clientFd) + ")");
Expand Down Expand Up @@ -339,6 +344,17 @@ void Server::handleMessage(Client *client, const std::string &raw)
dispatchCommand(client, msg);
}

void Server::updateEpollInterest(Client *client)
{
int fd = client->getFd();
uint32_t want = EPOLLIN | (client->hasPendingData() ? EPOLLOUT : 0u);
std::map<int, uint32_t>::iterator it = _epollMask.find(fd);
if (it != _epollMask.end() && it->second == want)
return;
modifyEpoll(fd, want);
_epollMask[fd] = want;
}

void Server::checkTimeouts()
{
time_t now = std::time(NULL);
Expand Down Expand Up @@ -487,6 +503,7 @@ void Server::disconnectClient(int fd, const std::string &reason)
send(fd, pending.c_str(), pending.size(), 0);
}

_epollMask.erase(fd);
removeFromEpoll(fd);
close(fd);
Log::info("client disconnected: " + client->getNickname()
Expand Down
Loading