StreamRecvInterface.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file StreamRecvInterface.h
  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. * @section DESCRIPTION
  23. *
  24. * Interface allowing a stream receiver to receive stream data from a stream sender.
  25. *
  26. * Note that this interface behaves exactly the same and has the same code as
  27. * {@link StreamRecvInterface} if names and its external semantics are disregarded.
  28. * If you modify this file, you should probably modify {@link StreamRecvInterface}
  29. * too.
  30. */
  31. #ifndef BADVPN_FLOW_STREAMRECVINTERFACE_H
  32. #define BADVPN_FLOW_STREAMRECVINTERFACE_H
  33. #include <stdint.h>
  34. #include <stddef.h>
  35. #include <misc/dead.h>
  36. #include <misc/debug.h>
  37. #include <misc/debugin.h>
  38. #include <system/DebugObject.h>
  39. #include <system/BPending.h>
  40. typedef void (*StreamRecvInterface_handler_recv) (void *user, uint8_t *data, int data_len);
  41. typedef void (*StreamRecvInterface_handler_done) (void *user, int data_len);
  42. typedef struct {
  43. // provider data
  44. StreamRecvInterface_handler_recv handler_operation;
  45. void *user_provider;
  46. // user data
  47. StreamRecvInterface_handler_done handler_done;
  48. void *user_user;
  49. // jobs
  50. BPending job_operation;
  51. BPending job_done;
  52. // packet supplied by user
  53. uint8_t *buf;
  54. int buf_len;
  55. // length supplied by done
  56. int done_len;
  57. DebugObject d_obj;
  58. #ifndef NDEBUG
  59. DebugIn d_in_operation;
  60. DebugIn d_in_done;
  61. dead_t d_dead;
  62. int d_user_busy;
  63. #endif
  64. } StreamRecvInterface;
  65. static void StreamRecvInterface_Init (StreamRecvInterface *i, StreamRecvInterface_handler_recv handler_operation, void *user, BPendingGroup *pg);
  66. static void StreamRecvInterface_Free (StreamRecvInterface *i);
  67. static void StreamRecvInterface_Done (StreamRecvInterface *i, int data_len);
  68. static void StreamRecvInterface_Receiver_Init (StreamRecvInterface *i, StreamRecvInterface_handler_done handler_done, void *user);
  69. static void StreamRecvInterface_Receiver_Recv (StreamRecvInterface *i, uint8_t *data, int data_len);
  70. void _StreamRecvInterface_job_operation (StreamRecvInterface *i);
  71. void _StreamRecvInterface_job_done (StreamRecvInterface *i);
  72. void StreamRecvInterface_Init (StreamRecvInterface *i, StreamRecvInterface_handler_recv handler_operation, void *user, BPendingGroup *pg)
  73. {
  74. // init arguments
  75. i->handler_operation = handler_operation;
  76. i->user_provider = user;
  77. // set no user
  78. i->handler_done = NULL;
  79. // init jobs
  80. BPending_Init(&i->job_operation, pg, (BPending_handler)_StreamRecvInterface_job_operation, i);
  81. BPending_Init(&i->job_done, pg, (BPending_handler)_StreamRecvInterface_job_done, i);
  82. DebugObject_Init(&i->d_obj);
  83. #ifndef NDEBUG
  84. DebugIn_Init(&i->d_in_operation);
  85. DebugIn_Init(&i->d_in_done);
  86. DEAD_INIT(i->d_dead);
  87. i->d_user_busy = 0;
  88. #endif
  89. }
  90. void StreamRecvInterface_Free (StreamRecvInterface *i)
  91. {
  92. #ifndef NDEBUG
  93. DEAD_KILL(i->d_dead);
  94. #endif
  95. DebugObject_Free(&i->d_obj);
  96. // free jobs
  97. BPending_Free(&i->job_done);
  98. BPending_Free(&i->job_operation);
  99. }
  100. void StreamRecvInterface_Done (StreamRecvInterface *i, int data_len)
  101. {
  102. ASSERT(data_len > 0)
  103. ASSERT(data_len <= i->buf_len)
  104. ASSERT(i->d_user_busy)
  105. ASSERT(i->buf_len > 0)
  106. ASSERT(i->handler_done)
  107. ASSERT(!BPending_IsSet(&i->job_operation))
  108. DebugObject_Access(&i->d_obj);
  109. #ifndef NDEBUG
  110. DebugIn_AmOut(&i->d_in_done);
  111. #endif
  112. i->done_len = data_len;
  113. BPending_Set(&i->job_done);
  114. }
  115. void StreamRecvInterface_Receiver_Init (StreamRecvInterface *i, StreamRecvInterface_handler_done handler_done, void *user)
  116. {
  117. ASSERT(handler_done)
  118. ASSERT(!i->handler_done)
  119. DebugObject_Access(&i->d_obj);
  120. i->handler_done = handler_done;
  121. i->user_user = user;
  122. }
  123. void StreamRecvInterface_Receiver_Recv (StreamRecvInterface *i, uint8_t *data, int data_len)
  124. {
  125. ASSERT(data_len > 0)
  126. ASSERT(data)
  127. ASSERT(!i->d_user_busy)
  128. ASSERT(i->handler_done)
  129. DebugObject_Access(&i->d_obj);
  130. #ifndef NDEBUG
  131. DebugIn_AmOut(&i->d_in_operation);
  132. #endif
  133. i->buf = data;
  134. i->buf_len = data_len;
  135. #ifndef NDEBUG
  136. i->d_user_busy = 1;
  137. #endif
  138. BPending_Set(&i->job_operation);
  139. }
  140. #endif