summaryrefslogtreecommitdiff
path: root/gtk/bio-gsocket.c
blob: dbf17a2b00e265555dfdb16a6e9bfabce880697e (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
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2012 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 <string.h>
#include <glib.h>

#include "spice-util.h"
#include "bio-gsocket.h"

typedef struct bio_gsocket_method {
    BIO_METHOD method;
    GSocket *gsocket;
} bio_gsocket_method;

#define BIO_GET_GSOCKET(bio)  (((bio_gsocket_method*)bio->method)->gsocket)

static int bio_gsocket_bwrite(BIO *bio, const char *in, int inl)
{
    int ret;
    GError *error = NULL;

    ret = g_socket_send(BIO_GET_GSOCKET(bio),
                        in, inl, NULL, &error);
    BIO_clear_retry_flags(bio);

    if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
        BIO_set_retry_write(bio);
    if (error != NULL) {
        g_warning("%s", error->message);
        g_clear_error(&error);
    }

    return ret;
}

static int bio_gsocket_bread(BIO *bio, char *out, int outl)
{
    int ret;
    GError *error = NULL;

    ret = g_socket_receive(BIO_GET_GSOCKET(bio),
                           out, outl, NULL, &error);
    BIO_clear_retry_flags(bio);

    if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
        BIO_set_retry_read(bio);
    else if (error != NULL)
        g_warning("%s", error->message);

    g_clear_error(&error);

    return ret;
}

static int bio_gsocket_destroy(BIO *bio)
{
    if (bio == NULL || bio->method == NULL)
        return 0;

    SPICE_DEBUG("bio gsocket destroy");
    g_free(bio->method);
    bio->method = NULL;;

    return 1;
}

static int bio_gsocket_bputs(BIO *bio, const char *str)
{
    int n, ret;

    n = strlen(str);
    ret = bio_gsocket_bwrite(bio, str, n);

    return ret;
}

G_GNUC_INTERNAL
BIO* bio_new_gsocket(GSocket *gsocket)
{
    BIO *bio = BIO_new_socket(g_socket_get_fd(gsocket), BIO_NOCLOSE);

    bio_gsocket_method *bio_method = g_new(bio_gsocket_method, 1);
    bio_method->method = *bio->method;
    bio_method->gsocket = gsocket;

    bio->method->destroy(bio);
    bio->method = (BIO_METHOD*)bio_method;

    bio->method->bwrite = bio_gsocket_bwrite;
    bio->method->bread = bio_gsocket_bread;
    bio->method->bputs = bio_gsocket_bputs;
    bio->method->destroy = bio_gsocket_destroy;

    return bio;
}