StreamPassInterface.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @file StreamPassInterface.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 sender to pass stream data to a stream receiver.
  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_STREAMPASSINTERFACE_H
  32. #define BADVPN_FLOW_STREAMPASSINTERFACE_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 (*StreamPassInterface_handler_send) (void *user, uint8_t *data, int data_len);
  41. typedef void (*StreamPassInterface_handler_done) (void *user, int data_len);
  42. typedef struct {
  43. // provider data
  44. StreamPassInterface_handler_send handler_operation;
  45. void *user_provider;
  46. // user data
  47. StreamPassInterface_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. } StreamPassInterface;
  65. static void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg);
  66. static void StreamPassInterface_Free (StreamPassInterface *i);
  67. static void StreamPassInterface_Done (StreamPassInterface *i, int data_len);
  68. static void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user);
  69. static void StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len);
  70. #ifndef NDEBUG
  71. static int StreamPassInterface_InClient (StreamPassInterface *i);
  72. static int StreamPassInterface_InDone (StreamPassInterface *i);
  73. #endif
  74. void _StreamPassInterface_job_operation (StreamPassInterface *i);
  75. void _StreamPassInterface_job_done (StreamPassInterface *i);
  76. void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg)
  77. {
  78. // init arguments
  79. i->handler_operation = handler_operation;
  80. i->user_provider = user;
  81. // set no user
  82. i->handler_done = NULL;
  83. // init jobs
  84. BPending_Init(&i->job_operation, pg, (BPending_handler)_StreamPassInterface_job_operation, i);
  85. BPending_Init(&i->job_done, pg, (BPending_handler)_StreamPassInterface_job_done, i);
  86. DebugObject_Init(&i->d_obj);
  87. #ifndef NDEBUG
  88. DebugIn_Init(&i->d_in_operation);
  89. DebugIn_Init(&i->d_in_done);
  90. DEAD_INIT(i->d_dead);
  91. i->d_user_busy = 0;
  92. #endif
  93. }
  94. void StreamPassInterface_Free (StreamPassInterface *i)
  95. {
  96. #ifndef NDEBUG
  97. DEAD_KILL(i->d_dead);
  98. #endif
  99. DebugObject_Free(&i->d_obj);
  100. // free jobs
  101. BPending_Free(&i->job_done);
  102. BPending_Free(&i->job_operation);
  103. }
  104. void StreamPassInterface_Done (StreamPassInterface *i, int data_len)
  105. {
  106. ASSERT(data_len > 0)
  107. ASSERT(data_len <= i->buf_len)
  108. ASSERT(i->d_user_busy)
  109. ASSERT(i->buf_len > 0)
  110. ASSERT(i->handler_done)
  111. ASSERT(!BPending_IsSet(&i->job_operation))
  112. DebugObject_Access(&i->d_obj);
  113. #ifndef NDEBUG
  114. DebugIn_AmOut(&i->d_in_done);
  115. #endif
  116. i->done_len = data_len;
  117. BPending_Set(&i->job_done);
  118. }
  119. void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user)
  120. {
  121. ASSERT(handler_done)
  122. ASSERT(!i->handler_done)
  123. DebugObject_Access(&i->d_obj);
  124. i->handler_done = handler_done;
  125. i->user_user = user;
  126. }
  127. void StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len)
  128. {
  129. ASSERT(data_len > 0)
  130. ASSERT(data)
  131. ASSERT(!i->d_user_busy)
  132. ASSERT(i->handler_done)
  133. DebugObject_Access(&i->d_obj);
  134. #ifndef NDEBUG
  135. DebugIn_AmOut(&i->d_in_operation);
  136. #endif
  137. i->buf = data;
  138. i->buf_len = data_len;
  139. #ifndef NDEBUG
  140. i->d_user_busy = 1;
  141. #endif
  142. BPending_Set(&i->job_operation);
  143. }
  144. #ifndef NDEBUG
  145. int StreamPassInterface_InClient (StreamPassInterface *i)
  146. {
  147. DebugObject_Access(&i->d_obj);
  148. return DebugIn_In(&i->d_in_operation);
  149. }
  150. int StreamPassInterface_InDone (StreamPassInterface *i)
  151. {
  152. DebugObject_Access(&i->d_obj);
  153. return DebugIn_In(&i->d_in_done);
  154. }
  155. #endif
  156. #endif