Skip to content

Instantly share code, notes, and snippets.

@DamonHao
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save DamonHao/bfdd263b5b0d2499b6c1 to your computer and use it in GitHub Desktop.

Select an option

Save DamonHao/bfdd263b5b0d2499b6c1 to your computer and use it in GitHub Desktop.
libev cpp example
/*
* test_io.cc
*
* Created on: Jun 4, 2014
* Author: damonhao
*/
#include <ev++.h>
#include <stdio.h>
#define BUFFERSIZE 50
using namespace ev;
class MyClass
{
public:
MyClass(ev::loop_ref loop, int fd) :
ioWatcher_(loop)
{
ioWatcher_.set<MyClass, &MyClass::io_cb>(this);
ioWatcher_.set(fd, ev::READ);
}
void start()
{
ioWatcher_.start();
}
private:
void io_cb(ev::io &io_watcher, int revents)
{
puts("in io_cb");
char buf[BUFFERSIZE];
if (gets(buf) != NULL)
{
puts(buf);
}
else
{
puts("read fd error");
}
io_watcher.stop();
}
ev::io ioWatcher_;
};
int main()
{
default_loop loop;
// default_loop loop1 = loop;
// loop_ref haha = loop;
MyClass my_io(loop, STDIN_FILENO);
my_io.start();
loop.run();
return 0;
}
/*
* test_timer.cc
*
* Created on: Jun 13, 2014
* Author: damonhao
*/
#include <stdio.h>
#include <ev++.h>
using namespace ev;
class MyClass
{
public:
MyClass(ev::loop_ref loop) :
timerWatcher_(loop)
{
timerWatcher_.set<MyClass, &MyClass::timerCallBack2>(this);
}
void start()
{
timerWatcher_.start(2.0, 0.0);
}
private:
void timerCallBack1(ev::timer &io_watcher, int revents)
{
puts("timeout1");
}
//at most timer's call back should have no arguments
void timerCallBack2()
{
puts("timeout2");
}
ev::timer timerWatcher_;
};
int main()
{
default_loop loop;
MyClass myTimer(loop);
myTimer.start();
loop.run();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment