| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #ifndef LUAT_WEBSOCKET_H
- #define LUAT_WEBSOCKET_H
- enum
- {
- WEBSOCKET_MSG_RELEASE = 0,
- WEBSOCKET_MSG_PUBLISH = 1,
- WEBSOCKET_MSG_TIMER_PING = 2,
- WEBSOCKET_MSG_CONNACK = 3,
- WEBSOCKET_MSG_RECONNECT = 4,
- WEBSOCKET_MSG_SENT = 5,
- WEBSOCKET_MSG_DISCONNECT = 6
- };
- #ifdef LUAT_USE_PSRAM
- #define WEBSOCKET_PAYLOAD_MAX (32*1024)
- #define WEBSOCKET_MAX_READ (16*1024)
- #else
- #define WEBSOCKET_PAYLOAD_MAX (8*1024)
- #define WEBSOCKET_MAX_READ (4*1024)
- #endif
- #define WEBSOCKET_RECV_BUF_LEN_MAX (WEBSOCKET_PAYLOAD_MAX + 16)
- typedef struct
- {
- // http_parser parser;// websocket broker
- network_ctrl_t *netc; // websocket netc
- luat_ip_addr_t ip_addr; // websocket ip
- char host[192]; // websocket host
- char uri[256];
- uint8_t is_tls;
- uint16_t remote_port; // 远程端口号
- uint16_t buffer_offset; // 用于标识pkg_buff当前有多少数据
- uint8_t pkg_buff[WEBSOCKET_RECV_BUF_LEN_MAX + 16];
- int websocket_cb_id; // websocket lua回调函数
- void* websocket_cb; /**< websocket 回调函数*/
- uint32_t keepalive; // 心跳时长 单位s
- uint8_t adapter_index; // 适配器索引号, 似乎并没有什么用
- uint8_t websocket_state; // websocket状态
- uint8_t reconnect; // websocket是否重连
- uint32_t reconnect_time; // websocket重连时间 单位ms
- void *reconnect_timer; // websocket重连定时器
- void *ping_timer; // websocket_ping定时器
- int websocket_ref; // 强制引用自身避免被GC
- char* headers;
- int frame_wait;
- } luat_websocket_ctrl_t;
- typedef struct luat_websocket_connopts
- {
- const char *url;
- uint16_t keepalive;
- uint8_t use_ipv6;
- } luat_websocket_connopts_t;
- typedef struct luat_websocket_pkg
- {
- uint8_t FIN;
- uint8_t OPT_CODE;
- uint8_t R;
- uint8_t mark;
- uint16_t plen; // 最多支持64k
- const char *payload;
- } luat_websocket_pkg_t;
- #define WebSocket_OP_CONTINUE 0x0 /* 0000 - continue frame */
- #define WebSocket_OP_TEXT 0x1 /* 0001 - text frame */
- #define WebSocket_OP_BINARY 0x2 /* 0010 - binary frame */
- #define WebSocket_OP_CLOSE 0x8 /* 1000 - close frame */
- #define WebSocket_OP_PING 0x9 /* 1001 - ping frame */
- #define WebSocket_OP_PONG 0xA /* 1010 - pong frame */
- typedef void (*luat_websocket_cb_t)(luat_websocket_ctrl_t *websocket_ctrl, int arg1, int arg2);
- int luat_websocket_connect(luat_websocket_ctrl_t *websocket_ctrl);
- int luat_websocket_set_cb(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_cb_t websocket_cb);
- int luat_websocket_send_packet(void *socket_info, const void *buf, unsigned int count);
- void luat_websocket_close_socket(luat_websocket_ctrl_t *websocket_ctrl);
- void luat_websocket_release_socket(luat_websocket_ctrl_t *websocket_ctrl);
- void luat_websocket_ping(luat_websocket_ctrl_t *websocket_ctrl);
- void luat_websocket_reconnect(luat_websocket_ctrl_t *websocket_ctrl);
- int luat_websocket_init(luat_websocket_ctrl_t *websocket_ctrl, int adapter_index);
- int luat_websocket_autoreconn(luat_websocket_ctrl_t *websocket_ctrl, uint8_t reconnect,uint32_t reconnect_time);
- int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_connopts_t* opts);
- int luat_websocket_payload(char *buff, luat_websocket_pkg_t *pkg, size_t limit);
- int luat_websocket_send_frame(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_pkg_t *pkg);
- int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, char *headers);
- #endif
|