FragmentProtoDisassembler.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = (struct fragmentproto_chunk_header *)(o->out + o->out_used);
  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. // write chunk data
  56. memcpy(o->out + o->out_used + sizeof(struct fragmentproto_chunk_header), o->in + o->in_used, chunk_len);
  57. // increment pointers
  58. o->in_used += chunk_len;
  59. o->out_used += sizeof(struct fragmentproto_chunk_header) + chunk_len;
  60. } while (IN_AVAIL > 0 && OUT_AVAIL > 0);
  61. // have we finished the input packet?
  62. if (IN_AVAIL == 0) {
  63. // set no input packet
  64. o->in_len = -1;
  65. // increment frame ID
  66. o->frame_id++;
  67. // finish input
  68. PacketPassInterface_Done(&o->input);
  69. }
  70. // should we finish the output packet?
  71. if (OUT_AVAIL <= 0 || o->latency < 0) {
  72. // set no output packet
  73. o->out = NULL;
  74. // stop timer (if it's running)
  75. if (o->latency >= 0) {
  76. BReactor_RemoveTimer(o->reactor, &o->timer);
  77. }
  78. // finish output
  79. PacketRecvInterface_Done(&o->output, o->out_used);
  80. } else {
  81. // start timer if we have output and it's not running (output was empty before)
  82. if (!BTimer_IsRunning(&o->timer)) {
  83. BReactor_SetTimer(o->reactor, &o->timer);
  84. }
  85. }
  86. }
  87. static void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len)
  88. {
  89. ASSERT(data_len >= 0)
  90. ASSERT(o->in_len == -1)
  91. // set input packet
  92. o->in_len = data_len;
  93. o->in = data;
  94. o->in_used = 0;
  95. // if there is no output, wait for it
  96. if (!o->out) {
  97. return;
  98. }
  99. write_chunks(o);
  100. }
  101. static void input_handler_requestcancel (FragmentProtoDisassembler *o)
  102. {
  103. ASSERT(o->in_len >= 0)
  104. ASSERT(!o->out)
  105. // set no input packet
  106. o->in_len = -1;
  107. // finish input
  108. PacketPassInterface_Done(&o->input);
  109. }
  110. static void output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data)
  111. {
  112. ASSERT(data)
  113. ASSERT(!o->out)
  114. // set output packet
  115. o->out = data;
  116. o->out_used = 0;
  117. // if there is no input, wait for it
  118. if (o->in_len < 0) {
  119. return;
  120. }
  121. write_chunks(o);
  122. }
  123. static void timer_handler (FragmentProtoDisassembler *o)
  124. {
  125. ASSERT(o->latency >= 0)
  126. ASSERT(o->out)
  127. ASSERT(o->in_len == -1)
  128. // set no output packet
  129. o->out = NULL;
  130. // finish output
  131. PacketRecvInterface_Done(&o->output, o->out_used);
  132. }
  133. void FragmentProtoDisassembler_Init (FragmentProtoDisassembler *o, BReactor *reactor, int input_mtu, int output_mtu, int chunk_mtu, btime_t latency)
  134. {
  135. ASSERT(input_mtu >= 0)
  136. ASSERT(input_mtu <= UINT16_MAX)
  137. ASSERT(output_mtu > sizeof(struct fragmentproto_chunk_header))
  138. ASSERT(chunk_mtu > 0 || chunk_mtu < 0)
  139. // init arguments
  140. o->reactor = reactor;
  141. o->output_mtu = output_mtu;
  142. o->chunk_mtu = chunk_mtu;
  143. o->latency = latency;
  144. // init input
  145. PacketPassInterface_Init(&o->input, input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(reactor));
  146. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  147. // init output
  148. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, BReactor_PendingGroup(reactor));
  149. // init timer
  150. if (o->latency >= 0) {
  151. BTimer_Init(&o->timer, o->latency, (BTimer_handler)timer_handler, o);
  152. }
  153. // have no input packet
  154. o->in_len = -1;
  155. // have no output packet
  156. o->out = NULL;
  157. // start with zero frame ID
  158. o->frame_id = 0;
  159. DebugObject_Init(&o->d_obj);
  160. }
  161. void FragmentProtoDisassembler_Free (FragmentProtoDisassembler *o)
  162. {
  163. DebugObject_Free(&o->d_obj);
  164. // free timer
  165. if (o->latency >= 0) {
  166. BReactor_RemoveTimer(o->reactor, &o->timer);
  167. }
  168. // free output
  169. PacketRecvInterface_Free(&o->output);
  170. // free input
  171. PacketPassInterface_Free(&o->input);
  172. }
  173. PacketPassInterface * FragmentProtoDisassembler_GetInput (FragmentProtoDisassembler *o)
  174. {
  175. DebugObject_Access(&o->d_obj);
  176. return &o->input;
  177. }
  178. PacketRecvInterface * FragmentProtoDisassembler_GetOutput (FragmentProtoDisassembler *o)
  179. {
  180. DebugObject_Access(&o->d_obj);
  181. return &o->output;
  182. }