Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2008 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : /**
18 : : * @file
19 : : * This header defines the EventLoop class.
20 : : */
21 : :
22 : : #ifndef __SSRC_WISP_SERVICE_EVENT_LOOP_H
23 : : #define __SSRC_WISP_SERVICE_EVENT_LOOP_H
24 : :
25 : : #include <ssrc/wisp/service/EventHandler.h>
26 : :
27 : : __BEGIN_NS_SSRC_WISP_SERVICE
28 : :
29 : : struct EventLoopState;
30 : :
31 : : /**
32 : : *
33 : : */
34 : : class EventLoop {
35 : : public:
36 : :
37 : : static const bool Once = true;
38 : : static const bool Persist = false;
39 : :
40 : : typedef int EventIO;
41 : :
42 : : static const EventIO None;
43 : : static const EventIO Read;
44 : : static const EventIO Write;
45 : : static const EventIO Error;
46 : : static const EventIO Hangup;
47 : : /*
48 : : static const EventIO Signal;
49 : : static const EventIO Timeout;
50 : : */
51 : : explicit EventLoop() SSRC_DECL_THROW(std::runtime_error);
52 : :
53 : : ~EventLoop();
54 : :
55 : : void add_handler(EventHandler & handler,
56 : : const int events,
57 : : const TimeValue & timeout = InfiniteTimeValue,
58 : : const bool once = Persist)
59 : : SSRC_DECL_THROW(std::runtime_error);
60 : :
61 : : void remove_handler(EventHandler & handler);
62 : :
63 : : unsigned int count_handlers();
64 : :
65 : : unsigned int count_io_handlers();
66 : :
67 : : bool running();
68 : :
69 : : void start();
70 : :
71 : : void stop();
72 : :
73 : : private:
74 : : // Only allow EventLoop.cc to see implementation details.
75 : : // This speeds up client compilation times and avoids potential
76 : : // conflicts arising from our placing system headers in custom
77 : : // namespaces (e.g., Linux:: for <sys/epoll.h>). This comes
78 : : // at the expense of awkwardness, requiring client code to
79 : : // specify events as int instead of the native type,
80 : : // and inability to inline accessor functions.
81 : : EventLoopState *_state;
82 : : };
83 : :
84 : : class EventInfo {
85 : : friend void EventLoop::start();
86 : :
87 : : //int _fd;
88 : : bool _timeout;
89 : : int _io_events;
90 : : EventLoop & _loop;
91 : : TimeValue _now;
92 : :
93 : : public:
94 : :
95 : 3 : explicit EventInfo(EventLoop & loop,
96 : : //const int fd = -1,
97 : : const int io_events = EventLoop::None,
98 : : const bool timeout = false) :
99 : 3 : /* _fd(fd),*/ _timeout(timeout), _io_events(io_events), _loop(loop), _now()
100 : 3 : { }
101 : :
102 : : int io_events() const {
103 : : return _io_events;
104 : : }
105 : :
106 : : /*
107 : : int descriptor() const {
108 : : return _fd;
109 : : }
110 : : */
111 : 3 : EventLoop & event_loop() const {
112 : 3 : return _loop;
113 : : }
114 : : /*
115 : : bool signal_event() const {
116 : : return (_event & 0);
117 : : }
118 : : */
119 : :
120 : : /**
121 : : * This value does not necessarily have any relation to system time.
122 : : * It is only valid to compare it against time stamps of other events.
123 : : */
124 : : const TimeValue & now() const {
125 : : return _now;
126 : : }
127 : :
128 : 8 : bool timeout_event() const {
129 : 8 : return _timeout;
130 : : }
131 : :
132 : 8 : bool error_event() const {
133 : 8 : return (_io_events & EventLoop::Error);
134 : : }
135 : :
136 : 8 : bool hangup_event() const {
137 : 8 : return (_io_events & EventLoop::Hangup);
138 : : }
139 : :
140 : 8 : bool read_event() const {
141 : 8 : return (_io_events & EventLoop::Read);
142 : : }
143 : :
144 : 8 : bool write_event() const {
145 : 8 : return (_io_events & EventLoop::Write);
146 : : }
147 : : };
148 : :
149 : : __END_NS_SSRC_WISP_SERVICE
150 : :
151 : : #endif
|