LineBuffer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file LineBuffer.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 <stdlib.h>
  30. #include <string.h>
  31. #include <base/BLog.h>
  32. #include <flow/LineBuffer.h>
  33. #include <generated/blog_channel_LineBuffer.h>
  34. static void input_handler_done (LineBuffer *o, int data_len)
  35. {
  36. DebugObject_Access(&o->d_obj);
  37. ASSERT(data_len > 0)
  38. ASSERT(data_len <= o->buf_size - o->buf_used)
  39. // update buffer
  40. o->buf_used += data_len;
  41. // look for newline
  42. int i;
  43. for (i = o->buf_used - data_len; i < o->buf_used; i++) {
  44. if (o->buf[i] == o->nl_char) {
  45. break;
  46. }
  47. }
  48. if (i < o->buf_used || o->buf_used == o->buf_size) {
  49. if (i == o->buf_used) {
  50. BLog(BLOG_WARNING, "line too long");
  51. }
  52. // pass to output
  53. o->buf_consumed = (i < o->buf_used ? i + 1 : i);
  54. PacketPassInterface_Sender_Send(o->output, o->buf, o->buf_consumed);
  55. } else {
  56. // receive more data
  57. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  58. }
  59. }
  60. static void output_handler_done (LineBuffer *o)
  61. {
  62. DebugObject_Access(&o->d_obj);
  63. ASSERT(o->buf_consumed > 0)
  64. ASSERT(o->buf_consumed <= o->buf_used)
  65. // update buffer
  66. memmove(o->buf, o->buf + o->buf_consumed, o->buf_used - o->buf_consumed);
  67. o->buf_used -= o->buf_consumed;
  68. // look for newline
  69. int i;
  70. for (i = 0; i < o->buf_used; i++) {
  71. if (o->buf[i] == o->nl_char) {
  72. break;
  73. }
  74. }
  75. if (i < o->buf_used || o->buf_used == o->buf_size) {
  76. // pass to output
  77. o->buf_consumed = (i < o->buf_used ? i + 1 : i);
  78. PacketPassInterface_Sender_Send(o->output, o->buf, o->buf_consumed);
  79. } else {
  80. // receive more data
  81. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  82. }
  83. }
  84. int LineBuffer_Init (LineBuffer *o, StreamRecvInterface *input, PacketPassInterface *output, int buf_size, uint8_t nl_char)
  85. {
  86. ASSERT(buf_size > 0)
  87. ASSERT(PacketPassInterface_GetMTU(output) >= buf_size)
  88. // init arguments
  89. o->input = input;
  90. o->output = output;
  91. o->buf_size = buf_size;
  92. o->nl_char = nl_char;
  93. // init input
  94. StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
  95. // init output
  96. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  97. // set buffer empty
  98. o->buf_used = 0;
  99. // allocate buffer
  100. if (!(o->buf = malloc(o->buf_size))) {
  101. BLog(BLOG_ERROR, "malloc failed");
  102. goto fail0;
  103. }
  104. // start receiving
  105. StreamRecvInterface_Receiver_Recv(o->input, o->buf, o->buf_size);
  106. DebugObject_Init(&o->d_obj);
  107. return 1;
  108. fail0:
  109. return 0;
  110. }
  111. void LineBuffer_Free (LineBuffer *o)
  112. {
  113. DebugObject_Free(&o->d_obj);
  114. // free buffer
  115. free(o->buf);
  116. }