1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/frontend/vnc_server/vnc_server.h"
18
19 #include <memory>
20
21 #include <android-base/logging.h>
22 #include "common/libs/utils/tcp_socket.h"
23 #include "host/frontend/vnc_server/blackboard.h"
24 #include "host/frontend/vnc_server/frame_buffer_watcher.h"
25 #include "host/frontend/vnc_server/jpeg_compressor.h"
26 #include "host/frontend/vnc_server/virtual_inputs.h"
27 #include "host/frontend/vnc_server/vnc_client_connection.h"
28 #include "host/frontend/vnc_server/vnc_utils.h"
29
30 using cuttlefish::vnc::VncServer;
31
VncServer(int port,bool aggressive,cuttlefish::vnc::ScreenConnector & screen_connector,cuttlefish::confui::HostVirtualInput & confui_input)32 VncServer::VncServer(int port, bool aggressive,
33 cuttlefish::vnc::ScreenConnector& screen_connector,
34 cuttlefish::confui::HostVirtualInput& confui_input)
35 : server_(port),
36 virtual_inputs_(VirtualInputs::Get(confui_input)),
37 frame_buffer_watcher_{&bb_, screen_connector},
38 aggressive_{aggressive} {}
39
MainLoop()40 void VncServer::MainLoop() {
41 while (true) {
42 LOG(DEBUG) << "Awaiting connections";
43 auto connection = server_.Accept();
44 LOG(DEBUG) << "Accepted a client connection";
45 StartClient(std::move(connection));
46 }
47 }
48
StartClient(ClientSocket sock)49 void VncServer::StartClient(ClientSocket sock) {
50 std::thread t(&VncServer::StartClientThread, this, std::move(sock));
51 t.detach();
52 }
53
StartClientThread(ClientSocket sock)54 void VncServer::StartClientThread(ClientSocket sock) {
55 // NOTE if VncServer is expected to be destroyed, we have a problem here.
56 // All of the client threads will be pointing to the VncServer's
57 // data members. In the current setup, if the VncServer is destroyed with
58 // clients still running, the clients will all be left with dangling
59 // pointers.
60 frame_buffer_watcher_.IncClientCount();
61 VncClientConnection client(std::move(sock), virtual_inputs_, &bb_,
62 aggressive_);
63 client.StartSession();
64 frame_buffer_watcher_.DecClientCount();
65 }
66