join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
reactor.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_REACTOR_HPP__
26#define __JOIN_REACTOR_HPP__
27
28// libjoin.
29#include <join/condition.hpp>
30
31// C++.
32#include <thread>
33#include <vector>
34
35// C.
36#include <sys/epoll.h>
37
38namespace join
39{
44 {
45 public:
49 EventHandler () = default;
50
54 virtual ~EventHandler () = default;
55
60 virtual int handle () const noexcept = 0;
61
62 protected:
66 virtual void onReceive ()
67 {
68 // do nothing.
69 }
70
74 virtual void onClose ()
75 {
76 // do nothing.
77 }
78
82 virtual void onError ()
83 {
84 // do nothing.
85 }
86
88 friend class Reactor;
89 };
90
94 class Reactor
95 {
96 public:
100 Reactor ();
101
106 Reactor (const Reactor& other) = delete;
107
112 Reactor (Reactor&& other) = delete;
113
119 Reactor& operator= (const Reactor& other) = delete;
120
126 Reactor& operator= (Reactor&& other) = delete;
127
131 ~Reactor ();
132
138 int addHandler (EventHandler* handler);
139
145 int delHandler (EventHandler* handler);
146
151 static Reactor* instance ();
152
153 protected:
157 void dispatch ();
158
160 int _eventfd = -1;
161
163 int _epoll = -1;
164
166 std::thread::id _threadId;
167
170
173
175 bool _running = false;
176
178 int _num = 0;
179 };
180}
181
182#endif
condition variable class.
Definition condition.hpp:42
Event handler interface class.
Definition reactor.hpp:44
virtual void onError()
method called when an error occured on handle.
Definition reactor.hpp:82
virtual int handle() const noexcept=0
get native handle.
virtual void onClose()
method called when handle is closed.
Definition reactor.hpp:74
virtual ~EventHandler()=default
destroy instance.
EventHandler()=default
create instance.
virtual void onReceive()
method called when data are ready to be read on handle.
Definition reactor.hpp:66
Reactor class.
Definition reactor.hpp:95
Reactor(Reactor &&other)=delete
move constructor.
void dispatch()
dispatch events received.
Definition reactor.cpp:168
Reactor()
default constructor.
Definition reactor.cpp:44
int delHandler(EventHandler *handler)
delete handler from reactor.
Definition reactor.cpp:120
int addHandler(EventHandler *handler)
add handler to reactor.
Definition reactor.cpp:77
bool _running
status.
Definition reactor.hpp:175
int _epoll
epoll descriptor.
Definition reactor.hpp:163
static Reactor * instance()
create the Reactor instance.
Definition reactor.cpp:158
int _eventfd
eventfd descriptor.
Definition reactor.hpp:160
std::thread::id _threadId
thread id.
Definition reactor.hpp:166
Reactor & operator=(const Reactor &other)=delete
copy assignment operator.
~Reactor()
destroy instance.
Definition reactor.cpp:58
Reactor(const Reactor &other)=delete
copy constructor.
int _num
number of handlers.
Definition reactor.hpp:178
RecursiveMutex _mutex
protection mutex.
Definition reactor.hpp:169
Condition _threadStatus
thread status event.
Definition reactor.hpp:172
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:106
Definition acceptor.hpp:32