diff --git a/00-first-demo.md b/00-first-demo.md index 013b283b90b2554347e1155a4a7ea2835321c273..f57b8122afb4cf85b70777324572d78ac443c212 100644 --- a/00-first-demo.md +++ b/00-first-demo.md @@ -12,7 +12,7 @@ $(TARGET): g++ -o $(TARGET) $(LDFLAGS) $(LIBS) ``` -[示例工程目录](00-first-demo) +[示例工程目录](examples/00-first-demo) 然后执行 `make && ./demo`,效果: ![执行效果](images/000-first-demo.png) diff --git a/01-first-module.md b/01-first-module.md index bf11085a3d2669f015180df9726dab2fea62cdb7..2ccf4f92f1a98e38ea69b83eeb6e91c60a3d3bf9 100644 --- a/01-first-module.md +++ b/01-first-module.md @@ -24,9 +24,9 @@ void RegisterApps(Module &apps, Context &ctx) { } ``` 然后再修改 Makefile,将 app\_main.cpp 加入到源文件中。 -见:[Makefile](01-first-module/Makefile) +见:[Makefile](examples/01-first-module/Makefile) -[示例工程目录](01-first-module) +[示例工程目录](examples/01-first-module) 编译执行:`make && ./demo`,运行结果: ![运行效果图](images/002-your-first-module-1.png) @@ -52,9 +52,9 @@ class MyModule : public tbox::main::Module { 为了能使用`LogTag()`日志打印函数,我们还需要添加`#include `。 完成之后执行 `make`。在编译的时侯,我们看到了一条编译警告: ![没有指定LOG\_MODULE\_ID警告](images/004-compile-warn.png) -它是在说我们程序没有指定日志的模块名。这仅是一条警告,我们可以忽略它。不过,我建议你在 Makefile 的`CXXFLAGS`定义中添加`-DLOG_MODULE_ID='"demo"'` 进行定义。 +它是在说我们程序没有指定日志的模块名。这仅是一条警告,我们可以忽略它。不过,我建议你在 Makefile 的`CXXFLAGS`定义中添加`-DMODULE_ID='"demo"'` 进行定义。 -[示例工程目录](02-add-log-tag) +[示例工程目录](examples/02-add-log-tag) 编译后执行,然后按 ctrl+c 退出程序,完整的日志打印效果: ![运行效果图](images/003-your-first-module-with-log.png) diff --git a/02-add-log-tag.md b/02-add-log-tag.md index c215cc1f13a6ca55203b0bfbb92a2bd6e3618d3d..c0f87a86a085c68217ce1caede15c25878a8365e 100644 --- a/02-add-log-tag.md +++ b/02-add-log-tag.md @@ -3,18 +3,19 @@ 调试日志是程序中一个比较重要的一部分。通常,我们在开发程序的时候,会直接使用 `printf()` 或 `std::cout`,`std::cerr` 在终端上打印日志。但这样打印,有很多不足:1)面日志格式混乱;2)能提供的调试信息不够充分;3)输出的方式太过单一;4)没有日志等级筛选功能。 因此,我们会去寻找开源的日志系统库,如:spdlog, glog, log4cxx,来满足日志打印需求。 -好在,当你使用 tbox.main 框架时,你根本就不需要为日志打印而发愁,因为它自带日志打印系统。你直接用就可以了。其它的不需要你关心。 +好在,当你使用 tbox.main 框架时,你根本就不需要为日志打印而发愁,因为它自带日志打印系统。你直接用就可以了,其它的不需要你关心。 日志等级有: |值|等级|名称|显示颜色| |:-|:-|:-|:-| -|0|FATAL|代码错误|暗红| +|0|FATAL|代码错误|红底黑字| |1|ERROR|错误|红| -|2|WARN|警告|黄| -|3|NOTICE|注意|淡黄| -|4|INFO|信息|绿| -|5|DEBUG|调试|淡蓝| -|6|TRACE|跟踪|紫| +|2|WARN|警告|黄底黑字| +|3|NOTICE|注意|黄| +|4|IMPORTANT|重要|绿底黑字| +|5|INFO|信息|绿| +|6|DEBUG|调试|淡蓝| +|7|TRACE|跟踪|紫| 日志打印函数有: |函数|等级|用途| @@ -23,8 +24,9 @@ |`LogErr(fmt,...)`|ERROR|打印导致业务完全不可用的严重错误。区别于FATAL,指的是非程序级错误,比如配置文件打不开| |`LogWarn(fmt,...)`|WARN|打印影响部分功能的错误。区别于ERROR,这种错误不整响主要功能| |`LogNotice(fmt,...)`|NOTICE|打印外部因素引起的轻微异常,这种异常不会影响功能,但需要注意。如对方的协议格式错误、版本不一致| -|`LogInfo(fmt,...)`|INFO|打印与外部交互的信息,用于鉴别问题是外部的,还是内部的| -|`LogDbg(fmt,...)`|DEBUG|打印内部模块之间的信息,用于鉴别问题是属于内部的哪个模块的| +|`LogImportant(fmt,...)`|IMPORTANT|打印与外界交互中非常重要的信息,尽量少用,以突显其稀有性| +|`LogInfo(fmt,...)`|INFO|打印与外界交互的普通信息,用于鉴别问题是外部的,还是内部的| +|`LogDbg(fmt,...)`|DEBUG|打印内部模块之间的信息,用于鉴别问题是属于内部的哪个模块的,以及处理方式| |`LogTrace(fmt,...)`|TRACE|打印临时查问题所需的日志信息| |`LogUndo()`|NOTICE|标记有未实现的功能,通用创建一个空函数时,就会放置一个LogUndo()| |`LogTag()`|TRACE|用于打印运行标记,观察程序有没有运行过标记位置| @@ -36,7 +38,7 @@ 下面,我们来实际操作一下,在MyModule的onInit()尝试所有的日志打印函数: ![](images/012-log-print-code.png) -[示例工程目录](06-log-print/) +[示例工程目录](examples/06-log-print/) 编译执行效果: ![日志打印效果](images/011-log-print.png) diff --git a/04-timer-event.md b/04-timer-event.md index b3243f09fe4e42ae0765f50b80036d0d65057201..4c12550c7fb8b90d926814b74329dc08137163e5 100644 --- a/04-timer-event.md +++ b/04-timer-event.md @@ -22,7 +22,7 @@ (8) 在 `onStart()` 中启动定时器; (9) 在 `onStop()` 中停止定时器; -[示例工程目录](07-timer-event) +[示例工程目录](examples/07-timer-event) 编译后,执行效果: ![](images/015-timer-result.png) diff --git a/05-fd-event.md b/05-fd-event.md index bcf2e93ff6cded003355216af92732df98886636..d0453b029f26dd809c5eae44f88509c33085f181 100644 --- a/05-fd-event.md +++ b/05-fd-event.md @@ -12,7 +12,7 @@ ![代码](images/016-fdevent-code.png) 在(9)处定义了接收到stdin输入的处理过程。首先,检查一下是不是可读事件,如果是才处理。然后读0文件描述符,即stdin。将数据读到`buff`缓冲中。最后,根据输入的内容进行处理。 -[示例工程目录](08-fd-event) +[示例工程目录](examples/08-fd-event) 编译执行效果: ![结果](images/017-fdevent-result.png) diff --git a/06-http-server.md b/06-http-server.md index 97e3700c84ad4981da60f78e663f0e57d69bfd59..43f94b538f968d4c3b867438929c5475f3de298c 100644 --- a/06-http-server.md +++ b/06-http-server.md @@ -12,7 +12,7 @@ 为了演示,我们写一个非常简单的http服务程序,当我们访问它的时候,显示hello页面。 ![http代码](images/023-tiny-http-code.png) -[示例程序目录](10-tiny-http-server) +[示例程序目录](examples/10-tiny-http-server) 编译执行,使用 curl 测试,结果如下: ![http结果](images/024-tiny-http-result.png) diff --git a/08-terminal.md b/08-terminal.md index 119a0469c5f19371ac199c5728494de9b215c36d..10890e67ae2aab180be97d1bea3e95e460563210 100644 --- a/08-terminal.md +++ b/08-terminal.md @@ -60,7 +60,7 @@ OK,我们将http的示例copy过来,在其原有基础上进行修改: - `const Session &s`,会话对象,每个连接都是独立的。我们可以使用它的 `send(const std::string &txt)` 方法可以向终端回复内容; - `const Args &a`,参数列表,本质上就是 `std::vector`。需要说明的是:`a[0]` 永远是命令本身,后之才是参数内容。 -[示例工程目录](12-terminal) +[示例工程目录](examples/12-terminal) 编译后运行,再使用 telnet 登陆上去进行操作: ![](images/038-http-server-terminal.png) diff --git a/09-add-app-info.md b/09-add-app-info.md index d7a4e06365b51ff7bc651ad0b18293dde55ede19..2d73eb0599098bb46849ebd37f472227238ea5b2 100644 --- a/09-add-app-info.md +++ b/09-add-app-info.md @@ -7,7 +7,7 @@ ![添加代码](images/006-add-code.png) 上面定义了三个函数:`GetAppDescribe()`、`GetAppVersion()`、`GetAppBuildTime()`,分析用于告析 tbox.main 框架当前应用的描述、版本号、编译时间点。 -[完整示例代码](03-add-app-info) +[完整示例代码](examples/03-add-app-info) 重新构建,再次执行 `./demo -v; ./demo -h` 效果: ![](images/007-has-desc.png) @@ -34,7 +34,7 @@ └── module.h ``` -[示例工程目录](04-normal-app-demo) +[示例工程目录](examples/04-normal-app-demo) 建议:在 my/ 目录下的所有代码,均以 `my` 作为顶层命名空间,以防在下一节的多Module情况下,命名空间污染。 diff --git a/10-multi-modules.md b/10-multi-modules.md index 2802dd050efbc57a1c9a75920d06e0c23aecd64a..81704f01d6b3fd1c61bf11fb6f5016b5efa0d2f4 100644 --- a/10-multi-modules.md +++ b/10-multi-modules.md @@ -28,7 +28,7 @@ └── module.h ``` -[示例工程目录](05-two-modules) +[示例工程目录](examples/05-two-modules) 构建后运行: ![多Module在同一进程运行效果](images/009-two-modules.png) diff --git a/12-timer-pool.md b/12-timer-pool.md new file mode 100644 index 0000000000000000000000000000000000000000..89784bd9068f4e77cdca34fefc2e4cf4e5d456e9 --- /dev/null +++ b/12-timer-pool.md @@ -0,0 +1,28 @@ +# 定时器池 + +在 [定时器](04-timer-event.md) 这一节中,我们学习了TimerEvent的实用,对定时器有了一定的了解。 + +在实际的项目中,我们会发现`TimerEvent`使用起来不是很方便。为了使用它,我们需要调`ctx().loop()->newTimerEvent()`返回一个定时器对象的指针。为保证资源不泄漏,我们需要在当将Module的析构函数中进行销毁。有时我们只是临时需要一个单次定时器而已。这样的生命期操控不方便,很繁琐。 +为此,我们引入了`TimerPool`这个模块。 +TimerPool模块帮我们管理定时器的生命期。我们需要使用定时器的时候,调它的`doAfter()`或者`doEvent()`即可,非常方便。 + +在 tbox.main 框架中,直带定时器池。使用时只需要:`ctx().timer_pool()` 就可以得到`TimerPool`对象的指针。 +下面,我将使用一个示例程序进行展示: + +![](images/045-timer-pool-code.png) + +(1) 在`onStart()` 中,分别启动了两个定时器。一个是用的`doEvery()`,另一个是`doAfter()`; +(2) 在`onStop()` 中,取消了这两个定时器; + +[示例工程目录](examples/13-timer-pool) + +编译后,执行效果: +![](images/044-timer-pool-run.png) +可以看到周期性定时器有每秒触发。在第5秒时,单次定时器也触发了。 + +**注意**: +虽然创建定时器方便了,但一定要注意生命期倒挂风险。 +所谓“生命期倒挂”具体表示为:所创建定时器时传入了一个生命期比较短的对象。在定时器触发时,这个对象的生命期已经提前结束了,进而导致不可预期的程序崩溃问题。 + +------- +[[返回主页]](README.md) diff --git a/13-timer-fd.md b/13-timer-fd.md new file mode 100644 index 0000000000000000000000000000000000000000..1895d3d9ad1416efa444149413eec7facc816c07 --- /dev/null +++ b/13-timer-fd.md @@ -0,0 +1,34 @@ +# TimerFd + +在 [定时器](04-timer-event.md) 这一节中,我们提及到`TimerEvent`的实现。本质上,它是通过`epoll()`或`select()`中阻塞的超时参数来实现定时任务的。`TimerEvent`在实际运用中的会有约400us的误差。如果想要精度更好的定时器,则需要使用本节介绍的`TimerFd`。 +`TimerFd` 定义在 `eventx/timer_fd.h` 中,是基于Linux内核提供的`timer_fd`实现的。具体的原理可阅读相关文章《[Linux fd 系列 — 定时器 timerfd 是什么?](https://zhuanlan.zhihu.com/p/409434419)》 + +本节通过一个示例来引导大家掌握`TimerFd`的使用方法。 + +![](images/047-timer-fd-code.png) + +1) 与`TimerEvent`不同的是`TimerFd`不属于`Loop`的一部分,也没有被纳入到框架。要使用它,都需要`#include ` +2) 在`MyModule`中分别实列化两个`TimerFd`对象:`oneshot_timer_`与`repeat_timer_`; +3) 在`MyModule`的初始化列表中对`oneshot_timer_`与`repeat_timer_`进行构造。注意,它的初始化需要`ctx().loop()`获取的`Loop`对象指针; +4) 在`onInit()`中对它们进行初始化,只传一个时间的表示单次触发;传两个时间的表示单次触发之后,还需要重复触发; +5) 在`onStart()`中使能它们。在工程中,不是一定要在`onStart()`中使能它们,根据业务需要我们也可以在需要的时间再使能它们; +6) 在`onStop()`中关闭它们; +7) 在`onCleanup()`中清理它们; + +[示例源码](examples/14-timer-fd/app_main.cpp) +[示例工程目录](examples/14-timer-fd) + +编译后,执行效果: +![](images/046-timer-fd-run.png) +我们注意到,它的定时精确误差在5us以内,非常可观。 + +思考:为什么使用`TimerFd`的精准度会比`TimerEvent`高这么多? +猜想:由于`TimerFd`是基于Linux内核中的`timer_fd`实现的。在`timer_fd`触发超时时,内核立即让`timer_fd`的文件描述符变成可读,鉴于定时器的实时性要求,Linux内核会立即唤醒对应的线程并执行。而基于`epoll()`与`select()`超时参数的`TimerEvent`则得不到Linux内核的特殊照顾。当`epoll()`或`select()`等待超时后,Linux内核只将对应的线程设置为READY状态并排队等待CPU资源,以致于执行的时间点存在较大的误差。 + +问题:`TimerEvent`,`TimerPool`,`TimerFd` 这三种实现方式如何抉择? +回答: +- 对于日常对精度要求不太高(如1ms精度可接受)的定时执行场景,可从`TimerEvent`与`TimerPool`中任选一种。如果是对精度要求高,要保证100us以内误差的,使用`TimerFd`; +- 遇到首次触发与后续触发间隔不一致的,采用`TimerFd`。 + +------- +[[返回主页]](README.md) diff --git a/README.md b/README.md index 3c188ea09c551bdb6db4f5377a10f8e5cd6c5217..4e1414f3418901bccd4cead96d1777feae032288 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ ## 子线程向主线程委派任务 ## 定时器池使用 +前面了解了定时器的使用,这里来了解一种创建定时器更方便的方式。 +[[点击前往]](12-timer-pool.md) + +## TimerFd的使用 +学习另一种定时更精准的定时器。 +[[点击前往]](13-timer-fd.md) ## 运行时异常捕获功能 @@ -84,4 +90,5 @@ ## 实战 ### 打造一个咖啡机 -学习状态机、行为树的使用 [[点击前往]](50-coffee-machine.md) +学习状态机、行为树的使用 +[[点击前往]](50-coffee-machine.md) diff --git a/00-first-demo/Makefile b/examples/00-first-demo/Makefile similarity index 100% rename from 00-first-demo/Makefile rename to examples/00-first-demo/Makefile diff --git a/01-first-module/Makefile b/examples/01-first-module/Makefile similarity index 100% rename from 01-first-module/Makefile rename to examples/01-first-module/Makefile diff --git a/01-first-module/app_main.cpp b/examples/01-first-module/app_main.cpp similarity index 100% rename from 01-first-module/app_main.cpp rename to examples/01-first-module/app_main.cpp diff --git a/examples/02-add-log-tag/Makefile b/examples/02-add-log-tag/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9d913c0f083be941c8739d6b869cc491dfc7c55f --- /dev/null +++ b/examples/02-add-log-tag/Makefile @@ -0,0 +1,24 @@ +TARGET:=demo + +OBJECTS:=app_main.o + +CXXFLAGS:=-I$(HOME)/.tbox/include -DMODULE_ID='"demo"' -std=c++11 +LDFLAGS:=-L$(HOME)/.tbox/lib -rdynamic +LIBS:=\ + -ltbox_main \ + -ltbox_coroutine \ + -ltbox_trace \ + -ltbox_terminal \ + -ltbox_network \ + -ltbox_eventx \ + -ltbox_event \ + -ltbox_log \ + -ltbox_util \ + -ltbox_base \ + -lpthread -ldl + +$(TARGET): $(OBJECTS) + g++ -o $@ $^ $(LDFLAGS) $(LIBS) + +clean: + rm *.o diff --git a/02-add-log-tag/app_main.cpp b/examples/02-add-log-tag/app_main.cpp similarity index 100% rename from 02-add-log-tag/app_main.cpp rename to examples/02-add-log-tag/app_main.cpp diff --git a/02-add-log-tag/Makefile b/examples/03-add-app-info/Makefile similarity index 100% rename from 02-add-log-tag/Makefile rename to examples/03-add-app-info/Makefile diff --git a/03-add-app-info/app_main.cpp b/examples/03-add-app-info/app_main.cpp similarity index 100% rename from 03-add-app-info/app_main.cpp rename to examples/03-add-app-info/app_main.cpp diff --git a/04-normal-app-demo/Makefile b/examples/04-normal-app-demo/Makefile similarity index 100% rename from 04-normal-app-demo/Makefile rename to examples/04-normal-app-demo/Makefile diff --git a/04-normal-app-demo/apps.cpp b/examples/04-normal-app-demo/apps.cpp similarity index 100% rename from 04-normal-app-demo/apps.cpp rename to examples/04-normal-app-demo/apps.cpp diff --git a/04-normal-app-demo/build.cpp b/examples/04-normal-app-demo/build.cpp similarity index 100% rename from 04-normal-app-demo/build.cpp rename to examples/04-normal-app-demo/build.cpp diff --git a/04-normal-app-demo/info.cpp b/examples/04-normal-app-demo/info.cpp similarity index 100% rename from 04-normal-app-demo/info.cpp rename to examples/04-normal-app-demo/info.cpp diff --git a/04-normal-app-demo/my/app.mk b/examples/04-normal-app-demo/my/app.mk similarity index 100% rename from 04-normal-app-demo/my/app.mk rename to examples/04-normal-app-demo/my/app.mk diff --git a/04-normal-app-demo/my/module.cpp b/examples/04-normal-app-demo/my/module.cpp similarity index 100% rename from 04-normal-app-demo/my/module.cpp rename to examples/04-normal-app-demo/my/module.cpp diff --git a/04-normal-app-demo/my/module.h b/examples/04-normal-app-demo/my/module.h similarity index 100% rename from 04-normal-app-demo/my/module.h rename to examples/04-normal-app-demo/my/module.h diff --git a/05-two-modules/Makefile b/examples/05-two-modules/Makefile similarity index 100% rename from 05-two-modules/Makefile rename to examples/05-two-modules/Makefile diff --git a/05-two-modules/apps.cpp b/examples/05-two-modules/apps.cpp similarity index 100% rename from 05-two-modules/apps.cpp rename to examples/05-two-modules/apps.cpp diff --git a/05-two-modules/build.cpp b/examples/05-two-modules/build.cpp similarity index 100% rename from 05-two-modules/build.cpp rename to examples/05-two-modules/build.cpp diff --git a/05-two-modules/info.cpp b/examples/05-two-modules/info.cpp similarity index 100% rename from 05-two-modules/info.cpp rename to examples/05-two-modules/info.cpp diff --git a/05-two-modules/my/app.mk b/examples/05-two-modules/my/app.mk similarity index 100% rename from 05-two-modules/my/app.mk rename to examples/05-two-modules/my/app.mk diff --git a/05-two-modules/my/module.cpp b/examples/05-two-modules/my/module.cpp similarity index 100% rename from 05-two-modules/my/module.cpp rename to examples/05-two-modules/my/module.cpp diff --git a/05-two-modules/my/module.h b/examples/05-two-modules/my/module.h similarity index 100% rename from 05-two-modules/my/module.h rename to examples/05-two-modules/my/module.h diff --git a/05-two-modules/your/app.mk b/examples/05-two-modules/your/app.mk similarity index 100% rename from 05-two-modules/your/app.mk rename to examples/05-two-modules/your/app.mk diff --git a/05-two-modules/your/module.cpp b/examples/05-two-modules/your/module.cpp similarity index 100% rename from 05-two-modules/your/module.cpp rename to examples/05-two-modules/your/module.cpp diff --git a/05-two-modules/your/module.h b/examples/05-two-modules/your/module.h similarity index 100% rename from 05-two-modules/your/module.h rename to examples/05-two-modules/your/module.h diff --git a/03-add-app-info/Makefile b/examples/06-log-print/Makefile similarity index 100% rename from 03-add-app-info/Makefile rename to examples/06-log-print/Makefile diff --git a/06-log-print/app_main.cpp b/examples/06-log-print/app_main.cpp similarity index 95% rename from 06-log-print/app_main.cpp rename to examples/06-log-print/app_main.cpp index f4546bf069337c6db1d893b0b0dffba4dd6ce746..ad8dcc05065054b7f900b04b66440d767dc0b552 100644 --- a/06-log-print/app_main.cpp +++ b/examples/06-log-print/app_main.cpp @@ -12,6 +12,7 @@ class MyModule : public tbox::main::Module { LogErr("this is error log"); LogWarn("this is warn log"); LogNotice("this is notice log"); + LogImportant("this is important log"); LogInfo("this is info log"); LogDbg("this is debug log"); LogTrace("this is trace log"); diff --git a/06-log-print/Makefile b/examples/07-timer-event/Makefile similarity index 100% rename from 06-log-print/Makefile rename to examples/07-timer-event/Makefile diff --git a/07-timer-event/app_main.cpp b/examples/07-timer-event/app_main.cpp similarity index 100% rename from 07-timer-event/app_main.cpp rename to examples/07-timer-event/app_main.cpp diff --git a/07-timer-event/Makefile b/examples/08-fd-event/Makefile similarity index 100% rename from 07-timer-event/Makefile rename to examples/08-fd-event/Makefile diff --git a/08-fd-event/app_main.cpp b/examples/08-fd-event/app_main.cpp similarity index 100% rename from 08-fd-event/app_main.cpp rename to examples/08-fd-event/app_main.cpp diff --git a/08-fd-event/Makefile b/examples/09-signal-event/Makefile similarity index 100% rename from 08-fd-event/Makefile rename to examples/09-signal-event/Makefile diff --git a/09-signal-event/app_main.cpp b/examples/09-signal-event/app_main.cpp similarity index 100% rename from 09-signal-event/app_main.cpp rename to examples/09-signal-event/app_main.cpp diff --git a/10-tiny-http-server/Makefile b/examples/10-tiny-http-server/Makefile similarity index 100% rename from 10-tiny-http-server/Makefile rename to examples/10-tiny-http-server/Makefile diff --git a/10-tiny-http-server/app_main.cpp b/examples/10-tiny-http-server/app_main.cpp similarity index 100% rename from 10-tiny-http-server/app_main.cpp rename to examples/10-tiny-http-server/app_main.cpp diff --git a/11-parameters/Makefile b/examples/11-parameters/Makefile similarity index 100% rename from 11-parameters/Makefile rename to examples/11-parameters/Makefile diff --git a/11-parameters/app_main.cpp b/examples/11-parameters/app_main.cpp similarity index 100% rename from 11-parameters/app_main.cpp rename to examples/11-parameters/app_main.cpp diff --git a/11-parameters/config.json b/examples/11-parameters/config.json similarity index 100% rename from 11-parameters/config.json rename to examples/11-parameters/config.json diff --git a/12-terminal/Makefile b/examples/12-terminal/Makefile similarity index 100% rename from 12-terminal/Makefile rename to examples/12-terminal/Makefile diff --git a/12-terminal/app_main.cpp b/examples/12-terminal/app_main.cpp similarity index 100% rename from 12-terminal/app_main.cpp rename to examples/12-terminal/app_main.cpp diff --git a/09-signal-event/Makefile b/examples/13-timer-pool/Makefile similarity index 100% rename from 09-signal-event/Makefile rename to examples/13-timer-pool/Makefile diff --git a/examples/13-timer-pool/app_main.cpp b/examples/13-timer-pool/app_main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbcaa42958e4a1a247970ac55d7db21b27d45a2f --- /dev/null +++ b/examples/13-timer-pool/app_main.cpp @@ -0,0 +1,43 @@ +#include +#include + +class MyModule : public tbox::main::Module { + public: + explicit MyModule(tbox::main::Context &ctx) + : tbox::main::Module("my", ctx) + { } + + public: + virtual bool onStart() { + //! 启动周期性定时 + repeat_timer_token_ = ctx().timer_pool()->doEvery(std::chrono::seconds(1), [this] { onTick(); }); + //! 启动单次性定时 + oneshot_timer_token_ = ctx().timer_pool()->doAfter(std::chrono::seconds(5), [] { LogTag(); }); + return true; + } + + virtual void onStop() { + //! 退出时要记得cancel所有的定时器,以防生命期倒挂问题 + ctx().timer_pool()->cancel(repeat_timer_token_); + ctx().timer_pool()->cancel(oneshot_timer_token_); + } + + private: + void onTick() { + ++count_; + LogDbg("count:%d", count_); + } + + private: + tbox::eventx::TimerPool::TimerToken repeat_timer_token_; + tbox::eventx::TimerPool::TimerToken oneshot_timer_token_; + int count_ = 0; +}; + +namespace tbox { +namespace main { +void RegisterApps(Module &apps, Context &ctx) { + apps.add(new MyModule(ctx)); +} +} +} diff --git a/examples/14-timer-fd/Makefile b/examples/14-timer-fd/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..526d44604420f9f10215e8eba8db574873bc2ddb --- /dev/null +++ b/examples/14-timer-fd/Makefile @@ -0,0 +1,24 @@ +TARGET:=demo + +OBJECTS:=app_main.o + +CXXFLAGS:=-I$(HOME)/.tbox/include -DLOG_MODULE_ID='"demo"' -std=c++11 +LDFLAGS:=-L$(HOME)/.tbox/lib -rdynamic +LIBS:=\ + -ltbox_main \ + -ltbox_coroutine \ + -ltbox_trace \ + -ltbox_terminal \ + -ltbox_network \ + -ltbox_eventx \ + -ltbox_event \ + -ltbox_log \ + -ltbox_util \ + -ltbox_base \ + -lpthread -ldl + +$(TARGET): $(OBJECTS) + g++ -o $@ $^ $(LDFLAGS) $(LIBS) + +clean: + rm *.o diff --git a/examples/14-timer-fd/app_main.cpp b/examples/14-timer-fd/app_main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3984014f5294ef9f9810d395cf955115c1f5200c --- /dev/null +++ b/examples/14-timer-fd/app_main.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +class MyModule : public tbox::main::Module { + public: + explicit MyModule(tbox::main::Context &ctx) + : tbox::main::Module("my", ctx) + , oneshot_timer_(ctx.loop()) + , repeat_timer_(ctx.loop()) + { } + + public: + virtual bool onInit(const tbox::Json &) { + //! 只设置一个时间,表示单次定时5秒 + oneshot_timer_.initialize(std::chrono::seconds(5)); + oneshot_timer_.setCallback([] { LogDbg("oneshot"); }); + + //! 设置了两个时间,第一个时间表示首次触发为100ms,第二个时间表示后续间隔1秒触发 + repeat_timer_.initialize(std::chrono::milliseconds(100), std::chrono::seconds(1)); + repeat_timer_.setCallback([] { LogDbg("repeat"); }); + + return true; + } + + virtual bool onStart() { + oneshot_timer_.enable(); + repeat_timer_.enable(); + return true; + } + + virtual void onStop() { + repeat_timer_.disable(); + oneshot_timer_.disable(); + } + + virtual void onCleanup() { + repeat_timer_.cleanup(); + oneshot_timer_.cleanup(); + } + + private: + tbox::eventx::TimerFd oneshot_timer_; + tbox::eventx::TimerFd repeat_timer_; +}; + +namespace tbox { +namespace main { +void RegisterApps(Module &apps, Context &ctx) { + apps.add(new MyModule(ctx)); +} +} +} diff --git a/50-coffee-machine/Makefile b/examples/50-coffee-machine/Makefile similarity index 100% rename from 50-coffee-machine/Makefile rename to examples/50-coffee-machine/Makefile diff --git a/50-coffee-machine/apps.cpp b/examples/50-coffee-machine/apps.cpp similarity index 100% rename from 50-coffee-machine/apps.cpp rename to examples/50-coffee-machine/apps.cpp diff --git a/50-coffee-machine/build.cpp b/examples/50-coffee-machine/build.cpp similarity index 100% rename from 50-coffee-machine/build.cpp rename to examples/50-coffee-machine/build.cpp diff --git a/50-coffee-machine/coffee-machine b/examples/50-coffee-machine/coffee-machine similarity index 100% rename from 50-coffee-machine/coffee-machine rename to examples/50-coffee-machine/coffee-machine diff --git a/50-coffee-machine/coffee/app.cpp b/examples/50-coffee-machine/coffee/app.cpp similarity index 100% rename from 50-coffee-machine/coffee/app.cpp rename to examples/50-coffee-machine/coffee/app.cpp diff --git a/50-coffee-machine/coffee/app.h b/examples/50-coffee-machine/coffee/app.h similarity index 100% rename from 50-coffee-machine/coffee/app.h rename to examples/50-coffee-machine/coffee/app.h diff --git a/50-coffee-machine/coffee/app.mk b/examples/50-coffee-machine/coffee/app.mk similarity index 100% rename from 50-coffee-machine/coffee/app.mk rename to examples/50-coffee-machine/coffee/app.mk diff --git a/50-coffee-machine/info.cpp b/examples/50-coffee-machine/info.cpp similarity index 100% rename from 50-coffee-machine/info.cpp rename to examples/50-coffee-machine/info.cpp diff --git a/images/000-first-demo.png b/images/000-first-demo.png index f6ea771022d8462ebf97750001b9a7c0fe804518..cdc632774de5d3859b3d4831bb1c5059f2f649ba 100644 Binary files a/images/000-first-demo.png and b/images/000-first-demo.png differ diff --git a/images/001-first-demo-tips.png b/images/001-first-demo-tips.png index 24de43e841265a7ff90d3963f6154910b729ffdb..143c393f2d7560f8b911aeb26ca8f05a82415835 100644 Binary files a/images/001-first-demo-tips.png and b/images/001-first-demo-tips.png differ diff --git a/images/002-your-first-module-1.png b/images/002-your-first-module-1.png index 4b83dfaaf587d9d230eca9c9142e27419a3f6697..9dbe62892437a38eaac80d367dfa52f285e1dc8e 100644 Binary files a/images/002-your-first-module-1.png and b/images/002-your-first-module-1.png differ diff --git a/images/003-your-first-module-with-log.png b/images/003-your-first-module-with-log.png index d0408cf4752af2e34f6bb537df14429c83a45522..1149dbfdc2a134733f01cd309c6f2891d248ce6b 100644 Binary files a/images/003-your-first-module-with-log.png and b/images/003-your-first-module-with-log.png differ diff --git a/images/011-log-print.png b/images/011-log-print.png index abbb3506fba55a4b48dc741c074f40a8dd4c2cbb..312209615f7898784789e628f7f9f1dee4d21aca 100644 Binary files a/images/011-log-print.png and b/images/011-log-print.png differ diff --git a/images/012-log-print-code.png b/images/012-log-print-code.png index 6d838609da8cae7e71156e03585b66bcd2ed109a..fa9351767cc032c1289440aeb459f0b8661c103a 100644 Binary files a/images/012-log-print-code.png and b/images/012-log-print-code.png differ diff --git a/images/013-log-field.png b/images/013-log-field.png index 9a51789d59f4dcb3e0d313c78cea6a9e338ab89c..28001fa2f3c9ce5d8c63377c86339684db5b449c 100644 Binary files a/images/013-log-field.png and b/images/013-log-field.png differ diff --git a/images/015-timer-result.png b/images/015-timer-result.png index e2565421058a7e8e6390c8a9697c9e2e6849ddee..9a4e52a5f17a1bea7f5171f8a092e2f684dc2cd5 100644 Binary files a/images/015-timer-result.png and b/images/015-timer-result.png differ diff --git a/images/044-timer-pool-run.png b/images/044-timer-pool-run.png new file mode 100644 index 0000000000000000000000000000000000000000..12e4a17720e29e00940e12ccc4d51eed895398f7 Binary files /dev/null and b/images/044-timer-pool-run.png differ diff --git a/images/045-timer-pool-code.png b/images/045-timer-pool-code.png new file mode 100644 index 0000000000000000000000000000000000000000..9b7b8f5d3beafc782d46a55803341b2f60dac55e Binary files /dev/null and b/images/045-timer-pool-code.png differ diff --git a/images/046-timer-fd-run.png b/images/046-timer-fd-run.png new file mode 100644 index 0000000000000000000000000000000000000000..6ef0ef778904b27f1a30f9f435678a2a9300141c Binary files /dev/null and b/images/046-timer-fd-run.png differ diff --git a/images/047-timer-fd-code.png b/images/047-timer-fd-code.png new file mode 100644 index 0000000000000000000000000000000000000000..cedca57ffaa5a7c8d0e93954905c1d62e3685bca Binary files /dev/null and b/images/047-timer-fd-code.png differ