hloop_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * @build: make examples
  3. * @usage: bin/hloop_test
  4. * bin/nc 127.0.0.1 10514
  5. * nc 127.0.0.1 10514
  6. *
  7. */
  8. #include "hloop.h"
  9. #include "hbase.h"
  10. #include "hlog.h"
  11. #include "nlog.h"
  12. void mylogger(int loglevel, const char* buf, int len) {
  13. if (loglevel >= LOG_LEVEL_ERROR) {
  14. stderr_logger(loglevel, buf, len);
  15. }
  16. if (loglevel >= LOG_LEVEL_INFO) {
  17. file_logger(loglevel, buf, len);
  18. }
  19. network_logger(loglevel, buf, len);
  20. }
  21. void on_idle(hidle_t* idle) {
  22. printf("on_idle: event_id=%llu\tpriority=%d\tuserdata=%ld\n",
  23. LLU(hevent_id(idle)), hevent_priority(idle), (long)(intptr_t)(hevent_userdata(idle)));
  24. }
  25. void on_timer(htimer_t* timer) {
  26. hloop_t* loop = hevent_loop(timer);
  27. printf("on_timer: event_id=%llu\tpriority=%d\tuserdata=%ld\ttime=%llus\thrtime=%lluus\n",
  28. LLU(hevent_id(timer)), hevent_priority(timer), (long)(intptr_t)(hevent_userdata(timer)),
  29. LLU(hloop_now(loop)), LLU(hloop_now_hrtime(loop)));
  30. }
  31. void cron_hourly(htimer_t* timer) {
  32. time_t tt;
  33. time(&tt);
  34. printf("cron_hourly: %s\n", ctime(&tt));
  35. }
  36. void timer_write_log(htimer_t* timer) {
  37. static int cnt = 0;
  38. hlogd("[%d] Do you recv me?", ++cnt);
  39. hlogi("[%d] Do you recv me?", ++cnt);
  40. hloge("[%d] Do you recv me?", ++cnt);
  41. }
  42. void on_stdin(hio_t* io, void* buf, int readbytes) {
  43. printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  44. printf("> %s\n", (char*)buf);
  45. if (strncmp((char*)buf, "quit", 4) == 0) {
  46. hloop_stop(hevent_loop(io));
  47. }
  48. }
  49. void on_custom_events(hevent_t* ev) {
  50. printf("on_custom_events event_type=%d userdata=%ld\n", (int)ev->event_type, (long)ev->userdata);
  51. }
  52. int main() {
  53. // memcheck atexit
  54. HV_MEMCHECK;
  55. hloop_t* loop = hloop_new(0);
  56. // test idle and priority
  57. for (int i = HEVENT_LOWEST_PRIORITY; i <= HEVENT_HIGHEST_PRIORITY; ++i) {
  58. hidle_t* idle = hidle_add(loop, on_idle, 10);
  59. hevent_set_priority(idle, i);
  60. }
  61. // test timer timeout
  62. for (int i = 1; i <= 10; ++i) {
  63. htimer_t* timer = htimer_add(loop, on_timer, i*1000, 3);
  64. hevent_set_userdata(timer, (void*)(intptr_t)i);
  65. }
  66. // test timer period
  67. int minute = time(NULL)%3600/60;
  68. htimer_add_period(loop, cron_hourly, minute+1, -1, -1, -1, -1, INFINITE);
  69. // test network_logger
  70. htimer_add(loop, timer_write_log, 1000, INFINITE);
  71. logger_set_handler(hlog, mylogger);
  72. hlog_set_file("loop.log");
  73. #ifndef _MSC_VER
  74. logger_enable_color(hlog, 1);
  75. #endif
  76. nlog_listen(loop, DEFAULT_LOG_PORT);
  77. // test nonblock stdin
  78. printf("input 'quit' to quit loop\n");
  79. char buf[64];
  80. hread(loop, 0, buf, sizeof(buf), on_stdin);
  81. // test custom_events
  82. for (int i = 0; i < 10; ++i) {
  83. hevent_t ev;
  84. memset(&ev, 0, sizeof(ev));
  85. ev.event_type = (hevent_type_e)(HEVENT_TYPE_CUSTOM + i);
  86. ev.cb = on_custom_events;
  87. ev.userdata = (void*)(long)i;
  88. hloop_post_event(loop, &ev);
  89. }
  90. hloop_run(loop);
  91. hloop_free(&loop);
  92. return 0;
  93. }