luat_websocket.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef LUAT_WEBSOCKET_H
  2. #define LUAT_WEBSOCKET_H
  3. enum
  4. {
  5. WEBSOCKET_MSG_RELEASE = 0,
  6. WEBSOCKET_MSG_PUBLISH = 1,
  7. WEBSOCKET_MSG_TIMER_PING = 2,
  8. WEBSOCKET_MSG_CONNACK = 3,
  9. WEBSOCKET_MSG_RECONNECT = 4,
  10. WEBSOCKET_MSG_SENT = 5,
  11. WEBSOCKET_MSG_DISCONNECT = 6
  12. };
  13. #define WEBSOCKET_RECV_BUF_LEN_MAX 4096
  14. typedef struct
  15. {
  16. // http_parser parser;// websocket broker
  17. network_ctrl_t *netc; // websocket netc
  18. luat_ip_addr_t ip_addr; // websocket ip
  19. char host[192]; // websocket host
  20. char uri[256];
  21. uint8_t is_tls;
  22. uint16_t remote_port; // 远程端口号
  23. uint16_t buffer_offset; // 用于标识pkg_buff当前有多少数据
  24. uint8_t pkg_buff[WEBSOCKET_RECV_BUF_LEN_MAX + 4];
  25. int websocket_cb_id; // websocket lua回调函数
  26. void* websocket_cb; /**< websocket 回调函数*/
  27. uint32_t keepalive; // 心跳时长 单位s
  28. uint8_t adapter_index; // 适配器索引号, 似乎并没有什么用
  29. uint8_t websocket_state; // websocket状态
  30. uint8_t reconnect; // websocket是否重连
  31. uint32_t reconnect_time; // websocket重连时间 单位ms
  32. void *reconnect_timer; // websocket重连定时器
  33. void *ping_timer; // websocket_ping定时器
  34. int websocket_ref; // 强制引用自身避免被GC
  35. char* headers;
  36. int frame_wait;
  37. } luat_websocket_ctrl_t;
  38. typedef struct luat_websocket_connopts
  39. {
  40. const char *url;
  41. uint16_t keepalive;
  42. uint8_t use_ipv6;
  43. } luat_websocket_connopts_t;
  44. typedef struct luat_websocket_pkg
  45. {
  46. uint8_t FIN;
  47. uint8_t OPT_CODE;
  48. uint8_t R;
  49. uint8_t mark;
  50. uint16_t plen; // 最多支持64k
  51. const char *payload;
  52. } luat_websocket_pkg_t;
  53. #define WebSocket_OP_CONTINUE 0x0 /* 0000 - continue frame */
  54. #define WebSocket_OP_TEXT 0x1 /* 0001 - text frame */
  55. #define WebSocket_OP_BINARY 0x2 /* 0010 - binary frame */
  56. #define WebSocket_OP_CLOSE 0x8 /* 1000 - close frame */
  57. #define WebSocket_OP_PING 0x9 /* 1001 - ping frame */
  58. #define WebSocket_OP_PONG 0xA /* 1010 - pong frame */
  59. typedef void (*luat_websocket_cb_t)(luat_websocket_ctrl_t *websocket_ctrl, int arg1, int arg2);
  60. int luat_websocket_connect(luat_websocket_ctrl_t *websocket_ctrl);
  61. int luat_websocket_set_cb(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_cb_t websocket_cb);
  62. int luat_websocket_send_packet(void *socket_info, const void *buf, unsigned int count);
  63. void luat_websocket_close_socket(luat_websocket_ctrl_t *websocket_ctrl);
  64. void luat_websocket_release_socket(luat_websocket_ctrl_t *websocket_ctrl);
  65. void luat_websocket_ping(luat_websocket_ctrl_t *websocket_ctrl);
  66. void luat_websocket_reconnect(luat_websocket_ctrl_t *websocket_ctrl);
  67. int luat_websocket_init(luat_websocket_ctrl_t *websocket_ctrl, int adapter_index);
  68. int luat_websocket_autoreconn(luat_websocket_ctrl_t *websocket_ctrl, uint8_t reconnect,uint32_t reconnect_time);
  69. int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_connopts_t* opts);
  70. int luat_websocket_payload(char *buff, luat_websocket_pkg_t *pkg, size_t limit);
  71. int luat_websocket_send_frame(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_pkg_t *pkg);
  72. int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, char *headers);
  73. #endif