1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*
Copyright (C) 2009-2015 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "dummy-channel-client.h"
#include "red-channel.h"
static void dummy_channel_client_initable_interface_init(GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE(DummyChannelClient, dummy_channel_client, RED_TYPE_CHANNEL_CLIENT,
G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE,
dummy_channel_client_initable_interface_init))
#define DUMMY_CHANNEL_CLIENT_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_DUMMY_CHANNEL_CLIENT, DummyChannelClientPrivate))
struct DummyChannelClientPrivate
{
gboolean connected;
};
static int dummy_channel_client_pre_create_validate(RedChannel *channel, RedClient *client)
{
uint32_t type, id;
g_object_get(channel, "channel-type", &type, "id", &id, NULL);
if (red_client_get_channel(client, type, id)) {
spice_printerr("Error client %p: duplicate channel type %d id %d",
client, type, id);
return FALSE;
}
return TRUE;
}
static gboolean dummy_channel_client_initable_init(GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GError *local_error = NULL;
DummyChannelClient *self = DUMMY_CHANNEL_CLIENT(initable);
RedChannelClient *rcc = RED_CHANNEL_CLIENT(self);
RedClient *client = red_channel_client_get_client(rcc);
RedChannel *channel = red_channel_client_get_channel(rcc);
uint32_t type, id;
g_object_get(channel, "channel-type", &type, "id", &id, NULL);
pthread_mutex_lock(&client->lock);
if (!dummy_channel_client_pre_create_validate(channel,
client)) {
g_set_error(&local_error,
SPICE_SERVER_ERROR,
SPICE_SERVER_ERROR_FAILED,
"Client %p: duplicate channel type %d id %d",
client, type, id);
goto cleanup;
}
rcc->incoming.header.data = rcc->incoming.header_buf;
red_channel_add_client(channel, rcc);
red_client_add_channel(client, rcc);
cleanup:
pthread_mutex_unlock(&client->lock);
if (local_error) {
g_warning("Failed to create channel client: %s", local_error->message);
g_propagate_error(error, local_error);
}
return local_error == NULL;
}
static void dummy_channel_client_initable_interface_init(GInitableIface *iface)
{
iface->init = dummy_channel_client_initable_init;
}
static gboolean dummy_channel_client_is_connected(RedChannelClient *rcc)
{
return DUMMY_CHANNEL_CLIENT(rcc)->priv->connected;
}
static void dummy_channel_client_disconnect(RedChannelClient *rcc)
{
DummyChannelClient *self = DUMMY_CHANNEL_CLIENT(rcc);
RedChannel *channel = red_channel_client_get_channel(rcc);
GList *link;
uint32_t type, id;
if (channel && (link = g_list_find(red_channel_get_clients(channel), rcc))) {
g_object_get(channel, "channel-type", &type, "id", &id, NULL);
spice_printerr("rcc=%p (channel=%p type=%d id=%d)", rcc, channel,
type, id);
red_channel_remove_client(channel, link->data);
}
self->priv->connected = FALSE;
}
static void
dummy_channel_client_class_init(DummyChannelClientClass *klass)
{
RedChannelClientClass *cc_class = RED_CHANNEL_CLIENT_CLASS(klass);
g_type_class_add_private(klass, sizeof(DummyChannelClientPrivate));
cc_class->is_connected = dummy_channel_client_is_connected;
cc_class->disconnect = dummy_channel_client_disconnect;
}
static void
dummy_channel_client_init(DummyChannelClient *self)
{
self->priv = DUMMY_CHANNEL_CLIENT_PRIVATE(self);
self->priv->connected = TRUE;
}
RedChannelClient* dummy_channel_client_create(RedChannel *channel,
RedClient *client,
int num_common_caps,
uint32_t *common_caps,
int num_caps, uint32_t *caps)
{
RedChannelClient *rcc;
GArray *common_caps_array = NULL, *caps_array = NULL;
if (common_caps) {
common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
num_common_caps);
g_array_append_vals(common_caps_array, common_caps, num_common_caps);
}
if (caps) {
caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
g_array_append_vals(caps_array, caps, num_caps);
}
rcc = g_initable_new(TYPE_DUMMY_CHANNEL_CLIENT,
NULL, NULL,
"channel", channel,
"client", client,
"caps", caps_array,
"common-caps", common_caps_array,
NULL);
if (caps_array)
g_array_unref(caps_array);
if (common_caps_array)
g_array_unref(common_caps_array);
return rcc;
}
|