1 #pragma once 2 3 #include "ikvm_args.hpp" 4 #include "ikvm_input.hpp" 5 #include "ikvm_video.hpp" 6 7 #include <rfb/rfb.h> 8 9 #include <vector> 10 11 namespace ikvm 12 { 13 /* 14 * @class Server 15 * @brief Manages the RFB server connection and updates 16 */ 17 class 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 */ ClientDataikvm::Server::ClientData32 ClientData(int s, Input* i) : skipFrame(s), input(i), last_crc{-1} 33 { 34 needUpdate = false; 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 bool needUpdate; 45 int64_t last_crc; 46 }; 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 62 /* @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 */ wantsFrame() const74 inline bool wantsFrame() const 75 { 76 return server->clientHead; 77 } 78 /* 79 * @brief Get the Video object 80 * 81 * @return Reference to the Video object 82 */ getVideo() const83 inline const Video& getVideo() const 84 { 85 return video; 86 } 87 88 private: 89 /* 90 * @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 clientFramebufferUpdateRequest( 96 rfbClientPtr cl, rfbFramebufferUpdateRequestMsg* furMsg); 97 /* 98 * @brief Handler for a client disconnecting 99 * 100 * @param[in] cl - Handle to the client object 101 */ 102 static void clientGone(rfbClientPtr cl); 103 /* 104 * @brief Handler for client connecting 105 * 106 * @param[in] cl - Handle to the client object 107 */ 108 static enum rfbNewClientAction newClient(rfbClientPtr cl); 109 110 /* @brief Performs the resize operation on the framebuffer */ 111 void doResize(); 112 113 /* @brief Boolean to indicate if a resize operation is on-going */ 114 bool pendingResize; 115 /* @brief Number of frames handled since a client connected */ 116 int frameCounter; 117 /* @brief Number of connected clients */ 118 unsigned int numClients; 119 /* @brief Microseconds to process RFB events every frame */ 120 long int processTime; 121 /* @brief Handle to the RFB server object */ 122 rfbScreenInfoPtr server; 123 /* @brief Reference to the Input object */ 124 Input& input; 125 /* @brief Reference to the Video object */ 126 Video& video; 127 /* @brief Default framebuffer storage */ 128 std::vector<char> framebuffer; 129 /* @brief Identical frames detection */ 130 bool calcFrameCRC; 131 /* @brief Cursor bitmap width */ 132 static constexpr int cursorWidth = 20; 133 /* @brief Cursor bitmap height */ 134 static constexpr int cursorHeight = 20; 135 /* @brief Cursor bitmap */ 136 static constexpr char cursor[] = 137 " " 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[] = 159 " o " 160 "oxo " 161 "oxxo " 162 "oxxxo " 163 "oxxxxo " 164 "oxxxxxo " 165 "oxxxxxxo " 166 "oxxxxxxxo " 167 "oxxxxxxxxo " 168 "oxxxxxxxxxo " 169 "oxxxxxxxxxxo " 170 "oxxxxxxxxxxxo " 171 "oxxxxxxxoooo " 172 "oxxxxxxxo " 173 "oxxxooxxxo " 174 "oxxo oxxxo " 175 "oxo oxxxo " 176 " o oxxxo " 177 " oxo " 178 " o "; 179 }; 180 181 } // namespace ikvm 182