1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-connection.h"
25 #include "dbus-list.h"
26 #include "dbus-transport.h"
27 #include "dbus-watch.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-list.h"
30 #include "dbus-hash.h"
31 #include "dbus-message-internal.h"
32 #include "dbus-threads.h"
35 * @defgroup DBusConnection DBusConnection
37 * @brief Connection to another application
39 * A DBusConnection represents a connection to another
40 * application. Messages can be sent and received via this connection.
42 * The connection maintains a queue of incoming messages and a queue
43 * of outgoing messages. dbus_connection_pop_message() and friends
44 * can be used to read incoming messages from the queue.
45 * Outgoing messages are automatically discarded as they are
46 * written to the network.
48 * In brief a DBusConnection is a message queue associated with some
49 * message transport mechanism such as a socket.
54 * @defgroup DBusConnectionInternals DBusConnection implementation details
55 * @ingroup DBusInternals
56 * @brief Implementation details of DBusConnection
61 /** Opaque typedef for DBusDataSlot */
62 typedef struct DBusDataSlot DBusDataSlot;
63 /** DBusDataSlot is used to store application data on the connection */
66 void *data; /**< The application data */
67 DBusFreeFunction free_data_func; /**< Free the application data */
71 * Implementation details of DBusConnection. All fields are private.
75 int refcount; /**< Reference count. */
77 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
78 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
80 int n_outgoing; /**< Length of outgoing queue. */
81 int n_incoming; /**< Length of incoming queue. */
83 DBusTransport *transport; /**< Object that sends/receives messages over network. */
84 DBusWatchList *watches; /**< Stores active watches. */
86 DBusConnectionErrorFunction error_function; /**< Callback for errors. */
87 void *error_data; /**< Data for error callback. */
88 DBusFreeFunction error_free_data_function; /**< Free function for error callback data. */
89 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
90 DBusList *filter_list; /**< List of filters. */
91 int filters_serial; /**< Increments when the list of filters is changed. */
92 int handlers_serial; /**< Increments when the handler table is changed. */
93 DBusDataSlot *data_slots; /**< Data slots */
94 int n_slots; /**< Slots allocated so far. */
97 static void _dbus_connection_free_data_slots (DBusConnection *connection);
100 * Adds a message to the incoming message queue, returning #FALSE
101 * if there's insufficient memory to queue the message.
103 * @param connection the connection.
104 * @param message the message to queue.
105 * @returns #TRUE on success.
108 _dbus_connection_queue_received_message (DBusConnection *connection,
109 DBusMessage *message)
111 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
113 if (!_dbus_list_append (&connection->incoming_messages,
117 dbus_message_ref (message);
118 connection->n_incoming += 1;
120 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
121 message, connection->n_incoming);
127 * Checks whether there are messages in the outgoing message queue.
129 * @param connection the connection.
130 * @returns #TRUE if the outgoing queue is non-empty.
133 _dbus_connection_have_messages_to_send (DBusConnection *connection)
135 return connection->outgoing_messages != NULL;
139 * Gets the next outgoing message. The message remains in the
140 * queue, and the caller does not own a reference to it.
142 * @param connection the connection.
143 * @returns the message to be sent.
146 _dbus_connection_get_message_to_send (DBusConnection *connection)
148 return _dbus_list_get_last (&connection->outgoing_messages);
152 * Notifies the connection that a message has been sent, so the
153 * message can be removed from the outgoing queue.
155 * @param connection the connection.
156 * @param message the message that was sent.
159 _dbus_connection_message_sent (DBusConnection *connection,
160 DBusMessage *message)
162 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
163 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
165 _dbus_list_pop_last (&connection->outgoing_messages);
166 dbus_message_unref (message);
168 connection->n_outgoing -= 1;
170 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
171 message, connection->n_outgoing);
173 if (connection->n_outgoing == 0)
174 _dbus_transport_messages_pending (connection->transport,
175 connection->n_outgoing);
179 * Adds a watch using the connection's DBusAddWatchFunction if
180 * available. Otherwise records the watch to be added when said
181 * function is available. Also re-adds the watch if the
182 * DBusAddWatchFunction changes. May fail due to lack of memory.
184 * @param connection the connection.
185 * @param watch the watch to add.
186 * @returns #TRUE on success.
189 _dbus_connection_add_watch (DBusConnection *connection,
192 if (connection->watches) /* null during finalize */
193 return _dbus_watch_list_add_watch (connection->watches,
200 * Removes a watch using the connection's DBusRemoveWatchFunction
201 * if available. It's an error to call this function on a watch
202 * that was not previously added.
204 * @param connection the connection.
205 * @param watch the watch to remove.
208 _dbus_connection_remove_watch (DBusConnection *connection,
211 if (connection->watches) /* null during finalize */
212 _dbus_watch_list_remove_watch (connection->watches,
217 handle_error (DBusConnection *connection,
218 DBusResultCode result)
220 if (result != DBUS_RESULT_SUCCESS &&
221 connection->error_function != NULL)
223 dbus_connection_ref (connection);
224 (* connection->error_function) (connection, result,
225 connection->error_data);
226 dbus_connection_unref (connection);
231 set_result_handled (DBusConnection *connection,
232 DBusResultCode *result_address,
233 DBusResultCode result)
235 dbus_set_result (result_address, result);
236 handle_error (connection, result);
240 * Reports a transport error to the connection. Typically
241 * results in an application error callback being invoked.
243 * @param connection the connection.
244 * @param result_code the error code.
247 _dbus_connection_transport_error (DBusConnection *connection,
248 DBusResultCode result_code)
250 handle_error (connection, result_code);
254 * Queues incoming messages and sends outgoing messages for this
255 * connection, optionally blocking in the process. Each call to
256 * _dbus_connection_do_iteration() will call select() or poll() one
257 * time and then read or write data if possible.
259 * The purpose of this function is to be able to flush outgoing
260 * messages or queue up incoming messages without returning
261 * control to the application and causing reentrancy weirdness.
263 * The flags parameter allows you to specify whether to
264 * read incoming messages, write outgoing messages, or both,
265 * and whether to block if no immediate action is possible.
267 * The timeout_milliseconds parameter does nothing unless the
268 * iteration is blocking.
270 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
271 * wasn't specified, then it's impossible to block, even if
272 * you specify DBUS_ITERATION_BLOCK; in that case the function
273 * returns immediately.
275 * @param connection the connection.
276 * @param flags iteration flags.
277 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
280 _dbus_connection_do_iteration (DBusConnection *connection,
282 int timeout_milliseconds)
284 if (connection->n_outgoing == 0)
285 flags &= ~DBUS_ITERATION_DO_WRITING;
287 _dbus_transport_do_iteration (connection->transport,
288 flags, timeout_milliseconds);
292 * Creates a new connection for the given transport. A transport
293 * represents a message stream that uses some concrete mechanism, such
294 * as UNIX domain sockets. May return #NULL if insufficient
295 * memory exists to create the connection.
297 * @param transport the transport.
298 * @returns the new connection, or #NULL on failure.
301 _dbus_connection_new_for_transport (DBusTransport *transport)
303 DBusConnection *connection;
304 DBusWatchList *watch_list;
305 DBusHashTable *handler_table;
309 handler_table = NULL;
311 watch_list = _dbus_watch_list_new ();
312 if (watch_list == NULL)
316 _dbus_hash_table_new (DBUS_HASH_STRING,
318 if (handler_table == NULL)
321 connection = dbus_new0 (DBusConnection, 1);
322 if (connection == NULL)
325 connection->refcount = 1;
326 connection->transport = transport;
327 connection->watches = watch_list;
328 connection->handler_table = handler_table;
329 connection->filter_list = NULL;
331 connection->data_slots = NULL;
332 connection->n_slots = 0;
334 _dbus_transport_ref (transport);
335 _dbus_transport_set_connection (transport, connection);
341 if (connection != NULL)
342 dbus_free (connection);
345 _dbus_hash_table_unref (handler_table);
348 _dbus_watch_list_free (watch_list);
354 * Used to notify a connection when a DBusMessageHandler is
355 * destroyed, so the connection can drop any reference
358 * @param connection the connection
359 * @param handler the handler
362 _dbus_connection_handler_destroyed (DBusConnection *connection,
363 DBusMessageHandler *handler)
368 _dbus_hash_iter_init (connection->handler_table, &iter);
369 while (_dbus_hash_iter_next (&iter))
371 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
374 _dbus_hash_iter_remove_entry (&iter);
377 link = _dbus_list_get_first_link (&connection->filter_list);
380 DBusMessageHandler *h = link->data;
381 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
384 _dbus_list_remove_link (&connection->filter_list,
394 * @addtogroup DBusConnection
400 * Opens a new connection to a remote address.
402 * @todo specify what the address parameter is. Right now
403 * it's just the name of a UNIX domain socket. It should be
404 * something more complex that encodes which transport to use.
406 * If the open fails, the function returns #NULL, and provides
407 * a reason for the failure in the result parameter. Pass
408 * #NULL for the result parameter if you aren't interested
409 * in the reason for failure.
411 * @param address the address.
412 * @param result address where a result code can be returned.
413 * @returns new connection, or #NULL on failure.
416 dbus_connection_open (const char *address,
417 DBusResultCode *result)
419 DBusConnection *connection;
420 DBusTransport *transport;
422 transport = _dbus_transport_open (address, result);
423 if (transport == NULL)
426 connection = _dbus_connection_new_for_transport (transport);
428 _dbus_transport_unref (transport);
430 if (connection == NULL)
432 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
440 * Increments the reference count of a DBusConnection.
442 * @param connection the connection.
445 dbus_connection_ref (DBusConnection *connection)
447 connection->refcount += 1;
451 * Decrements the reference count of a DBusConnection, and finalizes
452 * it if the count reaches zero. If a connection is still connected
453 * when it's finalized, it will be disconnected (that is, associated
454 * file handles will be closed).
456 * @param connection the connection.
459 dbus_connection_unref (DBusConnection *connection)
461 _dbus_assert (connection != NULL);
462 _dbus_assert (connection->refcount > 0);
464 connection->refcount -= 1;
465 if (connection->refcount == 0)
470 /* free error data as a side effect */
471 dbus_connection_set_error_function (connection,
474 _dbus_watch_list_free (connection->watches);
475 connection->watches = NULL;
477 _dbus_connection_free_data_slots (connection);
479 _dbus_hash_iter_init (connection->handler_table, &iter);
480 while (_dbus_hash_iter_next (&iter))
482 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
484 _dbus_message_handler_remove_connection (h, connection);
487 link = _dbus_list_get_first_link (&connection->filter_list);
490 DBusMessageHandler *h = link->data;
491 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
493 _dbus_message_handler_remove_connection (h, connection);
498 _dbus_hash_table_unref (connection->handler_table);
499 connection->handler_table = NULL;
501 _dbus_list_clear (&connection->filter_list);
503 _dbus_list_foreach (&connection->outgoing_messages,
504 (DBusForeachFunction) dbus_message_unref,
506 _dbus_list_clear (&connection->outgoing_messages);
508 _dbus_list_foreach (&connection->incoming_messages,
509 (DBusForeachFunction) dbus_message_unref,
511 _dbus_list_clear (&connection->incoming_messages);
513 _dbus_transport_unref (connection->transport);
515 dbus_free (connection);
520 * Closes the connection, so no further data can be sent or received.
521 * Any further attempts to send data will result in errors. This
522 * function does not affect the connection's reference count. It's
523 * safe to disconnect a connection more than once; all calls after the
524 * first do nothing. It's impossible to "reconnect" a connection, a
525 * new connection must be created.
527 * @param connection the connection.
530 dbus_connection_disconnect (DBusConnection *connection)
532 _dbus_transport_disconnect (connection->transport);
536 * Gets whether the connection is currently connected. All
537 * connections are connected when they are opened. A connection may
538 * become disconnected when the remote application closes its end, or
539 * exits; a connection may also be disconnected with
540 * dbus_connection_disconnect().
542 * @param connection the connection.
543 * @returns #TRUE if the connection is still alive.
546 dbus_connection_get_is_connected (DBusConnection *connection)
548 return _dbus_transport_get_is_connected (connection->transport);
552 * Adds a message to the outgoing message queue. Does not block to
553 * write the message to the network; that happens asynchronously. to
554 * force the message to be written, call dbus_connection_flush().
556 * If the function fails, it returns #FALSE and returns the
557 * reason for failure via the result parameter.
558 * The result parameter can be #NULL if you aren't interested
559 * in the reason for the failure.
561 * @param connection the connection.
562 * @param message the message to write.
563 * @param result address where result code can be placed.
564 * @returns #TRUE on success.
567 dbus_connection_send_message (DBusConnection *connection,
568 DBusMessage *message,
569 DBusResultCode *result)
571 if (!_dbus_list_prepend (&connection->outgoing_messages,
574 set_result_handled (connection, result, DBUS_RESULT_NO_MEMORY);
578 dbus_message_ref (message);
579 connection->n_outgoing += 1;
581 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
582 message, connection->n_outgoing);
584 _dbus_message_lock (message);
586 if (connection->n_outgoing == 1)
587 _dbus_transport_messages_pending (connection->transport,
588 connection->n_outgoing);
594 * Queues a message to send, as with dbus_connection_send_message(),
595 * but also sets up a DBusMessageHandler to receive a reply to the
596 * message. If no reply is received in the given timeout_milliseconds,
597 * expires the pending reply and sends the DBusMessageHandler a
598 * synthetic error reply (generated in-process, not by the remote
599 * application) indicating that a timeout occurred.
601 * Reply handlers see their replies after message filters see them,
602 * but before message handlers added with
603 * dbus_connection_register_handler() see them, regardless of the
604 * reply message's name. Reply handlers are only handed a single
605 * message as a reply, after a reply has been seen the handler is
606 * removed. If a filter filters out the reply before the handler sees
607 * it, the handler is not removed but the timeout will immediately
608 * fire again. If a filter was dumb and kept removing the timeout
609 * reply then we'd get in an infinite loop.
611 * If #NULL is passed for the reply_handler, the timeout reply will
612 * still be generated and placed into the message queue, but no
613 * specific message handler will receive the reply.
615 * If -1 is passed for the timeout, a sane default timeout is used. -1
616 * is typically the best value for the timeout for this reason, unless
617 * you want a very short or very long timeout. There is no way to
618 * avoid a timeout entirely, other than passing INT_MAX for the
619 * timeout to postpone it indefinitely.
621 * @param connection the connection
622 * @param message the message to send
623 * @param reply_handler message handler expecting the reply, or #NULL
624 * @param timeout_milliseconds timeout in milliseconds or -1 for default
625 * @param result return location for result code
626 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
628 * @todo this function isn't implemented because we need message serials
629 * and other slightly more rich DBusMessage implementation in order to
630 * implement it. The basic idea will be to keep a hash of serials we're
631 * expecting a reply to, and also to add a way to tell GLib or Qt to
632 * install a timeout. Then install a timeout which is the shortest
633 * timeout of any pending reply.
635 * @todo implement non-reentrant "block for reply" variant. i.e. send
636 * a message, block until we get a reply, then pull reply out of
637 * message queue and return it, *without dispatching any handlers for
638 * any other messages* - used for non-reentrant "method calls" We can
639 * block properly for this using _dbus_connection_do_iteration().
643 dbus_connection_send_message_with_reply (DBusConnection *connection,
644 DBusMessage *message,
645 DBusMessageHandler *reply_handler,
646 int timeout_milliseconds,
647 DBusResultCode *result)
650 return dbus_connection_send_message (connection, message, result);
654 * Blocks until the outgoing message queue is empty.
656 * @param connection the connection.
659 dbus_connection_flush (DBusConnection *connection)
661 while (connection->n_outgoing > 0)
662 _dbus_connection_do_iteration (connection,
663 DBUS_ITERATION_DO_WRITING |
664 DBUS_ITERATION_BLOCK,
669 * Gets the number of messages in the incoming message queue.
671 * @param connection the connection.
672 * @returns the number of messages in the queue.
675 dbus_connection_get_n_messages (DBusConnection *connection)
677 return connection->n_incoming;
681 * Returns the first-received message from the incoming message queue,
682 * leaving it in the queue. The caller does not own a reference to the
683 * returned message. If the queue is empty, returns #NULL.
685 * @param connection the connection.
686 * @returns next message in the incoming queue.
689 dbus_connection_peek_message (DBusConnection *connection)
691 return _dbus_list_get_first (&connection->incoming_messages);
695 * Returns the first-received message from the incoming message queue,
696 * removing it from the queue. The caller owns a reference to the
697 * returned message. If the queue is empty, returns #NULL.
699 * @param connection the connection.
700 * @returns next message in the incoming queue.
703 dbus_connection_pop_message (DBusConnection *connection)
705 if (connection->n_incoming > 0)
707 DBusMessage *message;
709 message = _dbus_list_pop_first (&connection->incoming_messages);
710 connection->n_incoming -= 1;
712 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
713 message, connection->n_incoming);
722 * Pops the first-received message from the current incoming message
723 * queue, runs any handlers for it, then unrefs the message.
725 * @param connection the connection
726 * @returns #TRUE if the queue is not empty after dispatch
728 * @todo this function is not properly robust against reentrancy,
729 * that is, if handlers are added/removed while dispatching
730 * a message, things will get messed up.
733 dbus_connection_dispatch_message (DBusConnection *connection)
735 DBusMessage *message;
739 DBusHandlerResult result;
742 dbus_connection_ref (connection);
744 message = dbus_connection_pop_message (connection);
747 dbus_connection_unref (connection);
751 filter_serial = connection->filters_serial;
752 handler_serial = connection->handlers_serial;
754 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
756 link = _dbus_list_get_first_link (&connection->filter_list);
759 DBusMessageHandler *handler = link->data;
760 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
762 result = _dbus_message_handler_handle_message (handler, connection,
765 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
768 if (filter_serial != connection->filters_serial)
770 _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
777 name = dbus_message_get_name (message);
780 DBusMessageHandler *handler;
782 handler = _dbus_hash_table_lookup_string (connection->handler_table,
787 result = _dbus_message_handler_handle_message (handler, connection,
790 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
793 if (handler_serial != connection->handlers_serial)
795 _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
802 dbus_connection_unref (connection);
803 dbus_message_unref (message);
805 return connection->n_incoming > 0;
809 * Sets the error handler function for the connection.
811 * @param connection the connection.
812 * @param error_function the error handler.
813 * @param data data to pass to the error handler.
814 * @param free_data_function function to be called to free the data.
817 dbus_connection_set_error_function (DBusConnection *connection,
818 DBusConnectionErrorFunction error_function,
820 DBusFreeFunction free_data_function)
822 if (connection->error_free_data_function != NULL)
823 (* connection->error_free_data_function) (connection->error_data);
825 connection->error_function = error_function;
826 connection->error_data = data;
827 connection->error_free_data_function = free_data_function;
831 * Sets the watch functions for the connection. These functions are
832 * responsible for making the application's main loop aware of file
833 * descriptors that need to be monitored for events, using select() or
834 * poll(). When using Qt, typically the DBusAddWatchFunction would
835 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
836 * could call g_io_add_watch(), or could be used as part of a more
839 * The DBusWatch can be queried for the file descriptor to watch using
840 * dbus_watch_get_fd(), and for the events to watch for using
841 * dbus_watch_get_flags(). The flags returned by
842 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
843 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
844 * all watches implicitly include a watch for hangups, errors, and
845 * other exceptional conditions.
847 * Once a file descriptor becomes readable or writable, or an exception
848 * occurs, dbus_connection_handle_watch() should be called to
849 * notify the connection of the file descriptor's condition.
851 * dbus_connection_handle_watch() cannot be called during the
852 * DBusAddWatchFunction, as the connection will not be ready to handle
855 * It is not allowed to reference a DBusWatch after it has been passed
856 * to remove_function.
858 * @param connection the connection.
859 * @param add_function function to begin monitoring a new descriptor.
860 * @param remove_function function to stop monitoring a descriptor.
861 * @param data data to pass to add_function and remove_function.
862 * @param free_data_function function to be called to free the data.
865 dbus_connection_set_watch_functions (DBusConnection *connection,
866 DBusAddWatchFunction add_function,
867 DBusRemoveWatchFunction remove_function,
869 DBusFreeFunction free_data_function)
871 /* ref connection for slightly better reentrancy */
872 dbus_connection_ref (connection);
874 _dbus_watch_list_set_functions (connection->watches,
875 add_function, remove_function,
876 data, free_data_function);
878 /* drop our paranoid refcount */
879 dbus_connection_unref (connection);
883 * Called to notify the connection when a previously-added watch
884 * is ready for reading or writing, or has an exception such
887 * @param connection the connection.
888 * @param watch the watch.
889 * @param condition the current condition of the file descriptors being watched.
892 dbus_connection_handle_watch (DBusConnection *connection,
894 unsigned int condition)
896 _dbus_transport_handle_watch (connection->transport,
901 * Adds a message filter. Filters are handlers that are run on
902 * all incoming messages, prior to the normal handlers
903 * registered with dbus_connection_register_handler().
904 * Filters are run in the order that they were added.
905 * The same handler can be added as a filter more than once, in
906 * which case it will be run more than once.
908 * @param connection the connection
909 * @param handler the handler
910 * @returns #TRUE on success, #FALSE if not enough memory.
913 dbus_connection_add_filter (DBusConnection *connection,
914 DBusMessageHandler *handler)
916 if (!_dbus_message_handler_add_connection (handler, connection))
919 if (!_dbus_list_append (&connection->filter_list,
922 _dbus_message_handler_remove_connection (handler, connection);
926 connection->filters_serial += 1;
932 * Removes a previously-added message filter. It is a programming
933 * error to call this function for a handler that has not
934 * been added as a filter. If the given handler was added
935 * more than once, only one instance of it will be removed
936 * (the most recently-added instance).
938 * @param connection the connection
939 * @param handler the handler to remove
943 dbus_connection_remove_filter (DBusConnection *connection,
944 DBusMessageHandler *handler)
946 if (!_dbus_list_remove_last (&connection->filter_list, handler))
948 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
952 _dbus_message_handler_remove_connection (handler, connection);
954 connection->filters_serial += 1;
958 * Registers a handler for a list of message names. A single handler
959 * can be registered for any number of message names, but each message
960 * name can only have one handler at a time. It's not allowed to call
961 * this function with the name of a message that already has a
962 * handler. If the function returns #FALSE, the handlers were not
963 * registered due to lack of memory.
965 * @param connection the connection
966 * @param handler the handler
967 * @param messages_to_handle the messages to handle
968 * @param n_messages the number of message names in messages_to_handle
969 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
973 dbus_connection_register_handler (DBusConnection *connection,
974 DBusMessageHandler *handler,
975 const char **messages_to_handle,
981 while (i < n_messages)
986 key = _dbus_strdup (messages_to_handle[i]);
990 if (!_dbus_hash_iter_lookup (connection->handler_table,
998 if (_dbus_hash_iter_get_value (&iter) != NULL)
1000 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1001 messages_to_handle[i]);
1002 dbus_free (key); /* won't have replaced the old key with the new one */
1006 if (!_dbus_message_handler_add_connection (handler, connection))
1008 _dbus_hash_iter_remove_entry (&iter);
1009 /* key has freed on nuking the entry */
1013 _dbus_hash_iter_set_value (&iter, handler);
1015 connection->handlers_serial += 1;
1023 /* unregister everything registered so far,
1024 * so we don't fail partially
1026 dbus_connection_unregister_handler (connection,
1035 * Unregisters a handler for a list of message names. The handlers
1036 * must have been previously registered.
1038 * @param connection the connection
1039 * @param handler the handler
1040 * @param messages_to_handle the messages to handle
1041 * @param n_messages the number of message names in messages_to_handle
1045 dbus_connection_unregister_handler (DBusConnection *connection,
1046 DBusMessageHandler *handler,
1047 const char **messages_to_handle,
1053 while (i < n_messages)
1057 if (!_dbus_hash_iter_lookup (connection->handler_table,
1058 (char*) messages_to_handle[i], FALSE,
1061 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1062 messages_to_handle[i]);
1064 else if (_dbus_hash_iter_get_value (&iter) != handler)
1066 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1067 messages_to_handle[i]);
1071 _dbus_hash_iter_remove_entry (&iter);
1072 _dbus_message_handler_remove_connection (handler, connection);
1078 connection->handlers_serial += 1;
1081 static int *allocated_slots = NULL;
1082 static int n_allocated_slots = 0;
1083 static int n_used_slots = 0;
1084 static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
1087 * Allocates an integer ID to be used for storing application-specific
1088 * data on any DBusConnection. The allocated ID may then be used
1089 * with dbus_connection_set_data() and dbus_connection_get_data().
1090 * If allocation fails, -1 is returned.
1092 * @returns -1 on failure, otherwise the data slot ID
1095 dbus_connection_allocate_data_slot (void)
1099 if (!dbus_static_mutex_lock (&allocated_slots_lock))
1102 if (n_used_slots < n_allocated_slots)
1105 while (slot < n_allocated_slots)
1107 if (allocated_slots[slot] < 0)
1109 allocated_slots[slot] = slot;
1116 _dbus_assert (slot < n_allocated_slots);
1123 tmp = dbus_realloc (allocated_slots,
1124 sizeof (int) * (n_allocated_slots + 1));
1128 allocated_slots = tmp;
1129 slot = n_allocated_slots;
1130 n_allocated_slots += 1;
1132 allocated_slots[slot] = slot;
1135 _dbus_assert (slot >= 0);
1136 _dbus_assert (slot < n_allocated_slots);
1139 dbus_static_mutex_unlock (&allocated_slots_lock);
1144 * Deallocates a global ID for connection data slots.
1145 * dbus_connection_get_data() and dbus_connection_set_data()
1146 * may no longer be used with this slot.
1147 * Existing data stored on existing DBusConnection objects
1148 * will be freed when the connection is finalized,
1149 * but may not be retrieved (and may only be replaced
1150 * if someone else reallocates the slot).
1152 * @param slot the slot to deallocate
1155 dbus_connection_free_data_slot (int slot)
1157 dbus_static_mutex_lock (&allocated_slots_lock);
1159 _dbus_assert (slot < n_allocated_slots);
1160 _dbus_assert (allocated_slots[slot] == slot);
1162 allocated_slots[slot] = -1;
1165 if (n_used_slots == 0)
1167 dbus_free (allocated_slots);
1168 allocated_slots = NULL;
1169 n_allocated_slots = 0;
1172 dbus_static_mutex_unlock (&allocated_slots_lock);
1176 * Stores a pointer on a DBusConnection, along
1177 * with an optional function to be used for freeing
1178 * the data when the data is set again, or when
1179 * the connection is finalized. The slot number
1180 * must have been allocated with dbus_connection_allocate_data_slot().
1182 * @param connection the connection
1183 * @param slot the slot number
1184 * @param data the data to store
1185 * @param free_data_func finalizer function for the data
1186 * @returns #TRUE if there was enough memory to store the data
1189 dbus_connection_set_data (DBusConnection *connection,
1192 DBusFreeFunction free_data_func)
1194 _dbus_assert (slot < n_allocated_slots);
1195 _dbus_assert (allocated_slots[slot] == slot);
1197 if (slot >= connection->n_slots)
1202 tmp = dbus_realloc (connection->data_slots,
1203 sizeof (DBusDataSlot) * (slot + 1));
1207 connection->data_slots = tmp;
1208 i = connection->n_slots;
1209 connection->n_slots = slot + 1;
1210 while (i < connection->n_slots)
1212 connection->data_slots[i].data = NULL;
1213 connection->data_slots[i].free_data_func = NULL;
1218 _dbus_assert (slot < connection->n_slots);
1220 if (connection->data_slots[slot].free_data_func)
1221 (* connection->data_slots[slot].free_data_func) (connection->data_slots[slot].data);
1223 connection->data_slots[slot].data = data;
1224 connection->data_slots[slot].free_data_func = free_data_func;
1230 * Retrieves data previously set with dbus_connection_set_data().
1231 * The slot must still be allocated (must not have been freed).
1233 * @param connection the connection
1234 * @param slot the slot to get data from
1235 * @returns the data, or #NULL if not found
1238 dbus_connection_get_data (DBusConnection *connection,
1241 _dbus_assert (slot < n_allocated_slots);
1242 _dbus_assert (allocated_slots[slot] == slot);
1244 if (slot >= connection->n_slots)
1247 return connection->data_slots[slot].data;
1251 _dbus_connection_free_data_slots (DBusConnection *connection)
1256 while (i < connection->n_slots)
1258 if (connection->data_slots[i].free_data_func)
1259 (* connection->data_slots[i].free_data_func) (connection->data_slots[i].data);
1260 connection->data_slots[i].data = NULL;
1261 connection->data_slots[i].free_data_func = NULL;
1265 dbus_free (connection->data_slots);
1266 connection->data_slots = NULL;
1267 connection->n_slots = 0;