diff options
author | Joursoir <chat@joursoir.net> | 2020-11-23 20:07:36 +0300 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2020-11-23 20:07:36 +0300 |
commit | cd2decce51c32b8c0531a87ea849e9f8e80db0d3 (patch) | |
tree | 7ee12a1ff8ba8ad95036e63dac186d40f97e0a5c /src/server/sockets.hpp | |
parent | a2ee75888739b66b3bd20d2a1f1e465947f1fbc4 (diff) | |
download | want-chat-cd2decce51c32b8c0531a87ea849e9f8e80db0d3.tar.gz want-chat-cd2decce51c32b8c0531a87ea849e9f8e80db0d3.tar.bz2 want-chat-cd2decce51c32b8c0531a87ea849e9f8e80db0d3.zip |
server: add first (work with sockets) level of abstraction
Diffstat (limited to 'src/server/sockets.hpp')
-rw-r--r-- | src/server/sockets.hpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/server/sockets.hpp b/src/server/sockets.hpp new file mode 100644 index 0000000..2f47e3b --- /dev/null +++ b/src/server/sockets.hpp @@ -0,0 +1,37 @@ +#ifndef SOCKETS_H +#define SOCKETS_H + +#include <unistd.h> +#include <sys/poll.h> + +class FdHandler { // abstract class + int fd; +public: + FdHandler(int i_fd) : fd(i_fd) {} + virtual ~FdHandler() { close(fd); } + + int GetFd() const { return fd; } + + virtual bool WantRead() const { return true; } + virtual bool WantWrite() const { return false; } + virtual void Handle(bool r, bool w) = 0; +}; + +class EventSelector { + FdHandler **fd_array; + struct pollfd *fds; + int fd_array_len; + int max_fd; + + bool exit_flag; +public: + EventSelector() : fd_array(0), fds(0), max_fd(-1), exit_flag(false) {} + ~EventSelector(); + + void Add(FdHandler *h); + bool Remove(FdHandler *h); + + void Run(); + void BreakLoop() { exit_flag = true; } +}; +#endif
\ No newline at end of file |