Skip to content

Instantly share code, notes, and snippets.

@mkroman
Created March 2, 2012 16:48
Show Gist options
  • Save mkroman/1959586 to your computer and use it in GitHub Desktop.
Save mkroman/1959586 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "visichat/controller.hpp"
using namespace VisiChat;
Controller::Controller()
{
m_eventBase = event_base_new();
}
void Controller::run()
{
EVCallback<Controller> wrapper(this, &Controller::callback);
event* timerEvent = event_new(m_eventBase, -1, EV_TIMEOUT | EV_PERSIST, static_cast<event_callback_fn>(&wrapper), this);
timeval timeInterval = { 2, 0 };
event_add(timerEvent, &timeInterval);
event_base_dispatch(m_eventBase);
}
void Controller::callback(int fd, short flags, void *data)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
#pragma once
#include <event.h>
#include <time.h>
namespace VisiChat
{
template<class T>
class EVCallback
{
typedef void (T::*callbackFunctor)(int, short, void*);
public:
EVCallback(T* pInstance, callbackFunctor pFunctor)
: m_pInstance(pInstance), m_pFunctor(pFunctor)
{
}
void operator()(int fileDescriptor, short flags, void* data)
{
(m_pInstance->*m_pFunctor)(fileDescriptor, flags, data);
}
private:
T* m_pInstance;
callbackFunctor m_pFunctor;
};
class Controller
{
public:
Controller();
void run();
void close();
void callback(int fd, short flags, void *data);
private:
event_base* m_eventBase;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment