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 <glog/logging.h>
20 #include "common/libs/tcp_socket/tcp_socket.h"
21 #include "host/frontend/vnc_server/blackboard.h"
22 #include "host/frontend/vnc_server/frame_buffer_watcher.h"
23 #include "host/frontend/vnc_server/jpeg_compressor.h"
24 #include "host/frontend/vnc_server/virtual_inputs.h"
25 #include "host/frontend/vnc_server/vnc_client_connection.h"
26 #include "host/frontend/vnc_server/vnc_utils.h"
27
28 using cvd::vnc::VncServer;
29
VncServer(int port,bool aggressive)30 VncServer::VncServer(int port, bool aggressive)
31 : server_(port),
32 virtual_inputs_(VirtualInputs::Get()),
33 frame_buffer_watcher_{&bb_},
34 aggressive_{aggressive} {}
35
MainLoop()36 void VncServer::MainLoop() {
37 while (true) {
38 LOG(INFO) << "Awaiting connections";
39 auto connection = server_.Accept();
40 LOG(INFO) << "Accepted a client connection";
41 StartClient(std::move(connection));
42 }
43 }
44
StartClient(ClientSocket sock)45 void VncServer::StartClient(ClientSocket sock) {
46 std::thread t(&VncServer::StartClientThread, this, std::move(sock));
47 t.detach();
48 }
49
StartClientThread(ClientSocket sock)50 void VncServer::StartClientThread(ClientSocket sock) {
51 // NOTE if VncServer is expected to be destroyed, we have a problem here.
52 // All of the client threads will be pointing to the VncServer's
53 // data members. In the current setup, if the VncServer is destroyed with
54 // clients still running, the clients will all be left with dangling
55 // pointers.
56 VncClientConnection client(std::move(sock), virtual_inputs_, &bb_,
57 aggressive_);
58 client.StartSession();
59 }
60