1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003 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-timeout.h"
27 #include "dbus-transport.h"
28 #include "dbus-watch.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-list.h"
31 #include "dbus-hash.h"
32 #include "dbus-message-internal.h"
33 #include "dbus-threads.h"
36 * @defgroup DBusConnection DBusConnection
38 * @brief Connection to another application
40 * A DBusConnection represents a connection to another
41 * application. Messages can be sent and received via this connection.
43 * The connection maintains a queue of incoming messages and a queue
44 * of outgoing messages. dbus_connection_pop_message() and friends
45 * can be used to read incoming messages from the queue.
46 * Outgoing messages are automatically discarded as they are
47 * written to the network.
49 * In brief a DBusConnection is a message queue associated with some
50 * message transport mechanism such as a socket.
55 * @defgroup DBusConnectionInternals DBusConnection implementation details
56 * @ingroup DBusInternals
57 * @brief Implementation details of DBusConnection
62 /** Opaque typedef for DBusDataSlot */
63 typedef struct DBusDataSlot DBusDataSlot;
64 /** DBusDataSlot is used to store application data on the connection */
67 void *data; /**< The application data */
68 DBusFreeFunction free_data_func; /**< Free the application data */
72 * Implementation details of DBusConnection. All fields are private.
76 int refcount; /**< Reference count. */
78 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
79 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
81 int n_outgoing; /**< Length of outgoing queue. */
82 int n_incoming; /**< Length of incoming queue. */
84 DBusTransport *transport; /**< Object that sends/receives messages over network. */
85 DBusWatchList *watches; /**< Stores active watches. */
86 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
88 DBusDisconnectFunction disconnect_function; /**< Callback on disconnect. */
89 void *disconnect_data; /**< Data for disconnect callback. */
90 DBusFreeFunction disconnect_free_data_function; /**< Free function for disconnect callback data. */
91 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
92 DBusList *filter_list; /**< List of filters. */
93 int filters_serial; /**< Increments when the list of filters is changed. */
94 int handlers_serial; /**< Increments when the handler table is changed. */
95 DBusDataSlot *data_slots; /**< Data slots */
96 int n_slots; /**< Slots allocated so far. */
98 DBusCounter *connection_counter; /**< Counter that we decrement when finalized */
100 int client_serial; /**< Client serial. Increments each time a message is sent */
101 unsigned int disconnect_notified : 1; /**< Already called disconnect_function */
104 static void _dbus_connection_free_data_slots (DBusConnection *connection);
107 * Adds a message to the incoming message queue, returning #FALSE
108 * if there's insufficient memory to queue the message.
110 * @param connection the connection.
111 * @param message the message to queue.
112 * @returns #TRUE on success.
115 _dbus_connection_queue_received_message (DBusConnection *connection,
116 DBusMessage *message)
118 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
120 if (!_dbus_list_append (&connection->incoming_messages,
124 dbus_message_ref (message);
125 connection->n_incoming += 1;
127 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
128 message, connection->n_incoming);
134 * Checks whether there are messages in the outgoing message queue.
136 * @param connection the connection.
137 * @returns #TRUE if the outgoing queue is non-empty.
140 _dbus_connection_have_messages_to_send (DBusConnection *connection)
142 return connection->outgoing_messages != NULL;
146 * Gets the next outgoing message. The message remains in the
147 * queue, and the caller does not own a reference to it.
149 * @param connection the connection.
150 * @returns the message to be sent.
153 _dbus_connection_get_message_to_send (DBusConnection *connection)
155 return _dbus_list_get_last (&connection->outgoing_messages);
159 * Notifies the connection that a message has been sent, so the
160 * message can be removed from the outgoing queue.
162 * @param connection the connection.
163 * @param message the message that was sent.
166 _dbus_connection_message_sent (DBusConnection *connection,
167 DBusMessage *message)
169 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
170 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
172 _dbus_list_pop_last (&connection->outgoing_messages);
173 dbus_message_unref (message);
175 connection->n_outgoing -= 1;
177 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
178 message, connection->n_outgoing);
180 if (connection->n_outgoing == 0)
181 _dbus_transport_messages_pending (connection->transport,
182 connection->n_outgoing);
186 * Adds a watch using the connection's DBusAddWatchFunction if
187 * available. Otherwise records the watch to be added when said
188 * function is available. Also re-adds the watch if the
189 * DBusAddWatchFunction changes. May fail due to lack of memory.
191 * @param connection the connection.
192 * @param watch the watch to add.
193 * @returns #TRUE on success.
196 _dbus_connection_add_watch (DBusConnection *connection,
199 if (connection->watches) /* null during finalize */
200 return _dbus_watch_list_add_watch (connection->watches,
207 * Removes a watch using the connection's DBusRemoveWatchFunction
208 * if available. It's an error to call this function on a watch
209 * that was not previously added.
211 * @param connection the connection.
212 * @param watch the watch to remove.
215 _dbus_connection_remove_watch (DBusConnection *connection,
218 if (connection->watches) /* null during finalize */
219 _dbus_watch_list_remove_watch (connection->watches,
224 * Tells the connection that the transport has been disconnected.
225 * Results in calling the application disconnect callback.
226 * Only has an effect the first time it's called.
228 * @param connection the connection
231 _dbus_connection_notify_disconnected (DBusConnection *connection)
233 if (connection->disconnect_function != NULL &&
234 !connection->disconnect_notified)
236 connection->disconnect_notified = TRUE;
237 dbus_connection_ref (connection);
238 (* connection->disconnect_function) (connection,
239 connection->disconnect_data);
240 dbus_connection_unref (connection);
245 * Queues incoming messages and sends outgoing messages for this
246 * connection, optionally blocking in the process. Each call to
247 * _dbus_connection_do_iteration() will call select() or poll() one
248 * time and then read or write data if possible.
250 * The purpose of this function is to be able to flush outgoing
251 * messages or queue up incoming messages without returning
252 * control to the application and causing reentrancy weirdness.
254 * The flags parameter allows you to specify whether to
255 * read incoming messages, write outgoing messages, or both,
256 * and whether to block if no immediate action is possible.
258 * The timeout_milliseconds parameter does nothing unless the
259 * iteration is blocking.
261 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
262 * wasn't specified, then it's impossible to block, even if
263 * you specify DBUS_ITERATION_BLOCK; in that case the function
264 * returns immediately.
266 * @param connection the connection.
267 * @param flags iteration flags.
268 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
271 _dbus_connection_do_iteration (DBusConnection *connection,
273 int timeout_milliseconds)
275 if (connection->n_outgoing == 0)
276 flags &= ~DBUS_ITERATION_DO_WRITING;
278 _dbus_transport_do_iteration (connection->transport,
279 flags, timeout_milliseconds);
283 * Creates a new connection for the given transport. A transport
284 * represents a message stream that uses some concrete mechanism, such
285 * as UNIX domain sockets. May return #NULL if insufficient
286 * memory exists to create the connection.
288 * @param transport the transport.
289 * @returns the new connection, or #NULL on failure.
292 _dbus_connection_new_for_transport (DBusTransport *transport)
294 DBusConnection *connection;
295 DBusWatchList *watch_list;
296 DBusTimeoutList *timeout_list;
297 DBusHashTable *handler_table;
301 handler_table = NULL;
303 watch_list = _dbus_watch_list_new ();
304 if (watch_list == NULL)
307 timeout_list = _dbus_timeout_list_new ();
308 if (timeout_list == NULL)
312 _dbus_hash_table_new (DBUS_HASH_STRING,
314 if (handler_table == NULL)
317 connection = dbus_new0 (DBusConnection, 1);
318 if (connection == NULL)
321 connection->refcount = 1;
322 connection->transport = transport;
323 connection->watches = watch_list;
324 connection->timeouts = timeout_list;
325 connection->handler_table = handler_table;
326 connection->filter_list = NULL;
328 connection->data_slots = NULL;
329 connection->n_slots = 0;
330 connection->client_serial = 1;
331 connection->disconnect_notified = FALSE;
333 _dbus_transport_ref (transport);
334 _dbus_transport_set_connection (transport, connection);
340 if (connection != NULL)
341 dbus_free (connection);
344 _dbus_hash_table_unref (handler_table);
347 _dbus_watch_list_free (watch_list);
353 _dbus_connection_get_next_client_serial (DBusConnection *connection)
357 serial = connection->client_serial++;
359 if (connection->client_serial < 0)
360 connection->client_serial = 1;
366 * Used to notify a connection when a DBusMessageHandler is
367 * destroyed, so the connection can drop any reference
370 * @param connection the connection
371 * @param handler the handler
374 _dbus_connection_handler_destroyed (DBusConnection *connection,
375 DBusMessageHandler *handler)
380 _dbus_hash_iter_init (connection->handler_table, &iter);
381 while (_dbus_hash_iter_next (&iter))
383 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
386 _dbus_hash_iter_remove_entry (&iter);
389 link = _dbus_list_get_first_link (&connection->filter_list);
392 DBusMessageHandler *h = link->data;
393 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
396 _dbus_list_remove_link (&connection->filter_list,
404 * Adds the counter used to count the number of open connections.
405 * Increments the counter by one, and saves it to be decremented
406 * again when this connection is finalized.
408 * @param connection a #DBusConnection
409 * @param counter counter that tracks number of connections
412 _dbus_connection_set_connection_counter (DBusConnection *connection,
413 DBusCounter *counter)
415 _dbus_assert (connection->connection_counter == NULL);
417 connection->connection_counter = counter;
418 _dbus_counter_ref (connection->connection_counter);
419 _dbus_counter_adjust (connection->connection_counter, 1);
425 * @addtogroup DBusConnection
431 * Opens a new connection to a remote address.
433 * @todo specify what the address parameter is. Right now
434 * it's just the name of a UNIX domain socket. It should be
435 * something more complex that encodes which transport to use.
437 * If the open fails, the function returns #NULL, and provides
438 * a reason for the failure in the result parameter. Pass
439 * #NULL for the result parameter if you aren't interested
440 * in the reason for failure.
442 * @param address the address.
443 * @param result address where a result code can be returned.
444 * @returns new connection, or #NULL on failure.
447 dbus_connection_open (const char *address,
448 DBusResultCode *result)
450 DBusConnection *connection;
451 DBusTransport *transport;
453 transport = _dbus_transport_open (address, result);
454 if (transport == NULL)
457 connection = _dbus_connection_new_for_transport (transport);
459 _dbus_transport_unref (transport);
461 if (connection == NULL)
463 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
471 * Increments the reference count of a DBusConnection.
473 * @param connection the connection.
476 dbus_connection_ref (DBusConnection *connection)
478 connection->refcount += 1;
482 * Decrements the reference count of a DBusConnection, and finalizes
483 * it if the count reaches zero. If a connection is still connected
484 * when it's finalized, it will be disconnected (that is, associated
485 * file handles will be closed).
487 * @param connection the connection.
490 dbus_connection_unref (DBusConnection *connection)
492 _dbus_assert (connection != NULL);
493 _dbus_assert (connection->refcount > 0);
495 connection->refcount -= 1;
496 if (connection->refcount == 0)
501 dbus_connection_disconnect (connection);
503 /* free disconnect data as a side effect */
504 dbus_connection_set_disconnect_function (connection,
507 if (connection->connection_counter != NULL)
509 /* subtract ourselves from the counter */
510 _dbus_counter_adjust (connection->connection_counter, - 1);
511 _dbus_counter_unref (connection->connection_counter);
512 connection->connection_counter = NULL;
515 _dbus_watch_list_free (connection->watches);
516 connection->watches = NULL;
518 _dbus_connection_free_data_slots (connection);
520 _dbus_hash_iter_init (connection->handler_table, &iter);
521 while (_dbus_hash_iter_next (&iter))
523 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
525 _dbus_message_handler_remove_connection (h, connection);
528 link = _dbus_list_get_first_link (&connection->filter_list);
531 DBusMessageHandler *h = link->data;
532 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
534 _dbus_message_handler_remove_connection (h, connection);
539 _dbus_hash_table_unref (connection->handler_table);
540 connection->handler_table = NULL;
542 _dbus_list_clear (&connection->filter_list);
544 _dbus_list_foreach (&connection->outgoing_messages,
545 (DBusForeachFunction) dbus_message_unref,
547 _dbus_list_clear (&connection->outgoing_messages);
549 _dbus_list_foreach (&connection->incoming_messages,
550 (DBusForeachFunction) dbus_message_unref,
552 _dbus_list_clear (&connection->incoming_messages);
554 _dbus_transport_unref (connection->transport);
556 dbus_free (connection);
561 * Closes the connection, so no further data can be sent or received.
562 * Any further attempts to send data will result in errors. This
563 * function does not affect the connection's reference count. It's
564 * safe to disconnect a connection more than once; all calls after the
565 * first do nothing. It's impossible to "reconnect" a connection, a
566 * new connection must be created.
568 * @param connection the connection.
571 dbus_connection_disconnect (DBusConnection *connection)
573 _dbus_transport_disconnect (connection->transport);
577 * Gets whether the connection is currently connected. All
578 * connections are connected when they are opened. A connection may
579 * become disconnected when the remote application closes its end, or
580 * exits; a connection may also be disconnected with
581 * dbus_connection_disconnect().
583 * @param connection the connection.
584 * @returns #TRUE if the connection is still alive.
587 dbus_connection_get_is_connected (DBusConnection *connection)
589 return _dbus_transport_get_is_connected (connection->transport);
593 * Gets whether the connection was authenticated. (Note that
594 * if the connection was authenticated then disconnected,
595 * this function still returns #TRUE)
597 * @param connection the connection
598 * @returns #TRUE if the connection was ever authenticated
601 dbus_connection_get_is_authenticated (DBusConnection *connection)
603 return _dbus_transport_get_is_authenticated (connection->transport);
607 * Adds a message to the outgoing message queue. Does not block to
608 * write the message to the network; that happens asynchronously. to
609 * force the message to be written, call dbus_connection_flush().
611 * If the function fails, it returns #FALSE and returns the
612 * reason for failure via the result parameter.
613 * The result parameter can be #NULL if you aren't interested
614 * in the reason for the failure.
616 * @param connection the connection.
617 * @param message the message to write.
618 * @param result address where result code can be placed.
619 * @returns #TRUE on success.
622 dbus_connection_send_message (DBusConnection *connection,
623 DBusMessage *message,
624 DBusResultCode *result)
626 if (!_dbus_list_prepend (&connection->outgoing_messages,
629 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
633 dbus_message_ref (message);
634 connection->n_outgoing += 1;
636 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
637 message, connection->n_outgoing);
639 _dbus_message_set_client_serial (message, _dbus_connection_get_next_client_serial (connection));
640 _dbus_message_lock (message);
642 if (connection->n_outgoing == 1)
643 _dbus_transport_messages_pending (connection->transport,
644 connection->n_outgoing);
650 * Queues a message to send, as with dbus_connection_send_message(),
651 * but also sets up a DBusMessageHandler to receive a reply to the
652 * message. If no reply is received in the given timeout_milliseconds,
653 * expires the pending reply and sends the DBusMessageHandler a
654 * synthetic error reply (generated in-process, not by the remote
655 * application) indicating that a timeout occurred.
657 * Reply handlers see their replies after message filters see them,
658 * but before message handlers added with
659 * dbus_connection_register_handler() see them, regardless of the
660 * reply message's name. Reply handlers are only handed a single
661 * message as a reply, after a reply has been seen the handler is
662 * removed. If a filter filters out the reply before the handler sees
663 * it, the handler is not removed but the timeout will immediately
664 * fire again. If a filter was dumb and kept removing the timeout
665 * reply then we'd get in an infinite loop.
667 * If #NULL is passed for the reply_handler, the timeout reply will
668 * still be generated and placed into the message queue, but no
669 * specific message handler will receive the reply.
671 * If -1 is passed for the timeout, a sane default timeout is used. -1
672 * is typically the best value for the timeout for this reason, unless
673 * you want a very short or very long timeout. There is no way to
674 * avoid a timeout entirely, other than passing INT_MAX for the
675 * timeout to postpone it indefinitely.
677 * @param connection the connection
678 * @param message the message to send
679 * @param reply_handler message handler expecting the reply, or #NULL
680 * @param timeout_milliseconds timeout in milliseconds or -1 for default
681 * @param result return location for result code
682 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
684 * @todo this function isn't implemented because we need message serials
685 * and other slightly more rich DBusMessage implementation in order to
686 * implement it. The basic idea will be to keep a hash of serials we're
687 * expecting a reply to, and also to add a way to tell GLib or Qt to
688 * install a timeout. Then install a timeout which is the shortest
689 * timeout of any pending reply.
691 * @todo implement non-reentrant "block for reply" variant. i.e. send
692 * a message, block until we get a reply, then pull reply out of
693 * message queue and return it, *without dispatching any handlers for
694 * any other messages* - used for non-reentrant "method calls" We can
695 * block properly for this using _dbus_connection_do_iteration().
699 dbus_connection_send_message_with_reply (DBusConnection *connection,
700 DBusMessage *message,
701 DBusMessageHandler *reply_handler,
702 int timeout_milliseconds,
703 DBusResultCode *result)
706 return dbus_connection_send_message (connection, message, result);
710 * Blocks until the outgoing message queue is empty.
712 * @param connection the connection.
715 dbus_connection_flush (DBusConnection *connection)
717 while (connection->n_outgoing > 0)
718 _dbus_connection_do_iteration (connection,
719 DBUS_ITERATION_DO_WRITING |
720 DBUS_ITERATION_BLOCK,
725 * Gets the number of messages in the incoming message queue.
727 * @param connection the connection.
728 * @returns the number of messages in the queue.
731 dbus_connection_get_n_messages (DBusConnection *connection)
733 return connection->n_incoming;
737 * Returns the first-received message from the incoming message queue,
738 * leaving it in the queue. The caller does not own a reference to the
739 * returned message. If the queue is empty, returns #NULL.
741 * @param connection the connection.
742 * @returns next message in the incoming queue.
745 dbus_connection_peek_message (DBusConnection *connection)
747 return _dbus_list_get_first (&connection->incoming_messages);
751 * Returns the first-received message from the incoming message queue,
752 * removing it from the queue. The caller owns a reference to the
753 * returned message. If the queue is empty, returns #NULL.
755 * @param connection the connection.
756 * @returns next message in the incoming queue.
759 dbus_connection_pop_message (DBusConnection *connection)
761 if (connection->n_incoming > 0)
763 DBusMessage *message;
765 message = _dbus_list_pop_first (&connection->incoming_messages);
766 connection->n_incoming -= 1;
768 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
769 message, connection->n_incoming);
778 * Pops the first-received message from the current incoming message
779 * queue, runs any handlers for it, then unrefs the message.
781 * @param connection the connection
782 * @returns #TRUE if the queue is not empty after dispatch
784 * @todo this function is not properly robust against reentrancy,
785 * that is, if handlers are added/removed while dispatching
786 * a message, things will get messed up.
789 dbus_connection_dispatch_message (DBusConnection *connection)
791 DBusMessage *message;
795 DBusHandlerResult result;
798 dbus_connection_ref (connection);
800 message = dbus_connection_pop_message (connection);
803 dbus_connection_unref (connection);
807 filter_serial = connection->filters_serial;
808 handler_serial = connection->handlers_serial;
810 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
812 link = _dbus_list_get_first_link (&connection->filter_list);
815 DBusMessageHandler *handler = link->data;
816 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
818 result = _dbus_message_handler_handle_message (handler, connection,
821 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
824 if (filter_serial != connection->filters_serial)
826 _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
833 name = dbus_message_get_name (message);
836 DBusMessageHandler *handler;
838 handler = _dbus_hash_table_lookup_string (connection->handler_table,
843 result = _dbus_message_handler_handle_message (handler, connection,
846 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
849 if (handler_serial != connection->handlers_serial)
851 _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
858 dbus_connection_unref (connection);
859 dbus_message_unref (message);
861 return connection->n_incoming > 0;
865 * Sets the disconnect handler function for the connection.
866 * Will be called exactly once, when the connection is
869 * @param connection the connection.
870 * @param disconnect_function the disconnect handler.
871 * @param data data to pass to the disconnect handler.
872 * @param free_data_function function to be called to free the data.
875 dbus_connection_set_disconnect_function (DBusConnection *connection,
876 DBusDisconnectFunction disconnect_function,
878 DBusFreeFunction free_data_function)
880 if (connection->disconnect_free_data_function != NULL)
881 (* connection->disconnect_free_data_function) (connection->disconnect_data);
883 connection->disconnect_function = disconnect_function;
884 connection->disconnect_data = data;
885 connection->disconnect_free_data_function = free_data_function;
889 * Sets the watch functions for the connection. These functions are
890 * responsible for making the application's main loop aware of file
891 * descriptors that need to be monitored for events, using select() or
892 * poll(). When using Qt, typically the DBusAddWatchFunction would
893 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
894 * could call g_io_add_watch(), or could be used as part of a more
897 * The DBusWatch can be queried for the file descriptor to watch using
898 * dbus_watch_get_fd(), and for the events to watch for using
899 * dbus_watch_get_flags(). The flags returned by
900 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
901 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
902 * all watches implicitly include a watch for hangups, errors, and
903 * other exceptional conditions.
905 * Once a file descriptor becomes readable or writable, or an exception
906 * occurs, dbus_connection_handle_watch() should be called to
907 * notify the connection of the file descriptor's condition.
909 * dbus_connection_handle_watch() cannot be called during the
910 * DBusAddWatchFunction, as the connection will not be ready to handle
913 * It is not allowed to reference a DBusWatch after it has been passed
914 * to remove_function.
916 * @param connection the connection.
917 * @param add_function function to begin monitoring a new descriptor.
918 * @param remove_function function to stop monitoring a descriptor.
919 * @param data data to pass to add_function and remove_function.
920 * @param free_data_function function to be called to free the data.
923 dbus_connection_set_watch_functions (DBusConnection *connection,
924 DBusAddWatchFunction add_function,
925 DBusRemoveWatchFunction remove_function,
927 DBusFreeFunction free_data_function)
929 /* ref connection for slightly better reentrancy */
930 dbus_connection_ref (connection);
932 _dbus_watch_list_set_functions (connection->watches,
933 add_function, remove_function,
934 data, free_data_function);
936 /* drop our paranoid refcount */
937 dbus_connection_unref (connection);
941 * Sets the timeout functions for the connection. These functions are
942 * responsible for making the application's main loop aware of timeouts.
943 * When using Qt, typically the DBusAddTimeoutFunction would create a
944 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
947 * The DBusTimeout can be queried for the timer interval using
948 * dbus_timeout_get_interval.
950 * Once a timeout occurs, dbus_timeout_handle should be call to invoke
951 * the timeout's callback.
954 dbus_connection_set_timeout_functions (DBusConnection *connection,
955 DBusAddTimeoutFunction add_function,
956 DBusRemoveTimeoutFunction remove_function,
958 DBusFreeFunction free_data_function)
960 /* ref connection for slightly better reentrancy */
961 dbus_connection_ref (connection);
963 _dbus_timeout_list_set_functions (connection->timeouts,
964 add_function, remove_function,
965 data, free_data_function);
967 /* drop our paranoid refcount */
968 dbus_connection_unref (connection);
972 * Called to notify the connection when a previously-added watch
973 * is ready for reading or writing, or has an exception such
976 * @param connection the connection.
977 * @param watch the watch.
978 * @param condition the current condition of the file descriptors being watched.
981 dbus_connection_handle_watch (DBusConnection *connection,
983 unsigned int condition)
985 _dbus_transport_handle_watch (connection->transport,
990 * Adds a message filter. Filters are handlers that are run on
991 * all incoming messages, prior to the normal handlers
992 * registered with dbus_connection_register_handler().
993 * Filters are run in the order that they were added.
994 * The same handler can be added as a filter more than once, in
995 * which case it will be run more than once.
997 * @param connection the connection
998 * @param handler the handler
999 * @returns #TRUE on success, #FALSE if not enough memory.
1002 dbus_connection_add_filter (DBusConnection *connection,
1003 DBusMessageHandler *handler)
1005 if (!_dbus_message_handler_add_connection (handler, connection))
1008 if (!_dbus_list_append (&connection->filter_list,
1011 _dbus_message_handler_remove_connection (handler, connection);
1015 connection->filters_serial += 1;
1021 * Removes a previously-added message filter. It is a programming
1022 * error to call this function for a handler that has not
1023 * been added as a filter. If the given handler was added
1024 * more than once, only one instance of it will be removed
1025 * (the most recently-added instance).
1027 * @param connection the connection
1028 * @param handler the handler to remove
1032 dbus_connection_remove_filter (DBusConnection *connection,
1033 DBusMessageHandler *handler)
1035 if (!_dbus_list_remove_last (&connection->filter_list, handler))
1037 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
1041 _dbus_message_handler_remove_connection (handler, connection);
1043 connection->filters_serial += 1;
1047 * Registers a handler for a list of message names. A single handler
1048 * can be registered for any number of message names, but each message
1049 * name can only have one handler at a time. It's not allowed to call
1050 * this function with the name of a message that already has a
1051 * handler. If the function returns #FALSE, the handlers were not
1052 * registered due to lack of memory.
1054 * @param connection the connection
1055 * @param handler the handler
1056 * @param messages_to_handle the messages to handle
1057 * @param n_messages the number of message names in messages_to_handle
1058 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
1062 dbus_connection_register_handler (DBusConnection *connection,
1063 DBusMessageHandler *handler,
1064 const char **messages_to_handle,
1070 while (i < n_messages)
1075 key = _dbus_strdup (messages_to_handle[i]);
1079 if (!_dbus_hash_iter_lookup (connection->handler_table,
1087 if (_dbus_hash_iter_get_value (&iter) != NULL)
1089 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1090 messages_to_handle[i]);
1091 dbus_free (key); /* won't have replaced the old key with the new one */
1095 if (!_dbus_message_handler_add_connection (handler, connection))
1097 _dbus_hash_iter_remove_entry (&iter);
1098 /* key has freed on nuking the entry */
1102 _dbus_hash_iter_set_value (&iter, handler);
1104 connection->handlers_serial += 1;
1112 /* unregister everything registered so far,
1113 * so we don't fail partially
1115 dbus_connection_unregister_handler (connection,
1124 * Unregisters a handler for a list of message names. The handlers
1125 * must have been previously registered.
1127 * @param connection the connection
1128 * @param handler the handler
1129 * @param messages_to_handle the messages to handle
1130 * @param n_messages the number of message names in messages_to_handle
1134 dbus_connection_unregister_handler (DBusConnection *connection,
1135 DBusMessageHandler *handler,
1136 const char **messages_to_handle,
1142 while (i < n_messages)
1146 if (!_dbus_hash_iter_lookup (connection->handler_table,
1147 (char*) messages_to_handle[i], FALSE,
1150 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1151 messages_to_handle[i]);
1153 else if (_dbus_hash_iter_get_value (&iter) != handler)
1155 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1156 messages_to_handle[i]);
1160 _dbus_hash_iter_remove_entry (&iter);
1161 _dbus_message_handler_remove_connection (handler, connection);
1167 connection->handlers_serial += 1;
1170 static int *allocated_slots = NULL;
1171 static int n_allocated_slots = 0;
1172 static int n_used_slots = 0;
1173 static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
1176 * Allocates an integer ID to be used for storing application-specific
1177 * data on any DBusConnection. The allocated ID may then be used
1178 * with dbus_connection_set_data() and dbus_connection_get_data().
1179 * If allocation fails, -1 is returned.
1181 * @returns -1 on failure, otherwise the data slot ID
1184 dbus_connection_allocate_data_slot (void)
1188 if (!dbus_static_mutex_lock (&allocated_slots_lock))
1191 if (n_used_slots < n_allocated_slots)
1194 while (slot < n_allocated_slots)
1196 if (allocated_slots[slot] < 0)
1198 allocated_slots[slot] = slot;
1205 _dbus_assert (slot < n_allocated_slots);
1212 tmp = dbus_realloc (allocated_slots,
1213 sizeof (int) * (n_allocated_slots + 1));
1217 allocated_slots = tmp;
1218 slot = n_allocated_slots;
1219 n_allocated_slots += 1;
1221 allocated_slots[slot] = slot;
1224 _dbus_assert (slot >= 0);
1225 _dbus_assert (slot < n_allocated_slots);
1228 dbus_static_mutex_unlock (&allocated_slots_lock);
1233 * Deallocates a global ID for connection data slots.
1234 * dbus_connection_get_data() and dbus_connection_set_data()
1235 * may no longer be used with this slot.
1236 * Existing data stored on existing DBusConnection objects
1237 * will be freed when the connection is finalized,
1238 * but may not be retrieved (and may only be replaced
1239 * if someone else reallocates the slot).
1241 * @param slot the slot to deallocate
1244 dbus_connection_free_data_slot (int slot)
1246 dbus_static_mutex_lock (&allocated_slots_lock);
1248 _dbus_assert (slot < n_allocated_slots);
1249 _dbus_assert (allocated_slots[slot] == slot);
1251 allocated_slots[slot] = -1;
1254 if (n_used_slots == 0)
1256 dbus_free (allocated_slots);
1257 allocated_slots = NULL;
1258 n_allocated_slots = 0;
1261 dbus_static_mutex_unlock (&allocated_slots_lock);
1265 * Stores a pointer on a DBusConnection, along
1266 * with an optional function to be used for freeing
1267 * the data when the data is set again, or when
1268 * the connection is finalized. The slot number
1269 * must have been allocated with dbus_connection_allocate_data_slot().
1271 * @param connection the connection
1272 * @param slot the slot number
1273 * @param data the data to store
1274 * @param free_data_func finalizer function for the data
1275 * @returns #TRUE if there was enough memory to store the data
1278 dbus_connection_set_data (DBusConnection *connection,
1281 DBusFreeFunction free_data_func)
1283 _dbus_assert (slot < n_allocated_slots);
1284 _dbus_assert (allocated_slots[slot] == slot);
1286 if (slot >= connection->n_slots)
1291 tmp = dbus_realloc (connection->data_slots,
1292 sizeof (DBusDataSlot) * (slot + 1));
1296 connection->data_slots = tmp;
1297 i = connection->n_slots;
1298 connection->n_slots = slot + 1;
1299 while (i < connection->n_slots)
1301 connection->data_slots[i].data = NULL;
1302 connection->data_slots[i].free_data_func = NULL;
1307 _dbus_assert (slot < connection->n_slots);
1309 if (connection->data_slots[slot].free_data_func)
1310 (* connection->data_slots[slot].free_data_func) (connection->data_slots[slot].data);
1312 connection->data_slots[slot].data = data;
1313 connection->data_slots[slot].free_data_func = free_data_func;
1319 * Retrieves data previously set with dbus_connection_set_data().
1320 * The slot must still be allocated (must not have been freed).
1322 * @param connection the connection
1323 * @param slot the slot to get data from
1324 * @returns the data, or #NULL if not found
1327 dbus_connection_get_data (DBusConnection *connection,
1330 _dbus_assert (slot < n_allocated_slots);
1331 _dbus_assert (allocated_slots[slot] == slot);
1333 if (slot >= connection->n_slots)
1336 return connection->data_slots[slot].data;
1340 _dbus_connection_free_data_slots (DBusConnection *connection)
1345 while (i < connection->n_slots)
1347 if (connection->data_slots[i].free_data_func)
1348 (* connection->data_slots[i].free_data_func) (connection->data_slots[i].data);
1349 connection->data_slots[i].data = NULL;
1350 connection->data_slots[i].free_data_func = NULL;
1354 dbus_free (connection->data_slots);
1355 connection->data_slots = NULL;
1356 connection->n_slots = 0;
1360 * Specifies the maximum size message this connection is allowed to
1361 * receive. Larger messages will result in disconnecting the
1364 * @param connection a #DBusConnection
1365 * @param size maximum message size the connection can receive, in bytes
1368 dbus_connection_set_max_message_size (DBusConnection *connection,
1371 _dbus_transport_set_max_message_size (connection->transport,
1376 * Gets the value set by dbus_connection_set_max_message_size().
1378 * @param connection the connection
1379 * @returns the max size of a single message
1382 dbus_connection_get_max_message_size (DBusConnection *connection)
1384 return _dbus_transport_get_max_message_size (connection->transport);
1388 * Sets the maximum total number of bytes that can be used for all messages
1389 * received on this connection. Messages count toward the maximum until
1390 * they are finalized. When the maximum is reached, the connection will
1391 * not read more data until some messages are finalized.
1393 * The semantics of the maximum are: if outstanding messages are
1394 * already above the maximum, additional messages will not be read.
1395 * The semantics are not: if the next message would cause us to exceed
1396 * the maximum, we don't read it. The reason is that we don't know the
1397 * size of a message until after we read it.
1399 * Thus, the max live messages size can actually be exceeded
1400 * by up to the maximum size of a single message.
1402 * Also, if we read say 1024 bytes off the wire in a single read(),
1403 * and that contains a half-dozen small messages, we may exceed the
1404 * size max by that amount. But this should be inconsequential.
1406 * @param connection the connection
1407 * @param size the maximum size in bytes of all outstanding messages
1410 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
1413 _dbus_transport_set_max_live_messages_size (connection->transport,
1418 * Gets the value set by dbus_connection_set_max_live_messages_size().
1420 * @param connection the connection
1421 * @returns the max size of all live messages
1424 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
1426 return _dbus_transport_get_max_live_messages_size (connection->transport);