Debian Codegraph @

device-src/directtcp-connection.c

raw
1 /*
2 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
3 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Contact information: Zmanda Inc., 465 N Mathlida Ave, Suite 300
20 * Sunnyvale, CA 94085, or: http://www.zmanda.com
21 */
22
23 #include "amanda.h"
24 #include "directtcp-connection.h"
25 #include <glib-object.h>
26
27 /*
28 * DirectTCPConnection class implementation
29 */
30
31 static GObjectClass *parent_class = NULL;
32
33 static void
34 directtcp_connection_finalize(GObject *goself)
35 {
36 DirectTCPConnection *self = DIRECTTCP_CONNECTION(goself);
37
38 /* close this socket if necessary, failing fatally if that doesn't work */
39 if (!self->closed) {
40 char *errmsg;
41
42 g_warning("connection freed without being closed first; any error will be fatal");
43 errmsg = directtcp_connection_close(self);
44 if (errmsg)
45 error("while closing directtcp connection: %s", errmsg);
46 }
47
48 G_OBJECT_CLASS(parent_class)->finalize(goself);
49 }
50
51 static void
52 directtcp_connection_class_init(
53 DirectTCPConnectionClass * c)
54 {
55 GObjectClass *goc = (GObjectClass *)c;
56
57 goc->finalize = directtcp_connection_finalize;
58
59 parent_class = g_type_class_peek_parent(c);
60 }
61
62 GType
63 directtcp_connection_get_type(void)
64 {
65 static GType type = 0;
66 if (G_UNLIKELY(type == 0)) {
67 static const GTypeInfo info = {
68 sizeof (DirectTCPConnectionClass),
69 (GBaseInitFunc) NULL,
70 (GBaseFinalizeFunc) NULL,
71 (GClassInitFunc) directtcp_connection_class_init,
72 (GClassFinalizeFunc) NULL,
73 NULL /* class_data */,
74 sizeof (DirectTCPConnection),
75 0 /* n_preallocs */,
76 (GInstanceInitFunc) NULL,
77 NULL
78 };
79
80 type = g_type_register_static (G_TYPE_OBJECT, "DirectTCPConnection", &info,
81 (GTypeFlags)G_TYPE_FLAG_ABSTRACT);
82 }
83 return type;
84 }
85
86 char *
87 directtcp_connection_close(
88 DirectTCPConnection *self)
89 {
90 DirectTCPConnectionClass *klass = DIRECTTCP_CONNECTION_GET_CLASS(self);
91 char *rv;
92
93 /* Note that this also tracks the 'closed' value, which is used by finalize
94 * to ensure that the connection has been closed */
95
96 g_assert(!self->closed);
97
98 g_assert(klass->close);
99 rv = klass->close(self);
100 self->closed = TRUE;
101 return rv;
102 }
103
104 /*
105 * DirectTCPConnSocket class implementation
106 */
107
108 DirectTCPConnectionSocket *
109 directtcp_connection_socket_new(
110 int socket)
111 {
112 DirectTCPConnectionSocket *conn = DIRECTTCP_CONNECTION_SOCKET(g_object_new(TYPE_DIRECTTCP_CONNECTION_SOCKET, NULL));
113 conn->socket = socket;
114 return conn;
115 }
116
117 static char *
118 directtcp_connection_socket_close(DirectTCPConnection *dself)
119 {
120 DirectTCPConnectionSocket *self = DIRECTTCP_CONNECTION_SOCKET(dself);
121 if (self->socket >= 0 && close(self->socket)) {
122 return g_strdup_printf("while closing socket: %s", strerror(errno));
123 }
124 self->socket = -1;
125
126 return NULL;
127 }
128
129 static void
130 directtcp_connection_socket_init(DirectTCPConnectionSocket *self)
131 {
132 self->socket = -1;
133 }
134
135 static void
136 directtcp_connection_socket_class_init(DirectTCPConnectionSocketClass * c)
137 {
138 DirectTCPConnectionClass *connc = (DirectTCPConnectionClass *)c;
139
140 connc->close = directtcp_connection_socket_close;
141 }
142
143 GType
144 directtcp_connection_socket_get_type (void)
145 {
146 static GType type = 0;
147
148 if (G_UNLIKELY(type == 0)) {
149 static const GTypeInfo info = {
150 sizeof (DirectTCPConnectionSocketClass),
151 (GBaseInitFunc) NULL,
152 (GBaseFinalizeFunc) NULL,
153 (GClassInitFunc) directtcp_connection_socket_class_init,
154 (GClassFinalizeFunc) NULL,
155 NULL /* class_data */,
156 sizeof (DirectTCPConnectionSocket),
157 0 /* n_preallocs */,
158 (GInstanceInitFunc) directtcp_connection_socket_init,
159 NULL
160 };
161
162 type = g_type_register_static(TYPE_DIRECTTCP_CONNECTION,
163 "DirectTCPConnectionSocket", &info, (GTypeFlags)0);
164 }
165
166 return type;
167 }