Reply to WS_PING with WS_PONG to fix OSD websocket disconnections
This commit is contained in:
@@ -23,7 +23,7 @@
|
|||||||
#define READ_BUFFER_SIZE 9000
|
#define READ_BUFFER_SIZE 9000
|
||||||
|
|
||||||
static std::string ws_format_frame(int type, uint64_t size);
|
static std::string ws_format_frame(int type, uint64_t size);
|
||||||
static bool ws_parse_frame(std::string & buf, int & type, std::string & res);
|
static bool ws_parse_frame(std::string & buf, uint8_t & type, std::string & res);
|
||||||
static void parse_http_headers(std::string & res, http_response_t *parsed);
|
static void parse_http_headers(std::string & res, http_response_t *parsed);
|
||||||
|
|
||||||
struct http_co_t
|
struct http_co_t
|
||||||
@@ -70,7 +70,7 @@ struct http_co_t
|
|||||||
void submit_read(bool check_timeout);
|
void submit_read(bool check_timeout);
|
||||||
void submit_send();
|
void submit_send();
|
||||||
bool handle_read();
|
bool handle_read();
|
||||||
void post_message(int type, const std::string & msg);
|
void post_message(uint8_t type, const std::string & msg);
|
||||||
void send_request(const std::string & host, const std::string & request,
|
void send_request(const std::string & host, const std::string & request,
|
||||||
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback);
|
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback);
|
||||||
};
|
};
|
||||||
@@ -199,12 +199,12 @@ void http_co_t::send_request(const std::string & host, const std::string & reque
|
|||||||
stackout();
|
stackout();
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_post_message(http_co_t *handler, int type, const std::string & msg)
|
void http_post_message(http_co_t *handler, uint8_t type, const std::string & msg)
|
||||||
{
|
{
|
||||||
handler->post_message(type, msg);
|
handler->post_message(type, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_co_t::post_message(int type, const std::string & msg)
|
void http_co_t::post_message(uint8_t type, const std::string & msg)
|
||||||
{
|
{
|
||||||
stackin();
|
stackin();
|
||||||
if (state == HTTP_CO_WEBSOCKET)
|
if (state == HTTP_CO_WEBSOCKET)
|
||||||
@@ -608,7 +608,13 @@ bool http_co_t::handle_read()
|
|||||||
{
|
{
|
||||||
while (ws_parse_frame(response, parsed.ws_msg_type, parsed.body))
|
while (ws_parse_frame(response, parsed.ws_msg_type, parsed.body))
|
||||||
{
|
{
|
||||||
response_callback(&parsed);
|
if (parsed.ws_msg_type == WS_PING)
|
||||||
|
{
|
||||||
|
// Reply with WS_PONG
|
||||||
|
post_message(WS_PONG, "");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
response_callback(&parsed);
|
||||||
parsed.body = "";
|
parsed.body = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -698,7 +704,7 @@ static std::string ws_format_frame(int type, uint64_t size)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ws_parse_frame(std::string & buf, int & type, std::string & res)
|
static bool ws_parse_frame(std::string & buf, uint8_t & type, std::string & res)
|
||||||
{
|
{
|
||||||
uint64_t hdr = 2;
|
uint64_t hdr = 2;
|
||||||
if (buf.size() < hdr)
|
if (buf.size() < hdr)
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ struct http_response_t
|
|||||||
int status_code = 0;
|
int status_code = 0;
|
||||||
std::string status_line;
|
std::string status_line;
|
||||||
std::map<std::string, std::string> headers;
|
std::map<std::string, std::string> headers;
|
||||||
int ws_msg_type = -1;
|
uint8_t ws_msg_type = -1;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
||||||
void parse_json_response(std::string & error, json11::Json & r) const;
|
void parse_json_response(std::string & error, json11::Json & r) const;
|
||||||
@@ -46,5 +46,5 @@ http_co_t* open_websocket(timerfd_manager_t *tfd, const std::string & host, cons
|
|||||||
int timeout, std::function<void(const http_response_t *msg)> on_message);
|
int timeout, std::function<void(const http_response_t *msg)> on_message);
|
||||||
void http_request(http_co_t *handler, const std::string & host, const std::string & request,
|
void http_request(http_co_t *handler, const std::string & host, const std::string & request,
|
||||||
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback);
|
const http_options_t & options, std::function<void(const http_response_t *response)> response_callback);
|
||||||
void http_post_message(http_co_t *handler, int type, const std::string & msg);
|
void http_post_message(http_co_t *handler, uint8_t type, const std::string & msg);
|
||||||
void http_close(http_co_t *co);
|
void http_close(http_co_t *co);
|
||||||
|
|||||||
Reference in New Issue
Block a user