first commit: echo server
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Epoll.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: acossari <acossari@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/08/26 11:34:23 by acossari #+# #+# */
|
||||
/* Updated: 2026/08/26 11:34:25 by acossari ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <span>
|
||||
#include <string>
|
||||
|
||||
#include "net/FileDescriptor.hpp"
|
||||
|
||||
namespace webserv {
|
||||
|
||||
class Epoll final {
|
||||
public:
|
||||
class Error : public std::exception {
|
||||
public:
|
||||
explicit Error(std::string message);
|
||||
const char* what() const noexcept override;
|
||||
|
||||
private:
|
||||
std::string msg_;
|
||||
};
|
||||
|
||||
// wait() returns at most this many ready events per call. Smaller batches
|
||||
// require more calls, while larger batches delay the next loop iteration.
|
||||
// The value is a middle ground. Excess events remain for later calls.
|
||||
static constexpr std::size_t EVENT_BATCH_CAPACITY = 256;
|
||||
|
||||
Epoll();
|
||||
|
||||
void add(int fd, std::uint32_t events, std::uint64_t token);
|
||||
void modify(int fd, std::uint32_t events, std::uint64_t token);
|
||||
void remove(int fd) noexcept;
|
||||
|
||||
std::span<const epoll_event> wait(int timeoutMillis);
|
||||
|
||||
private:
|
||||
FileDescriptor fd_;
|
||||
std::array<epoll_event, EVENT_BATCH_CAPACITY> events_{};
|
||||
};
|
||||
|
||||
} // namespace webserv
|
||||
@@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* EventToken.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: acossari <acossari@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/08/25 13:26:42 by acossari #+# #+# */
|
||||
/* Updated: 2026/08/25 19:06:13 by acossari ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace webserv {
|
||||
|
||||
enum class EndpointKind {
|
||||
ClientSocket,
|
||||
CgiStdin,
|
||||
CgiStdout,
|
||||
Listener,
|
||||
};
|
||||
|
||||
// Identifies which registered endpoint produced an epoll event without storing
|
||||
// a pointer. For Listener, index selects a listener; for every other kind it
|
||||
// selects a connection slot, while kind identifies its client socket or CGI
|
||||
// pipe. epoll_wait() returns a batch in which an old event may remain after its
|
||||
// endpoint has been retired and re-registered. generation distinguishes that
|
||||
// old registration from the current one even when index and kind are equal.
|
||||
//
|
||||
// Packed layout in epoll_event.data.u64:
|
||||
// bits 0-29 | index | 30 bits
|
||||
// bits 30-31 | kind | 2 bits
|
||||
// bits 32-63 | generation | 32 bits
|
||||
struct EventToken {
|
||||
static constexpr std::uint64_t INDEX_BITS = 30;
|
||||
static constexpr std::uint64_t KIND_BITS = 2;
|
||||
|
||||
// List-initialization gives 1 the exact std::uint32_t type before shifting.
|
||||
// std::uint32_t{1} << INDEX_BITS = 1,073,741,824
|
||||
// (std::uint32_t{1} << INDEX_BITS) - 1 = 1,073,741,823
|
||||
// Binary: 100...000 (30 zeros) - 1 = 011...111 (30 ones).
|
||||
static constexpr std::uint32_t MAX_INDEX =
|
||||
(std::uint32_t{1} << INDEX_BITS) - 1;
|
||||
|
||||
std::uint32_t index;
|
||||
EndpointKind kind;
|
||||
std::uint32_t generation;
|
||||
};
|
||||
|
||||
std::uint64_t pack(EventToken token);
|
||||
EventToken unpack(std::uint64_t token);
|
||||
|
||||
} // namespace webserv
|
||||
Reference in New Issue
Block a user