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 DBusDisconnectFunction disconnect_function; /**< Callback on disconnect. */
87 void *disconnect_data; /**< Data for disconnect callback. */
88 DBusFreeFunction disconnect_free_data_function; /**< Free function for disconnect 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. */
96 int client_serial; /**< Client serial. Increments each time a message is sent */
97 unsigned int disconnect_notified : 1; /**< Already called disconnect_function */
100 static void _dbus_connection_free_data_slots (DBusConnection *connection);
103 * Adds a message to the incoming message queue, returning #FALSE
104 * if there's insufficient memory to queue the message.
106 * @param connection the connection.
107 * @param message the message to queue.
108 * @returns #TRUE on success.
111 _dbus_connection_queue_received_message (DBusConnection *connection,
112 DBusMessage *message)
114 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
116 if (!_dbus_list_append (&connection->incoming_messages,
120 dbus_message_ref (message);
121 connection->n_incoming += 1;
123 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
124 message, connection->n_incoming);
130 * Checks whether there are messages in the outgoing message queue.
132 * @param connection the connection.
133 * @returns #TRUE if the outgoing queue is non-empty.
136 _dbus_connection_have_messages_to_send (DBusConnection *connection)
138 return connection->outgoing_messages != NULL;
142 * Gets the next outgoing message. The message remains in the
143 * queue, and the caller does not own a reference to it.
145 * @param connection the connection.
146 * @returns the message to be sent.
149 _dbus_connection_get_message_to_send (DBusConnection *connection)
151 return _dbus_list_get_last (&connection->outgoing_messages);
155 * Notifies the connection that a message has been sent, so the
156 * message can be removed from the outgoing queue.
158 * @param connection the connection.
159 * @param message the message that was sent.
162 _dbus_connection_message_sent (DBusConnection *connection,
163 DBusMessage *message)
165 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
166 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
168 _dbus_list_pop_last (&connection->outgoing_messages);
169 dbus_message_unref (message);
171 connection->n_outgoing -= 1;
173 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
174 message, connection->n_outgoing);
176 if (connection->n_outgoing == 0)
177 _dbus_transport_messages_pending (connection->transport,
178 connection->n_outgoing);
182 * Adds a watch using the connection's DBusAddWatchFunction if
183 * available. Otherwise records the watch to be added when said
184 * function is available. Also re-adds the watch if the
185 * DBusAddWatchFunction changes. May fail due to lack of memory.
187 * @param connection the connection.
188 * @param watch the watch to add.
189 * @returns #TRUE on success.
192 _dbus_connection_add_watch (DBusConnection *connection,
195 if (connection->watches) /* null during finalize */
196 return _dbus_watch_list_add_watch (connection->watches,
203 * Removes a watch using the connection's DBusRemoveWatchFunction
204 * if available. It's an error to call this function on a watch
205 * that was not previously added.
207 * @param connection the connection.
208 * @param watch the watch to remove.
211 _dbus_connection_remove_watch (DBusConnection *connection,
214 if (connection->watches) /* null during finalize */
215 _dbus_watch_list_remove_watch (connection->watches,
220 * Tells the connection that the transport has been disconnected.
221 * Results in calling the application disconnect callback.
222 * Only has an effect the first time it's called.
224 * @param connection the connection
227 _dbus_connection_notify_disconnected (DBusConnection *connection)
229 if (connection->disconnect_function != NULL &&
230 !connection->disconnect_notified)
232 connection->disconnect_notified = TRUE;
233 dbus_connection_ref (connection);
234 (* connection->disconnect_function) (connection,
235 connection->disconnect_data);
236 dbus_connection_unref (connection);
241 * Queues incoming messages and sends outgoing messages for this
242 * connection, optionally blocking in the process. Each call to
243 * _dbus_connection_do_iteration() will call select() or poll() one
244 * time and then read or write data if possible.
246 * The purpose of this function is to be able to flush outgoing
247 * messages or queue up incoming messages without returning
248 * control to the application and causing reentrancy weirdness.
250 * The flags parameter allows you to specify whether to
251 * read incoming messages, write outgoing messages, or both,
252 * and whether to block if no immediate action is possible.
254 * The timeout_milliseconds parameter does nothing unless the
255 * iteration is blocking.
257 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
258 * wasn't specified, then it's impossible to block, even if
259 * you specify DBUS_ITERATION_BLOCK; in that case the function
260 * returns immediately.
262 * @param connection the connection.
263 * @param flags iteration flags.
264 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
267 _dbus_connection_do_iteration (DBusConnection *connection,
269 int timeout_milliseconds)
271 if (connection->n_outgoing == 0)
272 flags &= ~DBUS_ITERATION_DO_WRITING;
274 _dbus_transport_do_iteration (connection->transport,
275 flags, timeout_milliseconds);
279 * Creates a new connection for the given transport. A transport
280 * represents a message stream that uses some concrete mechanism, such
281 * as UNIX domain sockets. May return #NULL if insufficient
282 * memory exists to create the connection.
284 * @param transport the transport.
285 * @returns the new connection, or #NULL on failure.
288 _dbus_connection_new_for_transport (DBusTransport *transport)
290 DBusConnection *connection;
291 DBusWatchList *watch_list;
292 DBusHashTable *handler_table;
296 handler_table = NULL;
298 watch_list = _dbus_watch_list_new ();
299 if (watch_list == NULL)
303 _dbus_hash_table_new (DBUS_HASH_STRING,
305 if (handler_table == NULL)
308 connection = dbus_new0 (DBusConnection, 1);
309 if (connection == NULL)
312 connection->refcount = 1;
313 connection->transport = transport;
314 connection->watches = watch_list;
315 connection->handler_table = handler_table;
316 connection->filter_list = NULL;
318 connection->data_slots = NULL;
319 connection->n_slots = 0;
320 connection->client_serial = 1;
321 connection->disconnect_notified = FALSE;
323 _dbus_transport_ref (transport);
324 _dbus_transport_set_connection (transport, connection);
330 if (connection != NULL)
331 dbus_free (connection);
334 _dbus_hash_table_unref (handler_table);
337 _dbus_watch_list_free (watch_list);
343 _dbus_connection_get_next_client_serial (DBusConnection *connection)
347 serial = connection->client_serial++;
349 if (connection->client_serial < 0)
350 connection->client_serial = 1;
356 * Used to notify a connection when a DBusMessageHandler is
357 * destroyed, so the connection can drop any reference
360 * @param connection the connection
361 * @param handler the handler
364 _dbus_connection_handler_destroyed (DBusConnection *connection,
365 DBusMessageHandler *handler)
370 _dbus_hash_iter_init (connection->handler_table, &iter);
371 while (_dbus_hash_iter_next (&iter))
373 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
376 _dbus_hash_iter_remove_entry (&iter);
379 link = _dbus_list_get_first_link (&connection->filter_list);
382 DBusMessageHandler *h = link->data;
383 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
386 _dbus_list_remove_link (&connection->filter_list,
396 * @addtogroup DBusConnection
402 * Opens a new connection to a remote address.
404 * @todo specify what the address parameter is. Right now
405 * it's just the name of a UNIX domain socket. It should be
406 * something more complex that encodes which transport to use.
408 * If the open fails, the function returns #NULL, and provides
409 * a reason for the failure in the result parameter. Pass
410 * #NULL for the result parameter if you aren't interested
411 * in the reason for failure.
413 * @param address the address.
414 * @param result address where a result code can be returned.
415 * @returns new connection, or #NULL on failure.
418 dbus_connection_open (const char *address,
419 DBusResultCode *result)
421 DBusConnection *connection;
422 DBusTransport *transport;
424 transport = _dbus_transport_open (address, result);
425 if (transport == NULL)
428 connection = _dbus_connection_new_for_transport (transport);
430 _dbus_transport_unref (transport);
432 if (connection == NULL)
434 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
442 * Increments the reference count of a DBusConnection.
444 * @param connection the connection.
447 dbus_connection_ref (DBusConnection *connection)
449 connection->refcount += 1;
453 * Decrements the reference count of a DBusConnection, and finalizes
454 * it if the count reaches zero. If a connection is still connected
455 * when it's finalized, it will be disconnected (that is, associated
456 * file handles will be closed).
458 * @param connection the connection.
461 dbus_connection_unref (DBusConnection *connection)
463 _dbus_assert (connection != NULL);
464 _dbus_assert (connection->refcount > 0);
466 connection->refcount -= 1;
467 if (connection->refcount == 0)
472 dbus_connection_disconnect (connection);
474 /* free disconnect data as a side effect */
475 dbus_connection_set_disconnect_function (connection,
478 _dbus_watch_list_free (connection->watches);
479 connection->watches = NULL;
481 _dbus_connection_free_data_slots (connection);
483 _dbus_hash_iter_init (connection->handler_table, &iter);
484 while (_dbus_hash_iter_next (&iter))
486 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
488 _dbus_message_handler_remove_connection (h, connection);
491 link = _dbus_list_get_first_link (&connection->filter_list);
494 DBusMessageHandler *h = link->data;
495 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
497 _dbus_message_handler_remove_connection (h, connection);
502 _dbus_hash_table_unref (connection->handler_table);
503 connection->handler_table = NULL;
505 _dbus_list_clear (&connection->filter_list);
507 _dbus_list_foreach (&connection->outgoing_messages,
508 (DBusForeachFunction) dbus_message_unref,
510 _dbus_list_clear (&connection->outgoing_messages);
512 _dbus_list_foreach (&connection->incoming_messages,
513 (DBusForeachFunction) dbus_message_unref,
515 _dbus_list_clear (&connection->incoming_messages);
517 _dbus_transport_unref (connection->transport);
519 dbus_free (connection);
524 * Closes the connection, so no further data can be sent or received.
525 * Any further attempts to send data will result in errors. This
526 * function does not affect the connection's reference count. It's
527 * safe to disconnect a connection more than once; all calls after the
528 * first do nothing. It's impossible to "reconnect" a connection, a
529 * new connection must be created.
531 * @param connection the connection.
534 dbus_connection_disconnect (DBusConnection *connection)
536 _dbus_transport_disconnect (connection->transport);
540 * Gets whether the connection is currently connected. All
541 * connections are connected when they are opened. A connection may
542 * become disconnected when the remote application closes its end, or
543 * exits; a connection may also be disconnected with
544 * dbus_connection_disconnect().
546 * @param connection the connection.
547 * @returns #TRUE if the connection is still alive.
550 dbus_connection_get_is_connected (DBusConnection *connection)
552 return _dbus_transport_get_is_connected (connection->transport);
556 * Adds a message to the outgoing message queue. Does not block to
557 * write the message to the network; that happens asynchronously. to
558 * force the message to be written, call dbus_connection_flush().
560 * If the function fails, it returns #FALSE and returns the
561 * reason for failure via the result parameter.
562 * The result parameter can be #NULL if you aren't interested
563 * in the reason for the failure.
565 * @param connection the connection.
566 * @param message the message to write.
567 * @param result address where result code can be placed.
568 * @returns #TRUE on success.
571 dbus_connection_send_message (DBusConnection *connection,
572 DBusMessage *message,
573 DBusResultCode *result)
575 if (!_dbus_list_prepend (&connection->outgoing_messages,
578 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
582 dbus_message_ref (message);
583 connection->n_outgoing += 1;
585 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
586 message, connection->n_outgoing);
588 _dbus_message_set_client_serial (message, _dbus_connection_get_next_client_serial (connection));
589 _dbus_message_lock (message);
591 if (connection->n_outgoing == 1)
592 _dbus_transport_messages_pending (connection->transport,
593 connection->n_outgoing);
599 * Queues a message to send, as with dbus_connection_send_message(),
600 * but also sets up a DBusMessageHandler to receive a reply to the
601 * message. If no reply is received in the given timeout_milliseconds,
602 * expires the pending reply and sends the DBusMessageHandler a
603 * synthetic error reply (generated in-process, not by the remote
604 * application) indicating that a timeout occurred.
606 * Reply handlers see their replies after message filters see them,
607 * but before message handlers added with
608 * dbus_connection_register_handler() see them, regardless of the
609 * reply message's name. Reply handlers are only handed a single
610 * message as a reply, after a reply has been seen the handler is
611 * removed. If a filter filters out the reply before the handler sees
612 * it, the handler is not removed but the timeout will immediately
613 * fire again. If a filter was dumb and kept removing the timeout
614 * reply then we'd get in an infinite loop.
616 * If #NULL is passed for the reply_handler, the timeout reply will
617 * still be generated and placed into the message queue, but no
618 * specific message handler will receive the reply.
620 * If -1 is passed for the timeout, a sane default timeout is used. -1
621 * is typically the best value for the timeout for this reason, unless
622 * you want a very short or very long timeout. There is no way to
623 * avoid a timeout entirely, other than passing INT_MAX for the
624 * timeout to postpone it indefinitely.
626 * @param connection the connection
627 * @param message the message to send
628 * @param reply_handler message handler expecting the reply, or #NULL
629 * @param timeout_milliseconds timeout in milliseconds or -1 for default
630 * @param result return location for result code
631 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
633 * @todo this function isn't implemented because we need message serials
634 * and other slightly more rich DBusMessage implementation in order to
635 * implement it. The basic idea will be to keep a hash of serials we're
636 * expecting a reply to, and also to add a way to tell GLib or Qt to
637 * install a timeout. Then install a timeout which is the shortest
638 * timeout of any pending reply.
640 * @todo implement non-reentrant "block for reply" variant. i.e. send
641 * a message, block until we get a reply, then pull reply out of
642 * message queue and return it, *without dispatching any handlers for
643 * any other messages* - used for non-reentrant "method calls" We can
644 * block properly for this using _dbus_connection_do_iteration().
648 dbus_connection_send_message_with_reply (DBusConnection *connection,
649 DBusMessage *message,
650 DBusMessageHandler *reply_handler,
651 int timeout_milliseconds,
652 DBusResultCode *result)
655 return dbus_connection_send_message (connection, message, result);
659 * Blocks until the outgoing message queue is empty.
661 * @param connection the connection.
664 dbus_connection_flush (DBusConnection *connection)
666 while (connection->n_outgoing > 0)
667 _dbus_connection_do_iteration (connection,
668 DBUS_ITERATION_DO_WRITING |
669 DBUS_ITERATION_BLOCK,
674 * Gets the number of messages in the incoming message queue.
676 * @param connection the connection.
677 * @returns the number of messages in the queue.
680 dbus_connection_get_n_messages (DBusConnection *connection)
682 return connection->n_incoming;
686 * Returns the first-received message from the incoming message queue,
687 * leaving it in the queue. The caller does not own a reference to the
688 * returned message. If the queue is empty, returns #NULL.
690 * @param connection the connection.
691 * @returns next message in the incoming queue.
694 dbus_connection_peek_message (DBusConnection *connection)
696 return _dbus_list_get_first (&connection->incoming_messages);
700 * Returns the first-received message from the incoming message queue,
701 * removing it from the queue. The caller owns a reference to the
702 * returned message. If the queue is empty, returns #NULL.
704 * @param connection the connection.
705 * @returns next message in the incoming queue.
708 dbus_connection_pop_message (DBusConnection *connection)
710 if (connection->n_incoming > 0)
712 DBusMessage *message;
714 message = _dbus_list_pop_first (&connection->incoming_messages);
715 connection->n_incoming -= 1;
717 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
718 message, connection->n_incoming);
727 * Pops the first-received message from the current incoming message
728 * queue, runs any handlers for it, then unrefs the message.
730 * @param connection the connection
731 * @returns #TRUE if the queue is not empty after dispatch
733 * @todo this function is not properly robust against reentrancy,
734 * that is, if handlers are added/removed while dispatching
735 * a message, things will get messed up.
738 dbus_connection_dispatch_message (DBusConnection *connection)
740 DBusMessage *message;
744 DBusHandlerResult result;
747 dbus_connection_ref (connection);
749 message = dbus_connection_pop_message (connection);
752 dbus_connection_unref (connection);
756 filter_serial = connection->filters_serial;
757 handler_serial = connection->handlers_serial;
759 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
761 link = _dbus_list_get_first_link (&connection->filter_list);
764 DBusMessageHandler *handler = link->data;
765 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
767 result = _dbus_message_handler_handle_message (handler, connection,
770 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
773 if (filter_serial != connection->filters_serial)
775 _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
782 name = dbus_message_get_name (message);
785 DBusMessageHandler *handler;
787 handler = _dbus_hash_table_lookup_string (connection->handler_table,
792 result = _dbus_message_handler_handle_message (handler, connection,
795 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
798 if (handler_serial != connection->handlers_serial)
800 _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
807 dbus_connection_unref (connection);
808 dbus_message_unref (message);
810 return connection->n_incoming > 0;
814 * Sets the disconnect handler function for the connection.
815 * Will be called exactly once, when the connection is
818 * @param connection the connection.
819 * @param disconnect_function the disconnect handler.
820 * @param data data to pass to the disconnect handler.
821 * @param free_data_function function to be called to free the data.
824 dbus_connection_set_disconnect_function (DBusConnection *connection,
825 DBusDisconnectFunction disconnect_function,
827 DBusFreeFunction free_data_function)
829 if (connection->disconnect_free_data_function != NULL)
830 (* connection->disconnect_free_data_function) (connection->disconnect_data);
832 connection->disconnect_function = disconnect_function;
833 connection->disconnect_data = data;
834 connection->disconnect_free_data_function = free_data_function;
838 * Sets the watch functions for the connection. These functions are
839 * responsible for making the application's main loop aware of file
840 * descriptors that need to be monitored for events, using select() or
841 * poll(). When using Qt, typically the DBusAddWatchFunction would
842 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
843 * could call g_io_add_watch(), or could be used as part of a more
846 * The DBusWatch can be queried for the file descriptor to watch using
847 * dbus_watch_get_fd(), and for the events to watch for using
848 * dbus_watch_get_flags(). The flags returned by
849 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
850 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
851 * all watches implicitly include a watch for hangups, errors, and
852 * other exceptional conditions.
854 * Once a file descriptor becomes readable or writable, or an exception
855 * occurs, dbus_connection_handle_watch() should be called to
856 * notify the connection of the file descriptor's condition.
858 * dbus_connection_handle_watch() cannot be called during the
859 * DBusAddWatchFunction, as the connection will not be ready to handle
862 * It is not allowed to reference a DBusWatch after it has been passed
863 * to remove_function.
865 * @param connection the connection.
866 * @param add_function function to begin monitoring a new descriptor.
867 * @param remove_function function to stop monitoring a descriptor.
868 * @param data data to pass to add_function and remove_function.
869 * @param free_data_function function to be called to free the data.
872 dbus_connection_set_watch_functions (DBusConnection *connection,
873 DBusAddWatchFunction add_function,
874 DBusRemoveWatchFunction remove_function,
876 DBusFreeFunction free_data_function)
878 /* ref connection for slightly better reentrancy */
879 dbus_connection_ref (connection);
881 _dbus_watch_list_set_functions (connection->watches,
882 add_function, remove_function,
883 data, free_data_function);
885 /* drop our paranoid refcount */
886 dbus_connection_unref (connection);
890 * Called to notify the connection when a previously-added watch
891 * is ready for reading or writing, or has an exception such
894 * @param connection the connection.
895 * @param watch the watch.
896 * @param condition the current condition of the file descriptors being watched.
899 dbus_connection_handle_watch (DBusConnection *connection,
901 unsigned int condition)
903 _dbus_transport_handle_watch (connection->transport,
908 * Adds a message filter. Filters are handlers that are run on
909 * all incoming messages, prior to the normal handlers
910 * registered with dbus_connection_register_handler().
911 * Filters are run in the order that they were added.
912 * The same handler can be added as a filter more than once, in
913 * which case it will be run more than once.
915 * @param connection the connection
916 * @param handler the handler
917 * @returns #TRUE on success, #FALSE if not enough memory.
920 dbus_connection_add_filter (DBusConnection *connection,
921 DBusMessageHandler *handler)
923 if (!_dbus_message_handler_add_connection (handler, connection))
926 if (!_dbus_list_append (&connection->filter_list,
929 _dbus_message_handler_remove_connection (handler, connection);
933 connection->filters_serial += 1;
939 * Removes a previously-added message filter. It is a programming
940 * error to call this function for a handler that has not
941 * been added as a filter. If the given handler was added
942 * more than once, only one instance of it will be removed
943 * (the most recently-added instance).
945 * @param connection the connection
946 * @param handler the handler to remove
950 dbus_connection_remove_filter (DBusConnection *connection,
951 DBusMessageHandler *handler)
953 if (!_dbus_list_remove_last (&connection->filter_list, handler))
955 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
959 _dbus_message_handler_remove_connection (handler, connection);
961 connection->filters_serial += 1;
965 * Registers a handler for a list of message names. A single handler
966 * can be registered for any number of message names, but each message
967 * name can only have one handler at a time. It's not allowed to call
968 * this function with the name of a message that already has a
969 * handler. If the function returns #FALSE, the handlers were not
970 * registered due to lack of memory.
972 * @param connection the connection
973 * @param handler the handler
974 * @param messages_to_handle the messages to handle
975 * @param n_messages the number of message names in messages_to_handle
976 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
980 dbus_connection_register_handler (DBusConnection *connection,
981 DBusMessageHandler *handler,
982 const char **messages_to_handle,
988 while (i < n_messages)
993 key = _dbus_strdup (messages_to_handle[i]);
997 if (!_dbus_hash_iter_lookup (connection->handler_table,
1005 if (_dbus_hash_iter_get_value (&iter) != NULL)
1007 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1008 messages_to_handle[i]);
1009 dbus_free (key); /* won't have replaced the old key with the new one */
1013 if (!_dbus_message_handler_add_connection (handler, connection))
1015 _dbus_hash_iter_remove_entry (&iter);
1016 /* key has freed on nuking the entry */
1020 _dbus_hash_iter_set_value (&iter, handler);
1022 connection->handlers_serial += 1;
1030 /* unregister everything registered so far,
1031 * so we don't fail partially
1033 dbus_connection_unregister_handler (connection,
1042 * Unregisters a handler for a list of message names. The handlers
1043 * must have been previously registered.
1045 * @param connection the connection
1046 * @param handler the handler
1047 * @param messages_to_handle the messages to handle
1048 * @param n_messages the number of message names in messages_to_handle
1052 dbus_connection_unregister_handler (DBusConnection *connection,
1053 DBusMessageHandler *handler,
1054 const char **messages_to_handle,
1060 while (i < n_messages)
1064 if (!_dbus_hash_iter_lookup (connection->handler_table,
1065 (char*) messages_to_handle[i], FALSE,
1068 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1069 messages_to_handle[i]);
1071 else if (_dbus_hash_iter_get_value (&iter) != handler)
1073 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1074 messages_to_handle[i]);
1078 _dbus_hash_iter_remove_entry (&iter);
1079 _dbus_message_handler_remove_connection (handler, connection);
1085 connection->handlers_serial += 1;
1088 static int *allocated_slots = NULL;
1089 static int n_allocated_slots = 0;
1090 static int n_used_slots = 0;
1091 static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
1094 * Allocates an integer ID to be used for storing application-specific
1095 * data on any DBusConnection. The allocated ID may then be used
1096 * with dbus_connection_set_data() and dbus_connection_get_data().
1097 * If allocation fails, -1 is returned.
1099 * @returns -1 on failure, otherwise the data slot ID
1102 dbus_connection_allocate_data_slot (void)
1106 if (!dbus_static_mutex_lock (&allocated_slots_lock))
1109 if (n_used_slots < n_allocated_slots)
1112 while (slot < n_allocated_slots)
1114 if (allocated_slots[slot] < 0)
1116 allocated_slots[slot] = slot;
1123 _dbus_assert (slot < n_allocated_slots);
1130 tmp = dbus_realloc (allocated_slots,
1131 sizeof (int) * (n_allocated_slots + 1));
1135 allocated_slots = tmp;
1136 slot = n_allocated_slots;
1137 n_allocated_slots += 1;
1139 allocated_slots[slot] = slot;
1142 _dbus_assert (slot >= 0);
1143 _dbus_assert (slot < n_allocated_slots);
1146 dbus_static_mutex_unlock (&allocated_slots_lock);
1151 * Deallocates a global ID for connection data slots.
1152 * dbus_connection_get_data() and dbus_connection_set_data()
1153 * may no longer be used with this slot.
1154 * Existing data stored on existing DBusConnection objects
1155 * will be freed when the connection is finalized,
1156 * but may not be retrieved (and may only be replaced
1157 * if someone else reallocates the slot).
1159 * @param slot the slot to deallocate
1162 dbus_connection_free_data_slot (int slot)
1164 dbus_static_mutex_lock (&allocated_slots_lock);
1166 _dbus_assert (slot < n_allocated_slots);
1167 _dbus_assert (allocated_slots[slot] == slot);
1169 allocated_slots[slot] = -1;
1172 if (n_used_slots == 0)
1174 dbus_free (allocated_slots);
1175 allocated_slots = NULL;
1176 n_allocated_slots = 0;
1179 dbus_static_mutex_unlock (&allocated_slots_lock);
1183 * Stores a pointer on a DBusConnection, along
1184 * with an optional function to be used for freeing
1185 * the data when the data is set again, or when
1186 * the connection is finalized. The slot number
1187 * must have been allocated with dbus_connection_allocate_data_slot().
1189 * @param connection the connection
1190 * @param slot the slot number
1191 * @param data the data to store
1192 * @param free_data_func finalizer function for the data
1193 * @returns #TRUE if there was enough memory to store the data
1196 dbus_connection_set_data (DBusConnection *connection,
1199 DBusFreeFunction free_data_func)
1201 _dbus_assert (slot < n_allocated_slots);
1202 _dbus_assert (allocated_slots[slot] == slot);
1204 if (slot >= connection->n_slots)
1209 tmp = dbus_realloc (connection->data_slots,
1210 sizeof (DBusDataSlot) * (slot + 1));
1214 connection->data_slots = tmp;
1215 i = connection->n_slots;
1216 connection->n_slots = slot + 1;
1217 while (i < connection->n_slots)
1219 connection->data_slots[i].data = NULL;
1220 connection->data_slots[i].free_data_func = NULL;
1225 _dbus_assert (slot < connection->n_slots);
1227 if (connection->data_slots[slot].free_data_func)
1228 (* connection->data_slots[slot].free_data_func) (connection->data_slots[slot].data);
1230 connection->data_slots[slot].data = data;
1231 connection->data_slots[slot].free_data_func = free_data_func;
1237 * Retrieves data previously set with dbus_connection_set_data().
1238 * The slot must still be allocated (must not have been freed).
1240 * @param connection the connection
1241 * @param slot the slot to get data from
1242 * @returns the data, or #NULL if not found
1245 dbus_connection_get_data (DBusConnection *connection,
1248 _dbus_assert (slot < n_allocated_slots);
1249 _dbus_assert (allocated_slots[slot] == slot);
1251 if (slot >= connection->n_slots)
1254 return connection->data_slots[slot].data;
1258 _dbus_connection_free_data_slots (DBusConnection *connection)
1263 while (i < connection->n_slots)
1265 if (connection->data_slots[i].free_data_func)
1266 (* connection->data_slots[i].free_data_func) (connection->data_slots[i].data);
1267 connection->data_slots[i].data = NULL;
1268 connection->data_slots[i].free_data_func = NULL;
1272 dbus_free (connection->data_slots);
1273 connection->data_slots = NULL;
1274 connection->n_slots = 0;