xref: /openbmc/obmc-ikvm/ikvm_server.hpp (revision fda1393c)
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
96         clientFramebufferUpdateRequest(rfbClientPtr cl,
97                                        rfbFramebufferUpdateRequestMsg* furMsg);
98     /*
99      * @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 */
119     unsigned int numClients;
120     /* @brief Microseconds to process RFB events every frame */
121     long int processTime;
122     /* @brief Handle to the RFB server object */
123     rfbScreenInfoPtr server;
124     /* @brief Reference to the Input object */
125     Input& input;
126     /* @brief Reference to the Video object */
127     Video& video;
128     /* @brief Default framebuffer storage */
129     std::vector<char> framebuffer;
130     /* @brief Identical frames detection */
131     bool calcFrameCRC;
132     /* @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           ";
178 };
179 
180 } // namespace ikvm
181