StreamSocketSource.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file StreamSocketSource.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. #include <misc/debug.h>
  23. #include <flow/StreamSocketSource.h>
  24. static void report_error (StreamSocketSource *s, int error)
  25. {
  26. #ifndef NDEBUG
  27. DEAD_ENTER(s->d_dead)
  28. #endif
  29. FlowErrorReporter_ReportError(&s->rep, &error);
  30. #ifndef NDEBUG
  31. ASSERT(DEAD_KILLED)
  32. DEAD_LEAVE(s->d_dead);
  33. #endif
  34. }
  35. static void try_recv (StreamSocketSource *s)
  36. {
  37. ASSERT(s->out_avail > 0)
  38. int res = BSocket_Recv(s->bsock, s->out, s->out_avail);
  39. if (res < 0 && BSocket_GetError(s->bsock) == BSOCKET_ERROR_LATER) {
  40. // wait for socket in socket_handler
  41. BSocket_EnableEvent(s->bsock, BSOCKET_READ);
  42. return;
  43. }
  44. if (res < 0) {
  45. report_error(s, STREAMSOCKETSOURCE_ERROR_BSOCKET);
  46. return;
  47. }
  48. if (res == 0) {
  49. report_error(s, STREAMSOCKETSOURCE_ERROR_CLOSED);
  50. return;
  51. }
  52. ASSERT(res > 0)
  53. ASSERT(res <= s->out_avail)
  54. // finish packet
  55. s->out_avail = -1;
  56. StreamRecvInterface_Done(&s->output, res);
  57. }
  58. static void output_handler_recv (StreamSocketSource *s, uint8_t *data, int data_avail)
  59. {
  60. ASSERT(data_avail > 0)
  61. ASSERT(s->out_avail == -1)
  62. DebugObject_Access(&s->d_obj);
  63. // set packet
  64. s->out_avail = data_avail;
  65. s->out = data;
  66. try_recv(s);
  67. return;
  68. }
  69. static void socket_handler (StreamSocketSource *s, int event)
  70. {
  71. ASSERT(s->out_avail > 0)
  72. ASSERT(event == BSOCKET_READ)
  73. DebugObject_Access(&s->d_obj);
  74. BSocket_DisableEvent(s->bsock, BSOCKET_READ);
  75. try_recv(s);
  76. return;
  77. }
  78. void StreamSocketSource_Init (StreamSocketSource *s, FlowErrorReporter rep, BSocket *bsock, BPendingGroup *pg)
  79. {
  80. // init arguments
  81. s->rep = rep;
  82. s->bsock = bsock;
  83. // add socket event handler
  84. BSocket_AddEventHandler(s->bsock, BSOCKET_READ, (BSocket_handler)socket_handler, s);
  85. // init output
  86. StreamRecvInterface_Init(&s->output, (StreamRecvInterface_handler_recv)output_handler_recv, s, pg);
  87. // have no output packet
  88. s->out_avail = -1;
  89. DebugObject_Init(&s->d_obj);
  90. #ifndef NDEBUG
  91. DEAD_INIT(s->d_dead);
  92. #endif
  93. }
  94. void StreamSocketSource_Free (StreamSocketSource *s)
  95. {
  96. #ifndef NDEBUG
  97. DEAD_KILL(s->d_dead);
  98. #endif
  99. DebugObject_Free(&s->d_obj);
  100. // free output
  101. StreamRecvInterface_Free(&s->output);
  102. // remove socket event handler
  103. BSocket_RemoveEventHandler(s->bsock, BSOCKET_READ);
  104. }
  105. StreamRecvInterface * StreamSocketSource_GetOutput (StreamSocketSource *s)
  106. {
  107. DebugObject_Access(&s->d_obj);
  108. return &s->output;
  109. }