213 words
1 minute
ft_irc — Non-Blocking C++98 IRC Server
ft_irc: Internet Relay Chat Server from Scratch
ft_irc is a 1337 / 42 Rank 5 project requiring an RFC 2812-compliant Internet Relay Chat server in C++98, capable of multiplexing concurrent client connections without blocking.
Architectural Design
- I/O Multiplexing with
poll(): A single-threaded event loop manages listener sockets, incoming client events, and write queues without polling loops or CPU starvation. - Non-Blocking Sockets: Sockets are configured with
O_NONBLOCKviafcntlto prevent slow or malicious clients from blocking server dispatching. - Protocol Parsing: Strict parsing of IRC commands including
PASS,NICK,USER,JOIN,PRIVMSG,MODE,KICK,INVITE,TOPIC,PART, andQUIT. - Channel Modes: Support for channel flags (
+iinvite-only,+ttopic lock,+kpassword key,+ochannel operator status,+luser count limit).
int pollCount = poll(&pollFds[0], pollFds.size(), -1);if (pollCount < 0 && !Server::signalReceived) { throw std::runtime_error("poll() error encountered");}
for (size_t i = 0; i < pollFds.size(); ++i) { if (pollFds[i].revents & POLLIN) { if (pollFds[i].fd == serverSocketFd) { acceptNewClient(); } else { handleClientData(pollFds[i].fd); } }}TCP Stream and State Handling
- Partial Reads and Framing: Accumulates fragmented TCP packets into client-specific buffers until the standard
\r\nboundary is reached. - Buffer Bounds: Implements bounded buffers to protect against resource exhaustion.
- Graceful Teardown: Captures
SIGINTandSIGTERMto close socket descriptors cleanly and release resources.
Client Compatibility
Tested against standard IRC clients:
- Irssi
- HexChat
- Raw socket debugging via
nc/telnet
Sphynx | Ismail Larhrib