Socket网络编程--Libev库学习(2)

时间:2023-05-09 23:18:16

  这一小节讲各个观察器(Watcher)

  在libev下面watcher相当于EventHandler这么一个概念,通常里面会绑定fd回调函数以及我们需要关注的事件。 然后一旦触发事件之后会触发我们使用的回调函数,回调函数参数通常有reactor,watcher以及触发的事件。这里不打算重复文档里面的watcher 相关的内容和对应的API,但是对于某些内容的话可能会提到并且附带一些注释。之前我们还是看看通用过程,这里使用TYPE区分不同类型watcher.

 typedef void (*)(struct ev_loop *loop, ev_TYPE *watcher, int revents) callback; // callback都是这种类型
ev_init (ev_TYPE *watcher, callback); // 初始化watcher
ev_TYPE_set (ev_TYPE *watcher, [args]); // 设置watcher
ev_TYPE_init (ev_TYPE *watcher, callback, [args]); // 通常使用这个函数最方便,初始化和设置都在这里
ev_TYPE_start (loop, ev_TYPE *watcher); // 注册watcher
ev_TYPE_stop (loop, ev_TYPE *watcher); // 注销watcher
ev_set_priority (ev_TYPE *watcher, int priority); // 设置优先级
ev_feed_event (loop, ev_TYPE *watcher, int revents); // 这个做跨线程通知非常有用,相当于触发了某个事件。
bool ev_is_active (ev_TYPE *watcher); // watcher是否active.
bool ev_is_pending (ev_TYPE *watcher); // watcher是否pending.
int ev_clear_pending (loop, ev_TYPE *watcher); // 清除watcher pending状态并且返回事件

  wacther的状态有下面这么几种:

  (1) initialiased.调用init函数初始化
  (2) active.调用start进行注册
  (3) pending.已经触发事件但是没有处理
  (4) inactive.调用stop注销。这个状态等同于initialised这个状态。

  ev_io

 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ev.h> static void stdin_callback(struct ev_loop *loop,ev_io *w,int revents)
{
char str[];
if(revents & EV_READ)
{
//stdin might have data for us
printf("有数据可读\n");
scanf("%s",str);
ev_io_stop(loop,w);
}
else if(revents & EV_WRITE)
{
//stdout might have data for us
printf("有数据输出\n");
//ev_break(loop,EVBREAK_ONE);
}
printf("water:%d\n",ev_is_active(w));
} int main(int argc,char **argv)
{
struct ev_loop * main_loop = ev_default_loop();
//这里的ev_default_loop可以使用ev_loop_new动态分配一个,然后使用ev_loop_destroy销毁。
//struct ev_loop * epoller = ev_loop_new(EVBACKEND_EPOLL | EVFLAG_NOENV);
//这里一般是使用EVBACKEND_EPOLL模型,同样的还有EVBACKEND_SELECT EVBACKEND_POLL EVBACKEND_KQUEUE EVBACKEND_DEVPOLL EVBACKEND_PORT 如果默认,那么ev会自动判断系统环境,选择最适合的模型,Linux一般为epoll bsd一般为kqueue什么的。
ev_io stdin_watcher;
ev_init(&stdin_watcher,stdin_callback);
ev_io_set(&stdin_watcher,STDIN_FILENO,EV_READ|EV_WRITE);
ev_io_start(main_loop,&stdin_watcher); //ev_run(main_loop,EVRUN_ONCE); //void ev_set_io_collect_interval (EV_P_ ev_tstamp interval);//这个是设置轮询的时间
//typedef double ev_tstamp
ev_set_io_collect_interval(main_loop,.);//2秒
ev_run(main_loop,);
//ev_is_active(ev_TYPE * watcher);//用于判断watcher是否为active
printf("main:%d\n",ev_is_active(&stdin_watcher)); //initialiased.调用init函数初始化
//active.调用start进行注册
//pending.已经触发事件但是没有处理
//inactive.调用stop注销。这个状态等同于initialised这个状态 return ;
}

  ev_timer

 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ev.h> static void three_second_callback(struct ev_loop *loop,ev_timer *w,int revents)
{
//这是一个3秒触发的计时器
printf("3秒触发器\n");
}
static void five_second_callback(struct ev_loop *loop,ev_timer *w,int revents)
{
//这是一个5秒触发的计时器
printf("5秒触发器\n");
}
static void the_second_callback(struct ev_loop *loop,ev_timer *w,int revents)
{
//这是一个10秒触发的计时器
printf("10秒触发器\n");
} int main(int argc, char **args)
{
struct ev_loop * main_loop=ev_default_loop(); ev_timer mytimer_watcher3;
ev_timer mytimer_watcher5; ev_init(&mytimer_watcher3,three_second_callback);
ev_timer_set(&mytimer_watcher3,,);
ev_timer_start(main_loop,&mytimer_watcher3);
ev_run(main_loop,);//这个在ev_io上是一直判断的。但是这个触发器只会触发一次,不会每3秒触发一次。这是个问题。 ev_init(&mytimer_watcher5,five_second_callback);
ev_timer_set(&mytimer_watcher5,,);
ev_timer_start(main_loop,&mytimer_watcher5);
ev_run(main_loop,); ev_timer_start(main_loop,&mytimer_watcher3);
ev_timer_start(main_loop,&mytimer_watcher5);
ev_run(main_loop,);//这里不会等待3,5秒,而是上一步后,直接输出,可见触发器只能用一次 ev_timer_set(&mytimer_watcher3,,);
ev_timer_start(main_loop,&mytimer_watcher3);
ev_timer_set(&mytimer_watcher5,,);
ev_timer_start(main_loop,&mytimer_watcher5);
ev_run(main_loop,);//这里就会等待了,要重新set一遍 return ;
}

  运行的结果是在第3秒输出(3秒触发器),第8秒输出(5秒触发器)(3秒触发器)(5秒触发器),第11秒输出(3秒触发器),第13秒输出(5秒触发器)。

  这个ev_timer居然不能重复,是不是没有解决办法呢?不是还有个ev_periodic这个可以实现周期性观察器。

  ev_periodic

 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ev.h> static void periodic_callback(struct ev_loop *loop,ev_periodic * w, int revents)
{
printf("每3秒执行一次\n");
//ev_break(loop,EVBREAK_ALL);
} //ev_tstamp=double
static ev_tstamp periodic_scheduler_callback(ev_periodic *w,ev_tstamp now)
{
return now+;//注意时间要加上个now,是一个绝对时间
} int main(int argc, char **args)
{
struct ev_loop * main_loop=ev_default_loop(); ev_periodic periodic_watcher;
//下面这个是第3个参数为3 是一个表达式
ev_init(&periodic_watcher,periodic_callback);
ev_periodic_set(&periodic_watcher,,,);
ev_periodic_start(main_loop,&periodic_watcher);
ev_run(main_loop,); //如果时间周期计算方式,不能通过一个表达式来表示,那么可以通过一个函数来表示,放在set的第4个参数
ev_init(&periodic_watcher,periodic_callback);
ev_periodic_set(&periodic_watcher,,,periodic_scheduler_callback);
ev_periodic_start(main_loop,&periodic_watcher);
ev_run(main_loop,);
//注意上下两部分不能通过运行,要注释掉一个才可以看到效果
return ;
}

  ev_signal

 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <ev.h> static void sigint_callback(struct ev_loop * loop,ev_signal *w,int revents)
{
if(revents & EV_SIGNAL)//用这个可以判断这次进来的是不是ev_signal 如果一个callback回调函数复用的话,就可以用这个来区分
{
printf("signal SIGINT\n");
ev_break(loop, EVBREAK_ALL);
}
} static void sigquit_callback(struct ev_loop * loop,ev_signal *w,int revents)
{
printf("signal SIGQUIT\n");
ev_break(loop, EVBREAK_ALL);
} int main(int argc, char **args)
{
struct ev_loop * main_loop=ev_default_loop(); ev_signal sigint_watcher;
ev_signal sigquit_watcher; ev_init(&sigint_watcher,sigint_callback);
ev_signal_set(&sigint_watcher,SIGINT/*Other want to catch*/);//这里多个信号不能用或符号| 连接起来
ev_signal_start(main_loop,&sigint_watcher); ev_init(&sigquit_watcher,sigquit_callback);
ev_signal_set(&sigquit_watcher,SIGQUIT/*Other want to catch*/);
ev_signal_start(main_loop,&sigquit_watcher); ev_run(main_loop,); return ;
}

  运行程序,输入Ctrl-C或Ctrl-\都是可以捕获的。

  ev_child

 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <ev.h> static void child_callback(struct ev_loop *loop,ev_child *w,int revents)
{
ev_child_stop(loop,w);
printf("Process %d exited with status %d\n",w->rpid,w->rstatus);
} int main(int argc, char **args)
{
struct ev_loop * main_loop=ev_default_loop();
pid_t pid; ev_child child_watcher; pid=fork();
if(pid<)
{
printf("Fork Error\n");
return -;
}
else if(pid==)//child
{
printf("child doing..\n");
return ;
}
else //father
{
sleep();//即使让子进程先执行,最后还是可以捕获到。
ev_init(&child_watcher,child_callback);
ev_child_set(&child_watcher,pid,);
//ev_child_start(EV_DEFAULT_ &child_watcher);
ev_child_start(main_loop,&child_watcher);
ev_run(main_loop,);
} //waitpid(pid,0,0);
return ;
}

  上面的例子,主进程通过pid将子进程绑定到了child_callback事件中,当子进程挂掉后,主进程就能捕捉的信号,然后调用child_callback函数。

  另一个测试场景:

  1 主进程启动后启动一个子进程。
  2 手动通过后台kill命令,kill掉子进程。
  3 主进程收到信息,打印出提示。

  上面代码第30行修改为while(1) ;  然后在第34行增加一行printf("pid:%d\n",pid); 然后运行,结果如下:

