/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */ /* Lac - Library for asynchronous communication * Copyright (C) 2000, 2001, 2002, 2003, 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 #include "lac.h" #include "lacinternals.h" /* * Prototypes for a primitive abstraction covering TCP and SSL */ typedef enum { UNDEFINED, CONNECT_IN_PROGRESS, CONNECTED, DISCONNECTED, } ConnectionState; struct _LacTcpConnection { int fd; LacTcpConnectionFunc callback; gpointer data; GQueue * pending_events; ConnectionState state; nul_buffer_t * unwritten; gboolean has_fd; gboolean in_emit_events; gboolean need_flush; gint ref_count; }; static void event_free (LacConnectionEvent *event) { if (event->type == LAC_CONNECTION_EVENT_READ) { /* FIXME: at some point we should support pushing * data back into the buffer, ie., it must be kept alive */ nul_buffer_free (event->read.buffer, TRUE); } else if (event->type == LAC_CONNECTION_EVENT_ERROR) g_error_free ((GError *)event->error.err); g_free (event); } static void queue_connect (LacTcpConnection *connection) { LacConnectionEvent *event = g_new (LacConnectionEvent, 1); event->type = LAC_CONNECTION_EVENT_CONNECT; g_queue_push_tail (connection->pending_events, event); } static void queue_read (LacTcpConnection *connection, nul_buffer_t *buffer) { LacConnectionEvent *event = g_new (LacConnectionEvent, 1); event->type = LAC_CONNECTION_EVENT_READ; event->read.buffer = buffer; event->read.data = nul_buffer_peek (buffer, &(event->read.len)); g_queue_push_tail (connection->pending_events, event); } static void queue_close (LacTcpConnection *connection, gboolean remote) { LacConnectionEvent *event = g_new (LacConnectionEvent, 1); event->type = LAC_CONNECTION_EVENT_CLOSE; event->close.remote_closed = remote; g_queue_push_tail (connection->pending_events, event); } static void queue_error (LacTcpConnection *connection, const GError *err) { LacConnectionEvent *event = g_new (LacConnectionEvent, 1); event->type = LAC_CONNECTION_EVENT_ERROR; event->error.err = g_error_copy (err); g_queue_push_tail (connection->pending_events, event); } static void emit_events (LacTcpConnection *connection) { LacConnectionEvent *event; if (connection->in_emit_events) return; connection->in_emit_events = TRUE; lac_tcp_connection_ref (connection); while ((event = g_queue_pop_head (connection->pending_events))) { if (connection->state != DISCONNECTED) { if (event->type == LAC_CONNECTION_EVENT_CLOSE || event->type == LAC_CONNECTION_EVENT_ERROR) { connection->state = DISCONNECTED; if (connection->has_fd) { lac_fd_remove_watch (connection->fd); lac_close (connection->fd, NULL); connection->has_fd = FALSE; } } connection->callback (connection, event); } event_free (event); } connection->in_emit_events = FALSE; lac_tcp_connection_unref (connection); } static gboolean emit_events_idle_handler (gpointer data) { LacTcpConnection *connection = data; emit_events (connection); lac_tcp_connection_unref (connection); return FALSE; /* don't call me again */ } static void emit_events_idle (LacTcpConnection *connection) { if (!g_queue_is_empty (connection->pending_events)) { lac_tcp_connection_ref (connection); g_idle_add ( emit_events_idle_handler, connection); } } static void lac_tcp_connection_do_reads (gpointer data) { LacTcpConnection *connection = data; gint len; enum { BUF_SIZE = 8280 }; do { GError *err = NULL; nul_buffer_t *buffer = nul_buffer_new (); gchar *buf = nul_buffer_alloc_tail (buffer, BUF_SIZE); len = lac_recv (connection->fd, buf, BUF_SIZE, &err); if (len > 0) { nul_buffer_delete_tail (buffer, BUF_SIZE - len); queue_read (connection, buffer); } else { nul_buffer_free (buffer, TRUE); if (len == 0) { queue_close (connection, TRUE); } else { g_assert (err); if (!g_error_matches ( err, LAC_SOCKET_ERROR, LAC_SOCKET_ERROR_AGAIN)) { queue_error (connection, err); } g_error_free (err); } } /* FIXME: check that we haven't used too much time? */ } while (len == BUF_SIZE); } static void lac_tcp_connection_writable (gpointer data); static void lac_tcp_connection_do_writes (LacTcpConnection *connection) { if (connection->state != CONNECTED) { lac_fd_set_write_callback (connection->fd, lac_tcp_connection_writable); return; } while (nul_buffer_get_length (connection->unwritten) > 0) { GError *err = NULL; const gchar *unwritten; gsize len, sent; unwritten = nul_buffer_peek (connection->unwritten, &len); sent = lac_send (connection->fd, unwritten, len, &err); if (err) { if (g_error_matches ( err, LAC_SOCKET_ERROR, LAC_SOCKET_ERROR_AGAIN)) { lac_fd_set_write_callback ( connection->fd, lac_tcp_connection_writable); } else { queue_error (connection, err); } g_error_free (err); return; } nul_buffer_delete_head (connection->unwritten, sent); /* FIXME check that we haven't used too much time? */ } lac_fd_set_write_callback (connection->fd, NULL); if (connection->need_flush) { lac_set_nagle (connection->fd, FALSE, NULL); lac_set_nagle (connection->fd, TRUE, NULL); connection->need_flush = FALSE; } } static void lac_tcp_connection_readable (gpointer data) { LacTcpConnection *connection = data; lac_tcp_connection_do_reads (connection); emit_events (connection); } static void lac_tcp_connection_writable (gpointer data) { LacTcpConnection *connection = data; if (connection->state == CONNECT_IN_PROGRESS) { connection->state = CONNECTED; queue_connect (connection); } lac_tcp_connection_do_writes (connection); emit_events (connection); } static void lac_tcp_connection_connect (LacTcpConnection *connection, const LacAddress *address, int port) { GError *err = NULL; lac_connect (connection->fd, address, port, &err); if (err) { if (g_error_matches ( err, LAC_SOCKET_ERROR, LAC_SOCKET_ERROR_IN_PROGRESS)) { connection->state = CONNECT_IN_PROGRESS; } else { queue_error (connection, err); } g_error_free (err); } else { connection->state = CONNECTED; queue_connect (connection); lac_tcp_connection_do_writes (connection); } } static void lac_tcp_connection_discard_pending_events (LacTcpConnection *connection) { LacConnectionEvent *event; while ((event = g_queue_pop_head (connection->pending_events))) event_free (event); } static void lac_tcp_connection_add_watch (LacTcpConnection *connection) { lac_fd_add_watch (connection->fd, connection); lac_fd_set_read_callback (connection->fd, lac_tcp_connection_readable); lac_fd_set_write_callback (connection->fd, lac_tcp_connection_writable); } LacTcpConnection * lac_tcp_connection_new (const LacAddress *address, gint port, LacTcpConnectionFunc callback, gpointer data) { LacTcpConnection *connection; GError *err = NULL; g_return_val_if_fail (address != NULL, NULL); g_return_val_if_fail (port > 0, NULL); g_return_val_if_fail (callback != NULL, NULL); connection = g_new (LacTcpConnection, 1); connection->ref_count = 1; connection->callback = callback; connection->data = data; connection->pending_events = g_queue_new (); connection->state = UNDEFINED; connection->has_fd = FALSE; connection->unwritten = nul_buffer_new (); connection->in_emit_events = FALSE; connection->need_flush = FALSE; connection->fd = lac_socket_tcp (&err); if (err) { queue_error (connection, err); emit_events_idle (connection); g_error_free (err); return connection; } connection->has_fd = TRUE; lac_set_blocking (connection->fd, FALSE, &err); if (err) { queue_error (connection, err); emit_events_idle (connection); g_error_free (err); return connection; } lac_tcp_connection_add_watch (connection); lac_tcp_connection_connect (connection, address, port); emit_events_idle (connection); return connection; } gpointer lac_tcp_connection_get_data (LacTcpConnection *connection) { g_return_val_if_fail (connection != NULL, NULL); return connection->data; } void lac_tcp_connection_write (LacTcpConnection *connection, const gchar *data, guint len) { gboolean do_writes; g_return_if_fail (connection != NULL); g_return_if_fail (data != NULL); g_return_if_fail (connection->state != DISCONNECTED); if (len == 0) return; do_writes = (nul_buffer_get_length (connection->unwritten) == 0); nul_buffer_append (connection->unwritten, data, len); if (do_writes) { lac_tcp_connection_do_writes (connection); emit_events (connection); } } void lac_tcp_connection_close (LacTcpConnection *connection) { g_return_if_fail (connection != NULL); lac_tcp_connection_discard_pending_events (connection); queue_close (connection, FALSE); emit_events (connection); } LacTcpConnection * lac_tcp_connection_ref (LacTcpConnection *connection) { ++connection->ref_count; return connection; } void lac_tcp_connection_unref (LacTcpConnection *connection) { if (--connection->ref_count == 0) { lac_tcp_connection_discard_pending_events (connection); g_queue_free (connection->pending_events); nul_buffer_free (connection->unwritten, TRUE); if (connection->has_fd) { lac_fd_remove_watch (connection->fd); lac_close (connection->fd, NULL); } g_free (connection); } } gboolean lac_tcp_connection_is_connected (LacTcpConnection *connection) { return connection->state == CONNECTED; } void lac_tcp_connection_flush (LacTcpConnection *connection) { connection->need_flush = TRUE; if (nul_buffer_get_length (connection->unwritten) == 0) lac_tcp_connection_do_writes (connection); }