Commit 0363701c authored by Havoc Pennington's avatar Havoc Pennington

2003-01-18 Havoc Pennington <hp@pobox.com>

	* dbus/dbus-connection.c (dbus_connection_unref): disconnect the
	connection if it hasn't been already.

	* dbus/dbus-connection.h: kill off the idea of an ErrorFunction,
	replace with DisconnectFunction.
parent 6ac750b0
2003-01-18 Havoc Pennington <hp@pobox.com>
* dbus/dbus-connection.c (dbus_connection_unref): disconnect the
connection if it hasn't been already.
* dbus/dbus-connection.h: kill off the idea of an ErrorFunction,
replace with DisconnectFunction.
2003-01-18 Havoc Pennington <hp@pobox.com>
Building --disable-verbose-mode --disable-asserts --disable-tests
......
......@@ -37,21 +37,16 @@ typedef struct
#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
static void
connection_error_handler (DBusConnection *connection,
DBusResultCode error_code,
void *data)
connection_disconnect_handler (DBusConnection *connection,
void *data)
{
BusConnectionData *d;
BusService *service;
_dbus_warn ("Error on connection: %s\n",
dbus_result_to_string (error_code));
_dbus_warn ("Disconnected\n");
d = BUS_CONNECTION_DATA (connection);
_dbus_assert (d != NULL);
/* we don't want to be called again since we're dropping the connection */
dbus_connection_set_error_function (connection, NULL, NULL, NULL);
_dbus_assert (d != NULL);
/* Drop any service ownership */
while ((service = _dbus_list_get_last (&d->services_owned)))
......@@ -151,9 +146,9 @@ bus_connection_setup (DBusConnection *connection)
connection,
NULL);
dbus_connection_set_error_function (connection,
connection_error_handler,
NULL, NULL);
dbus_connection_set_disconnect_function (connection,
connection_disconnect_handler,
NULL, NULL);
return TRUE;
}
......
......@@ -55,8 +55,7 @@ void _dbus_connection_do_iteration (DBusConnection *connect
unsigned int flags,
int timeout_milliseconds);
void _dbus_connection_transport_error (DBusConnection *connection,
DBusResultCode result_code);
void _dbus_connection_notify_disconnected (DBusConnection *connection);
void _dbus_connection_handler_destroyed (DBusConnection *connection,
DBusMessageHandler *handler);
......
......@@ -83,9 +83,9 @@ struct DBusConnection
DBusTransport *transport; /**< Object that sends/receives messages over network. */
DBusWatchList *watches; /**< Stores active watches. */
DBusConnectionErrorFunction error_function; /**< Callback for errors. */
void *error_data; /**< Data for error callback. */
DBusFreeFunction error_free_data_function; /**< Free function for error callback data. */
DBusDisconnectFunction disconnect_function; /**< Callback on disconnect. */
void *disconnect_data; /**< Data for disconnect callback. */
DBusFreeFunction disconnect_free_data_function; /**< Free function for disconnect callback data. */
DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
DBusList *filter_list; /**< List of filters. */
int filters_serial; /**< Increments when the list of filters is changed. */
......@@ -94,6 +94,7 @@ struct DBusConnection
int n_slots; /**< Slots allocated so far. */
int client_serial; /**< Client serial. Increments each time a message is sent */
unsigned int disconnect_notified : 1; /**< Already called disconnect_function */
};
static void _dbus_connection_free_data_slots (DBusConnection *connection);
......@@ -215,41 +216,25 @@ _dbus_connection_remove_watch (DBusConnection *connection,
watch);
}
static void
handle_error (DBusConnection *connection,
DBusResultCode result)
{
if (result != DBUS_RESULT_SUCCESS &&
connection->error_function != NULL)
{
dbus_connection_ref (connection);
(* connection->error_function) (connection, result,
connection->error_data);
dbus_connection_unref (connection);
}
}
static void
set_result_handled (DBusConnection *connection,
DBusResultCode *result_address,
DBusResultCode result)
{
dbus_set_result (result_address, result);
handle_error (connection, result);
}
/**
* Reports a transport error to the connection. Typically
* results in an application error callback being invoked.
* Tells the connection that the transport has been disconnected.
* Results in calling the application disconnect callback.
* Only has an effect the first time it's called.
*
* @param connection the connection.
* @param result_code the error code.
* @param connection the connection
*/
void
_dbus_connection_transport_error (DBusConnection *connection,
DBusResultCode result_code)
_dbus_connection_notify_disconnected (DBusConnection *connection)
{
handle_error (connection, result_code);
if (connection->disconnect_function != NULL &&
!connection->disconnect_notified)
{
connection->disconnect_notified = TRUE;
dbus_connection_ref (connection);
(* connection->disconnect_function) (connection,
connection->disconnect_data);
dbus_connection_unref (connection);
}
}
/**
......@@ -333,6 +318,7 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
connection->data_slots = NULL;
connection->n_slots = 0;
connection->client_serial = 1;
connection->disconnect_notified = FALSE;
_dbus_transport_ref (transport);
_dbus_transport_set_connection (transport, connection);
......@@ -482,10 +468,12 @@ dbus_connection_unref (DBusConnection *connection)
{
DBusHashIter iter;
DBusList *link;
dbus_connection_disconnect (connection);
/* free error data as a side effect */
dbus_connection_set_error_function (connection,
NULL, NULL, NULL);
/* free disconnect data as a side effect */
dbus_connection_set_disconnect_function (connection,
NULL, NULL, NULL);
_dbus_watch_list_free (connection->watches);
connection->watches = NULL;
......@@ -587,7 +575,7 @@ dbus_connection_send_message (DBusConnection *connection,
if (!_dbus_list_prepend (&connection->outgoing_messages,
message))
{
set_result_handled (connection, result, DBUS_RESULT_NO_MEMORY);
dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
return FALSE;
}
......@@ -823,25 +811,27 @@ dbus_connection_dispatch_message (DBusConnection *connection)
}
/**
* Sets the error handler function for the connection.
* Sets the disconnect handler function for the connection.
* Will be called exactly once, when the connection is
* disconnected.
*
* @param connection the connection.
* @param error_function the error handler.
* @param data data to pass to the error handler.
* @param disconnect_function the disconnect handler.
* @param data data to pass to the disconnect handler.
* @param free_data_function function to be called to free the data.
*/
void
dbus_connection_set_error_function (DBusConnection *connection,
DBusConnectionErrorFunction error_function,
void *data,
DBusFreeFunction free_data_function)
dbus_connection_set_disconnect_function (DBusConnection *connection,
DBusDisconnectFunction disconnect_function,
void *data,
DBusFreeFunction free_data_function)
{
if (connection->error_free_data_function != NULL)
(* connection->error_free_data_function) (connection->error_data);
if (connection->disconnect_free_data_function != NULL)
(* connection->disconnect_free_data_function) (connection->disconnect_data);
connection->error_function = error_function;
connection->error_data = data;
connection->error_free_data_function = free_data_function;
connection->disconnect_function = disconnect_function;
connection->disconnect_data = data;
connection->disconnect_free_data_function = free_data_function;
}
/**
......
......@@ -40,7 +40,7 @@ typedef struct DBusMessageHandler DBusMessageHandler;
typedef enum
{
DBUS_HANDLER_RESULT_REMOVE_MESSAGE, /**< Remove this message, no further processing. */
DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS /**< Run any additional handlers that are interested on this message. */
DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS /**< Run any additional handlers that are interested in this message. */
} DBusHandlerResult;
typedef enum
......@@ -60,9 +60,8 @@ typedef void (* DBusAddWatchFunction) (DBusWatch *watch,
typedef void (* DBusRemoveWatchFunction) (DBusWatch *watch,
void *data);
typedef void (* DBusConnectionErrorFunction) (DBusConnection *connection,
DBusResultCode error_code,
void *data);
typedef void (* DBusDisconnectFunction) (DBusConnection *connection,
void *data);
DBusConnection* dbus_connection_open (const char *address,
DBusResultCode *result);
......@@ -86,18 +85,19 @@ dbus_bool_t dbus_connection_send_message_with_reply (DBusConnection *connect
int timeout_milliseconds,
DBusResultCode *result);
void dbus_connection_set_error_function (DBusConnection *connection,
DBusConnectionErrorFunction error_function,
void *data,
DBusFreeFunction free_data_function);
void dbus_connection_set_watch_functions (DBusConnection *connection,
DBusAddWatchFunction add_function,
DBusRemoveWatchFunction remove_function,
void *data,
DBusFreeFunction free_data_function);
void dbus_connection_handle_watch (DBusConnection *connection,
DBusWatch *watch,
unsigned int condition);
void dbus_connection_set_disconnect_function (DBusConnection *connection,
DBusDisconnectFunction function,
void *data,
DBusFreeFunction free_data_function);
void dbus_connection_set_watch_functions (DBusConnection *connection,
DBusAddWatchFunction add_function,
DBusRemoveWatchFunction remove_function,
void *data,
DBusFreeFunction free_data_function);
void dbus_connection_handle_watch (DBusConnection *connection,
DBusWatch *watch,
unsigned int condition);
int dbus_watch_get_fd (DBusWatch *watch);
unsigned int dbus_watch_get_flags (DBusWatch *watch);
......
......@@ -166,8 +166,6 @@ do_io_error (DBusTransport *transport)
{
_dbus_transport_ref (transport);
_dbus_transport_disconnect (transport);
_dbus_connection_transport_error (transport->connection,
DBUS_RESULT_DISCONNECTED);
_dbus_transport_unref (transport);
}
......@@ -769,8 +767,6 @@ unix_handle_watch (DBusTransport *transport,
if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
{
_dbus_transport_disconnect (transport);
_dbus_connection_transport_error (transport->connection,
DBUS_RESULT_DISCONNECTED);
return;
}
......
......@@ -215,8 +215,7 @@ _dbus_transport_disconnect (DBusTransport *transport)
transport->disconnected = TRUE;
_dbus_connection_transport_error (transport->connection,
DBUS_RESULT_DISCONNECTED);
_dbus_connection_notify_disconnected (transport->connection);
DBUS_TRANSPORT_RELEASE_REF (transport);
}
......@@ -308,11 +307,7 @@ _dbus_transport_handle_watch (DBusTransport *transport,
_dbus_assert (transport->vtable->handle_watch != NULL);
if (transport->disconnected)
{
_dbus_connection_transport_error (transport->connection,
DBUS_RESULT_DISCONNECTED);
return;
}
return;
if (dbus_watch_get_fd (watch) < 0)
{
......@@ -367,11 +362,7 @@ _dbus_transport_messages_pending (DBusTransport *transport,
_dbus_assert (transport->vtable->messages_pending != NULL);
if (transport->disconnected)
{
_dbus_connection_transport_error (transport->connection,
DBUS_RESULT_DISCONNECTED);
return;
}
return;
transport->messages_need_sending = queue_length > 0;
......@@ -404,11 +395,7 @@ _dbus_transport_do_iteration (DBusTransport *transport,
return; /* Nothing to do */
if (transport->disconnected)
{
_dbus_connection_transport_error (transport->connection,
DBUS_RESULT_DISCONNECTED);
return;
}
return;
DBUS_TRANSPORT_HOLD_REF (transport);
(* transport->vtable->do_iteration) (transport, flags,
......
......@@ -297,16 +297,10 @@ quit_mainloop (void)
}
static void
error_handler (DBusConnection *connection,
DBusResultCode error_code,
void *data)
disconnect_handler (DBusConnection *connection,
void *data)
{
fprintf (stderr,
"Error on connection: %s\n",
dbus_result_to_string (error_code));
/* we don't want to be called again since we're dropping the connection */
dbus_connection_set_error_function (connection, NULL, NULL, NULL);
fprintf (stderr, "Disconnected\n");
_dbus_list_remove (&connections, connection);
dbus_connection_unref (connection);
......@@ -322,9 +316,9 @@ setup_connection (DBusConnection *connection)
connection,
NULL);
dbus_connection_set_error_function (connection,
error_handler,
NULL, NULL);
dbus_connection_set_disconnect_function (connection,
disconnect_handler,
NULL, NULL);
dbus_connection_ref (connection);
_dbus_list_append (&connections, connection);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment