sproto.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef sproto_h
  2. #define sproto_h
  3. #include <stddef.h>
  4. struct sproto;
  5. struct sproto_type;
  6. #define SPROTO_REQUEST 0
  7. #define SPROTO_RESPONSE 1
  8. // type (sproto_arg.type)
  9. #define SPROTO_TINTEGER 0
  10. #define SPROTO_TBOOLEAN 1
  11. #define SPROTO_TSTRING 2
  12. #define SPROTO_TDOUBLE 3
  13. #define SPROTO_TSTRUCT 4
  14. // container type
  15. #define SPROTO_TARRAY 0x80
  16. // sub type of string (sproto_arg.extra)
  17. #define SPROTO_TSTRING_STRING 0
  18. #define SPROTO_TSTRING_BINARY 1
  19. #define SPROTO_CB_ERROR -1
  20. #define SPROTO_CB_NIL -2
  21. #define SPROTO_CB_NOARRAY -3
  22. struct sproto * sproto_create(const void * proto, size_t sz);
  23. void sproto_release(struct sproto *);
  24. int sproto_prototag(const struct sproto *, const char * name);
  25. const char * sproto_protoname(const struct sproto *, int proto);
  26. // SPROTO_REQUEST(0) : request, SPROTO_RESPONSE(1): response
  27. struct sproto_type * sproto_protoquery(const struct sproto *, int proto, int what);
  28. int sproto_protoresponse(const struct sproto *, int proto);
  29. struct sproto_type * sproto_type(const struct sproto *, const char * type_name);
  30. int sproto_pack(const void * src, int srcsz, void * buffer, int bufsz);
  31. int sproto_unpack(const void * src, int srcsz, void * buffer, int bufsz);
  32. struct sproto_arg {
  33. void *ud;
  34. const char *tagname;
  35. int tagid;
  36. int type;
  37. struct sproto_type *subtype;
  38. void *value;
  39. int length;
  40. int index; // array base 1, negative value indicates that it is a empty array
  41. int mainindex; // for map
  42. int extra; // SPROTO_TINTEGER: decimal ; SPROTO_TSTRING 0:utf8 string 1:binary
  43. // When interpretd two fields struct as map, the following fields must not be NULL.
  44. const char *ktagname;
  45. const char *vtagname;
  46. };
  47. typedef int (*sproto_callback)(const struct sproto_arg *args);
  48. int sproto_decode(const struct sproto_type *, const void * data, int size, sproto_callback cb, void *ud);
  49. int sproto_encode(const struct sproto_type *, void * buffer, int size, sproto_callback cb, void *ud);
  50. // for debug use
  51. void sproto_dump(struct sproto *);
  52. const char * sproto_name(struct sproto_type *);
  53. #endif