proxygen
EventHandler.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
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.apache.org/licenses/LICENSE-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 #pragma once
18 
19 #include <cstddef>
20 
21 #include <boost/noncopyable.hpp>
22 #include <glog/logging.h>
23 
27 
28 namespace folly {
29 
30 class EventBase;
31 
39 class EventHandler : private boost::noncopyable {
40  public:
41  enum EventFlags {
42  NONE = 0,
43  READ = EV_READ,
44  WRITE = EV_WRITE,
46  PERSIST = EV_PERSIST,
47 // Temporary flag until EPOLLPRI is upstream on libevent.
48 #ifdef EV_PRI
49  PRI = EV_PRI,
50 #endif
51  };
52 
65  explicit EventHandler(EventBase* eventBase, int fd)
66  : EventHandler(eventBase, NetworkSocket::fromFd(fd)) {}
67  explicit EventHandler(
68  EventBase* eventBase = nullptr,
70 
76  virtual ~EventHandler();
77 
83  virtual void handlerReady(uint16_t events) noexcept = 0;
84 
100  bool registerHandler(uint16_t events) {
101  return registerImpl(events, false);
102  }
103 
107  void unregisterHandler();
108 
112  bool isHandlerRegistered() const {
114  }
115 
125  void attachEventBase(EventBase* eventBase);
126 
136  void detachEventBase();
137 
143  void changeHandlerFD(int fd) {
145  }
146 
148 
156  void initHandler(EventBase* eventBase, int fd) {
157  initHandler(eventBase, NetworkSocket::fromFd(fd));
158  }
159 
160  void initHandler(EventBase* eventBase, NetworkSocket fd);
161 
166  return (isHandlerRegistered()) ? uint16_t(event_.ev_events) : 0u;
167  }
168 
184  return registerImpl(events, true);
185  }
186 
187  bool isPending() const;
188 
189  private:
190  bool registerImpl(uint16_t events, bool internal);
191  void ensureNotRegistered(const char* fn);
192 
193  void setEventBase(EventBase* eventBase);
194 
195  static void libeventCallback(libevent_fd_t fd, short events, void* arg);
196 
197  struct event event_;
199 };
200 
201 } // namespace folly
struct event event_
Definition: EventHandler.h:197
bool registerImpl(uint16_t events, bool internal)
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
void initHandler(EventBase *eventBase, int fd)
Definition: EventHandler.h:156
bool isPending() const
void setEventBase(EventBase *eventBase)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void attachEventBase(EventBase *eventBase)
requires E e noexcept(noexcept(s.error(std::move(e))))
virtual void handlerReady(uint16_t events) noexcept=0
uint16_t getRegisteredEvents() const
Definition: EventHandler.h:165
EventBase * eventBase_
Definition: EventHandler.h:198
static bool isEventRegistered(const struct event *ev)
Definition: EventUtil.h:51
void changeHandlerFD(int fd)
Definition: EventHandler.h:143
EventHandler(EventBase *eventBase, int fd)
Definition: EventHandler.h:65
void ensureNotRegistered(const char *fn)
bool registerInternalHandler(uint16_t events)
Definition: EventHandler.h:183
bool registerHandler(uint16_t events)
Definition: EventHandler.h:100
static NetworkSocket fromFd(int fd)
Definition: NetworkSocket.h:44
int libevent_fd_t
Definition: Event.h:37
bool isHandlerRegistered() const
Definition: EventHandler.h:112