PRStreamSink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file PRStreamSink.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 <inttypes.h>
  23. #include <prerror.h>
  24. #include <misc/debug.h>
  25. #include <system/BLog.h>
  26. #include <nspr_support/PRStreamSink.h>
  27. #include <generated/blog_channel_PRStreamSink.h>
  28. static void report_error (PRStreamSink *s, int error)
  29. {
  30. DEBUGERROR(&s->d_err, FlowErrorReporter_ReportError(&s->rep, error))
  31. }
  32. static void try_send (PRStreamSink *s)
  33. {
  34. ASSERT(s->in_len > 0)
  35. int res = PR_Write(s->bprfd->prfd, s->in, s->in_len);
  36. if (res < 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  37. // wait for socket in prfd_handler
  38. BPRFileDesc_EnableEvent(s->bprfd, PR_POLL_WRITE);
  39. return;
  40. }
  41. if (res < 0) {
  42. BLog(BLOG_NOTICE, "PR_Write failed (%"PRIi32")", PR_GetError());
  43. report_error(s, PRSTREAMSINK_ERROR_NSPR);
  44. return;
  45. }
  46. ASSERT(res > 0)
  47. ASSERT(res <= s->in_len)
  48. // finish packet
  49. s->in_len = -1;
  50. StreamPassInterface_Done(&s->input, res);
  51. }
  52. static void input_handler_send (PRStreamSink *s, uint8_t *data, int data_len)
  53. {
  54. ASSERT(data_len > 0)
  55. ASSERT(s->in_len == -1)
  56. DebugObject_Access(&s->d_obj);
  57. // set packet
  58. s->in_len = data_len;
  59. s->in = data;
  60. try_send(s);
  61. return;
  62. }
  63. static void prfd_handler (PRStreamSink *s, PRInt16 event)
  64. {
  65. ASSERT(s->in_len > 0)
  66. ASSERT(event == PR_POLL_WRITE)
  67. DebugObject_Access(&s->d_obj);
  68. try_send(s);
  69. return;
  70. }
  71. void PRStreamSink_Init (PRStreamSink *s, FlowErrorReporter rep, BPRFileDesc *bprfd, BPendingGroup *pg)
  72. {
  73. // init arguments
  74. s->rep = rep;
  75. s->bprfd = bprfd;
  76. // add socket event handler
  77. BPRFileDesc_AddEventHandler(s->bprfd, PR_POLL_WRITE, (BPRFileDesc_handler)prfd_handler, s);
  78. // init input
  79. StreamPassInterface_Init(&s->input, (StreamPassInterface_handler_send)input_handler_send, s, pg);
  80. // have no input packet
  81. s->in_len = -1;
  82. DebugObject_Init(&s->d_obj);
  83. DebugError_Init(&s->d_err, BReactor_PendingGroup(BPRFileDesc_Reactor(s->bprfd)));
  84. }
  85. void PRStreamSink_Free (PRStreamSink *s)
  86. {
  87. DebugError_Free(&s->d_err);
  88. DebugObject_Free(&s->d_obj);
  89. // free input
  90. StreamPassInterface_Free(&s->input);
  91. // remove socket event handler
  92. BPRFileDesc_RemoveEventHandler(s->bprfd, PR_POLL_WRITE);
  93. }
  94. StreamPassInterface * PRStreamSink_GetInput (PRStreamSink *s)
  95. {
  96. DebugObject_Access(&s->d_obj);
  97. return &s->input;
  98. }