blob: ff51cfc7d29a1f738afa2a0486cf9b909b67420b [file] [log] [blame]
Eddie James21b177e2018-12-11 13:14:46 -06001#pragma once
2
3#include "ikvm_args.hpp"
4#include "ikvm_input.hpp"
5#include "ikvm_video.hpp"
6
Eddie James29f775a2018-12-11 13:22:54 -06007#include <rfb/rfb.h>
8
9#include <vector>
10
Eddie James21b177e2018-12-11 13:14:46 -060011namespace ikvm
12{
13
14/*
15 * @class Server
16 * @brief Manages the RFB server connection and updates
17 */
18class Server
19{
20 public:
21 /*
22 * @struct ClientData
23 * @brief Store necessary data for each connected RFB client
24 */
25 struct ClientData
26 {
27 /*
28 * @brief Constructs ClientData object
29 *
30 * @param[in] s - Number of frames to skip when client connects
31 * @param[in] i - Pointer to Input object
32 */
33 ClientData(int s, Input* i) : skipFrame(s), input(i)
34 {
35 }
36 ~ClientData() = default;
37 ClientData(const ClientData&) = default;
38 ClientData& operator=(const ClientData&) = default;
39 ClientData(ClientData&&) = default;
40 ClientData& operator=(ClientData&&) = default;
41
42 int skipFrame;
43 Input* input;
44 };
45
46 /*
47 * @brief Constructs Server object
48 *
49 * @param[in] args - Reference to Args object
50 * @param[in] i - Reference to Input object
51 * @param[in] v - Reference to Video object
52 */
53 Server(const Args& args, Input& i, Video& v);
54 ~Server();
55 Server(const Server&) = default;
56 Server& operator=(const Server&) = default;
57 Server(Server&&) = default;
58 Server& operator=(Server&&) = default;
59
Eddie James29f775a2018-12-11 13:22:54 -060060 /* @brief Resizes the RFB framebuffer */
61 void resize();
62 /* @brief Executes any pending RFB updates and client input */
63 void run();
64 /* @brief Sends pending video frame to clients */
65 void sendFrame();
66
67 /*
68 * @brief Indicates whether or not video data is desired
69 *
70 * @return Boolean to indicate whether any clients need a video frame
71 */
72 inline bool wantsFrame() const
73 {
74 return server->clientHead;
75 }
Eddie James21b177e2018-12-11 13:14:46 -060076 /*
77 * @brief Get the Video object
78 *
79 * @return Reference to the Video object
80 */
81 inline const Video& getVideo() const
82 {
83 return video;
84 }
85
86 private:
Eddie James29f775a2018-12-11 13:22:54 -060087 /*
88 * @brief Handler for a client disconnecting
89 *
90 * @param[in] cl - Handle to the client object
91 */
92 static void clientGone(rfbClientPtr cl);
93 /*
94 * @brief Handler for client connecting
95 *
96 * @param[in] cl - Handle to the client object
97 */
98 static enum rfbNewClientAction newClient(rfbClientPtr cl);
99
100 /* @brief Performs the resize operation on the framebuffer */
101 void doResize();
102
103 /* @brief Boolean to indicate if a resize operation is on-going */
104 bool pendingResize;
105 /* @brief Number of frames handled since a client connected */
106 int frameCounter;
107 /* @brief Number of connected clients */
108 int numClients;
109 /* @brief Microseconds to process RFB events every frame */
110 long int processTime;
111 /* @brief Handle to the RFB server object */
112 rfbScreenInfoPtr server;
Eddie James21b177e2018-12-11 13:14:46 -0600113 /* @brief Reference to the Input object */
114 Input& input;
115 /* @brief Reference to the Video object */
116 Video& video;
Eddie James29f775a2018-12-11 13:22:54 -0600117 /* @brief Default framebuffer storage */
118 std::vector<char> framebuffer;
Eddie James21b177e2018-12-11 13:14:46 -0600119};
120
121} // namespace ikvm