Skip to content

Commit

Permalink
fixed new scene methods and added proper payload unwrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
99oblivius committed Nov 3, 2024
1 parent 7227f23 commit 8071987
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
61 changes: 35 additions & 26 deletions src/handler/command-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ namespace {
}
}

void handle_event(std::string_view& packet, ActionType& action, std::string& msg, std::error_code& ec) {
void handle_event(std::string_view& packet, ActionType& action, std::string& payload, std::string& msg, std::error_code& ec) {
action = static_cast<uint8_t>(packet[0]) & 0x3F;
std::string payload;
if (packet.length() > 1) {
payload = std::string(packet.substr(1));
}
switch (static_cast<ActionTypeEvent>(action)) {
case ActionTypeEvent::PING:
ping(msg, ec);
Expand Down Expand Up @@ -90,34 +86,47 @@ namespace {
namespace CommandHandler {
void Handler(std::string_view packet, std::vector<uint8_t>& response, std::error_code& ec) {
ActionType action = 0xFF;
std::string payload;
std::string msg;

uint8_t header = 0;
uint8_t success_flag = 0;

if (packet.empty()) {
action = 0xFF;
ec = make_error_code(HandleError::EmptyMessage);
} else {
std::string payload;
if (packet.size() > 1) {
payload.assign(packet.begin() + 1, packet.end());
}
action = packet[0] & 0x3F;
if (static_cast<uint8_t>(packet[0]) & 0x80) {
handle_event(packet, action, msg, ec);
header |= 1 << 7;
success_flag = ec ? 0 : 1;
} else {
handle_request(packet, action, msg, ec);
success_flag = msg.empty() ? 0 : 1;
}
return;
}

std::string_view working_packet = packet;
if (!working_packet.empty() && working_packet.back() == '\n') {
working_packet = working_packet.substr(0, working_packet.size() - 1);
}

if (working_packet.empty()) {
ec = make_error_code(HandleError::EmptyMessage);
return;
}

header |= success_flag << 6;
uint8_t header = working_packet[0];
action = header & 0x3F;
bool is_event = (header & 0x80) != 0;

header |= static_cast<uint8_t>(action) & 0x3F;
response.push_back(header);
if (working_packet.size() > 1) {
payload = std::string(working_packet.begin() + 1, working_packet.end());
}

uint8_t response_header = 0;
uint8_t success_flag = 0;

if (is_event) {
handle_event(working_packet, action, payload, msg, ec);
response_header |= 1 << 7;
success_flag = ec ? 0 : 1;
} else {
handle_request(working_packet, action, msg, ec);
success_flag = msg.empty() ? 0 : 1;
}

response_header |= success_flag << 6;
response_header |= static_cast<uint8_t>(action) & 0x3F;
response.push_back(response_header);

if (!msg.empty()) {
response.insert(response.end(), msg.begin(), msg.end());
Expand Down
2 changes: 2 additions & 0 deletions src/handler/errors/handle-errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class HandleErrorCategory final : public std::error_category {
return "Recording error";
case HandleError::StreamingError:
return "Streaming error";
case HandleError::SceneError:
return "Scene Error";
default:
return "Unknown error";
}
Expand Down
5 changes: 0 additions & 5 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ void Network::read_message(std::shared_ptr<asio::ip::tcp::socket> client_socket)
asio::async_read_until(*client_socket, *buf, '\n',
[this, client_socket, buf](const asio::error_code& error, std::size_t bytes_transferred) {
if (!error) {
std::string payload;
std::string packet_data;
packet_data.resize(bytes_transferred);
std::istream is(buf.get());
is.read(&packet_data[0], bytes_transferred);

if (!packet_data.empty() && packet_data.back() == '\n') {
packet_data.pop_back();
}

std::vector<uint8_t> response;
std::error_code ec;
CommandHandler::Handler(packet_data, response, ec);
Expand Down

0 comments on commit 8071987

Please sign in to comment.