PRStreamSource.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file PRStreamSource.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 <base/BLog.h>
  26. #include <nspr_support/PRStreamSource.h>
  27. #include <generated/blog_channel_PRStreamSource.h>
  28. static void report_error (PRStreamSource *s, int error)
  29. {
  30. DEBUGERROR(&s->d_err, FlowErrorReporter_ReportError(&s->rep, error))
  31. }
  32. static void try_recv (PRStreamSource *s)
  33. {
  34. ASSERT(s->out_avail > 0)
  35. int res = PR_Read(s->bprfd->prfd, s->out, s->out_avail);
  36. if (res < 0 && PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  37. // wait for socket in prfd_handler
  38. BPRFileDesc_EnableEvent(s->bprfd, PR_POLL_READ);
  39. return;
  40. }
  41. if (res < 0) {
  42. BLog(BLOG_NOTICE, "PR_Read failed (%"PRIi32")", PR_GetError());
  43. report_error(s, PRSTREAMSOURCE_ERROR_NSPR);
  44. return;
  45. }
  46. if (res == 0) {
  47. BLog(BLOG_NOTICE, "Connection closed");
  48. report_error(s, PRSTREAMSOURCE_ERROR_CLOSED);
  49. return;
  50. }
  51. ASSERT(res > 0)
  52. ASSERT(res <= s->out_avail)
  53. // finish packet
  54. s->out_avail = -1;
  55. StreamRecvInterface_Done(&s->output, res);
  56. }
  57. static void output_handler_recv (PRStreamSource *s, uint8_t *data, int data_avail)
  58. {
  59. ASSERT(data_avail > 0)
  60. ASSERT(s->out_avail == -1)
  61. DebugObject_Access(&s->d_obj);
  62. // set packet
  63. s->out_avail = data_avail;
  64. s->out = data;
  65. try_recv(s);
  66. return;
  67. }
  68. static void prfd_handler (PRStreamSource *s, PRInt16 event)
  69. {
  70. ASSERT(s->out_avail > 0)
  71. ASSERT(event == PR_POLL_READ)
  72. DebugObject_Access(&s->d_obj);
  73. try_recv(s);
  74. return;
  75. }
  76. void PRStreamSource_Init (PRStreamSource *s, FlowErrorReporter rep, BPRFileDesc *bprfd, BPendingGroup *pg)
  77. {
  78. // init arguments
  79. s->rep = rep;
  80. s->bprfd = bprfd;
  81. // add socket event handler
  82. BPRFileDesc_AddEventHandler(s->bprfd, PR_POLL_READ, (BPRFileDesc_handler)prfd_handler, s);
  83. // init output
  84. StreamRecvInterface_Init(&s->output, (StreamRecvInterface_handler_recv)output_handler_recv, s, pg);
  85. // have no output packet
  86. s->out_avail = -1;
  87. DebugObject_Init(&s->d_obj);
  88. DebugError_Init(&s->d_err, BReactor_PendingGroup(BPRFileDesc_Reactor(s->bprfd)));
  89. }
  90. void PRStreamSource_Free (PRStreamSource *s)
  91. {
  92. DebugError_Free(&s->d_err);
  93. DebugObject_Free(&s->d_obj);
  94. // free output
  95. StreamRecvInterface_Free(&s->output);
  96. // remove socket event handler
  97. BPRFileDesc_RemoveEventHandler(s->bprfd, PR_POLL_READ);
  98. }
  99. StreamRecvInterface * PRStreamSource_GetOutput (PRStreamSource *s)
  100. {
  101. DebugObject_Access(&s->d_obj);
  102. return &s->output;
  103. }