summaryrefslogtreecommitdiff
path: root/server/common-graphics-channel.cpp
blob: 24a5d1badeb3b4f5240f06d365a9b69e64aa34db (plain)
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
/*
   Copyright (C) 2009-2016 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/>.
*/
#include <config.h>

#include <fcntl.h>

#include "common-graphics-channel.h"
#include "dcc.h"
#include "red-client.h"

#define CHANNEL_RECEIVE_BUF_SIZE 1024

struct CommonGraphicsChannelPrivate
{
    int during_target_migrate; /* TRUE when the client that is associated with the channel
                                  is during migration. Turned off when the vm is started.
                                  The flag is used to avoid sending messages that are artifacts
                                  of the transition from stopped vm to loaded vm (e.g., recreation
                                  of the primary surface) */
};

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(CommonGraphicsChannel, common_graphics_channel,
                                    RED_TYPE_CHANNEL)

struct CommonGraphicsChannelClientPrivate {
    uint8_t recv_buf[CHANNEL_RECEIVE_BUF_SIZE];
};

G_DEFINE_TYPE_WITH_PRIVATE(CommonGraphicsChannelClient, common_graphics_channel_client,
                           RED_TYPE_CHANNEL_CLIENT)

static uint8_t *common_alloc_recv_buf(RedChannelClient *rcc, uint16_t type, uint32_t size)
{
    CommonGraphicsChannelClient *common = COMMON_GRAPHICS_CHANNEL_CLIENT(rcc);

    /* SPICE_MSGC_MIGRATE_DATA is the only client message whose size is dynamic */
    if (type == SPICE_MSGC_MIGRATE_DATA) {
        return (uint8_t *) g_malloc(size);
    }

    if (size > sizeof(common->priv->recv_buf)) {
        spice_warning("unexpected message size %u (max is %zd)", size,
                      sizeof(common->priv->recv_buf));
        return NULL;
    }
    return common->priv->recv_buf;
}

static void common_release_recv_buf(RedChannelClient *rcc, uint16_t type, uint32_t size,
                                    uint8_t* msg)
{
    if (type == SPICE_MSGC_MIGRATE_DATA) {
        g_free(msg);
    }
}

bool common_channel_client_config_socket(RedChannelClient *rcc)
{
    RedClient *client = rcc->get_client();
    MainChannelClient *mcc = red_client_get_main(client);
    RedStream *stream = rcc->get_stream();
    gboolean is_low_bandwidth;

    // TODO - this should be dynamic, not one time at channel creation
    is_low_bandwidth = main_channel_client_is_low_bandwidth(mcc);
    if (!red_stream_set_auto_flush(stream, false)) {
        /* FIXME: Using Nagle's Algorithm can lead to apparent delays, depending
         * on the delayed ack timeout on the other side.
         * Instead of using Nagle's, we need to implement message buffering on
         * the application level.
         * see: http://www.stuartcheshire.org/papers/NagleDelayedAck/
         */
        red_stream_set_no_delay(stream, !is_low_bandwidth);
    }
    // TODO: move wide/narrow ack setting to red_channel.
    rcc->ack_set_client_window(is_low_bandwidth ? WIDE_CLIENT_ACK_WINDOW : NARROW_CLIENT_ACK_WINDOW);
    return TRUE;
}


static void
common_graphics_channel_class_init(CommonGraphicsChannelClass *klass)
{
}

static void
common_graphics_channel_init(CommonGraphicsChannel *self)
{
    self->priv = (CommonGraphicsChannelPrivate*) common_graphics_channel_get_instance_private(self);
}

void common_graphics_channel_set_during_target_migrate(CommonGraphicsChannel *self, gboolean value)
{
    self->priv->during_target_migrate = value;
}

gboolean common_graphics_channel_get_during_target_migrate(CommonGraphicsChannel *self)
{
    return self->priv->during_target_migrate;
}

static void
common_graphics_channel_client_init(CommonGraphicsChannelClient *self)
{
    self->priv = (CommonGraphicsChannelClientPrivate*)
        common_graphics_channel_client_get_instance_private(self);
}

static void
common_graphics_channel_client_class_init(CommonGraphicsChannelClientClass *klass)
{
    RedChannelClientClass *client_class = RED_CHANNEL_CLIENT_CLASS(klass);

    client_class->config_socket = common_channel_client_config_socket;
    client_class->alloc_recv_buf = common_alloc_recv_buf;
    client_class->release_recv_buf = common_release_recv_buf;
}