summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann <sandmann@redhat.com>2007-07-31 20:33:58 -0400
committerSøren Sandmann <sandmann@redhat.com>2007-07-31 20:33:58 -0400
commit2d6326cf8bd33604f6d8097e55a7d9165a5acaa7 (patch)
tree399bac8964b7b3c88c2a4bf4350c2d5d41085b79
parent3d57353ca2529d8ed609298939708dacfedc6d89 (diff)
Add LacGenConnection
-rw-r--r--src/Makefile.am2
-rw-r--r--src/lac.h32
-rw-r--r--src/lacgenconnection.c187
-rw-r--r--src/lactlsconnection.c2
4 files changed, 222 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index d6ab701..489571a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,7 +21,9 @@ liblac_1_la_SOURCES = \
lacuri.c \
lacconnection.c \
lactlsconnection.c \
+ lacgenconnection.c \
lachttp.c \
+ lacimap.c \
\
lacinternals.h \
lacdns-messages.h \
diff --git a/src/lac.h b/src/lac.h
index 92e05b1..8d60c99 100644
--- a/src/lac.h
+++ b/src/lac.h
@@ -357,6 +357,38 @@ gboolean lac_tls_connection_is_connected (LacTlsConnection
void lac_tls_connection_flush (LacTlsConnection *connection);
/*
+ * LacGenConnection
+ */
+typedef struct _LacGenConnection LacGenConnection;
+
+typedef void (* LacGenConnectionFunc) (LacGenConnection *connection,
+ const LacConnectionEvent *event);
+
+LacGenConnection * lac_gen_connection_new_tcp (const LacAddress *address,
+ gint port,
+ LacGenConnectionFunc callback,
+ gpointer data);
+LacGenConnection * lac_gen_connection_new_tls (const LacAddress *address,
+ gint port,
+ LacGenConnectionFunc callback,
+ gpointer data);
+gpointer lac_gen_connection_get_data (LacGenConnection *connection);
+void lac_gen_connection_write (LacGenConnection *connection,
+ const gchar *data,
+ guint len);
+void lac_gen_connection_write_cstr (LacGenConnection *connection,
+ const gchar *data);
+void lac_gen_connection_shutdown_write (LacGenConnection *connection);
+LacGenConnection * lac_gen_connection_ref (LacGenConnection *connection);
+void lac_gen_connection_unref (LacGenConnection *connection);
+G_CONST_RETURN LacAddress *lac_gen_connection_get_address (LacGenConnection *connection);
+gint lac_gen_connection_get_port (LacGenConnection *connection);
+void lac_gen_connection_close (LacGenConnection *connection);
+gboolean lac_gen_connection_is_connected (LacGenConnection *connection);
+void lac_gen_connection_flush (LacGenConnection *connection);
+
+
+/*
* URI
*/
typedef enum {
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);
+}
diff --git a/src/lactlsconnection.c b/src/lactlsconnection.c
index 9ca6334..3485e19 100644
--- a/src/lactlsconnection.c
+++ b/src/lactlsconnection.c
@@ -1,7 +1,7 @@
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */
/* Lac - Library for asynchronous communication
- * Copyright (C) 2000, 2001, 2002, 2003 Søren Sandmann
+ * 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