PRStreamSink.c 3.3 KB

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