grpcdef.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef GRPC_DEF_H_
  2. #define GRPC_DEF_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. // Length-Prefixed-Message
  7. // flags:1byte + length:4bytes = 5bytes
  8. #define GRPC_MESSAGE_HDLEN 5
  9. typedef struct {
  10. unsigned char flags;
  11. int length;
  12. } grpc_message_hd;
  13. typedef struct {
  14. unsigned char flags;
  15. int length;
  16. unsigned char* message;
  17. } grpc_message;
  18. static inline void grpc_message_hd_pack(const grpc_message_hd* hd, unsigned char* buf) {
  19. // hton
  20. int length = hd->length;
  21. unsigned char* p = buf;
  22. *p++ = hd->flags;
  23. *p++ = (length >> 24) & 0xFF;
  24. *p++ = (length >> 16) & 0xFF;
  25. *p++ = (length >> 8) & 0xFF;
  26. *p++ = length & 0xFF;
  27. }
  28. static inline void grpc_message_hd_unpack(const unsigned char* buf, grpc_message_hd* hd) {
  29. // ntoh
  30. const unsigned char* p = buf;
  31. hd->flags = *p++;
  32. hd->length = *p++ << 24;
  33. hd->length += *p++ << 16;
  34. hd->length += *p++ << 8;
  35. hd->length += *p++;
  36. }
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40. #endif // GRPC_DEF_H_