PRStreamSink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef NDEBUG
  28. DEAD_ENTER(s->d_dead)
  29. #endif
  30. FlowErrorReporter_ReportError(&s->rep, &error);
  31. #ifndef NDEBUG
  32. ASSERT(DEAD_KILLED)
  33. DEAD_LEAVE(s->d_dead);
  34. #endif
  35. }
  36. static void try_send (PRStreamSink *s)
  37. {
  38. ASSERT(s->in_len > 0)
  39. int res = PR_Write(s->bprfd->prfd, s->in, s->in_len);
  40. if (res < 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  41. // wait for socket in prfd_handler
  42. BPRFileDesc_EnableEvent(s->bprfd, PR_POLL_WRITE);
  43. return;
  44. }
  45. if (res < 0) {
  46. report_error(s, PRSTREAMSINK_ERROR_NSPR);
  47. return;
  48. }
  49. ASSERT(res > 0)
  50. ASSERT(res <= s->in_len)
  51. // finish packet
  52. s->in_len = -1;
  53. StreamPassInterface_Done(&s->input, res);
  54. }
  55. static void input_handler_send (PRStreamSink *s, uint8_t *data, int data_len)
  56. {
  57. ASSERT(data_len > 0)
  58. ASSERT(s->in_len == -1)
  59. DebugObject_Access(&s->d_obj);
  60. // set packet
  61. s->in_len = data_len;
  62. s->in = data;
  63. try_send(s);
  64. return;
  65. }
  66. static void prfd_handler (PRStreamSink *s, PRInt16 event)
  67. {
  68. ASSERT(s->in_len > 0)
  69. ASSERT(event == PR_POLL_WRITE)
  70. DebugObject_Access(&s->d_obj);
  71. try_send(s);
  72. return;
  73. }
  74. void PRStreamSink_Init (PRStreamSink *s, FlowErrorReporter rep, BPRFileDesc *bprfd, BPendingGroup *pg)
  75. {
  76. // init arguments
  77. s->rep = rep;
  78. s->bprfd = bprfd;
  79. // add socket event handler
  80. BPRFileDesc_AddEventHandler(s->bprfd, PR_POLL_WRITE, (BPRFileDesc_handler)prfd_handler, s);
  81. // init input
  82. StreamPassInterface_Init(&s->input, (StreamPassInterface_handler_send)input_handler_send, s, pg);
  83. // have no input packet
  84. s->in_len = -1;
  85. DebugObject_Init(&s->d_obj);
  86. #ifndef NDEBUG
  87. DEAD_INIT(s->d_dead);
  88. #endif
  89. }
  90. void PRStreamSink_Free (PRStreamSink *s)
  91. {
  92. #ifndef NDEBUG
  93. DEAD_KILL(s->d_dead);
  94. #endif
  95. DebugObject_Free(&s->d_obj);
  96. // free input
  97. StreamPassInterface_Free(&s->input);
  98. // remove socket event handler
  99. BPRFileDesc_RemoveEventHandler(s->bprfd, PR_POLL_WRITE);
  100. }
  101. StreamPassInterface * PRStreamSink_GetInput (PRStreamSink *s)
  102. {
  103. DebugObject_Access(&s->d_obj);
  104. return &s->input;
  105. }