-
Notifications
You must be signed in to change notification settings - Fork 1
/
FdEvents.h
103 lines (84 loc) · 3.04 KB
/
FdEvents.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// ***********************************************************************
// Filename : FdEvents.h
// Author : lizhenghn@gmail.com
// Created : 2015-05-17
// Description : 模仿lighttpd中的fdevents设计,实现FdEvent及FdEvents(I/O复用接口类)
//
// Last Modified By : LIZHENG
// Last Modified On : 2015-05-19
//
// Copyright (c) lizhenghn@gmail.com. All rights reserved.
// ***********************************************************************
#ifndef ZL_FDEVENTS_H
#define ZL_FDEVENTS_H
#include <vector>
#include <map>
//#define FDEVENTS_TYPE_EPOLL
#define FDEVENTS_TYPE_SELECT
//#define FDEVENTS_TYPE_POLL
//#define FDEVENTS_TYPE_KQUEUE
//#define FDEVENTS_TYPE_POLL
#define FDEVENT_NONE (0)
#define FDEVENT_IN (1<<0)
#define FDEVENT_PRI (1<<1)
#define FDEVENT_OUT (1<<2)
#define FDEVENT_HUP (1<<3)
#define FDEVENT_ERR (1<<4)
#define FDEVENT_NVAL (1<<5)
class FdEvent;
class FdEvents;
typedef int(*eventCallback)(FdEvents *poller, FdEvent *fe);
typedef int(*readCallback)(FdEvents *poller, FdEvent *fe);
typedef int(*writeCallback)(FdEvents *poller, FdEvent *fe);
class FdEvent
{
friend class FdEvents;
public:
static FdEvent* newFdEvent(int fd);
static void deleteFdEvent(FdEvent *fde);
public:
int fd() const { return fd_; }
void set_fd(int fd) { fd_ = fd; }
int events() const { return events_; }
//void set_events(int events) { revents_ = events; }
void enableReading() { events_ |= (FDEVENT_IN | FDEVENT_PRI); }
void enableWriting() { events_ |= FDEVENT_OUT; }
void enableAll() { events_ |= (FDEVENT_IN | FDEVENT_PRI | FDEVENT_OUT); }
void disableReading() { events_ &= ~(FDEVENT_IN | FDEVENT_PRI); }
void disableWriting() { events_ &= ~FDEVENT_OUT; }
void disableAll() { events_ = FDEVENT_NONE; }
bool isNoneEvent() const { return events_ == FDEVENT_NONE; }
void set_revents(int revents) { revents_ = revents; }
int revents() const { return revents_; }
public://private:
FdEvent(int fd);
~FdEvent();
public:
int fd_;
int events_;
int revents_;
eventCallback callback_;
};
FdEvents* createFdEvents();
class FdEvents
{
public:
typedef std::vector<FdEvent *> ChannelList;
typedef std::map<int, FdEvent *> ChannelMap;
public:
FdEvents();
virtual ~FdEvents();
public:
bool hasChannel(const FdEvent *channel) const;
FdEvent* getChannel(int sock) const;
public:
virtual FdEvent* addFdEvent(int fd, eventCallback cb) = 0;
virtual int addFdEvent(FdEvent *fe) = 0;
virtual int modFdEvent(FdEvent *fe) = 0;
virtual int delFdEvent(FdEvent *fe) = 0;
virtual int poll(std::vector<FdEvent *>& fdevents, int timeoutMs) = 0;
virtual const char* getName() const = 0;
protected:
ChannelMap channelMap_;
};
#endif /* ZL_FDEVENTS_H */