msg.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /** @file msg.h
  2. *
  3. * @brief Library to implement simple network protocol
  4. *
  5. * Message (request or answer) has the following structure:
  6. *
  7. * [ Message header ]
  8. * [ First parameter's header ]
  9. * [ ... ]
  10. * [ N-th parameter's header ]
  11. * [ First parameter's data ]
  12. * [ ... ]
  13. * [ N-th parameter's data ]
  14. *
  15. * Message header has such standard fields as:
  16. * - Magic number
  17. * - Protocol version major number
  18. * - Protocol version minor number
  19. * - Command code
  20. * - Status
  21. * - Request ID field to identify session
  22. * - Number of parameters
  23. * - Whole message length
  24. *
  25. * The length of message can vary. Header has constant length and
  26. * contains total length of whole message and number of parameters. There are
  27. * parameter headers for each parameter. Parameter's header contains parameter
  28. * type and length of parameter. After parameter headers array the parameter's
  29. * data follows in a sequence.
  30. */
  31. #ifndef _faux_msg_h
  32. #define _faux_msg_h
  33. #include <stdint.h>
  34. #include <signal.h>
  35. #include <faux/faux.h>
  36. #include <faux/list.h>
  37. #include <faux/net.h>
  38. #include <faux/async.h>
  39. typedef struct faux_msg_s faux_msg_t;
  40. // Debug variable. BOOL_TRUE for debug and BOOL_FALSE to switch debug off
  41. extern bool_t faux_msg_debug_flag;
  42. /** @brief Parameter header
  43. */
  44. typedef struct faux_phdr_s {
  45. uint16_t param_type; // Parameter type
  46. uint8_t reserved[2];
  47. uint32_t param_len; // Length of parameter (not including header)
  48. } faux_phdr_t;
  49. /** @brief Message header
  50. */
  51. typedef struct faux_hdr_s {
  52. uint32_t magic; // Magic number
  53. uint8_t major; // Major protocol version number
  54. uint8_t minor; // Minor protocol version number
  55. uint16_t cmd; // Command
  56. uint32_t status; // Status
  57. uint32_t req_id; // Some abstract ID of request. Identifies request/answer
  58. uint32_t param_num; // Number of parameters
  59. uint32_t len; // Length of whole message (including header)
  60. faux_phdr_t phdr[]; // Parameter headers (unknown length)
  61. } faux_hdr_t;
  62. C_DECL_BEGIN
  63. // Header functions
  64. void faux_hdr_set_cmd(faux_hdr_t *hdr, uint16_t cmd);
  65. uint16_t faux_hdr_cmd(const faux_hdr_t *hdr);
  66. void faux_hdr_set_status(faux_hdr_t *hdr, uint32_t status);
  67. uint32_t faux_hdr_status(const faux_hdr_t *hdr);
  68. void faux_hdr_set_req_id(faux_hdr_t *hdr, uint32_t req_id);
  69. uint32_t faux_hdr_req_id(const faux_hdr_t *hdr);
  70. void faux_hdr_set_param_num(faux_hdr_t *hdr, uint32_t param_num);
  71. uint32_t faux_hdr_param_num(const faux_hdr_t *hdr);
  72. void faux_hdr_set_len(faux_hdr_t *hdr, uint32_t len);
  73. int faux_hdr_len(const faux_hdr_t *hdr);
  74. void faux_hdr_set_magic(faux_hdr_t *hdr, uint32_t magic);
  75. uint32_t faux_hdr_magic(const faux_hdr_t *hdr);
  76. void faux_hdr_set_major(faux_hdr_t *hdr, uint8_t major);
  77. uint8_t faux_hdr_major(const faux_hdr_t *hdr);
  78. void faux_hdr_set_minor(faux_hdr_t *hdr, uint8_t minor);
  79. uint8_t faux_hdr_minor(const faux_hdr_t *hdr);
  80. // Parameter functions
  81. void faux_phdr_set_type(faux_phdr_t *phdr, uint16_t param_type);
  82. uint16_t faux_phdr_get_type(const faux_phdr_t *phdr);
  83. void faux_phdr_set_len(faux_phdr_t *phdr, uint32_t param_len);
  84. uint32_t faux_phdr_get_len(const faux_phdr_t *phdr);
  85. // Message functions
  86. faux_msg_t *faux_msg_new(uint32_t magic, uint8_t major, uint8_t minor);
  87. void faux_msg_free(faux_msg_t *msg);
  88. void faux_msg_set_cmd(faux_msg_t *msg, uint16_t cmd);
  89. uint16_t faux_msg_get_cmd(const faux_msg_t *msg);
  90. void faux_msg_set_status(faux_msg_t *msg, uint32_t status);
  91. uint32_t faux_msg_get_status(const faux_msg_t *msg);
  92. void faux_msg_set_req_id(faux_msg_t *msg, uint32_t req_id);
  93. uint32_t faux_msg_get_req_id(const faux_msg_t *msg);
  94. uint32_t faux_msg_get_param_num(const faux_msg_t *msg);
  95. int faux_msg_get_len(const faux_msg_t *msg);
  96. uint32_t faux_msg_get_magic(const faux_msg_t *msg);
  97. int faux_msg_get_major(const faux_msg_t *msg);
  98. int faux_msg_get_minor(const faux_msg_t *msg);
  99. ssize_t faux_msg_add_param(faux_msg_t *msg, uint16_t type,
  100. const void *buf, size_t len);
  101. faux_list_node_t *faux_msg_init_param_iter(const faux_msg_t *msg);
  102. faux_phdr_t *faux_msg_get_param_each(faux_list_node_t **node,
  103. uint16_t *param_type, void **param_data, uint32_t *param_len);
  104. faux_phdr_t *faux_msg_get_param_by_index(const faux_msg_t *msg, unsigned int index,
  105. uint16_t *param_type, void **param_data, uint32_t *param_len);
  106. faux_phdr_t *faux_msg_get_param_by_type(const faux_msg_t *msg,
  107. uint16_t param_type, void **param_data, uint32_t *param_len);
  108. char *faux_msg_get_str_param_by_type(const faux_msg_t *msg,
  109. uint16_t param_type);
  110. ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net);
  111. ssize_t faux_msg_send_async(const faux_msg_t *msg, faux_async_t *async);
  112. faux_msg_t *faux_msg_recv(faux_net_t *faux_net);
  113. bool_t faux_msg_iov(const faux_msg_t *msg, struct iovec **iov_out, size_t *iov_num_out);
  114. bool_t faux_msg_serialize(const faux_msg_t *msg, char **buf, size_t *len);
  115. faux_msg_t *faux_msg_deserialize_parts(const faux_hdr_t *hdr,
  116. const char *body, size_t body_len);
  117. faux_msg_t *faux_msg_deserialize(const char *data, size_t len);
  118. void faux_msg_debug(const faux_msg_t *msg);
  119. C_DECL_END
  120. #endif // _faux_msg_h