2 * Copyright (c) 2009 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 N Mathlida Ave, Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
22 #include "directtcp-connection.h"
23 #include <glib-object.h>
26 * DirectTCPConnection class implementation
29 static GObjectClass *parent_class = NULL;
32 directtcp_connection_finalize(GObject *goself)
34 DirectTCPConnection *self = DIRECTTCP_CONNECTION(goself);
36 /* close this socket if necessary, failing fatally if that doesn't work */
40 g_warning("connection freed without being closed first; any error will be fatal");
41 errmsg = directtcp_connection_close(self);
43 error("while closing directtcp connection: %s", errmsg);
46 G_OBJECT_CLASS(parent_class)->finalize(goself);
50 directtcp_connection_class_init(
51 DirectTCPConnectionClass * c)
53 GObjectClass *goc = (GObjectClass *)c;
55 goc->finalize = directtcp_connection_finalize;
57 parent_class = g_type_class_peek_parent(c);
61 directtcp_connection_get_type(void)
63 static GType type = 0;
64 if G_UNLIKELY(type == 0) {
65 static const GTypeInfo info = {
66 sizeof (DirectTCPConnectionClass),
68 (GBaseFinalizeFunc) NULL,
69 (GClassInitFunc) directtcp_connection_class_init,
70 (GClassFinalizeFunc) NULL,
71 NULL /* class_data */,
72 sizeof (DirectTCPConnection),
74 (GInstanceInitFunc) NULL,
78 type = g_type_register_static (G_TYPE_OBJECT, "DirectTCPConnection", &info,
79 (GTypeFlags)G_TYPE_FLAG_ABSTRACT);
85 directtcp_connection_close(
86 DirectTCPConnection *self)
88 DirectTCPConnectionClass *klass = DIRECTTCP_CONNECTION_GET_CLASS(self);
91 /* Note that this also tracks the 'closed' value, which is used by finalize
92 * to ensure that the connection has been closed */
94 g_assert(!self->closed);
96 g_assert(klass->close);
97 rv = klass->close(self);
103 * DirectTCPConnSocket class implementation
106 DirectTCPConnectionSocket *
107 directtcp_connection_socket_new(
110 DirectTCPConnectionSocket *conn = DIRECTTCP_CONNECTION_SOCKET(g_object_new(TYPE_DIRECTTCP_CONNECTION_SOCKET, NULL));
111 conn->socket = socket;
116 directtcp_connection_socket_close(DirectTCPConnection *dself)
118 DirectTCPConnectionSocket *self = DIRECTTCP_CONNECTION_SOCKET(dself);
119 if (self->socket >= 0 && close(self->socket)) {
120 return g_strdup_printf("while closing socket: %s", strerror(errno));
128 directtcp_connection_socket_init(DirectTCPConnectionSocket *self)
134 directtcp_connection_socket_class_init(DirectTCPConnectionSocketClass * c)
136 DirectTCPConnectionClass *connc = (DirectTCPConnectionClass *)c;
138 connc->close = directtcp_connection_socket_close;
142 directtcp_connection_socket_get_type (void)
144 static GType type = 0;
146 if G_UNLIKELY(type == 0) {
147 static const GTypeInfo info = {
148 sizeof (DirectTCPConnectionSocketClass),
149 (GBaseInitFunc) NULL,
150 (GBaseFinalizeFunc) NULL,
151 (GClassInitFunc) directtcp_connection_socket_class_init,
152 (GClassFinalizeFunc) NULL,
153 NULL /* class_data */,
154 sizeof (DirectTCPConnectionSocket),
156 (GInstanceInitFunc) directtcp_connection_socket_init,
160 type = g_type_register_static(TYPE_DIRECTTCP_CONNECTION,
161 "DirectTCPConnectionSocket", &info, (GTypeFlags)0);