Tarantool nginx upstream module
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tp_transcode.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Redistribution and use in source and binary forms, with or
  3. * without modification, are permitted provided that the following
  4. * conditions are met:
  5. *
  6. * 1. Redistributions of source code must retain the above
  7. * copyright notice, this list of conditions and the
  8. * following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY AUTHORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  19. * AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  26. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * Copyright (C) 2015 Tarantool AUTHORS:
  30. * please see AUTHORS file.
  31. */
  32. #ifndef TP_TRANSCODE_H_INCLUDED
  33. #define TP_TRANSCODE_H INCLUDED
  34. #include <stdlib.h>
  35. #include <stdbool.h>
  36. #include <string.h>
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /* {{{ API declaration */
  41. struct tp_transcode;
  42. enum tt_result;
  43. /** Underlying codec functions
  44. */
  45. typedef void *(*tp_codec_create)(struct tp_transcode*, char *, size_t);
  46. typedef void (*tp_codec_free)(void *);
  47. typedef enum tt_result (*tp_do_transcode)(void *, const char *, size_t);
  48. typedef enum tt_result (*tp_do_transcode_complete)(void *, size_t *);
  49. /** Underlying codec object
  50. */
  51. typedef struct tp_codec {
  52. void *ctx; /* Underlying codec context */
  53. tp_codec_create create; /* create codec context(i.e. 'ctx') function */
  54. tp_codec_free free; /* free codec context(i.e. 'ctx') function */
  55. tp_do_transcode transcode; /* transcode function */
  56. tp_do_transcode_complete complete; /* complete function */
  57. } tp_codec_t;
  58. /** Avaliable codecs
  59. */
  60. enum tp_codec_type {
  61. /** JSON PRC to Tarantool message (Yajl engine)
  62. */
  63. YAJL_JSON_TO_TP = 0,
  64. /** Tarantool reply message to JSON
  65. */
  66. TP_REPLY_TO_JSON,
  67. /** Tarantool message to JSON
  68. */
  69. TP_TO_JSON,
  70. TP_CODEC_MAX
  71. };
  72. /** Memory functions
  73. */
  74. typedef struct mem_fun {
  75. void *ctx;
  76. void*(*alloc)(void*, size_t);
  77. void*(*realloc)(void*, void *, size_t);
  78. void(*free)(void*, void *);
  79. } mem_fun_t;
  80. /** tp_transcode obj - underlying codec holder
  81. */
  82. typedef struct tp_transcode {
  83. tp_codec_t codec;
  84. mem_fun_t mf;
  85. char *errmsg;
  86. int errcode;
  87. int batch_size;
  88. } tp_transcode_t;
  89. /** Returns codes
  90. */
  91. enum tt_result {
  92. TP_TRANSCODE_OK = 1,
  93. TP_TRANSCODE_ERROR
  94. };
  95. ssize_t
  96. tp_read_payload(const char * const buf, const char * const end);
  97. /** Initialize struct tp_transcode.
  98. *
  99. * Returns TP_TRANSCODE_ERROR if codec not found or create codec failed
  100. * Returns TP_TRANSCODE_OK if codec found and initialize well
  101. */
  102. enum tt_result tp_transcode_init(tp_transcode_t *t,
  103. char *output, size_t output_size,
  104. enum tp_codec_type codec,
  105. mem_fun_t *mf);
  106. /** Free struct tp_transcode
  107. */
  108. void tp_transcode_free(tp_transcode_t *t);
  109. /** Feed transcoder (see tp_transcode_init).
  110. *
  111. * Returns TP_TRANSCODE_OK if bytes enought for finish transcoding
  112. * Returns TP_TRANSCODE_ERROR if error occurred
  113. */
  114. enum tt_result tp_transcode(tp_transcode_t *t, const char *b, size_t s);
  115. /** Finalize transcoding.
  116. *
  117. * Returns TP_TRANSCODE_OK if transcoding done
  118. * Returns TP_TRANSCODE_ERROR if error occurred
  119. */
  120. enum tt_result
  121. tp_transcode_complete(tp_transcode_t *t, size_t *complete_msg_size);
  122. /**
  123. * Dump Tarantool message to output in JSON format
  124. * Returns true, false
  125. */
  126. bool
  127. tp_dump(char *output, size_t output_size,
  128. const char *input, size_t input_size);
  129. #ifdef __cplusplus
  130. } /* extern "C" */
  131. #endif
  132. /* }}} */
  133. #endif