File: host_connector.cpp

package info (click to toggle)
anbox 0.0~git20210106-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 6,916 kB
  • sloc: cpp: 50,603; ansic: 5,056; sh: 1,150; xml: 850; java: 780; python: 460; makefile: 35; lisp: 7
file content (79 lines) | stat: -rw-r--r-- 2,521 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "android/service/host_connector.h"
#include "android/service/local_socket_connection.h"
#include "android/service/message_processor.h"
#include "android/service/android_api_skeleton.h"
#include "android/service/platform_api_stub.h"

#include "anbox/rpc/channel.h"

#include <functional>
#include <array>

namespace anbox {
HostConnector::HostConnector() :
    socket_(std::make_shared<LocalSocketConnection>("/dev/anbox_bridge")),
    pending_calls_(std::make_shared<rpc::PendingCallCache>()),
    android_api_skeleton_(std::make_shared<AndroidApiSkeleton>()),
    message_processor_(std::make_shared<MessageProcessor>(socket_, pending_calls_, android_api_skeleton_)),
    rpc_channel_(std::make_shared<rpc::Channel>(pending_calls_, socket_)),
    platform_api_stub_(std::make_shared<PlatformApiStub>(rpc_channel_)),
    running_(false) {
}

HostConnector::~HostConnector() {
}

void HostConnector::start() {
    if (running_)
        return;

    running_.exchange(true);
    thread_ = std::thread(std::bind(&HostConnector::main_loop, this));
}

void HostConnector::stop() {
    if (!running_.exchange(false))
        return;

    thread_.join();
}

void HostConnector::main_loop() {
    while (running_) {
        std::array<std::uint8_t, 8192> buffer;
        const auto bytes_read = socket_->read_all(buffer.data(), buffer.size());
        if (bytes_read == 0)
            break;

        // MessageProcessor wants an vector so give it what it wants until
        // we refactor this.
        std::vector<std::uint8_t> data;
        for (unsigned n = 0; n < bytes_read; n++)
            data.push_back(buffer[n]);

        if (!message_processor_->process_data(data))
            break;
    }
}

std::shared_ptr<anbox::PlatformApiStub> HostConnector::platform_api_stub() const {
    return platform_api_stub_;
}
} // namespace anbox