StreamSocketSink.c 3.0 KB

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