提交 bde909f0 编写于 作者: S siyangy 提交者: unacao

Dreamview: small refactor of SendData (#1702)

上级 fd9bd6a2
......@@ -59,7 +59,7 @@ SimulationWorldUpdater::SimulationWorldUpdater(WebSocketHandler *websocket,
response["type"] = "MapData";
response["data"] = Json::parse(retrieved_json_string);
websocket_->SendData(response.dump(), conn);
websocket_->SendData(conn, response.dump());
}
});
......@@ -72,7 +72,7 @@ SimulationWorldUpdater::SimulationWorldUpdater(WebSocketHandler *websocket,
}
auto response = sim_world_service_.GetUpdateAsJson(*radius);
websocket_->SendData(response.dump(), conn);
websocket_->SendData(conn, response.dump());
});
websocket_->RegisterMessageHandler(
......@@ -107,7 +107,7 @@ SimulationWorldUpdater::SimulationWorldUpdater(WebSocketHandler *websocket,
boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
to_send = simulation_world_json_;
}
websocket_->SendData(to_send, conn);
websocket_->SendData(conn, to_send, true);
});
}
......
......@@ -67,7 +67,7 @@ bool WebSocketHandler::BroadcastData(const std::string &data) {
bool all_success = true;
for (Connection *conn : connections_to_send) {
if (!SendData(data, conn, true)) {
if (!SendData(conn, data, true)) {
all_success = false;
}
}
......@@ -75,8 +75,8 @@ bool WebSocketHandler::BroadcastData(const std::string &data) {
return all_success;
}
bool WebSocketHandler::SendData(const std::string &data, Connection *conn,
bool is_broadcast) {
bool WebSocketHandler::SendData(Connection *conn, const std::string &data,
bool skippable) {
std::shared_ptr<std::mutex> connection_lock;
{
std::unique_lock<std::mutex> lock(mutex_);
......@@ -92,9 +92,10 @@ bool WebSocketHandler::SendData(const std::string &data, Connection *conn,
// Lock the connection while sending.
if (!connection_lock->try_lock()) {
// Skip sending data if:
// 1. This is a broadcast and there's higher priority data being sent.
// 1. Data is skippable according to sender and there's higher priority data
// being sent.
// 2. The connection has been closed.
if (is_broadcast) {
if (skippable) {
return false;
} else {
connection_lock->lock(); // Block to acquire the lock.
......
......@@ -104,12 +104,14 @@ class WebSocketHandler : public CivetWebSocketHandler {
/**
* @brief Sends the provided data to a specific connected client.
* @param data The message string to be sent.
*
* @param conn The connection to send to.
* @param is_broadcast whether this is a broadcast send.
* @param data The message string to be sent.
* @param skippable whether the data is allowed to be skipped if some other is
* being sent to this connection.
*/
bool SendData(const std::string &data, Connection *conn,
bool is_broadcast = false);
bool SendData(Connection *conn, const std::string &data,
bool skippable = false);
/**
* @brief Add a new message handler for a message type.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册