+public:
+ /** Constructor.
+ * The constructor transparently initializes
+ * the socket engine which the ircd is using.
+ * Please note that if there is a catastrophic
+ * failure (for example, you try and enable
+ * epoll on a 2.4 linux kernel) then this
+ * function may bail back to the shell.
+ * @return void, but it is acceptable for this function to bail back to
+ * the shell or operating system on fatal error.
+ */
+ static void Init();
+
+ /** Destructor.
+ * The destructor transparently tidies up
+ * any resources used by the socket engine.
+ */
+ static void Deinit();
+
+ /** Add an EventHandler object to the engine. Use AddFd to add a file
+ * descriptor to the engine and have the socket engine monitor it. You
+ * must provide an object derived from EventHandler which implements
+ * HandleEvent().
+ * @param eh An event handling object to add
+ * @param event_mask The initial event mask for the object
+ */
+ static bool AddFd(EventHandler* eh, int event_mask);
+
+ /** If you call this function and pass it an
+ * event handler, that event handler will
+ * receive the next available write event,
+ * even if the socket is a readable socket only.
+ * Developers should avoid constantly keeping
+ * an eventhandler in the writeable state,
+ * as this will consume large amounts of
+ * CPU time.
+ * @param eh The event handler to change
+ * @param event_mask The changes to make to the wait state
+ */
+ static void ChangeEventMask(EventHandler* eh, int event_mask);
+
+ /** Returns the number of file descriptors reported by the system this program may use
+ * when it was started.
+ * @return If positive, the number of file descriptors that the system reported that we
+ * may use. Otherwise (<= 0) this number could not be determined.
+ */
+ static int GetMaxFds() { return MAX_DESCRIPTORS; }
+
+ /** Returns the number of file descriptors being queried
+ * @return The set size
+ */
+ static size_t GetUsedFds() { return CurrentSetSize; }
+
+ /** Delete an event handler from the engine.
+ * This function call deletes an EventHandler
+ * from the engine, returning true if it succeeded
+ * and false if it failed. This does not free the
+ * EventHandler pointer using delete, if this is
+ * required you must do this yourself.
+ * @param eh The event handler object to remove
+ */
+ static void DelFd(EventHandler* eh);
+
+ /** Returns true if a file descriptor exists in
+ * the socket engine's list.
+ * @param fd The event handler to look for
+ * @return True if this fd has an event handler
+ */
+ static bool HasFd(int fd);
+
+ /** Returns the EventHandler attached to a specific fd.
+ * If the fd isnt in the socketengine, returns NULL.
+ * @param fd The event handler to look for
+ * @return A pointer to the event handler, or NULL
+ */
+ static EventHandler* GetRef(int fd);
+
+ /** Waits for events and dispatches them to handlers. Please note that
+ * this doesn't wait long, only a couple of milliseconds. It returns the
+ * number of events which occurred during this call. This method will
+ * dispatch events to their handlers by calling their
+ * EventHandler::HandleEvent() methods with the necessary EventType
+ * value.
+ * @return The number of events which have occured.
+ */
+ static int DispatchEvents();
+
+ /** Dispatch trial reads and writes. This causes the actual socket I/O
+ * to happen when writes have been pre-buffered.
+ */
+ static void DispatchTrialWrites();
+
+ /** Returns true if the file descriptors in the given event handler are
+ * within sensible ranges which can be handled by the socket engine.
+ */
+ static bool BoundsCheckFd(EventHandler* eh);
+
+ /** Abstraction for BSD sockets accept(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param addr The client IP address and port
+ * @param addrlen The size of the sockaddr parameter.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
+
+ /** Close the underlying fd of an event handler, remove it from the socket engine and set the fd to -1.
+ * @param eh The EventHandler to close.
+ * @return 0 on success, a negative value on error
+ */
+ static int Close(EventHandler* eh);
+
+ /** Abstraction for BSD sockets close(2).
+ * This function should emulate its namesake system call exactly.
+ * This function should emulate its namesake system call exactly.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Close(int fd);
+
+ /** Abstraction for BSD sockets send(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param buf The buffer in which the data that is sent is stored.
+ * @param len The size of the buffer.
+ * @param flags A flag value that controls the sending of the data.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Send(EventHandler* fd, const void *buf, size_t len, int flags);
+
+ /** Abstraction for BSD sockets recv(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param buf The buffer in which the data that is read is stored.
+ * @param len The size of the buffer.
+ * @param flags A flag value that controls the reception of the data.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Recv(EventHandler* fd, void *buf, size_t len, int flags);
+
+ /** Abstraction for BSD sockets recvfrom(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param buf The buffer in which the data that is read is stored.
+ * @param len The size of the buffer.
+ * @param flags A flag value that controls the reception of the data.
+ * @param from The remote IP address and port.
+ * @param fromlen The size of the from parameter.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen);
+
+ /** Abstraction for BSD sockets sendto(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param buf The buffer in which the data that is sent is stored.
+ * @param len The size of the buffer.
+ * @param flags A flag value that controls the sending of the data.
+ * @param to The remote IP address and port.
+ * @param tolen The size of the to parameter.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen);
+
+ /** Abstraction for BSD sockets connect(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param serv_addr The server IP address and port.
+ * @param addrlen The size of the sockaddr parameter.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen);
+
+ /** Make a file descriptor blocking.
+ * @param fd a file descriptor to set to blocking mode
+ * @return 0 on success, -1 on failure, errno is set appropriately.
+ */
+ static int Blocking(int fd);
+
+ /** Make a file descriptor nonblocking.
+ * @param fd A file descriptor to set to nonblocking mode
+ * @return 0 on success, -1 on failure, errno is set appropriately.
+ */
+ static int NonBlocking(int fd);
+
+ /** Abstraction for BSD sockets shutdown(2).
+ * This function should emulate its namesake system call exactly.
+ * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
+ * @param how What part of the socket to shut down
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Shutdown(EventHandler* fd, int how);
+
+ /** Abstraction for BSD sockets shutdown(2).
+ * This function should emulate its namesake system call exactly.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Shutdown(int fd, int how);
+
+ /** Abstraction for BSD sockets bind(2).
+ * This function should emulate its namesake system call exactly.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Bind(int fd, const irc::sockets::sockaddrs& addr);
+
+ /** Abstraction for BSD sockets listen(2).
+ * This function should emulate its namesake system call exactly.
+ * @return This method should return exactly the same values as the system call it emulates.
+ */
+ static int Listen(int sockfd, int backlog);
+
+ /** Set SO_REUSEADDR and SO_LINGER on this file descriptor
+ */
+ static void SetReuse(int sockfd);
+
+ /** This function is called immediately after fork().
+ * Some socket engines (notably kqueue) cannot have their
+ * handles inherited by forked processes. This method
+ * allows for the socket engine to re-create its handle
+ * after the daemon forks as the socket engine is created
+ * long BEFORE the daemon forks.
+ * @return void, but it is acceptable for this function to bail back to
+ * the shell or operating system on fatal error.
+ */
+ static void RecoverFromFork();
+
+ /** Get data transfer and event statistics
+ */
+ static const Statistics& GetStats() { return stats; }
+
+ /** Should we ignore the error in errno?
+ * Checks EAGAIN and WSAEWOULDBLOCK
+ */
+ static bool IgnoreError();
+
+ /** Return the last socket related error. strrerror(errno) on *nix
+ */
+ static std::string LastError();
+
+ /** Returns the error for the given error num, strerror(errnum) on *nix
+ */
+ static std::string GetError(int errnum);
+};