浏览代码

add comments

hewei.it 4 年之前
父节点
当前提交
4275df8574
共有 6 个文件被更改,包括 62 次插入0 次删除
  1. 8 0
      examples/consul/main.cpp
  2. 10 0
      examples/curl.cpp
  3. 20 0
      examples/hloop_test.c
  4. 14 0
      examples/hmain_test.cpp
  5. 5 0
      examples/htimer_test.c
  6. 5 0
      examples/nc.c

+ 8 - 0
examples/consul/main.cpp

@@ -4,6 +4,11 @@
 
 #include "consul.h"
 
+/*
+ * @介绍:consul服务注册与发现示例程序
+ *
+ */
+
 int main(int argc, char* argv[]) {
     if (argc < 3) {
         printf("Usage: consul_cli subcmd ServiceName [ServiceAddress ServicePort] [NodeIP NodePort]\n");
@@ -41,17 +46,20 @@ int main(int argc, char* argv[]) {
     consul_health_t health;
 
     if (strcmp(subcmd, "register") == 0) {
+        // 注册服务
         int ret = register_service(&node, &service, &health);
         printf("register_service retval=%d\n", ret);
         goto discover;
     }
     else if (strcmp(subcmd, "deregister") == 0) {
+        // 注销服务
         int ret = deregister_service(&node, &service);
         printf("deregister_service retval=%d\n", ret);
         goto discover;
     }
     else if (strcmp(subcmd, "discover") == 0) {
 discover:
+        // 发现服务
         std::vector<consul_service_t> services;
         discover_services(&node, ServiceName, services);
         for (auto& service : services) {

+ 10 - 0
examples/curl.cpp

@@ -7,6 +7,12 @@
  *         bin/curl -v 127.0.0.1:8080/echo -d 'hello,world!'
  */
 
+/*
+ * @介绍:curl是一个著名的URL命令行工具,支持ftp、telnet、smtp、http等多种协议,
+ *        最常用的还是用作http请求,这个示例代码使用libhv的http客户端实现了类似功能。
+ *
+ */
+
 #include "http_client.h"
 
 #ifdef _MSC_VER
@@ -114,6 +120,7 @@ int main(int argc, char* argv[]) {
     parse_cmdline(argc, argv);
 
     int ret = 0;
+    // 解析命令行参数,填充到请求对象
     HttpRequest req;
     if (grpc) {
         http_version = 2;
@@ -217,8 +224,10 @@ int main(int argc, char* argv[]) {
         }
     }
     HttpResponse res;
+    // 创建http客户端
     http_client_t* hc = http_client_new();
 send:
+    // 发送请求,接收响应
     ret = http_client_send(hc, &req, &res);
     if (verbose) {
         printf("%s\n", req.Dump(true,true).c_str());
@@ -243,6 +252,7 @@ send:
 #endif
         goto send;
     }
+    // 销毁http客户端
     http_client_del(hc);
     return ret;
 }

+ 20 - 0
examples/hloop_test.c

@@ -6,20 +6,29 @@
  *
  */
 
+/*
+ * @介绍:此程序是为了测试事件循环的各种事件,包括空闲事件、IO事件、定时器事件、自定义事件。
+ *
+ */
+
 #include "hloop.h"
 #include "hbase.h"
 #include "hlog.h"
 #include "nlog.h"
 
+// 自定义日志处理器
 void mylogger(int loglevel, const char* buf, int len) {
+    // ERROR以上级别日志打印到标准错误
     if (loglevel >= LOG_LEVEL_ERROR) {
         stderr_logger(loglevel, buf, len);
     }
 
+    // INFO以上级别日志写入日志文件
     if (loglevel >= LOG_LEVEL_INFO) {
         file_logger(loglevel, buf, len);
     }
 
+    // 所有日志都发送到网络日志器
     network_logger(loglevel, buf, len);
 }
 
@@ -61,27 +70,34 @@ void on_custom_events(hevent_t* ev) {
 }
 
 int main() {
+    // 在程序退出时做内存检查
     // memcheck atexit
     HV_MEMCHECK;
 
+    // 新建事件循环
     hloop_t* loop = hloop_new(0);
 
+    // 测试空闲事件和事件优先级
     // test idle and priority
     for (int i = HEVENT_LOWEST_PRIORITY; i <= HEVENT_HIGHEST_PRIORITY; ++i) {
         hidle_t* idle = hidle_add(loop, on_idle, 10);
         hevent_set_priority(idle, i);
     }
 
+    // 测试超时定时器
     // test timer timeout
     for (int i = 1; i <= 10; ++i) {
         htimer_t* timer = htimer_add(loop, on_timer, i*1000, 3);
         hevent_set_userdata(timer, (void*)(intptr_t)i);
     }
 
+    // 测试时间定时器,类似cron效果
+    // 获取下一分钟的分钟数,如30,则每小时的第30分钟会触发回调
     // test timer period
     int minute = time(NULL)%3600/60;
     htimer_add_period(loop, cron_hourly, minute+1, -1, -1, -1, -1, INFINITE);
 
+    // 测试网络日志器,类似android logcat效果,可直接使用telent/nc连接端口10514接收日志
     // test network_logger
     htimer_add(loop, timer_write_log, 1000, INFINITE);
     logger_set_handler(hlog, mylogger);
@@ -91,11 +107,13 @@ int main() {
 #endif
     nlog_listen(loop, DEFAULT_LOG_PORT);
 
+    // 测试非阻塞标准输入
     // test nonblock stdin
     printf("input 'quit' to quit loop\n");
     char buf[64];
     hread(loop, 0, buf, sizeof(buf), on_stdin);
 
+    // 测试自定义事件
     // test custom_events
     for (int i = 0; i < 10; ++i) {
         hevent_t ev;
@@ -106,7 +124,9 @@ int main() {
         hloop_post_event(loop, &ev);
     }
 
+    // 运行事件循环
     hloop_run(loop);
+    // 释放事件循环
     hloop_free(&loop);
     return 0;
 }

+ 14 - 0
examples/hmain_test.cpp

@@ -15,6 +15,20 @@
  *
  */
 
+/*
+ * @介绍:该示例程序展示了一个完整命令行程序所需的功能,包括
+ *        命令行解析
+ *        打印帮助信息
+ *        打印版本信息
+ *        信号处理
+ *        解析配置文件
+ *        写日志
+ *        后台运行
+ *        创建pid文件
+ *        多进程/多线程扩展
+ *
+ */
+
 typedef struct conf_ctx_s {
     IniParser* parser;
     int loglevel;

+ 5 - 0
examples/htimer_test.c

@@ -1,6 +1,11 @@
 #include "hloop.h"
 #include "hbase.h"
 
+/*
+ * @介绍:此程序是为了测试定时器增加、删除、重置接口。
+ *
+ */
+
 void on_timer(htimer_t* timer) {
     printf("time=%llus on_timer\n", LLU(hloop_now(hevent_loop(timer))));
 }

+ 5 - 0
examples/nc.c

@@ -12,6 +12,11 @@
  *          > [Enter]
  */
 
+/*
+ * @介绍:nc是unix下著名的网络连接客户端,此示例程序使用libhv实现了nc类似的功能。
+ *
+ */
+
 #include "hloop.h"
 #include "hbase.h"
 #include "hsocket.h"