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