PRStreamSink.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <prerror.h>
  23. #include <misc/debug.h>
  24. #include <nspr_support/PRStreamSink.h>
  25. static void report_error (PRStreamSink *s, int error)
  26. {
  27. DEBUGERROR(&s->d_err, FlowErrorReporter_ReportError(&s->rep, &error))
  28. }
  29. static void try_send (PRStreamSink *s)
  30. {
  31. ASSERT(s->in_len > 0)
  32. int res = PR_Write(s->bprfd->prfd, s->in, s->in_len);
  33. if (res < 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  34. // wait for socket in prfd_handler
  35. BPRFileDesc_EnableEvent(s->bprfd, PR_POLL_WRITE);
  36. return;
  37. }
  38. if (res < 0) {
  39. report_error(s, PRSTREAMSINK_ERROR_NSPR);
  40. return;
  41. }
  42. ASSERT(res > 0)
  43. ASSERT(res <= s->in_len)
  44. // finish packet
  45. s->in_len = -1;
  46. StreamPassInterface_Done(&s->input, res);
  47. }
  48. static void input_handler_send (PRStreamSink *s, uint8_t *data, int data_len)
  49. {
  50. ASSERT(data_len > 0)
  51. ASSERT(s->in_len == -1)
  52. DebugObject_Access(&s->d_obj);
  53. // set packet
  54. s->in_len = data_len;
  55. s->in = data;
  56. try_send(s);
  57. return;
  58. }
  59. static void prfd_handler (PRStreamSink *s, PRInt16 event)
  60. {
  61. ASSERT(s->in_len > 0)
  62. ASSERT(event == PR_POLL_WRITE)
  63. DebugObject_Access(&s->d_obj);
  64. try_send(s);
  65. return;
  66. }
  67. void PRStreamSink_Init (PRStreamSink *s, FlowErrorReporter rep, BPRFileDesc *bprfd, BPendingGroup *pg)
  68. {
  69. // init arguments
  70. s->rep = rep;
  71. s->bprfd = bprfd;
  72. // add socket event handler
  73. BPRFileDesc_AddEventHandler(s->bprfd, PR_POLL_WRITE, (BPRFileDesc_handler)prfd_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, BReactor_PendingGroup(BPRFileDesc_Reactor(s->bprfd)));
  80. }
  81. void PRStreamSink_Free (PRStreamSink *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. BPRFileDesc_RemoveEventHandler(s->bprfd, PR_POLL_WRITE);
  89. }
  90. StreamPassInterface * PRStreamSink_GetInput (PRStreamSink *s)
  91. {
  92. DebugObject_Access(&s->d_obj);
  93. return &s->input;
  94. }