StreamSocketSink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file StreamSocketSink.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/StreamSocketSink.h>
  24. static void report_error (StreamSocketSink *s, int error)
  25. {
  26. DEAD_ENTER(s->dead)
  27. FlowErrorReporter_ReportError(&s->rep, &error);
  28. ASSERT(DEAD_KILLED)
  29. DEAD_LEAVE(s->dead);
  30. }
  31. static void try_send (StreamSocketSink *s)
  32. {
  33. ASSERT(s->in_len > 0)
  34. int res = BSocket_Send(s->bsock, s->in, s->in_len);
  35. if (res < 0 && BSocket_GetError(s->bsock) == BSOCKET_ERROR_LATER) {
  36. // wait for socket in socket_handler
  37. BSocket_EnableEvent(s->bsock, BSOCKET_WRITE);
  38. return;
  39. }
  40. if (res < 0) {
  41. report_error(s, STREAMSOCKETSINK_ERROR_BSOCKET);
  42. return;
  43. }
  44. ASSERT(res > 0)
  45. ASSERT(res <= s->in_len)
  46. // finish packet
  47. s->in_len = -1;
  48. StreamPassInterface_Done(&s->input, res);
  49. }
  50. static void input_handler_send (StreamSocketSink *s, uint8_t *data, int data_len)
  51. {
  52. ASSERT(data_len > 0)
  53. ASSERT(s->in_len == -1)
  54. DebugObject_Access(&s->d_obj);
  55. // set packet
  56. s->in_len = data_len;
  57. s->in = data;
  58. try_send(s);
  59. return;
  60. }
  61. static void socket_handler (StreamSocketSink *s, int event)
  62. {
  63. ASSERT(s->in_len > 0)
  64. ASSERT(event == BSOCKET_WRITE)
  65. DebugObject_Access(&s->d_obj);
  66. BSocket_DisableEvent(s->bsock, BSOCKET_WRITE);
  67. try_send(s);
  68. return;
  69. }
  70. void StreamSocketSink_Init (StreamSocketSink *s, FlowErrorReporter rep, BSocket *bsock, BPendingGroup *pg)
  71. {
  72. // init arguments
  73. s->rep = rep;
  74. s->bsock = bsock;
  75. // init dead var
  76. DEAD_INIT(s->dead);
  77. // add socket event handler
  78. BSocket_AddEventHandler(s->bsock, BSOCKET_WRITE, (BSocket_handler)socket_handler, s);
  79. // init input
  80. StreamPassInterface_Init(&s->input, (StreamPassInterface_handler_send)input_handler_send, s, pg);
  81. // have no input packet
  82. s->in_len = -1;
  83. DebugObject_Init(&s->d_obj);
  84. }
  85. void StreamSocketSink_Free (StreamSocketSink *s)
  86. {
  87. DebugObject_Free(&s->d_obj);
  88. // free input
  89. StreamPassInterface_Free(&s->input);
  90. // remove socket event handler
  91. BSocket_RemoveEventHandler(s->bsock, BSOCKET_WRITE);
  92. // free dead var
  93. DEAD_KILL(s->dead);
  94. }
  95. StreamPassInterface * StreamSocketSink_GetInput (StreamSocketSink *s)
  96. {
  97. DebugObject_Access(&s->d_obj);
  98. return &s->input;
  99. }