diff options
Diffstat (limited to 'src/lacgenconnection.c')
-rw-r--r-- | src/lacgenconnection.c | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/lacgenconnection.c b/src/lacgenconnection.c new file mode 100644 index 0000000..4c77144 --- /dev/null +++ b/src/lacgenconnection.c @@ -0,0 +1,187 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */ + +/* Lac - Library for asynchronous communication + * Copyright (C) 2007 Søren Sandmann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <string.h> +#include "lac.h" + +struct _LacGenConnection +{ + gboolean is_tcp; + + union + { + LacConnection *tcp; + LacTlsConnection *tls; + } u; + + LacGenConnectionFunc func; + gpointer data; + int ref_count; +}; + +static void +on_tcp_event (LacConnection *connection, + const LacConnectionEvent *event) +{ + LacGenConnection *gen = lac_connection_get_data (connection); + + gen->func (gen, event); +} + +static void +on_ssl_event (LacTlsConnection *connection, + const LacConnectionEvent *event) +{ + LacGenConnection *gen = lac_tls_connection_get_data (connection); + + gen->func (gen, event); +} + +LacGenConnection * +lac_gen_connection_new_tcp (const LacAddress *address, + gint port, + LacGenConnectionFunc callback, + gpointer data) +{ + LacGenConnection *connection = g_new0 (LacGenConnection, 1); + + connection->u.tcp = lac_connection_new (address, port, on_tcp_event, connection); + connection->ref_count = 1; + + return connection; +} + +LacGenConnection * +lac_gen_connection_new_tls (const LacAddress *address, + gint port, + LacGenConnectionFunc callback, + gpointer data) +{ + LacGenConnection *connection = g_new0 (LacGenConnection, 1); + + connection->u.tls = lac_tls_connection_new (address, port, on_ssl_event, connection); + connection->ref_count = 1; + + return connection; +} + +static void +lac_gen_connection_destroy (LacGenConnection *connection) +{ + if (connection->is_tcp) + lac_connection_unref (connection->u.tcp); + else + lac_tls_connection_unref (connection->u.tls); + + g_free (connection); +} + +LacGenConnection * +lac_gen_connection_ref (LacGenConnection *connection) +{ + connection->ref_count++; + + return connection; +} + +void +lac_gen_connection_unref (LacGenConnection *connection) +{ + if (--connection->ref_count == 0) + lac_gen_connection_destroy (connection); +} + +gpointer +lac_gen_connection_get_data (LacGenConnection *connection) +{ + return connection->data; +} + +void +lac_gen_connection_write (LacGenConnection *connection, + const gchar *data, + guint len) +{ + if (connection->is_tcp) + lac_connection_write (connection->u.tcp, data, len); + else + lac_tls_connection_write (connection->u.tls, data, len); +} + +void +lac_gen_connection_write_cstr (LacGenConnection *connection, + const gchar *data) +{ + gsize len; + + g_return_if_fail (connection != NULL); + g_return_if_fail (data != NULL); + + len = strlen (data); + + if (len > 0) + lac_gen_connection_write (connection, data, len); +} + +G_CONST_RETURN LacAddress * +lac_gen_connection_get_address (LacGenConnection *connection) +{ + if (connection->is_tcp) + return lac_connection_get_address (connection->u.tcp); + else + return lac_tls_connection_get_address (connection->u.tls); +} + +gint +lac_gen_connection_get_port (LacGenConnection *connection) +{ + if (connection->is_tcp) + return lac_connection_get_port (connection->u.tcp); + else + return lac_tls_connection_get_port (connection->u.tls); +} + +void +lac_gen_connection_close (LacGenConnection *connection) +{ + if (connection->is_tcp) + lac_connection_close (connection->u.tcp); + else + lac_tls_connection_close (connection->u.tls); +} + +gboolean +lac_gen_connection_is_connected (LacGenConnection *connection) +{ + if (connection->is_tcp) + return lac_connection_is_connected (connection->u.tcp); + else + return lac_tls_connection_is_connected (connection->u.tls); +} + +void +lac_gen_connection_flush (LacGenConnection *connection) +{ + if (connection->is_tcp) + lac_connection_flush (connection->u.tcp); + else + lac_tls_connection_flush (connection->u.tls); +} |