2017-05-15 01:19:44 -07:00
|
|
|
#ifndef PLASMA_EVENTS
|
|
|
|
#define PLASMA_EVENTS
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "ae/ae.h"
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Constant specifying that the timer is done and it will be removed.
|
2017-05-15 01:19:44 -07:00
|
|
|
constexpr int kEventLoopTimerDone = AE_NOMORE;
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Read event on the file descriptor.
|
2017-05-15 01:19:44 -07:00
|
|
|
constexpr int kEventLoopRead = AE_READABLE;
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Write event on the file descriptor.
|
2017-05-15 01:19:44 -07:00
|
|
|
constexpr int kEventLoopWrite = AE_WRITABLE;
|
|
|
|
|
|
|
|
class EventLoop {
|
|
|
|
public:
|
2017-05-31 23:24:23 +00:00
|
|
|
// Signature of the handler that will be called when there is a new event
|
|
|
|
// on the file descriptor that this handler has been registered for.
|
|
|
|
//
|
|
|
|
// The arguments are the event flags (read or write).
|
2017-05-15 01:19:44 -07:00
|
|
|
typedef std::function<void(int)> FileCallback;
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
// This handler will be called when a timer times out. The timer id is
|
|
|
|
// passed as an argument. The return is the number of milliseconds the timer
|
|
|
|
// shall be reset to or kEventLoopTimerDone if the timer shall not be
|
|
|
|
// triggered again.
|
2017-05-15 01:19:44 -07:00
|
|
|
typedef std::function<int(int64_t)> TimerCallback;
|
|
|
|
|
|
|
|
EventLoop();
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Add a new file event handler to the event loop.
|
|
|
|
///
|
|
|
|
/// @param fd The file descriptor we are listening to.
|
|
|
|
/// @param events The flags for events we are listening to (read or write).
|
|
|
|
/// @param callback The callback that will be called when the event happens.
|
|
|
|
/// @return Returns true if the event handler was added successfully.
|
2017-05-15 01:19:44 -07:00
|
|
|
bool add_file_event(int fd, int events, FileCallback callback);
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Remove a file event handler from the event loop.
|
|
|
|
///
|
|
|
|
/// @param fd The file descriptor of the event handler.
|
|
|
|
/// @return Void.
|
2017-05-15 01:19:44 -07:00
|
|
|
void remove_file_event(int fd);
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Register a handler that will be called after a time slice of
|
|
|
|
/// "timeout" milliseconds.
|
|
|
|
///
|
|
|
|
/// @param timeout The timeout in milliseconds.
|
|
|
|
/// @param callback The callback for the timeout.
|
|
|
|
/// @return The ID of the newly created timer.
|
2017-05-15 01:19:44 -07:00
|
|
|
int64_t add_timer(int64_t timeout, TimerCallback callback);
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Remove a timer handler from the event loop.
|
|
|
|
///
|
|
|
|
/// @param timer_id The ID of the timer that is to be removed.
|
|
|
|
/// @return The ae.c error code. TODO(pcm): needs to be standardized
|
2017-05-15 01:19:44 -07:00
|
|
|
int remove_timer(int64_t timer_id);
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
/// Run the event loop.
|
|
|
|
///
|
|
|
|
/// @return Void.
|
2017-05-15 01:19:44 -07:00
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void file_event_callback(aeEventLoop *loop,
|
|
|
|
int fd,
|
|
|
|
void *context,
|
|
|
|
int events);
|
|
|
|
|
|
|
|
static int timer_event_callback(aeEventLoop *loop,
|
|
|
|
long long timer_id,
|
|
|
|
void *context);
|
|
|
|
|
|
|
|
aeEventLoop *loop_;
|
|
|
|
std::unordered_map<int, std::unique_ptr<FileCallback>> file_callbacks_;
|
|
|
|
std::unordered_map<int64_t, std::unique_ptr<TimerCallback>> timer_callbacks_;
|
|
|
|
};
|
|
|
|
|
2017-05-31 23:24:23 +00:00
|
|
|
#endif // PLASMA_EVENTS
|