Socket网络编程--Libev库学习(2)

  注意上面有些是命令,有些是输出的中间结果。这样看起来很乱,但是终端运行结果就是这样。第1、6行是命令。

  ev_stat

 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ev.h> static void stat_callback(struct ev_loop *loop,ev_stat *w, int revents)
{
if(w->attr.st_nlink)
{
printf("The file size %ld\n",(long)w->attr.st_size);
}
else
{
printf("文件不存在\n");
}
} int main(int argc, char **args)
{
struct ev_loop *main_loop=ev_default_loop(); ev_stat stat_watcher;
ev_init(&stat_watcher,stat_callback);
ev_stat_set(&stat_watcher,"/home/myuser/hello.txt",);
ev_stat_start(main_loop,&stat_watcher); ev_run(main_loop,);
return ;
}

  我们创建hello.txt这个文件,然后输入字符,然后保存,然后再打开,修改就这样。运行过程图

Socket网络编程--Libev库学习(2)

  文件attr的其他属性。

文档原文:The previous attributes of the file. The callback gets invoked whenever
C<prev> != C<attr>, or, more precisely, one or more of these members
differ: C<st_dev>, C<st_ino>, C<st_mode>, C<st_nlink>, C<st_uid>,
C<st_gid>, C<st_rdev>, C<st_size>, C<st_atime>, C<st_mtime>, C<st_ctime>
文档解释:如果以前的文件有一点修改,无论是什么属性,都将触发这个回调函数。这个attr文件在这里可以获取到的属性成员有

  我们的stat_callback函数修改如下:

 static void stat_callback(struct ev_loop *loop,ev_stat *w, int revents)
{
if(w->attr.st_nlink)
{
printf("The file st_dev %d\n",w->attr.st_dev);
printf("The file st_ino %d\n",w->attr.st_ino);
printf("The file st_mode %d\n",w->attr.st_mode);
printf("The file st_nlink %d\n",w->attr.st_nlink);
printf("The file st_uid %d\n",w->attr.st_uid);
printf("The file st_gid %d\n",w->attr.st_gid);
printf("The file st_rdev %d\n",w->attr.st_rdev);
printf("The file st_size %d\n",w->attr.st_size);
printf("The file st_atime %d\n",w->attr.st_atime);
printf("The file st_mtime %d\n",w->attr.st_mtime);
printf("The file st_ctime %d\n",w->attr.st_ctime);
}
else
{
printf("文件不存在\n");
}
}

  运行结果:

Socket网络编程--Libev库学习(2)

  至于那些st_*的属性就不用说了,跟系统函数stat调用的返回结果是一样的。都是通用的。

  这一节到这里就结束了,这一节了解了几个最主要的watcher了。除了上面的几个外,还有下面这几个 ev_idle ev_prepare/ev_check ev_embed ev_fork ev_cleanup ev_async .

  本文地址: http://www.cnblogs.com/wunaozai/p/3954131.html