stdin_input.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file stdin_input.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Example program which reads stdin and waits for SIGINT and SIGTERM.
  25. */
  26. #include <stdio.h>
  27. #include <stddef.h>
  28. #include <base/DebugObject.h>
  29. #include <system/BReactor.c>
  30. #include <system/BNetwork.h>
  31. #include <system/BConnection.h>
  32. #include <system/BUnixSignal.h>
  33. #define BUF_SIZE 64
  34. BReactor reactor;
  35. BConnection pipe_con;
  36. BUnixSignal usignal;
  37. StreamRecvInterface *source_if;
  38. uint8_t buf[BUF_SIZE + 1];
  39. static void signal_handler (void *user, int signo)
  40. {
  41. fprintf(stderr, "received %s, exiting\n", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
  42. // exit event loop
  43. BReactor_Quit(&reactor, 1);
  44. }
  45. static void connection_handler (void *user, int event)
  46. {
  47. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  48. fprintf(stderr, "pipe closed\n");
  49. } else {
  50. fprintf(stderr, "pipe error\n");
  51. }
  52. // exit event loop
  53. BReactor_Quit(&reactor, (event == BCONNECTION_EVENT_RECVCLOSED ? 0 : 1));
  54. }
  55. static void input_handler_done (void *user, int data_len)
  56. {
  57. // receive next chunk
  58. StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE);
  59. // print this chunk
  60. buf[data_len] = '\0';
  61. printf("Received: '%s'\n", buf);
  62. }
  63. int main ()
  64. {
  65. int ret = 1;
  66. BLog_InitStdout();
  67. // init network
  68. if (!BNetwork_GlobalInit()) {
  69. fprintf(stderr, "BNetwork_GlobalInit failed\n");
  70. goto fail1;
  71. }
  72. // init reactor (event loop)
  73. if (!BReactor_Init(&reactor)) {
  74. fprintf(stderr, "BReactor_Init failed\n");
  75. goto fail1;
  76. }
  77. // init signal handling
  78. sigset_t set;
  79. sigemptyset(&set);
  80. sigaddset(&set, SIGINT);
  81. sigaddset(&set, SIGTERM);
  82. if (!BUnixSignal_Init(&usignal, &reactor, set, signal_handler, NULL)) {
  83. fprintf(stderr, "BUnixSignal_Init failed\n");
  84. goto fail2;
  85. }
  86. // init BConnection object backed by the stdin fd
  87. if (!BConnection_Init(&pipe_con, BCONNECTION_SOURCE_PIPE(0), &reactor, NULL, connection_handler)) {
  88. fprintf(stderr, "BConnection_Init failed\n");
  89. goto fail3;
  90. }
  91. // init connection receive interface
  92. BConnection_RecvAsync_Init(&pipe_con);
  93. source_if = BConnection_RecvAsync_GetIf(&pipe_con);
  94. // init receive done callback
  95. StreamRecvInterface_Receiver_Init(source_if, input_handler_done, NULL);
  96. // receive first chunk
  97. StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE);
  98. // run event loop
  99. ret = BReactor_Exec(&reactor);
  100. BConnection_RecvAsync_Free(&pipe_con);
  101. BConnection_Free(&pipe_con);
  102. fail3:
  103. BUnixSignal_Free(&usignal, 0);
  104. fail2:
  105. BReactor_Free(&reactor);
  106. fail1:
  107. BLog_Free();
  108. DebugObjectGlobal_Finish();
  109. return ret;
  110. }