summaryrefslogtreecommitdiffstats
path: root/src/server/sockets.hpp
blob: 2f47e3ba2f94e1d59fbf330a568c0b536b3b2a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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