StreamSocketSink.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <base/BLog.h>
  24. #include "StreamSocketSink.h"
  25. #include <generated/blog_channel_StreamSocketSink.h>
  26. static void report_error (StreamSocketSink *s, int error)
  27. {
  28. DEBUGERROR(&s->d_err, FlowErrorReporter_ReportError(&s->rep, error))
  29. }
  30. static void try_send (StreamSocketSink *s)
  31. {
  32. ASSERT(s->in_len > 0)
  33. int res = BSocket_Send(s->bsock, s->in, s->in_len);
  34. if (res < 0 && BSocket_GetError(s->bsock) == BSOCKET_ERROR_LATER) {
  35. // wait for socket in socket_handler
  36. BSocket_EnableEvent(s->bsock, BSOCKET_WRITE);
  37. return;
  38. }
  39. if (res < 0) {
  40. BLog(BLOG_NOTICE, "BSocket_Send failed (%d)", BSocket_GetError(s->bsock));
  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. // add socket event handler
  76. BSocket_AddEventHandler(s->bsock, BSOCKET_WRITE, (BSocket_handler)socket_handler, s);
  77. // init input
  78. StreamPassInterface_Init(&s->input, (StreamPassInterface_handler_send)input_handler_send, s, pg);
  79. // have no input packet
  80. s->in_len = -1;
  81. DebugObject_Init(&s->d_obj);
  82. DebugError_Init(&s->d_err, BReactor_PendingGroup(BSocket_Reactor(s->bsock)));
  83. }
  84. void StreamSocketSink_Free (StreamSocketSink *s)
  85. {
  86. DebugError_Free(&s->d_err);
  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. }
  93. StreamPassInterface * StreamSocketSink_GetInput (StreamSocketSink *s)
  94. {
  95. DebugObject_Access(&s->d_obj);
  96. return &s->input;
  97. }