1 /*
2    Copyright (C) 2009-2015 Red Hat, Inc.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef MAIN_CHANNEL_CLIENT_H_
19 #define MAIN_CHANNEL_CLIENT_H_
20 
21 #include <common/messages.h>
22 
23 #include "red-channel-client.h"
24 #include "main-channel.h"
25 #include "utils.hpp"
26 
27 #include "push-visibility.h"
28 
29 class MainChannelClientPrivate;
30 
31 MainChannelClient *main_channel_client_create(MainChannel *main_chan, RedClient *client,
32                                               RedStream *stream, uint32_t connection_id,
33                                               RedChannelCapabilities *caps);
34 
35 struct RedAgentDataPipeItem;
36 
37 class MainChannelClient final: public RedChannelClient
38 {
39 public:
40     void push_agent_tokens(uint32_t num_tokens);
41     void push_agent_data(red::shared_ptr<RedAgentDataPipeItem>&& item);
42     // TODO: huge. Consider making a reds_* interface for these functions
43     // and calling from main.
44     void push_init(int display_channels_hint, SpiceMouseMode current_mouse_mode,
45                    int is_client_mouse_allowed, int multi_media_time,
46                    int ram_hint);
47     void push_notify(const char *msg);
48     gboolean connect_semi_seamless();
49     void connect_seamless();
50     void handle_migrate_connected(int success, int seamless);
51     void handle_migrate_dst_do_seamless(uint32_t src_version);
52     void handle_migrate_end();
53     void migrate_cancel_wait();
54     void migrate_dst_complete();
55     gboolean migrate_src_complete(gboolean success);
56 
57     /*
58      * return TRUE if network test had been completed successfully.
59      * If FALSE, bitrate_per_sec is set to MAX_UINT64 and the roundtrip is set to 0
60      */
61     bool is_network_info_initialized() const;
62     bool is_low_bandwidth() const;
63     uint64_t get_bitrate_per_sec() const;
64     uint64_t get_roundtrip_ms() const;
65 
66     void push_name(const char *name);
67     void push_uuid(const uint8_t uuid[16]);
68 
69     uint32_t get_connection_id() const;
70 
71     MainChannelClient(MainChannel *channel,
72                       RedClient *client,
73                       RedStream *stream,
74                       RedChannelCapabilities *caps,
75                       uint32_t connection_id);
76 
77     void handle_pong(SpiceMsgPing *ping, uint32_t size);
78     void start_net_test(int test_rate);
get_channel()79     MainChannel* get_channel()
80     {
81         return static_cast<MainChannel*>(RedChannelClient::get_channel());
82     }
83 
84 protected:
85     virtual uint8_t *alloc_recv_buf(uint16_t type, uint32_t size) override;
86     virtual void release_recv_buf(uint16_t type, uint32_t size, uint8_t *msg) override;
87     virtual void on_disconnect() override;
88     virtual bool handle_message(uint16_t type, uint32_t size, void *message) override;
89     virtual void send_item(RedPipeItem *item)  override;
90     virtual bool handle_migrate_data(uint32_t size, void *message) override;
91     virtual void migrate() override;
92     virtual void handle_migrate_flush_mark() override;
93 
94 public:
95     red::unique_link<MainChannelClientPrivate> priv;
96 };
97 
98 enum {
99     RED_PIPE_ITEM_TYPE_MAIN_CHANNELS_LIST = RED_PIPE_ITEM_TYPE_CHANNEL_BASE,
100     RED_PIPE_ITEM_TYPE_MAIN_PING,
101     RED_PIPE_ITEM_TYPE_MAIN_MOUSE_MODE,
102     RED_PIPE_ITEM_TYPE_MAIN_AGENT_DISCONNECTED,
103     RED_PIPE_ITEM_TYPE_MAIN_AGENT_TOKEN,
104     RED_PIPE_ITEM_TYPE_MAIN_AGENT_DATA,
105     RED_PIPE_ITEM_TYPE_MAIN_MIGRATE_DATA,
106     RED_PIPE_ITEM_TYPE_MAIN_INIT,
107     RED_PIPE_ITEM_TYPE_MAIN_NOTIFY,
108     RED_PIPE_ITEM_TYPE_MAIN_MIGRATE_BEGIN,
109     RED_PIPE_ITEM_TYPE_MAIN_MIGRATE_BEGIN_SEAMLESS,
110     RED_PIPE_ITEM_TYPE_MAIN_MIGRATE_SWITCH_HOST,
111     RED_PIPE_ITEM_TYPE_MAIN_MULTI_MEDIA_TIME,
112     RED_PIPE_ITEM_TYPE_MAIN_NAME,
113     RED_PIPE_ITEM_TYPE_MAIN_UUID,
114     RED_PIPE_ITEM_TYPE_MAIN_AGENT_CONNECTED_TOKENS,
115     RED_PIPE_ITEM_TYPE_MAIN_REGISTERED_CHANNEL,
116 };
117 
118 struct RedAgentDataPipeItem: public RedPipeItemNum<RED_PIPE_ITEM_TYPE_MAIN_AGENT_DATA> {
119     int len = 0;
120     uint8_t data[SPICE_AGENT_MAX_DATA_SIZE];
121 };
122 
123 RedPipeItemPtr main_mouse_mode_item_new(SpiceMouseMode current_mode, int is_client_mouse_allowed);
124 
125 RedPipeItemPtr main_multi_media_time_item_new(uint32_t mm_time);
126 
127 RedPipeItemPtr registered_channel_item_new(RedChannel *channel);
128 
129 #include "pop-visibility.h"
130 
131 #endif /* MAIN_CHANNEL_CLIENT_H_ */
132