blob: 7ad48af0cf51d54f49f8ea55c09071bf51abff33 [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{
Eddie James21b177e2018-12-11 13:14:46 -060013/*
14 * @class Server
15 * @brief Manages the RFB server connection and updates
16 */
17class Server
18{
19 public:
20 /*
21 * @struct ClientData
22 * @brief Store necessary data for each connected RFB client
23 */
24 struct ClientData
25 {
26 /*
27 * @brief Constructs ClientData object
28 *
29 * @param[in] s - Number of frames to skip when client connects
30 * @param[in] i - Pointer to Input object
31 */
Paul Fertser2d2f3da2021-06-18 11:16:43 +000032 ClientData(int s, Input* i) : skipFrame(s), input(i), last_crc{-1}
Eddie James21b177e2018-12-11 13:14:46 -060033 {
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070034 needUpdate = false;
Eddie James21b177e2018-12-11 13:14:46 -060035 }
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;
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070044 bool needUpdate;
Paul Fertser2d2f3da2021-06-18 11:16:43 +000045 int64_t last_crc;
Eddie James21b177e2018-12-11 13:14:46 -060046 };
47
48 /*
49 * @brief Constructs Server object
50 *
51 * @param[in] args - Reference to Args object
52 * @param[in] i - Reference to Input object
53 * @param[in] v - Reference to Video object
54 */
55 Server(const Args& args, Input& i, Video& v);
56 ~Server();
57 Server(const Server&) = default;
58 Server& operator=(const Server&) = default;
59 Server(Server&&) = default;
60 Server& operator=(Server&&) = default;
61
Eddie James29f775a2018-12-11 13:22:54 -060062 /* @brief Resizes the RFB framebuffer */
63 void resize();
64 /* @brief Executes any pending RFB updates and client input */
65 void run();
66 /* @brief Sends pending video frame to clients */
67 void sendFrame();
68
69 /*
70 * @brief Indicates whether or not video data is desired
71 *
72 * @return Boolean to indicate whether any clients need a video frame
73 */
74 inline bool wantsFrame() const
75 {
76 return server->clientHead;
77 }
Eddie James21b177e2018-12-11 13:14:46 -060078 /*
79 * @brief Get the Video object
80 *
81 * @return Reference to the Video object
82 */
83 inline const Video& getVideo() const
84 {
85 return video;
86 }
87
88 private:
Eddie James29f775a2018-12-11 13:22:54 -060089 /*
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070090 * @brief Handler for a client frame update message
91 *
92 * @param[in] cl - Handle to the client object
93 * @param[in] furMsg - Pointer of the FUR message
94 */
95 static void
George Liuf79f6f52022-07-06 09:32:35 +080096 clientFramebufferUpdateRequest(rfbClientPtr cl,
97 rfbFramebufferUpdateRequestMsg* furMsg);
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070098 /*
Eddie James29f775a2018-12-11 13:22:54 -060099 * @brief Handler for a client disconnecting
100 *
101 * @param[in] cl - Handle to the client object
102 */
103 static void clientGone(rfbClientPtr cl);
104 /*
105 * @brief Handler for client connecting
106 *
107 * @param[in] cl - Handle to the client object
108 */
109 static enum rfbNewClientAction newClient(rfbClientPtr cl);
110
111 /* @brief Performs the resize operation on the framebuffer */
112 void doResize();
113
114 /* @brief Boolean to indicate if a resize operation is on-going */
115 bool pendingResize;
116 /* @brief Number of frames handled since a client connected */
117 int frameCounter;
118 /* @brief Number of connected clients */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800119 unsigned int numClients;
Eddie James29f775a2018-12-11 13:22:54 -0600120 /* @brief Microseconds to process RFB events every frame */
121 long int processTime;
122 /* @brief Handle to the RFB server object */
123 rfbScreenInfoPtr server;
Eddie James21b177e2018-12-11 13:14:46 -0600124 /* @brief Reference to the Input object */
125 Input& input;
126 /* @brief Reference to the Video object */
127 Video& video;
Eddie James29f775a2018-12-11 13:22:54 -0600128 /* @brief Default framebuffer storage */
129 std::vector<char> framebuffer;
Paul Fertser2d2f3da2021-06-18 11:16:43 +0000130 /* @brief Identical frames detection */
131 bool calcFrameCRC;
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800132 /* @brief Cursor bitmap width */
133 static constexpr int cursorWidth = 20;
134 /* @brief Cursor bitmap height */
135 static constexpr int cursorHeight = 20;
136 /* @brief Cursor bitmap */
137 static constexpr char cursor[] = " "
138 " x "
139 " xx "
140 " xxx "
141 " xxxx "
142 " xxxxx "
143 " xxxxxx "
144 " xxxxxxx "
145 " xxxxxxxx "
146 " xxxxxxxxx "
147 " xxxxxxxxxx "
148 " xxxxxxxxxxx "
149 " xxxxxxx "
150 " xxxxxxx "
151 " xxx xxx "
152 " xx xxx "
153 " x xxx "
154 " xxx "
155 " x "
156 " ";
157 /* @brief Cursor bitmap mask */
158 static constexpr char cursorMask[] = " o "
159 "oxo "
160 "oxxo "
161 "oxxxo "
162 "oxxxxo "
163 "oxxxxxo "
164 "oxxxxxxo "
165 "oxxxxxxxo "
166 "oxxxxxxxxo "
167 "oxxxxxxxxxo "
168 "oxxxxxxxxxxo "
169 "oxxxxxxxxxxxo "
170 "oxxxxxxxoooo "
171 "oxxxxxxxo "
172 "oxxxooxxxo "
173 "oxxo oxxxo "
174 "oxo oxxxo "
175 " o oxxxo "
176 " oxo "
177 " o ";
Eddie James21b177e2018-12-11 13:14:46 -0600178};
179
180} // namespace ikvm