luat_websocket.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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; // websocket lua回调函数
  26. uint32_t keepalive; // 心跳时长 单位s
  27. uint8_t adapter_index; // 适配器索引号, 似乎并没有什么用
  28. uint8_t websocket_state; // websocket状态
  29. uint8_t reconnect; // websocket是否重连
  30. uint32_t reconnect_time; // websocket重连时间 单位ms
  31. void *reconnect_timer; // websocket重连定时器
  32. void *ping_timer; // websocket_ping定时器
  33. int websocket_ref; // 强制引用自身避免被GC
  34. char* headers;
  35. int frame_wait;
  36. } luat_websocket_ctrl_t;
  37. typedef struct luat_websocket_connopts
  38. {
  39. const char *url;
  40. uint16_t is_tls;
  41. } luat_websocket_connopts_t;
  42. typedef struct luat_websocket_pkg
  43. {
  44. uint8_t FIN;
  45. uint8_t OPT_CODE;
  46. uint8_t R;
  47. uint8_t mark;
  48. uint16_t plen; // 最多支持64k
  49. char *payload;
  50. } luat_websocket_pkg_t;
  51. #define WebSocket_OP_CONTINUE 0x0 /* 0000 - continue frame */
  52. #define WebSocket_OP_TEXT 0x1 /* 0001 - text frame */
  53. #define WebSocket_OP_BINARY 0x2 /* 0010 - binary frame */
  54. #define WebSocket_OP_CLOSE 0x8 /* 1000 - close frame */
  55. #define WebSocket_OP_PING 0x9 /* 1001 - ping frame */
  56. #define WebSocket_OP_PONG 0xA /* 1010 - pong frame */
  57. int luat_websocket_connect(luat_websocket_ctrl_t *websocket_ctrl);
  58. int l_luat_websocket_msg_cb(luat_websocket_ctrl_t *ctrl, int arg1, int arg2);
  59. int32_t luat_websocket_callback(void *data, void *param);
  60. int luat_websocket_send_packet(void *socket_info, const void *buf, unsigned int count);
  61. void luat_websocket_close_socket(luat_websocket_ctrl_t *websocket_ctrl);
  62. void luat_websocket_release_socket(luat_websocket_ctrl_t *websocket_ctrl);
  63. void luat_websocket_ping(luat_websocket_ctrl_t *websocket_ctrl);
  64. void luat_websocket_reconnect(luat_websocket_ctrl_t *websocket_ctrl);
  65. int luat_websocket_init(luat_websocket_ctrl_t *websocket_ctrl, int adapter_index);
  66. int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, const char *url);
  67. int luat_websocket_payload(char *buff, luat_websocket_pkg_t *pkg, size_t limit);
  68. int luat_websocket_send_frame(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_pkg_t *pkg);
  69. int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, const char *headers);
  70. #endif