c056447ab92efb247e706f030ac7246ac8199deb
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / qemu / qemud_message_processor.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #include "anbox/qemu/qemud_message_processor.h"
19 #include "anbox/logger.h"
20 #include "anbox/utils.h"
21
22 #include <string.h>
23
24 namespace {
25 static constexpr const long header_size{4};
26 } // namespace
27
28 namespace anbox {
29 namespace qemu {
30 QemudMessageProcessor::QemudMessageProcessor(
31     const std::shared_ptr<network::SocketMessenger> &messenger)
32     : messenger_(messenger) {}
33
34 QemudMessageProcessor::~QemudMessageProcessor() {}
35
36 bool QemudMessageProcessor::process_data(const std::vector<std::uint8_t> &data) {
37   for (const auto &byte : data)
38     buffer_.push_back(byte);
39
40   return process_commands();
41 }
42
43 bool QemudMessageProcessor::process_commands() {
44   while (true) {
45     if (buffer_.size() < header_size)
46       break;
47
48     char header[header_size] = {0};
49     ::memcpy(header, buffer_.data(), header_size);
50
51     unsigned int body_size = 0;
52     ::sscanf(header, "%04x", &body_size);
53
54     // Double check that we have enough data to ready the whole body. If
55     // not we have to wait until we have everything.
56     size_t total_size = header_size + body_size;
57     if (buffer_.size() < total_size)
58       break;
59
60     std::string command;
61     // Make sure we only copy as much bytes as we have to and not more
62     command.insert(0,
63                    reinterpret_cast<const char *>(buffer_.data()) + header_size,
64                    body_size);
65
66     handle_command(command);
67
68     const auto consumed = header_size + body_size;
69     buffer_.erase(buffer_.begin(), buffer_.begin() + consumed);
70
71     const auto remaining = buffer_.size() - consumed;
72     if (remaining <= 0)
73       break;
74   }
75
76   return true;
77 }
78
79 void QemudMessageProcessor::send_header(const size_t &size) {
80   char header[header_size + 1];
81   std::snprintf(header, header_size + 1, "%04zx", size);
82   messenger_->send(header, header_size);
83 }
84
85 void QemudMessageProcessor::finish_message() {
86   // Send terminating NULL byte
87   messenger_->send(static_cast<const char *>(""), 1);
88 }
89 }  // namespace qemu
90 }  // namespace anbox