FragmentProtoDisassembler.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @file FragmentProtoDisassembler.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #include <string.h>
  31. #include <misc/debug.h>
  32. #include <misc/byteorder.h>
  33. #include <misc/minmax.h>
  34. #include "client/FragmentProtoDisassembler.h"
  35. static void write_chunks (FragmentProtoDisassembler *o)
  36. {
  37. #define IN_AVAIL (o->in_len - o->in_used)
  38. #define OUT_AVAIL ((o->output_mtu - o->out_used) - (int)sizeof(struct fragmentproto_chunk_header))
  39. ASSERT(o->in_len >= 0)
  40. ASSERT(o->out)
  41. ASSERT(OUT_AVAIL > 0)
  42. // write chunks to output packet
  43. do {
  44. // calculate chunk length
  45. int chunk_len = bmin_int(IN_AVAIL, OUT_AVAIL);
  46. if (o->chunk_mtu > 0) {
  47. chunk_len = bmin_int(chunk_len, o->chunk_mtu);
  48. }
  49. // write chunk header
  50. struct fragmentproto_chunk_header header;
  51. header.frame_id = htol16(o->frame_id);
  52. header.chunk_start = htol16(o->in_used);
  53. header.chunk_len = htol16(chunk_len);
  54. header.is_last = (chunk_len == IN_AVAIL);
  55. memcpy(o->out + o->out_used, &header, sizeof(header));
  56. // write chunk data
  57. memcpy(o->out + o->out_used + sizeof(struct fragmentproto_chunk_header), o->in + o->in_used, chunk_len);
  58. // increment pointers
  59. o->in_used += chunk_len;
  60. o->out_used += sizeof(struct fragmentproto_chunk_header) + chunk_len;
  61. } while (IN_AVAIL > 0 && OUT_AVAIL > 0);
  62. // have we finished the input packet?
  63. if (IN_AVAIL == 0) {
  64. // set no input packet
  65. o->in_len = -1;
  66. // increment frame ID
  67. o->frame_id++;
  68. // finish input
  69. PacketPassInterface_Done(&o->input);
  70. }
  71. // should we finish the output packet?
  72. if (OUT_AVAIL <= 0 || o->latency < 0) {
  73. // set no output packet
  74. o->out = NULL;
  75. // stop timer (if it's running)
  76. if (o->latency >= 0) {
  77. BReactor_RemoveTimer(o->reactor, &o->timer);
  78. }
  79. // finish output
  80. PacketRecvInterface_Done(&o->output, o->out_used);
  81. } else {
  82. // start timer if we have output and it's not running (output was empty before)
  83. if (!BTimer_IsRunning(&o->timer)) {
  84. BReactor_SetTimer(o->reactor, &o->timer);
  85. }
  86. }
  87. }
  88. static void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len)
  89. {
  90. ASSERT(data_len >= 0)
  91. ASSERT(o->in_len == -1)
  92. // set input packet
  93. o->in_len = data_len;
  94. o->in = data;
  95. o->in_used = 0;
  96. // if there is no output, wait for it
  97. if (!o->out) {
  98. return;
  99. }
  100. write_chunks(o);
  101. }
  102. static void input_handler_requestcancel (FragmentProtoDisassembler *o)
  103. {
  104. ASSERT(o->in_len >= 0)
  105. ASSERT(!o->out)
  106. // set no input packet
  107. o->in_len = -1;
  108. // finish input
  109. PacketPassInterface_Done(&o->input);
  110. }
  111. static void output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data)
  112. {
  113. ASSERT(data)
  114. ASSERT(!o->out)
  115. // set output packet
  116. o->out = data;
  117. o->out_used = 0;
  118. // if there is no input, wait for it
  119. if (o->in_len < 0) {
  120. return;
  121. }
  122. write_chunks(o);
  123. }
  124. static void timer_handler (FragmentProtoDisassembler *o)
  125. {
  126. ASSERT(o->latency >= 0)
  127. ASSERT(o->out)
  128. ASSERT(o->in_len == -1)
  129. // set no output packet
  130. o->out = NULL;
  131. // finish output
  132. PacketRecvInterface_Done(&o->output, o->out_used);
  133. }
  134. void FragmentProtoDisassembler_Init (FragmentProtoDisassembler *o, BReactor *reactor, int input_mtu, int output_mtu, int chunk_mtu, btime_t latency)
  135. {
  136. ASSERT(input_mtu >= 0)
  137. ASSERT(input_mtu <= UINT16_MAX)
  138. ASSERT(output_mtu > sizeof(struct fragmentproto_chunk_header))
  139. ASSERT(chunk_mtu > 0 || chunk_mtu < 0)
  140. // init arguments
  141. o->reactor = reactor;
  142. o->output_mtu = output_mtu;
  143. o->chunk_mtu = chunk_mtu;
  144. o->latency = latency;
  145. // init input
  146. PacketPassInterface_Init(&o->input, input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(reactor));
  147. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  148. // init output
  149. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, BReactor_PendingGroup(reactor));
  150. // init timer
  151. if (o->latency >= 0) {
  152. BTimer_Init(&o->timer, o->latency, (BTimer_handler)timer_handler, o);
  153. }
  154. // have no input packet
  155. o->in_len = -1;
  156. // have no output packet
  157. o->out = NULL;
  158. // start with zero frame ID
  159. o->frame_id = 0;
  160. DebugObject_Init(&o->d_obj);
  161. }
  162. void FragmentProtoDisassembler_Free (FragmentProtoDisassembler *o)
  163. {
  164. DebugObject_Free(&o->d_obj);
  165. // free timer
  166. if (o->latency >= 0) {
  167. BReactor_RemoveTimer(o->reactor, &o->timer);
  168. }
  169. // free output
  170. PacketRecvInterface_Free(&o->output);
  171. // free input
  172. PacketPassInterface_Free(&o->input);
  173. }
  174. PacketPassInterface * FragmentProtoDisassembler_GetInput (FragmentProtoDisassembler *o)
  175. {
  176. DebugObject_Access(&o->d_obj);
  177. return &o->input;
  178. }
  179. PacketRecvInterface * FragmentProtoDisassembler_GetOutput (FragmentProtoDisassembler *o)
  180. {
  181. DebugObject_Access(&o->d_obj);
  182. return &o->output;
  183. }