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
25 #include "dbus-connection.h"
26 #include "dbus-list.h"
27 #include "dbus-timeout.h"
28 #include "dbus-transport.h"
29 #include "dbus-watch.h"
30 #include "dbus-connection-internal.h"
31 #include "dbus-list.h"
32 #include "dbus-hash.h"
33 #include "dbus-message-internal.h"
34 #include "dbus-threads.h"
35 #include "dbus-protocol.h"
36 #include "dbus-dataslot.h"
37 #include "dbus-string.h"
38 #include "dbus-pending-call.h"
39 #include "dbus-object-tree.h"
42 #define CONNECTION_LOCK(connection) do { \
43 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \
44 dbus_mutex_lock ((connection)->mutex); \
46 #define CONNECTION_UNLOCK(connection) do { \
47 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \
48 dbus_mutex_unlock ((connection)->mutex); \
51 #define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex)
52 #define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex)
56 * @defgroup DBusConnection DBusConnection
58 * @brief Connection to another application
60 * A DBusConnection represents a connection to another
61 * application. Messages can be sent and received via this connection.
62 * The other application may be a message bus; for convenience, the
63 * function dbus_bus_get() is provided to automatically open a
64 * connection to the well-known message buses.
66 * In brief a DBusConnection is a message queue associated with some
67 * message transport mechanism such as a socket. The connection
68 * maintains a queue of incoming messages and a queue of outgoing
71 * Incoming messages are normally processed by calling
72 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any
73 * handlers registered for the topmost message in the message queue,
74 * then discards the message, then returns.
76 * dbus_connection_get_dispatch_status() indicates whether
77 * messages are currently in the queue that need dispatching.
78 * dbus_connection_set_dispatch_status_function() allows
79 * you to set a function to be used to monitor the dispatch status.
81 * If you're using GLib or Qt add-on libraries for D-BUS, there are
82 * special convenience APIs in those libraries that hide
83 * all the details of dispatch and watch/timeout monitoring.
84 * For example, dbus_connection_setup_with_g_main().
86 * If you aren't using these add-on libraries, you have to manually
87 * call dbus_connection_set_dispatch_status_function(),
88 * dbus_connection_set_watch_functions(),
89 * dbus_connection_set_timeout_functions() providing appropriate
90 * functions to integrate the connection with your application's main
93 * When you use dbus_connection_send() or one of its variants to send
94 * a message, the message is added to the outgoing queue. It's
95 * actually written to the network later; either in
96 * dbus_watch_handle() invoked by your main loop, or in
97 * dbus_connection_flush() which blocks until it can write out the
98 * entire outgoing queue. The GLib/Qt add-on libraries again
99 * handle the details here for you by setting up watch functions.
101 * When a connection is disconnected, you are guaranteed to get a
102 * message with the name #DBUS_MESSAGE_LOCAL_DISCONNECT.
104 * You may not drop the last reference to a #DBusConnection
105 * until that connection has been disconnected.
107 * You may dispatch the unprocessed incoming message queue even if the
108 * connection is disconnected. However, #DBUS_MESSAGE_LOCAL_DISCONNECT
109 * will always be the last message in the queue (obviously no messages
110 * are received after disconnection).
112 * #DBusConnection has thread locks and drops them when invoking user
113 * callbacks, so in general is transparently threadsafe. However,
114 * #DBusMessage does NOT have thread locks; you must not send the same
115 * message to multiple #DBusConnection that will be used from
120 * @defgroup DBusConnectionInternals DBusConnection implementation details
121 * @ingroup DBusInternals
122 * @brief Implementation details of DBusConnection
128 * Internal struct representing a message filter function
130 typedef struct DBusMessageFilter DBusMessageFilter;
133 * Internal struct representing a message filter function
135 struct DBusMessageFilter
137 DBusAtomic refcount; /**< Reference count */
138 DBusHandleMessageFunction function; /**< Function to call to filter */
139 void *user_data; /**< User data for the function */
140 DBusFreeFunction free_user_data_function; /**< Function to free the user data */
145 * Internals of DBusPreallocatedSend
147 struct DBusPreallocatedSend
149 DBusConnection *connection; /**< Connection we'd send the message to */
150 DBusList *queue_link; /**< Preallocated link in the queue */
151 DBusList *counter_link; /**< Preallocated link in the resource counter */
154 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
157 * Implementation details of DBusConnection. All fields are private.
159 struct DBusConnection
161 DBusAtomic refcount; /**< Reference count. */
163 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
165 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
166 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
168 dbus_bool_t io_path_acquired; /**< Protects transport io path */
169 DBusCondVar *io_path_cond; /**< Protects transport io path */
171 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
172 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
174 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
175 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
177 int n_outgoing; /**< Length of outgoing queue. */
178 int n_incoming; /**< Length of incoming queue. */
180 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */
182 DBusTransport *transport; /**< Object that sends/receives messages over network. */
183 DBusWatchList *watches; /**< Stores active watches. */
184 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
186 DBusList *filter_list; /**< List of filters. */
188 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
190 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */
192 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */
193 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
195 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
196 void *wakeup_main_data; /**< Application data for wakeup_main_function */
197 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
199 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */
200 void *dispatch_status_data; /**< Application data for dispatch_status_function */
201 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */
203 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */
205 DBusList *link_cache; /**< A cache of linked list links to prevent contention
206 * for the global linked list mempool lock
208 DBusObjectTree *objects; /**< Object path handlers registered with this connection */
211 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
212 DBusTimeout *timeout);
213 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
214 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
215 DBusDispatchStatus new_status);
216 static void _dbus_connection_last_unref (DBusConnection *connection);
219 _dbus_message_filter_ref (DBusMessageFilter *filter)
221 _dbus_assert (filter->refcount.value > 0);
222 _dbus_atomic_inc (&filter->refcount);
226 _dbus_message_filter_unref (DBusMessageFilter *filter)
228 _dbus_assert (filter->refcount.value > 0);
230 if (_dbus_atomic_dec (&filter->refcount) == 1)
232 if (filter->free_user_data_function)
233 (* filter->free_user_data_function) (filter->user_data);
240 * Acquires the connection lock.
242 * @param connection the connection.
245 _dbus_connection_lock (DBusConnection *connection)
247 CONNECTION_LOCK (connection);
251 * Releases the connection lock.
253 * @param connection the connection.
256 _dbus_connection_unlock (DBusConnection *connection)
258 CONNECTION_UNLOCK (connection);
262 * Wakes up the main loop if it is sleeping
263 * Needed if we're e.g. queueing outgoing messages
264 * on a thread while the mainloop sleeps.
266 * @param connection the connection.
269 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
271 if (connection->wakeup_main_function)
272 (*connection->wakeup_main_function) (connection->wakeup_main_data);
275 #ifdef DBUS_BUILD_TESTS
276 /* For now this function isn't used */
278 * Adds a message to the incoming message queue, returning #FALSE
279 * if there's insufficient memory to queue the message.
280 * Does not take over refcount of the message.
282 * @param connection the connection.
283 * @param message the message to queue.
284 * @returns #TRUE on success.
287 _dbus_connection_queue_received_message (DBusConnection *connection,
288 DBusMessage *message)
292 link = _dbus_list_alloc_link (message);
296 dbus_message_ref (message);
297 _dbus_connection_queue_received_message_link (connection, link);
304 * Adds a message-containing list link to the incoming message queue,
305 * taking ownership of the link and the message's current refcount.
306 * Cannot fail due to lack of memory.
308 * @param connection the connection.
309 * @param link the message link to queue.
312 _dbus_connection_queue_received_message_link (DBusConnection *connection,
315 DBusPendingCall *pending;
316 dbus_int32_t reply_serial;
317 DBusMessage *message;
319 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
321 _dbus_list_append_link (&connection->incoming_messages,
323 message = link->data;
325 /* If this is a reply we're waiting on, remove timeout for it */
326 reply_serial = dbus_message_get_reply_serial (message);
327 if (reply_serial != -1)
329 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
333 if (pending->timeout_added)
334 _dbus_connection_remove_timeout_locked (connection,
337 pending->timeout_added = FALSE;
341 connection->n_incoming += 1;
343 _dbus_connection_wakeup_mainloop (connection);
345 _dbus_verbose ("Message %p (%s) added to incoming queue %p, %d incoming\n",
347 dbus_message_get_interface (message) ?
348 dbus_message_get_interface (message) :
351 connection->n_incoming);
355 * Adds a link + message to the incoming message queue.
356 * Can't fail. Takes ownership of both link and message.
358 * @param connection the connection.
359 * @param link the list node and message to queue.
361 * @todo This needs to wake up the mainloop if it is in
362 * a poll/select and this is a multithreaded app.
365 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
368 _dbus_list_append_link (&connection->incoming_messages, link);
370 connection->n_incoming += 1;
372 _dbus_connection_wakeup_mainloop (connection);
374 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
375 link->data, connection, connection->n_incoming);
380 * Checks whether there are messages in the outgoing message queue.
382 * @param connection the connection.
383 * @returns #TRUE if the outgoing queue is non-empty.
386 _dbus_connection_have_messages_to_send (DBusConnection *connection)
388 return connection->outgoing_messages != NULL;
392 * Gets the next outgoing message. The message remains in the
393 * queue, and the caller does not own a reference to it.
395 * @param connection the connection.
396 * @returns the message to be sent.
399 _dbus_connection_get_message_to_send (DBusConnection *connection)
401 return _dbus_list_get_last (&connection->outgoing_messages);
405 * Notifies the connection that a message has been sent, so the
406 * message can be removed from the outgoing queue.
407 * Called with the connection lock held.
409 * @param connection the connection.
410 * @param message the message that was sent.
413 _dbus_connection_message_sent (DBusConnection *connection,
414 DBusMessage *message)
418 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
420 link = _dbus_list_get_last_link (&connection->outgoing_messages);
421 _dbus_assert (link != NULL);
422 _dbus_assert (link->data == message);
424 /* Save this link in the link cache */
425 _dbus_list_unlink (&connection->outgoing_messages,
427 _dbus_list_prepend_link (&connection->link_cache, link);
429 connection->n_outgoing -= 1;
431 _dbus_verbose ("Message %p (%s) removed from outgoing queue %p, %d left to send\n",
433 dbus_message_get_interface (message) ?
434 dbus_message_get_interface (message) :
436 connection, connection->n_outgoing);
438 /* Save this link in the link cache also */
439 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
441 _dbus_list_prepend_link (&connection->link_cache, link);
443 dbus_message_unref (message);
445 if (connection->n_outgoing == 0)
446 _dbus_transport_messages_pending (connection->transport,
447 connection->n_outgoing);
451 * Adds a watch using the connection's DBusAddWatchFunction if
452 * available. Otherwise records the watch to be added when said
453 * function is available. Also re-adds the watch if the
454 * DBusAddWatchFunction changes. May fail due to lack of memory.
456 * @param connection the connection.
457 * @param watch the watch to add.
458 * @returns #TRUE on success.
461 _dbus_connection_add_watch (DBusConnection *connection,
464 if (connection->watches) /* null during finalize */
465 return _dbus_watch_list_add_watch (connection->watches,
472 * Removes a watch using the connection's DBusRemoveWatchFunction
473 * if available. It's an error to call this function on a watch
474 * that was not previously added.
476 * @param connection the connection.
477 * @param watch the watch to remove.
480 _dbus_connection_remove_watch (DBusConnection *connection,
483 if (connection->watches) /* null during finalize */
484 _dbus_watch_list_remove_watch (connection->watches,
489 * Toggles a watch and notifies app via connection's
490 * DBusWatchToggledFunction if available. It's an error to call this
491 * function on a watch that was not previously added.
493 * @param connection the connection.
494 * @param watch the watch to toggle.
495 * @param enabled whether to enable or disable
498 _dbus_connection_toggle_watch (DBusConnection *connection,
502 if (connection->watches) /* null during finalize */
503 _dbus_watch_list_toggle_watch (connection->watches,
508 * Adds a timeout using the connection's DBusAddTimeoutFunction if
509 * available. Otherwise records the timeout to be added when said
510 * function is available. Also re-adds the timeout if the
511 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
512 * The timeout will fire repeatedly until removed.
514 * @param connection the connection.
515 * @param timeout the timeout to add.
516 * @returns #TRUE on success.
519 _dbus_connection_add_timeout (DBusConnection *connection,
520 DBusTimeout *timeout)
522 if (connection->timeouts) /* null during finalize */
523 return _dbus_timeout_list_add_timeout (connection->timeouts,
530 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
531 * if available. It's an error to call this function on a timeout
532 * that was not previously added.
534 * @param connection the connection.
535 * @param timeout the timeout to remove.
538 _dbus_connection_remove_timeout (DBusConnection *connection,
539 DBusTimeout *timeout)
541 if (connection->timeouts) /* null during finalize */
542 _dbus_timeout_list_remove_timeout (connection->timeouts,
547 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
548 DBusTimeout *timeout)
550 CONNECTION_LOCK (connection);
551 _dbus_connection_remove_timeout (connection, timeout);
552 CONNECTION_UNLOCK (connection);
556 * Toggles a timeout and notifies app via connection's
557 * DBusTimeoutToggledFunction if available. It's an error to call this
558 * function on a timeout that was not previously added.
560 * @param connection the connection.
561 * @param timeout the timeout to toggle.
562 * @param enabled whether to enable or disable
565 _dbus_connection_toggle_timeout (DBusConnection *connection,
566 DBusTimeout *timeout,
569 if (connection->timeouts) /* null during finalize */
570 _dbus_timeout_list_toggle_timeout (connection->timeouts,
575 * Tells the connection that the transport has been disconnected.
576 * Results in posting a disconnect message on the incoming message
577 * queue. Only has an effect the first time it's called.
579 * @param connection the connection
582 _dbus_connection_notify_disconnected (DBusConnection *connection)
584 if (connection->disconnect_message_link)
586 /* We haven't sent the disconnect message already */
587 _dbus_connection_queue_synthesized_message_link (connection,
588 connection->disconnect_message_link);
589 connection->disconnect_message_link = NULL;
594 _dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
595 DBusPendingCall *pending)
597 _dbus_assert (pending->reply_serial != 0);
599 if (!_dbus_connection_add_timeout (connection, pending->timeout))
602 if (!_dbus_hash_table_insert_int (connection->pending_replies,
603 pending->reply_serial,
606 _dbus_connection_remove_timeout (connection, pending->timeout);
610 pending->timeout_added = TRUE;
611 pending->connection = connection;
613 dbus_pending_call_ref (pending);
619 free_pending_call_on_hash_removal (void *data)
621 DBusPendingCall *pending;
628 if (pending->connection)
630 if (pending->timeout_added)
632 _dbus_connection_remove_timeout (pending->connection,
634 pending->timeout_added = FALSE;
637 pending->connection = NULL;
639 dbus_pending_call_unref (pending);
644 _dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
645 DBusPendingCall *pending)
647 /* The idea here is to avoid finalizing the pending call
648 * with the lock held, since there's a destroy notifier
649 * in pending call that goes out to application code.
651 dbus_pending_call_ref (pending);
652 _dbus_hash_table_remove_int (connection->pending_replies,
653 pending->reply_serial);
654 CONNECTION_UNLOCK (connection);
655 dbus_pending_call_unref (pending);
659 * Removes a pending call from the connection, such that
660 * the pending reply will be ignored. May drop the last
661 * reference to the pending call.
663 * @param connection the connection
664 * @param pending the pending call
667 _dbus_connection_remove_pending_call (DBusConnection *connection,
668 DBusPendingCall *pending)
670 CONNECTION_LOCK (connection);
671 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
675 * Completes a pending call with the given message,
676 * or if the message is #NULL, by timing out the pending call.
678 * @param pending the pending call
679 * @param message the message to complete the call with, or #NULL
680 * to time out the call
683 _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
684 DBusMessage *message)
688 message = pending->timeout_link->data;
689 _dbus_list_clear (&pending->timeout_link);
692 _dbus_verbose (" handing message %p to pending call\n", message);
694 _dbus_assert (pending->reply == NULL);
695 pending->reply = message;
696 dbus_message_ref (pending->reply);
698 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */
699 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending);
701 /* Must be called unlocked since it invokes app callback */
702 _dbus_pending_call_notify (pending);
703 dbus_pending_call_unref (pending);
707 * Acquire the transporter I/O path. This must be done before
708 * doing any I/O in the transporter. May sleep and drop the
709 * connection mutex while waiting for the I/O path.
711 * @param connection the connection.
712 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
713 * @returns TRUE if the I/O path was acquired.
716 _dbus_connection_acquire_io_path (DBusConnection *connection,
717 int timeout_milliseconds)
719 dbus_bool_t res = TRUE;
721 if (connection->io_path_acquired)
723 if (timeout_milliseconds != -1)
724 res = dbus_condvar_wait_timeout (connection->io_path_cond,
726 timeout_milliseconds);
728 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
733 _dbus_assert (!connection->io_path_acquired);
735 connection->io_path_acquired = TRUE;
742 * Release the I/O path when you're done with it. Only call
743 * after you've acquired the I/O. Wakes up at most one thread
744 * currently waiting to acquire the I/O path.
746 * @param connection the connection.
749 _dbus_connection_release_io_path (DBusConnection *connection)
751 _dbus_assert (connection->io_path_acquired);
753 connection->io_path_acquired = FALSE;
754 dbus_condvar_wake_one (connection->io_path_cond);
759 * Queues incoming messages and sends outgoing messages for this
760 * connection, optionally blocking in the process. Each call to
761 * _dbus_connection_do_iteration() will call select() or poll() one
762 * time and then read or write data if possible.
764 * The purpose of this function is to be able to flush outgoing
765 * messages or queue up incoming messages without returning
766 * control to the application and causing reentrancy weirdness.
768 * The flags parameter allows you to specify whether to
769 * read incoming messages, write outgoing messages, or both,
770 * and whether to block if no immediate action is possible.
772 * The timeout_milliseconds parameter does nothing unless the
773 * iteration is blocking.
775 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
776 * wasn't specified, then it's impossible to block, even if
777 * you specify DBUS_ITERATION_BLOCK; in that case the function
778 * returns immediately.
780 * @param connection the connection.
781 * @param flags iteration flags.
782 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
785 _dbus_connection_do_iteration (DBusConnection *connection,
787 int timeout_milliseconds)
789 if (connection->n_outgoing == 0)
790 flags &= ~DBUS_ITERATION_DO_WRITING;
792 if (_dbus_connection_acquire_io_path (connection,
793 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
795 _dbus_transport_do_iteration (connection->transport,
796 flags, timeout_milliseconds);
797 _dbus_connection_release_io_path (connection);
802 * Creates a new connection for the given transport. A transport
803 * represents a message stream that uses some concrete mechanism, such
804 * as UNIX domain sockets. May return #NULL if insufficient
805 * memory exists to create the connection.
807 * @param transport the transport.
808 * @returns the new connection, or #NULL on failure.
811 _dbus_connection_new_for_transport (DBusTransport *transport)
813 DBusConnection *connection;
814 DBusWatchList *watch_list;
815 DBusTimeoutList *timeout_list;
816 DBusHashTable *pending_replies;
818 DBusCondVar *message_returned_cond;
819 DBusCondVar *dispatch_cond;
820 DBusCondVar *io_path_cond;
821 DBusList *disconnect_link;
822 DBusMessage *disconnect_message;
823 DBusCounter *outgoing_counter;
824 DBusObjectTree *objects;
828 pending_replies = NULL;
831 message_returned_cond = NULL;
832 dispatch_cond = NULL;
834 disconnect_link = NULL;
835 disconnect_message = NULL;
836 outgoing_counter = NULL;
839 watch_list = _dbus_watch_list_new ();
840 if (watch_list == NULL)
843 timeout_list = _dbus_timeout_list_new ();
844 if (timeout_list == NULL)
848 _dbus_hash_table_new (DBUS_HASH_INT,
850 (DBusFreeFunction)free_pending_call_on_hash_removal);
851 if (pending_replies == NULL)
854 connection = dbus_new0 (DBusConnection, 1);
855 if (connection == NULL)
858 mutex = dbus_mutex_new ();
862 message_returned_cond = dbus_condvar_new ();
863 if (message_returned_cond == NULL)
866 dispatch_cond = dbus_condvar_new ();
867 if (dispatch_cond == NULL)
870 io_path_cond = dbus_condvar_new ();
871 if (io_path_cond == NULL)
874 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
875 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
878 if (disconnect_message == NULL)
881 disconnect_link = _dbus_list_alloc_link (disconnect_message);
882 if (disconnect_link == NULL)
885 outgoing_counter = _dbus_counter_new ();
886 if (outgoing_counter == NULL)
889 objects = _dbus_object_tree_new (connection);
893 if (_dbus_modify_sigpipe)
894 _dbus_disable_sigpipe ();
896 connection->refcount.value = 1;
897 connection->mutex = mutex;
898 connection->dispatch_cond = dispatch_cond;
899 connection->io_path_cond = io_path_cond;
900 connection->message_returned_cond = message_returned_cond;
901 connection->transport = transport;
902 connection->watches = watch_list;
903 connection->timeouts = timeout_list;
904 connection->pending_replies = pending_replies;
905 connection->outgoing_counter = outgoing_counter;
906 connection->filter_list = NULL;
907 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
908 connection->objects = objects;
910 _dbus_data_slot_list_init (&connection->slot_list);
912 connection->client_serial = 1;
914 connection->disconnect_message_link = disconnect_link;
916 if (!_dbus_transport_set_connection (transport, connection))
919 _dbus_transport_ref (transport);
924 if (disconnect_message != NULL)
925 dbus_message_unref (disconnect_message);
927 if (disconnect_link != NULL)
928 _dbus_list_free_link (disconnect_link);
930 if (io_path_cond != NULL)
931 dbus_condvar_free (io_path_cond);
933 if (dispatch_cond != NULL)
934 dbus_condvar_free (dispatch_cond);
936 if (message_returned_cond != NULL)
937 dbus_condvar_free (message_returned_cond);
940 dbus_mutex_free (mutex);
942 if (connection != NULL)
943 dbus_free (connection);
946 _dbus_hash_table_unref (pending_replies);
949 _dbus_watch_list_free (watch_list);
952 _dbus_timeout_list_free (timeout_list);
954 if (outgoing_counter)
955 _dbus_counter_unref (outgoing_counter);
958 _dbus_object_tree_unref (objects);
964 * Increments the reference count of a DBusConnection.
965 * Requires that the caller already holds the connection lock.
967 * @param connection the connection.
970 _dbus_connection_ref_unlocked (DBusConnection *connection)
972 #ifdef DBUS_HAVE_ATOMIC_INT
973 _dbus_atomic_inc (&connection->refcount);
975 _dbus_assert (connection->refcount.value > 0);
976 connection->refcount.value += 1;
981 * Decrements the reference count of a DBusConnection.
982 * Requires that the caller already holds the connection lock.
984 * @param connection the connection.
987 _dbus_connection_unref_unlocked (DBusConnection *connection)
989 dbus_bool_t last_unref;
991 _dbus_return_if_fail (connection != NULL);
993 /* The connection lock is better than the global
994 * lock in the atomic increment fallback
997 #ifdef DBUS_HAVE_ATOMIC_INT
998 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1000 _dbus_assert (connection->refcount.value > 0);
1002 connection->refcount.value -= 1;
1003 last_unref = (connection->refcount.value == 0);
1005 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
1010 _dbus_connection_last_unref (connection);
1013 static dbus_uint32_t
1014 _dbus_connection_get_next_client_serial (DBusConnection *connection)
1018 serial = connection->client_serial++;
1020 if (connection->client_serial < 0)
1021 connection->client_serial = 1;
1027 * A callback for use with dbus_watch_new() to create a DBusWatch.
1029 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch()
1030 * and the virtual handle_watch in DBusTransport if we got rid of it.
1031 * The reason this is some work is threading, see the _dbus_connection_handle_watch()
1034 * @param watch the watch.
1035 * @param condition the current condition of the file descriptors being watched.
1036 * @param data must be a pointer to a #DBusConnection
1037 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
1040 _dbus_connection_handle_watch (DBusWatch *watch,
1041 unsigned int condition,
1044 DBusConnection *connection;
1046 DBusDispatchStatus status;
1050 CONNECTION_LOCK (connection);
1051 _dbus_connection_acquire_io_path (connection, -1);
1052 retval = _dbus_transport_handle_watch (connection->transport,
1054 _dbus_connection_release_io_path (connection);
1056 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1058 /* this calls out to user code */
1059 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1067 * @addtogroup DBusConnection
1073 * Opens a new connection to a remote address.
1075 * @todo specify what the address parameter is. Right now
1076 * it's just the name of a UNIX domain socket. It should be
1077 * something more complex that encodes which transport to use.
1079 * If the open fails, the function returns #NULL, and provides
1080 * a reason for the failure in the result parameter. Pass
1081 * #NULL for the result parameter if you aren't interested
1082 * in the reason for failure.
1084 * @param address the address.
1085 * @param error address where an error can be returned.
1086 * @returns new connection, or #NULL on failure.
1089 dbus_connection_open (const char *address,
1092 DBusConnection *connection;
1093 DBusTransport *transport;
1095 _dbus_return_val_if_fail (address != NULL, NULL);
1096 _dbus_return_val_if_error_is_set (error, NULL);
1098 transport = _dbus_transport_open (address, error);
1099 if (transport == NULL)
1101 _DBUS_ASSERT_ERROR_IS_SET (error);
1105 connection = _dbus_connection_new_for_transport (transport);
1107 _dbus_transport_unref (transport);
1109 if (connection == NULL)
1111 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1119 * Increments the reference count of a DBusConnection.
1121 * @param connection the connection.
1124 dbus_connection_ref (DBusConnection *connection)
1126 _dbus_return_if_fail (connection != NULL);
1128 /* The connection lock is better than the global
1129 * lock in the atomic increment fallback
1132 #ifdef DBUS_HAVE_ATOMIC_INT
1133 _dbus_atomic_inc (&connection->refcount);
1135 CONNECTION_LOCK (connection);
1136 _dbus_assert (connection->refcount.value > 0);
1138 connection->refcount.value += 1;
1139 CONNECTION_UNLOCK (connection);
1144 free_outgoing_message (void *element,
1147 DBusMessage *message = element;
1148 DBusConnection *connection = data;
1150 _dbus_message_remove_size_counter (message,
1151 connection->outgoing_counter,
1153 dbus_message_unref (message);
1156 /* This is run without the mutex held, but after the last reference
1157 * to the connection has been dropped we should have no thread-related
1161 _dbus_connection_last_unref (DBusConnection *connection)
1165 _dbus_verbose ("Finalizing connection %p\n", connection);
1167 _dbus_assert (connection->refcount.value == 0);
1169 /* You have to disconnect the connection before unref:ing it. Otherwise
1170 * you won't get the disconnected message.
1172 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
1174 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
1175 _dbus_object_tree_free_all_unlocked (connection->objects);
1177 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
1178 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
1179 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
1181 _dbus_watch_list_free (connection->watches);
1182 connection->watches = NULL;
1184 _dbus_timeout_list_free (connection->timeouts);
1185 connection->timeouts = NULL;
1187 _dbus_data_slot_list_free (&connection->slot_list);
1189 link = _dbus_list_get_first_link (&connection->filter_list);
1190 while (link != NULL)
1192 DBusMessageFilter *filter = link->data;
1193 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
1195 filter->function = NULL;
1196 _dbus_message_filter_unref (filter); /* calls app callback */
1201 _dbus_list_clear (&connection->filter_list);
1203 /* ---- Done with stuff that invokes application callbacks */
1205 _dbus_object_tree_unref (connection->objects);
1207 _dbus_hash_table_unref (connection->pending_replies);
1208 connection->pending_replies = NULL;
1210 _dbus_list_clear (&connection->filter_list);
1212 _dbus_list_foreach (&connection->outgoing_messages,
1213 free_outgoing_message,
1215 _dbus_list_clear (&connection->outgoing_messages);
1217 _dbus_list_foreach (&connection->incoming_messages,
1218 (DBusForeachFunction) dbus_message_unref,
1220 _dbus_list_clear (&connection->incoming_messages);
1222 _dbus_counter_unref (connection->outgoing_counter);
1224 _dbus_transport_unref (connection->transport);
1226 if (connection->disconnect_message_link)
1228 DBusMessage *message = connection->disconnect_message_link->data;
1229 dbus_message_unref (message);
1230 _dbus_list_free_link (connection->disconnect_message_link);
1233 _dbus_list_clear (&connection->link_cache);
1235 dbus_condvar_free (connection->dispatch_cond);
1236 dbus_condvar_free (connection->io_path_cond);
1237 dbus_condvar_free (connection->message_returned_cond);
1239 dbus_mutex_free (connection->mutex);
1241 dbus_free (connection);
1245 * Decrements the reference count of a DBusConnection, and finalizes
1246 * it if the count reaches zero. It is a bug to drop the last reference
1247 * to a connection that has not been disconnected.
1249 * @todo in practice it can be quite tricky to never unref a connection
1250 * that's still connected; maybe there's some way we could avoid
1253 * @param connection the connection.
1256 dbus_connection_unref (DBusConnection *connection)
1258 dbus_bool_t last_unref;
1260 _dbus_return_if_fail (connection != NULL);
1262 /* The connection lock is better than the global
1263 * lock in the atomic increment fallback
1266 #ifdef DBUS_HAVE_ATOMIC_INT
1267 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
1269 CONNECTION_LOCK (connection);
1271 _dbus_assert (connection->refcount.value > 0);
1273 connection->refcount.value -= 1;
1274 last_unref = (connection->refcount.value == 0);
1277 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
1280 CONNECTION_UNLOCK (connection);
1284 _dbus_connection_last_unref (connection);
1288 * Closes the connection, so no further data can be sent or received.
1289 * Any further attempts to send data will result in errors. This
1290 * function does not affect the connection's reference count. It's
1291 * safe to disconnect a connection more than once; all calls after the
1292 * first do nothing. It's impossible to "reconnect" a connection, a
1293 * new connection must be created.
1295 * @param connection the connection.
1298 dbus_connection_disconnect (DBusConnection *connection)
1300 _dbus_return_if_fail (connection != NULL);
1302 CONNECTION_LOCK (connection);
1303 _dbus_transport_disconnect (connection->transport);
1304 CONNECTION_UNLOCK (connection);
1308 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
1310 return _dbus_transport_get_is_connected (connection->transport);
1314 * Gets whether the connection is currently connected. All
1315 * connections are connected when they are opened. A connection may
1316 * become disconnected when the remote application closes its end, or
1317 * exits; a connection may also be disconnected with
1318 * dbus_connection_disconnect().
1320 * @param connection the connection.
1321 * @returns #TRUE if the connection is still alive.
1324 dbus_connection_get_is_connected (DBusConnection *connection)
1328 _dbus_return_val_if_fail (connection != NULL, FALSE);
1330 CONNECTION_LOCK (connection);
1331 res = _dbus_connection_get_is_connected_unlocked (connection);
1332 CONNECTION_UNLOCK (connection);
1338 * Gets whether the connection was authenticated. (Note that
1339 * if the connection was authenticated then disconnected,
1340 * this function still returns #TRUE)
1342 * @param connection the connection
1343 * @returns #TRUE if the connection was ever authenticated
1346 dbus_connection_get_is_authenticated (DBusConnection *connection)
1350 _dbus_return_val_if_fail (connection != NULL, FALSE);
1352 CONNECTION_LOCK (connection);
1353 res = _dbus_transport_get_is_authenticated (connection->transport);
1354 CONNECTION_UNLOCK (connection);
1359 static DBusPreallocatedSend*
1360 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1362 DBusPreallocatedSend *preallocated;
1364 _dbus_return_val_if_fail (connection != NULL, NULL);
1366 preallocated = dbus_new (DBusPreallocatedSend, 1);
1367 if (preallocated == NULL)
1370 if (connection->link_cache != NULL)
1372 preallocated->queue_link =
1373 _dbus_list_pop_first_link (&connection->link_cache);
1374 preallocated->queue_link->data = NULL;
1378 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1379 if (preallocated->queue_link == NULL)
1383 if (connection->link_cache != NULL)
1385 preallocated->counter_link =
1386 _dbus_list_pop_first_link (&connection->link_cache);
1387 preallocated->counter_link->data = connection->outgoing_counter;
1391 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1392 if (preallocated->counter_link == NULL)
1396 _dbus_counter_ref (preallocated->counter_link->data);
1398 preallocated->connection = connection;
1400 return preallocated;
1403 _dbus_list_free_link (preallocated->queue_link);
1405 dbus_free (preallocated);
1411 * Preallocates resources needed to send a message, allowing the message
1412 * to be sent without the possibility of memory allocation failure.
1413 * Allows apps to create a future guarantee that they can send
1414 * a message regardless of memory shortages.
1416 * @param connection the connection we're preallocating for.
1417 * @returns the preallocated resources, or #NULL
1419 DBusPreallocatedSend*
1420 dbus_connection_preallocate_send (DBusConnection *connection)
1422 DBusPreallocatedSend *preallocated;
1424 _dbus_return_val_if_fail (connection != NULL, NULL);
1426 CONNECTION_LOCK (connection);
1429 _dbus_connection_preallocate_send_unlocked (connection);
1431 CONNECTION_UNLOCK (connection);
1433 return preallocated;
1437 * Frees preallocated message-sending resources from
1438 * dbus_connection_preallocate_send(). Should only
1439 * be called if the preallocated resources are not used
1440 * to send a message.
1442 * @param connection the connection
1443 * @param preallocated the resources
1446 dbus_connection_free_preallocated_send (DBusConnection *connection,
1447 DBusPreallocatedSend *preallocated)
1449 _dbus_return_if_fail (connection != NULL);
1450 _dbus_return_if_fail (preallocated != NULL);
1451 _dbus_return_if_fail (connection == preallocated->connection);
1453 _dbus_list_free_link (preallocated->queue_link);
1454 _dbus_counter_unref (preallocated->counter_link->data);
1455 _dbus_list_free_link (preallocated->counter_link);
1456 dbus_free (preallocated);
1460 _dbus_connection_send_preallocated_unlocked (DBusConnection *connection,
1461 DBusPreallocatedSend *preallocated,
1462 DBusMessage *message,
1463 dbus_uint32_t *client_serial)
1465 dbus_uint32_t serial;
1467 preallocated->queue_link->data = message;
1468 _dbus_list_prepend_link (&connection->outgoing_messages,
1469 preallocated->queue_link);
1471 _dbus_message_add_size_counter_link (message,
1472 preallocated->counter_link);
1474 dbus_free (preallocated);
1475 preallocated = NULL;
1477 dbus_message_ref (message);
1479 connection->n_outgoing += 1;
1481 _dbus_verbose ("Message %p (%s) added to outgoing queue %p, %d pending to send\n",
1483 dbus_message_get_interface (message) ?
1484 dbus_message_get_interface (message) :
1487 connection->n_outgoing);
1489 if (dbus_message_get_serial (message) == 0)
1491 serial = _dbus_connection_get_next_client_serial (connection);
1492 _dbus_message_set_serial (message, serial);
1494 *client_serial = serial;
1499 *client_serial = dbus_message_get_serial (message);
1502 _dbus_message_lock (message);
1504 if (connection->n_outgoing == 1)
1505 _dbus_transport_messages_pending (connection->transport,
1506 connection->n_outgoing);
1508 _dbus_connection_wakeup_mainloop (connection);
1512 * Sends a message using preallocated resources. This function cannot fail.
1513 * It works identically to dbus_connection_send() in other respects.
1514 * Preallocated resources comes from dbus_connection_preallocate_send().
1515 * This function "consumes" the preallocated resources, they need not
1516 * be freed separately.
1518 * @param connection the connection
1519 * @param preallocated the preallocated resources
1520 * @param message the message to send
1521 * @param client_serial return location for client serial assigned to the message
1524 dbus_connection_send_preallocated (DBusConnection *connection,
1525 DBusPreallocatedSend *preallocated,
1526 DBusMessage *message,
1527 dbus_uint32_t *client_serial)
1529 _dbus_return_if_fail (connection != NULL);
1530 _dbus_return_if_fail (preallocated != NULL);
1531 _dbus_return_if_fail (message != NULL);
1532 _dbus_return_if_fail (preallocated->connection == connection);
1533 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
1534 (dbus_message_get_interface (message) != NULL &&
1535 dbus_message_get_member (message) != NULL));
1536 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
1537 (dbus_message_get_interface (message) != NULL &&
1538 dbus_message_get_member (message) != NULL));
1540 CONNECTION_LOCK (connection);
1541 _dbus_connection_send_preallocated_unlocked (connection,
1543 message, client_serial);
1544 CONNECTION_UNLOCK (connection);
1548 _dbus_connection_send_unlocked (DBusConnection *connection,
1549 DBusMessage *message,
1550 dbus_uint32_t *client_serial)
1552 DBusPreallocatedSend *preallocated;
1554 _dbus_assert (connection != NULL);
1555 _dbus_assert (message != NULL);
1557 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
1558 if (preallocated == NULL)
1562 _dbus_connection_send_preallocated_unlocked (connection,
1570 * Adds a message to the outgoing message queue. Does not block to
1571 * write the message to the network; that happens asynchronously. To
1572 * force the message to be written, call dbus_connection_flush().
1573 * Because this only queues the message, the only reason it can
1574 * fail is lack of memory. Even if the connection is disconnected,
1575 * no error will be returned.
1577 * If the function fails due to lack of memory, it returns #FALSE.
1578 * The function will never fail for other reasons; even if the
1579 * connection is disconnected, you can queue an outgoing message,
1580 * though obviously it won't be sent.
1582 * @param connection the connection.
1583 * @param message the message to write.
1584 * @param client_serial return location for client serial.
1585 * @returns #TRUE on success.
1588 dbus_connection_send (DBusConnection *connection,
1589 DBusMessage *message,
1590 dbus_uint32_t *client_serial)
1592 _dbus_return_val_if_fail (connection != NULL, FALSE);
1593 _dbus_return_val_if_fail (message != NULL, FALSE);
1595 CONNECTION_LOCK (connection);
1597 if (!_dbus_connection_send_unlocked (connection, message, client_serial))
1599 CONNECTION_UNLOCK (connection);
1603 CONNECTION_UNLOCK (connection);
1608 reply_handler_timeout (void *data)
1610 DBusConnection *connection;
1611 DBusDispatchStatus status;
1612 DBusPendingCall *pending = data;
1614 connection = pending->connection;
1616 CONNECTION_LOCK (connection);
1617 if (pending->timeout_link)
1619 _dbus_connection_queue_synthesized_message_link (connection,
1620 pending->timeout_link);
1621 pending->timeout_link = NULL;
1624 _dbus_connection_remove_timeout (connection,
1626 pending->timeout_added = FALSE;
1628 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1630 /* Unlocks, and calls out to user code */
1631 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1637 * Queues a message to send, as with dbus_connection_send_message(),
1638 * but also returns a #DBusPendingCall used to receive a reply to the
1639 * message. If no reply is received in the given timeout_milliseconds,
1640 * this function expires the pending reply and generates a synthetic
1641 * error reply (generated in-process, not by the remote application)
1642 * indicating that a timeout occurred.
1644 * A #DBusPendingCall will see a reply message after any filters, but
1645 * before any object instances or other handlers. A #DBusPendingCall
1646 * will always see exactly one reply message, unless it's cancelled
1647 * with dbus_pending_call_cancel().
1649 * If a filter filters out the reply before the handler sees it, the
1650 * reply is immediately timed out and a timeout error reply is
1651 * generated. If a filter removes the timeout error reply then the
1652 * #DBusPendingCall will get confused. Filtering the timeout error
1653 * is thus considered a bug and will print a warning.
1655 * If #NULL is passed for the pending_return, the #DBusPendingCall
1656 * will still be generated internally, and used to track
1657 * the message reply timeout. This means a timeout error will
1658 * occur if no reply arrives, unlike with dbus_connection_send().
1660 * If -1 is passed for the timeout, a sane default timeout is used. -1
1661 * is typically the best value for the timeout for this reason, unless
1662 * you want a very short or very long timeout. There is no way to
1663 * avoid a timeout entirely, other than passing INT_MAX for the
1664 * timeout to postpone it indefinitely.
1666 * @param connection the connection
1667 * @param message the message to send
1668 * @param pending_return return location for a #DBusPendingCall object, or #NULL
1669 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1670 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1674 dbus_connection_send_with_reply (DBusConnection *connection,
1675 DBusMessage *message,
1676 DBusPendingCall **pending_return,
1677 int timeout_milliseconds)
1679 DBusPendingCall *pending;
1681 DBusList *reply_link;
1682 dbus_int32_t serial = -1;
1684 _dbus_return_val_if_fail (connection != NULL, FALSE);
1685 _dbus_return_val_if_fail (message != NULL, FALSE);
1686 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1689 *pending_return = NULL;
1691 pending = _dbus_pending_call_new (connection,
1692 timeout_milliseconds,
1693 reply_handler_timeout);
1695 if (pending == NULL)
1698 CONNECTION_LOCK (connection);
1700 /* Assign a serial to the message */
1701 if (dbus_message_get_serial (message) == 0)
1703 serial = _dbus_connection_get_next_client_serial (connection);
1704 _dbus_message_set_serial (message, serial);
1707 pending->reply_serial = serial;
1709 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
1710 "No reply within specified time");
1713 CONNECTION_UNLOCK (connection);
1714 dbus_pending_call_unref (pending);
1718 reply_link = _dbus_list_alloc_link (reply);
1721 CONNECTION_UNLOCK (connection);
1722 dbus_message_unref (reply);
1723 dbus_pending_call_unref (pending);
1727 pending->timeout_link = reply_link;
1729 /* Insert the serial in the pending replies hash;
1730 * hash takes a refcount on DBusPendingCall.
1731 * Also, add the timeout.
1733 if (!_dbus_connection_attach_pending_call_unlocked (connection,
1736 CONNECTION_UNLOCK (connection);
1737 dbus_pending_call_unref (pending);
1741 if (!_dbus_connection_send_unlocked (connection, message, NULL))
1743 _dbus_connection_detach_pending_call_and_unlock (connection,
1750 dbus_pending_call_ref (pending);
1751 *pending_return = pending;
1754 CONNECTION_UNLOCK (connection);
1760 check_for_reply_unlocked (DBusConnection *connection,
1761 dbus_uint32_t client_serial)
1765 link = _dbus_list_get_first_link (&connection->incoming_messages);
1767 while (link != NULL)
1769 DBusMessage *reply = link->data;
1771 if (dbus_message_get_reply_serial (reply) == client_serial)
1773 _dbus_list_remove_link (&connection->incoming_messages, link);
1774 connection->n_incoming -= 1;
1775 dbus_message_ref (reply);
1778 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1785 * Blocks a certain time period while waiting for a reply.
1786 * If no reply arrives, returns #NULL.
1788 * @todo could use performance improvements (it keeps scanning
1789 * the whole message queue for example) and has thread issues,
1790 * see comments in source
1792 * @param connection the connection
1793 * @param client_serial the reply serial to wait for
1794 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1795 * @returns the message that is the reply or #NULL if no reply
1798 _dbus_connection_block_for_reply (DBusConnection *connection,
1799 dbus_uint32_t client_serial,
1800 int timeout_milliseconds)
1802 long start_tv_sec, start_tv_usec;
1803 long end_tv_sec, end_tv_usec;
1804 long tv_sec, tv_usec;
1805 DBusDispatchStatus status;
1807 _dbus_return_val_if_fail (connection != NULL, NULL);
1808 _dbus_return_val_if_fail (client_serial != 0, NULL);
1809 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1811 if (timeout_milliseconds == -1)
1812 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
1814 /* it would probably seem logical to pass in _DBUS_INT_MAX
1815 * for infinite timeout, but then math below would get
1816 * all overflow-prone, so smack that down.
1818 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1819 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1821 /* Flush message queue */
1822 dbus_connection_flush (connection);
1824 CONNECTION_LOCK (connection);
1826 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1827 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1828 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1829 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1830 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1832 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec to %ld sec %ld usec\n",
1833 timeout_milliseconds,
1835 start_tv_sec, start_tv_usec,
1836 end_tv_sec, end_tv_usec);
1838 /* Now we wait... */
1839 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1840 * gets the message before we do?
1842 /* always block at least once as we know we don't have the reply yet */
1843 _dbus_connection_do_iteration (connection,
1844 DBUS_ITERATION_DO_READING |
1845 DBUS_ITERATION_BLOCK,
1846 timeout_milliseconds);
1850 /* queue messages and get status */
1851 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1853 if (status == DBUS_DISPATCH_DATA_REMAINS)
1857 reply = check_for_reply_unlocked (connection, client_serial);
1860 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1862 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
1864 /* Unlocks, and calls out to user code */
1865 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1871 _dbus_get_current_time (&tv_sec, &tv_usec);
1873 if (tv_sec < start_tv_sec)
1874 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
1875 else if (connection->disconnect_message_link == NULL)
1876 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
1877 else if (tv_sec < end_tv_sec ||
1878 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1880 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1881 (end_tv_usec - tv_usec) / 1000;
1882 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1883 _dbus_assert (timeout_milliseconds >= 0);
1885 if (status == DBUS_DISPATCH_NEED_MEMORY)
1887 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1888 * we may already have a reply in the buffer and just can't process
1891 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1893 if (timeout_milliseconds < 100)
1894 ; /* just busy loop */
1895 else if (timeout_milliseconds <= 1000)
1896 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1898 _dbus_sleep_milliseconds (1000);
1902 /* block again, we don't have the reply buffered yet. */
1903 _dbus_connection_do_iteration (connection,
1904 DBUS_ITERATION_DO_READING |
1905 DBUS_ITERATION_BLOCK,
1906 timeout_milliseconds);
1909 goto recheck_status;
1912 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1913 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1915 /* unlocks and calls out to user code */
1916 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1922 * Sends a message and blocks a certain time period while waiting for
1923 * a reply. This function does not reenter the main loop,
1924 * i.e. messages other than the reply are queued up but not
1925 * processed. This function is used to do non-reentrant "method
1928 * If a normal reply is received, it is returned, and removed from the
1929 * incoming message queue. If it is not received, #NULL is returned
1930 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is
1931 * received, it is converted to a #DBusError and returned as an error,
1932 * then the reply message is deleted. If something else goes wrong,
1933 * result is set to whatever is appropriate, such as
1934 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1936 * @param connection the connection
1937 * @param message the message to send
1938 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1939 * @param error return location for error message
1940 * @returns the message that is the reply or #NULL with an error code if the
1944 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1945 DBusMessage *message,
1946 int timeout_milliseconds,
1949 dbus_uint32_t client_serial;
1952 _dbus_return_val_if_fail (connection != NULL, NULL);
1953 _dbus_return_val_if_fail (message != NULL, NULL);
1954 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
1955 _dbus_return_val_if_error_is_set (error, NULL);
1957 if (!dbus_connection_send (connection, message, &client_serial))
1959 _DBUS_SET_OOM (error);
1963 reply = _dbus_connection_block_for_reply (connection,
1965 timeout_milliseconds);
1969 if (dbus_connection_get_is_connected (connection))
1970 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
1972 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
1976 else if (dbus_set_error_from_message (error, reply))
1978 dbus_message_unref (reply);
1986 * Blocks until the outgoing message queue is empty.
1988 * @param connection the connection.
1991 dbus_connection_flush (DBusConnection *connection)
1993 /* We have to specify DBUS_ITERATION_DO_READING here because
1994 * otherwise we could have two apps deadlock if they are both doing
1995 * a flush(), and the kernel buffers fill up. This could change the
1998 DBusDispatchStatus status;
2000 _dbus_return_if_fail (connection != NULL);
2002 CONNECTION_LOCK (connection);
2003 while (connection->n_outgoing > 0 &&
2004 _dbus_connection_get_is_connected_unlocked (connection))
2005 _dbus_connection_do_iteration (connection,
2006 DBUS_ITERATION_DO_READING |
2007 DBUS_ITERATION_DO_WRITING |
2008 DBUS_ITERATION_BLOCK,
2011 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2013 /* Unlocks and calls out to user code */
2014 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2017 /* Call with mutex held. Will drop it while waiting and re-acquire
2021 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
2023 _dbus_assert (connection->message_borrowed != NULL);
2025 while (connection->message_borrowed != NULL)
2026 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
2030 * Returns the first-received message from the incoming message queue,
2031 * leaving it in the queue. If the queue is empty, returns #NULL.
2033 * The caller does not own a reference to the returned message, and
2034 * must either return it using dbus_connection_return_message() or
2035 * keep it after calling dbus_connection_steal_borrowed_message(). No
2036 * one can get at the message while its borrowed, so return it as
2037 * quickly as possible and don't keep a reference to it after
2038 * returning it. If you need to keep the message, make a copy of it.
2040 * @param connection the connection.
2041 * @returns next message in the incoming queue.
2044 dbus_connection_borrow_message (DBusConnection *connection)
2046 DBusMessage *message;
2047 DBusDispatchStatus status;
2049 _dbus_return_val_if_fail (connection != NULL, NULL);
2050 /* can't borrow during dispatch */
2051 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
2053 /* this is called for the side effect that it queues
2054 * up any messages from the transport
2056 status = dbus_connection_get_dispatch_status (connection);
2057 if (status != DBUS_DISPATCH_DATA_REMAINS)
2060 CONNECTION_LOCK (connection);
2062 if (connection->message_borrowed != NULL)
2063 _dbus_connection_wait_for_borrowed (connection);
2065 message = _dbus_list_get_first (&connection->incoming_messages);
2068 connection->message_borrowed = message;
2070 CONNECTION_UNLOCK (connection);
2075 * Used to return a message after peeking at it using
2076 * dbus_connection_borrow_message().
2078 * @param connection the connection
2079 * @param message the message from dbus_connection_borrow_message()
2082 dbus_connection_return_message (DBusConnection *connection,
2083 DBusMessage *message)
2085 _dbus_return_if_fail (connection != NULL);
2086 _dbus_return_if_fail (message != NULL);
2087 /* can't borrow during dispatch */
2088 _dbus_return_if_fail (!connection->dispatch_acquired);
2090 CONNECTION_LOCK (connection);
2092 _dbus_assert (message == connection->message_borrowed);
2094 connection->message_borrowed = NULL;
2095 dbus_condvar_wake_all (connection->message_returned_cond);
2097 CONNECTION_UNLOCK (connection);
2101 * Used to keep a message after peeking at it using
2102 * dbus_connection_borrow_message(). Before using this function, see
2103 * the caveats/warnings in the documentation for
2104 * dbus_connection_pop_message().
2106 * @param connection the connection
2107 * @param message the message from dbus_connection_borrow_message()
2110 dbus_connection_steal_borrowed_message (DBusConnection *connection,
2111 DBusMessage *message)
2113 DBusMessage *pop_message;
2115 _dbus_return_if_fail (connection != NULL);
2116 _dbus_return_if_fail (message != NULL);
2117 /* can't borrow during dispatch */
2118 _dbus_return_if_fail (!connection->dispatch_acquired);
2120 CONNECTION_LOCK (connection);
2122 _dbus_assert (message == connection->message_borrowed);
2124 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
2125 _dbus_assert (message == pop_message);
2127 connection->n_incoming -= 1;
2129 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
2130 message, connection->n_incoming);
2132 connection->message_borrowed = NULL;
2133 dbus_condvar_wake_all (connection->message_returned_cond);
2135 CONNECTION_UNLOCK (connection);
2138 /* See dbus_connection_pop_message, but requires the caller to own
2139 * the lock before calling. May drop the lock while running.
2142 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
2144 if (connection->message_borrowed != NULL)
2145 _dbus_connection_wait_for_borrowed (connection);
2147 if (connection->n_incoming > 0)
2151 link = _dbus_list_pop_first_link (&connection->incoming_messages);
2152 connection->n_incoming -= 1;
2154 _dbus_verbose ("Message %p (%s) removed from incoming queue %p, %d incoming\n",
2156 dbus_message_get_interface (link->data) ?
2157 dbus_message_get_interface (link->data) :
2159 connection, connection->n_incoming);
2167 /* See dbus_connection_pop_message, but requires the caller to own
2168 * the lock before calling. May drop the lock while running.
2171 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
2175 link = _dbus_connection_pop_message_link_unlocked (connection);
2179 DBusMessage *message;
2181 message = link->data;
2183 _dbus_list_free_link (link);
2192 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
2193 DBusList *message_link)
2195 _dbus_assert (message_link != NULL);
2196 /* You can't borrow a message while a link is outstanding */
2197 _dbus_assert (connection->message_borrowed == NULL);
2199 _dbus_list_prepend_link (&connection->incoming_messages,
2201 connection->n_incoming += 1;
2203 _dbus_verbose ("Message %p (%s) put back into queue %p, %d incoming\n",
2205 dbus_message_get_interface (message_link->data) ?
2206 dbus_message_get_interface (message_link->data) :
2208 connection, connection->n_incoming);
2212 * Returns the first-received message from the incoming message queue,
2213 * removing it from the queue. The caller owns a reference to the
2214 * returned message. If the queue is empty, returns #NULL.
2216 * This function bypasses any message handlers that are registered,
2217 * and so using it is usually wrong. Instead, let the main loop invoke
2218 * dbus_connection_dispatch(). Popping messages manually is only
2219 * useful in very simple programs that don't share a #DBusConnection
2220 * with any libraries or other modules.
2222 * @param connection the connection.
2223 * @returns next message in the incoming queue.
2226 dbus_connection_pop_message (DBusConnection *connection)
2228 DBusMessage *message;
2229 DBusDispatchStatus status;
2231 /* this is called for the side effect that it queues
2232 * up any messages from the transport
2234 status = dbus_connection_get_dispatch_status (connection);
2235 if (status != DBUS_DISPATCH_DATA_REMAINS)
2238 CONNECTION_LOCK (connection);
2240 message = _dbus_connection_pop_message_unlocked (connection);
2242 _dbus_verbose ("Returning popped message %p\n", message);
2244 CONNECTION_UNLOCK (connection);
2250 * Acquire the dispatcher. This must be done before dispatching
2251 * messages in order to guarantee the right order of
2252 * message delivery. May sleep and drop the connection mutex
2253 * while waiting for the dispatcher.
2255 * @param connection the connection.
2258 _dbus_connection_acquire_dispatch (DBusConnection *connection)
2260 if (connection->dispatch_acquired)
2261 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
2262 _dbus_assert (!connection->dispatch_acquired);
2264 connection->dispatch_acquired = TRUE;
2268 * Release the dispatcher when you're done with it. Only call
2269 * after you've acquired the dispatcher. Wakes up at most one
2270 * thread currently waiting to acquire the dispatcher.
2272 * @param connection the connection.
2275 _dbus_connection_release_dispatch (DBusConnection *connection)
2277 _dbus_assert (connection->dispatch_acquired);
2279 connection->dispatch_acquired = FALSE;
2280 dbus_condvar_wake_one (connection->dispatch_cond);
2284 _dbus_connection_failed_pop (DBusConnection *connection,
2285 DBusList *message_link)
2287 _dbus_list_prepend_link (&connection->incoming_messages,
2289 connection->n_incoming += 1;
2292 static DBusDispatchStatus
2293 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
2295 if (connection->n_incoming > 0)
2296 return DBUS_DISPATCH_DATA_REMAINS;
2297 else if (!_dbus_transport_queue_messages (connection->transport))
2298 return DBUS_DISPATCH_NEED_MEMORY;
2301 DBusDispatchStatus status;
2303 status = _dbus_transport_get_dispatch_status (connection->transport);
2305 if (status != DBUS_DISPATCH_COMPLETE)
2307 else if (connection->n_incoming > 0)
2308 return DBUS_DISPATCH_DATA_REMAINS;
2310 return DBUS_DISPATCH_COMPLETE;
2315 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
2316 DBusDispatchStatus new_status)
2318 dbus_bool_t changed;
2319 DBusDispatchStatusFunction function;
2322 /* We have the lock */
2324 _dbus_connection_ref_unlocked (connection);
2326 changed = new_status != connection->last_dispatch_status;
2328 connection->last_dispatch_status = new_status;
2330 function = connection->dispatch_status_function;
2331 data = connection->dispatch_status_data;
2333 /* We drop the lock */
2334 CONNECTION_UNLOCK (connection);
2336 if (changed && function)
2338 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
2339 connection, new_status,
2340 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
2341 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
2342 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
2344 (* function) (connection, new_status, data);
2347 dbus_connection_unref (connection);
2351 * Gets the current state (what we would currently return
2352 * from dbus_connection_dispatch()) but doesn't actually
2353 * dispatch any messages.
2355 * @param connection the connection.
2356 * @returns current dispatch status
2359 dbus_connection_get_dispatch_status (DBusConnection *connection)
2361 DBusDispatchStatus status;
2363 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2365 CONNECTION_LOCK (connection);
2367 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2369 CONNECTION_UNLOCK (connection);
2375 * Processes data buffered while handling watches, queueing zero or
2376 * more incoming messages. Then pops the first-received message from
2377 * the current incoming message queue, runs any handlers for it, and
2378 * unrefs the message. Returns a status indicating whether messages/data
2379 * remain, more memory is needed, or all data has been processed.
2381 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS,
2382 * does not necessarily dispatch a message, as the data may
2383 * be part of authentication or the like.
2385 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
2387 * @todo right now a message filter gets run on replies to a pending
2388 * call in here, but not in the case where we block without
2389 * entering the main loop.
2391 * @param connection the connection
2392 * @returns dispatch status
2395 dbus_connection_dispatch (DBusConnection *connection)
2397 DBusMessage *message;
2398 DBusList *link, *filter_list_copy, *message_link;
2399 DBusHandlerResult result;
2400 DBusPendingCall *pending;
2401 dbus_int32_t reply_serial;
2402 DBusDispatchStatus status;
2404 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
2406 CONNECTION_LOCK (connection);
2407 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2408 if (status != DBUS_DISPATCH_DATA_REMAINS)
2410 /* unlocks and calls out to user code */
2411 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2415 /* We need to ref the connection since the callback could potentially
2416 * drop the last ref to it
2418 _dbus_connection_ref_unlocked (connection);
2420 _dbus_connection_acquire_dispatch (connection);
2422 /* This call may drop the lock during the execution (if waiting for
2423 * borrowed messages to be returned) but the order of message
2424 * dispatch if several threads call dispatch() is still
2425 * protected by the lock, since only one will get the lock, and that
2426 * one will finish the message dispatching
2428 message_link = _dbus_connection_pop_message_link_unlocked (connection);
2429 if (message_link == NULL)
2431 /* another thread dispatched our stuff */
2433 _dbus_connection_release_dispatch (connection);
2435 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2437 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2439 dbus_connection_unref (connection);
2444 message = message_link->data;
2446 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2448 reply_serial = dbus_message_get_reply_serial (message);
2449 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
2452 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
2454 _dbus_connection_release_dispatch (connection);
2456 _dbus_connection_failed_pop (connection, message_link);
2458 /* unlocks and calls user code */
2459 _dbus_connection_update_dispatch_status_and_unlock (connection,
2460 DBUS_DISPATCH_NEED_MEMORY);
2462 dbus_connection_unref (connection);
2464 return DBUS_DISPATCH_NEED_MEMORY;
2467 _dbus_list_foreach (&filter_list_copy,
2468 (DBusForeachFunction)_dbus_message_filter_ref,
2471 /* We're still protected from dispatch() reentrancy here
2472 * since we acquired the dispatcher
2474 CONNECTION_UNLOCK (connection);
2476 link = _dbus_list_get_first_link (&filter_list_copy);
2477 while (link != NULL)
2479 DBusMessageFilter *filter = link->data;
2480 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
2482 _dbus_verbose (" running filter on message %p\n", message);
2483 result = (* filter->function) (connection, message, filter->user_data);
2485 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2491 _dbus_list_foreach (&filter_list_copy,
2492 (DBusForeachFunction)_dbus_message_filter_unref,
2494 _dbus_list_clear (&filter_list_copy);
2496 CONNECTION_LOCK (connection);
2498 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2501 /* Did a reply we were waiting on get filtered? */
2502 if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
2504 /* Queue the timeout immediately! */
2505 if (pending->timeout_link)
2507 _dbus_connection_queue_synthesized_message_link (connection,
2508 pending->timeout_link);
2509 pending->timeout_link = NULL;
2513 /* We already queued the timeout? Then it was filtered! */
2514 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
2518 if (result == DBUS_HANDLER_RESULT_HANDLED)
2523 _dbus_pending_call_complete_and_unlock (pending, message);
2527 CONNECTION_LOCK (connection);
2531 /* We're still protected from dispatch() reentrancy here
2532 * since we acquired the dispatcher
2534 _dbus_verbose (" running object path dispatch on message %p (%s)\n",
2536 dbus_message_get_interface (message) ?
2537 dbus_message_get_interface (message) :
2540 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
2543 CONNECTION_LOCK (connection);
2545 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
2548 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
2552 DBusPreallocatedSend *preallocated;
2554 _dbus_verbose (" sending error %s\n",
2555 DBUS_ERROR_UNKNOWN_METHOD);
2557 if (!_dbus_string_init (&str))
2559 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2563 if (!_dbus_string_append_printf (&str,
2564 "Method \"%s\" on interface \"%s\" doesn't exist\n",
2565 dbus_message_get_member (message),
2566 dbus_message_get_interface (message)))
2568 _dbus_string_free (&str);
2569 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2573 reply = dbus_message_new_error (message,
2574 DBUS_ERROR_UNKNOWN_METHOD,
2575 _dbus_string_get_const_data (&str));
2576 _dbus_string_free (&str);
2580 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2584 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2586 if (preallocated == NULL)
2588 dbus_message_unref (reply);
2589 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
2593 _dbus_connection_send_preallocated_unlocked (connection, preallocated,
2596 dbus_message_unref (reply);
2598 result = DBUS_HANDLER_RESULT_HANDLED;
2601 _dbus_verbose (" done dispatching %p (%s) on connection %p\n", message,
2602 dbus_message_get_interface (message) ?
2603 dbus_message_get_interface (message) :
2608 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
2610 /* Put message back, and we'll start over.
2611 * Yes this means handlers must be idempotent if they
2612 * don't return HANDLED; c'est la vie.
2614 _dbus_connection_putback_message_link_unlocked (connection,
2619 _dbus_list_free_link (message_link);
2620 dbus_message_unref (message); /* don't want the message to count in max message limits
2621 * in computing dispatch status below
2625 _dbus_connection_release_dispatch (connection);
2627 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2629 /* unlocks and calls user code */
2630 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2632 dbus_connection_unref (connection);
2638 * Sets the watch functions for the connection. These functions are
2639 * responsible for making the application's main loop aware of file
2640 * descriptors that need to be monitored for events, using select() or
2641 * poll(). When using Qt, typically the DBusAddWatchFunction would
2642 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2643 * could call g_io_add_watch(), or could be used as part of a more
2644 * elaborate GSource. Note that when a watch is added, it may
2647 * The DBusWatchToggledFunction notifies the application that the
2648 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2649 * to check this. A disabled watch should have no effect, and enabled
2650 * watch should be added to the main loop. This feature is used
2651 * instead of simply adding/removing the watch because
2652 * enabling/disabling can be done without memory allocation. The
2653 * toggled function may be NULL if a main loop re-queries
2654 * dbus_watch_get_enabled() every time anyway.
2656 * The DBusWatch can be queried for the file descriptor to watch using
2657 * dbus_watch_get_fd(), and for the events to watch for using
2658 * dbus_watch_get_flags(). The flags returned by
2659 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2660 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2661 * all watches implicitly include a watch for hangups, errors, and
2662 * other exceptional conditions.
2664 * Once a file descriptor becomes readable or writable, or an exception
2665 * occurs, dbus_watch_handle() should be called to
2666 * notify the connection of the file descriptor's condition.
2668 * dbus_watch_handle() cannot be called during the
2669 * DBusAddWatchFunction, as the connection will not be ready to handle
2672 * It is not allowed to reference a DBusWatch after it has been passed
2673 * to remove_function.
2675 * If #FALSE is returned due to lack of memory, the failure may be due
2676 * to a #FALSE return from the new add_function. If so, the
2677 * add_function may have been called successfully one or more times,
2678 * but the remove_function will also have been called to remove any
2679 * successful adds. i.e. if #FALSE is returned the net result
2680 * should be that dbus_connection_set_watch_functions() has no effect,
2681 * but the add_function and remove_function may have been called.
2683 * @todo We need to drop the lock when we call the
2684 * add/remove/toggled functions which can be a side effect
2685 * of setting the watch functions.
2687 * @param connection the connection.
2688 * @param add_function function to begin monitoring a new descriptor.
2689 * @param remove_function function to stop monitoring a descriptor.
2690 * @param toggled_function function to notify of enable/disable
2691 * @param data data to pass to add_function and remove_function.
2692 * @param free_data_function function to be called to free the data.
2693 * @returns #FALSE on failure (no memory)
2696 dbus_connection_set_watch_functions (DBusConnection *connection,
2697 DBusAddWatchFunction add_function,
2698 DBusRemoveWatchFunction remove_function,
2699 DBusWatchToggledFunction toggled_function,
2701 DBusFreeFunction free_data_function)
2705 _dbus_return_val_if_fail (connection != NULL, FALSE);
2707 CONNECTION_LOCK (connection);
2708 /* ref connection for slightly better reentrancy */
2709 _dbus_connection_ref_unlocked (connection);
2711 /* FIXME this can call back into user code, and we need to drop the
2712 * connection lock when it does.
2714 retval = _dbus_watch_list_set_functions (connection->watches,
2715 add_function, remove_function,
2717 data, free_data_function);
2719 CONNECTION_UNLOCK (connection);
2720 /* drop our paranoid refcount */
2721 dbus_connection_unref (connection);
2727 * Sets the timeout functions for the connection. These functions are
2728 * responsible for making the application's main loop aware of timeouts.
2729 * When using Qt, typically the DBusAddTimeoutFunction would create a
2730 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2733 * The DBusTimeoutToggledFunction notifies the application that the
2734 * timeout has been enabled or disabled. Call
2735 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2736 * have no effect, and enabled timeout should be added to the main
2737 * loop. This feature is used instead of simply adding/removing the
2738 * timeout because enabling/disabling can be done without memory
2739 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2740 * to enable and disable. The toggled function may be NULL if a main
2741 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2742 * Whenever a timeout is toggled, its interval may change.
2744 * The DBusTimeout can be queried for the timer interval using
2745 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called
2746 * repeatedly, each time the interval elapses, starting after it has
2747 * elapsed once. The timeout stops firing when it is removed with the
2748 * given remove_function. The timer interval may change whenever the
2749 * timeout is added, removed, or toggled.
2751 * @param connection the connection.
2752 * @param add_function function to add a timeout.
2753 * @param remove_function function to remove a timeout.
2754 * @param toggled_function function to notify of enable/disable
2755 * @param data data to pass to add_function and remove_function.
2756 * @param free_data_function function to be called to free the data.
2757 * @returns #FALSE on failure (no memory)
2760 dbus_connection_set_timeout_functions (DBusConnection *connection,
2761 DBusAddTimeoutFunction add_function,
2762 DBusRemoveTimeoutFunction remove_function,
2763 DBusTimeoutToggledFunction toggled_function,
2765 DBusFreeFunction free_data_function)
2769 _dbus_return_val_if_fail (connection != NULL, FALSE);
2771 CONNECTION_LOCK (connection);
2772 /* ref connection for slightly better reentrancy */
2773 _dbus_connection_ref_unlocked (connection);
2775 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2776 add_function, remove_function,
2778 data, free_data_function);
2780 CONNECTION_UNLOCK (connection);
2781 /* drop our paranoid refcount */
2782 dbus_connection_unref (connection);
2788 * Sets the mainloop wakeup function for the connection. Thi function is
2789 * responsible for waking up the main loop (if its sleeping) when some some
2790 * change has happened to the connection that the mainloop needs to reconsiders
2791 * (e.g. a message has been queued for writing).
2792 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2793 * When using GLib, it would call g_main_context_wakeup().
2796 * @param connection the connection.
2797 * @param wakeup_main_function function to wake up the mainloop
2798 * @param data data to pass wakeup_main_function
2799 * @param free_data_function function to be called to free the data.
2802 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2803 DBusWakeupMainFunction wakeup_main_function,
2805 DBusFreeFunction free_data_function)
2808 DBusFreeFunction old_free_data;
2810 _dbus_return_if_fail (connection != NULL);
2812 CONNECTION_LOCK (connection);
2813 old_data = connection->wakeup_main_data;
2814 old_free_data = connection->free_wakeup_main_data;
2816 connection->wakeup_main_function = wakeup_main_function;
2817 connection->wakeup_main_data = data;
2818 connection->free_wakeup_main_data = free_data_function;
2820 CONNECTION_UNLOCK (connection);
2822 /* Callback outside the lock */
2824 (*old_free_data) (old_data);
2828 * Set a function to be invoked when the dispatch status changes.
2829 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then
2830 * dbus_connection_dispatch() needs to be called to process incoming
2831 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED
2832 * from inside the DBusDispatchStatusFunction. Indeed, almost
2833 * any reentrancy in this function is a bad idea. Instead,
2834 * the DBusDispatchStatusFunction should simply save an indication
2835 * that messages should be dispatched later, when the main loop
2838 * @param connection the connection
2839 * @param function function to call on dispatch status changes
2840 * @param data data for function
2841 * @param free_data_function free the function data
2844 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
2845 DBusDispatchStatusFunction function,
2847 DBusFreeFunction free_data_function)
2850 DBusFreeFunction old_free_data;
2852 _dbus_return_if_fail (connection != NULL);
2854 CONNECTION_LOCK (connection);
2855 old_data = connection->dispatch_status_data;
2856 old_free_data = connection->free_dispatch_status_data;
2858 connection->dispatch_status_function = function;
2859 connection->dispatch_status_data = data;
2860 connection->free_dispatch_status_data = free_data_function;
2862 CONNECTION_UNLOCK (connection);
2864 /* Callback outside the lock */
2866 (*old_free_data) (old_data);
2870 * Gets the UNIX user ID of the connection if any.
2871 * Returns #TRUE if the uid is filled in.
2872 * Always returns #FALSE on non-UNIX platforms.
2873 * Always returns #FALSE prior to authenticating the
2876 * @param connection the connection
2877 * @param uid return location for the user ID
2878 * @returns #TRUE if uid is filled in with a valid user ID
2881 dbus_connection_get_unix_user (DBusConnection *connection,
2886 _dbus_return_val_if_fail (connection != NULL, FALSE);
2887 _dbus_return_val_if_fail (uid != NULL, FALSE);
2889 CONNECTION_LOCK (connection);
2891 if (!_dbus_transport_get_is_authenticated (connection->transport))
2894 result = _dbus_transport_get_unix_user (connection->transport,
2896 CONNECTION_UNLOCK (connection);
2902 * Sets a predicate function used to determine whether a given user ID
2903 * is allowed to connect. When an incoming connection has
2904 * authenticated with a particular user ID, this function is called;
2905 * if it returns #TRUE, the connection is allowed to proceed,
2906 * otherwise the connection is disconnected.
2908 * If the function is set to #NULL (as it is by default), then
2909 * only the same UID as the server process will be allowed to
2912 * @param connection the connection
2913 * @param function the predicate
2914 * @param data data to pass to the predicate
2915 * @param free_data_function function to free the data
2918 dbus_connection_set_unix_user_function (DBusConnection *connection,
2919 DBusAllowUnixUserFunction function,
2921 DBusFreeFunction free_data_function)
2923 void *old_data = NULL;
2924 DBusFreeFunction old_free_function = NULL;
2926 _dbus_return_if_fail (connection != NULL);
2928 CONNECTION_LOCK (connection);
2929 _dbus_transport_set_unix_user_function (connection->transport,
2930 function, data, free_data_function,
2931 &old_data, &old_free_function);
2932 CONNECTION_UNLOCK (connection);
2934 if (old_free_function != NULL)
2935 (* old_free_function) (old_data);
2939 * Adds a message filter. Filters are handlers that are run on all
2940 * incoming messages, prior to the objects registered with
2941 * dbus_connection_register_object_path(). Filters are run in the
2942 * order that they were added. The same handler can be added as a
2943 * filter more than once, in which case it will be run more than once.
2944 * Filters added during a filter callback won't be run on the message
2947 * @todo we don't run filters on messages while blocking without
2948 * entering the main loop, since filters are run as part of
2949 * dbus_connection_dispatch().
2951 * @param connection the connection
2952 * @param function function to handle messages
2953 * @param user_data user data to pass to the function
2954 * @param free_data_function function to use for freeing user data
2955 * @returns #TRUE on success, #FALSE if not enough memory.
2958 dbus_connection_add_filter (DBusConnection *connection,
2959 DBusHandleMessageFunction function,
2961 DBusFreeFunction free_data_function)
2963 DBusMessageFilter *filter;
2965 _dbus_return_val_if_fail (connection != NULL, FALSE);
2966 _dbus_return_val_if_fail (function != NULL, FALSE);
2968 filter = dbus_new0 (DBusMessageFilter, 1);
2972 filter->refcount.value = 1;
2974 CONNECTION_LOCK (connection);
2976 if (!_dbus_list_append (&connection->filter_list,
2979 _dbus_message_filter_unref (filter);
2980 CONNECTION_UNLOCK (connection);
2984 /* Fill in filter after all memory allocated,
2985 * so we don't run the free_user_data_function
2986 * if the add_filter() fails
2989 filter->function = function;
2990 filter->user_data = user_data;
2991 filter->free_user_data_function = free_data_function;
2993 CONNECTION_UNLOCK (connection);
2998 * Removes a previously-added message filter. It is a programming
2999 * error to call this function for a handler that has not been added
3000 * as a filter. If the given handler was added more than once, only
3001 * one instance of it will be removed (the most recently-added
3004 * @param connection the connection
3005 * @param function the handler to remove
3006 * @param user_data user data for the handler to remove
3010 dbus_connection_remove_filter (DBusConnection *connection,
3011 DBusHandleMessageFunction function,
3015 DBusMessageFilter *filter;
3017 _dbus_return_if_fail (connection != NULL);
3018 _dbus_return_if_fail (function != NULL);
3020 CONNECTION_LOCK (connection);
3024 link = _dbus_list_get_last_link (&connection->filter_list);
3025 while (link != NULL)
3027 filter = link->data;
3029 if (filter->function == function &&
3030 filter->user_data == user_data)
3032 _dbus_list_remove_link (&connection->filter_list, link);
3033 filter->function = NULL;
3038 link = _dbus_list_get_prev_link (&connection->filter_list, link);
3041 CONNECTION_UNLOCK (connection);
3043 #ifndef DBUS_DISABLE_CHECKS
3046 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
3047 function, user_data);
3052 /* Call application code */
3053 if (filter->free_user_data_function)
3054 (* filter->free_user_data_function) (filter->user_data);
3056 filter->free_user_data_function = NULL;
3057 filter->user_data = NULL;
3059 _dbus_message_filter_unref (filter);
3063 * Registers a handler for a given path in the object hierarchy.
3064 * The given vtable handles messages sent to exactly the given path.
3067 * @param connection the connection
3068 * @param path #NULL-terminated array of path elements
3069 * @param vtable the virtual table
3070 * @param user_data data to pass to functions in the vtable
3071 * @returns #FALSE if not enough memory
3074 dbus_connection_register_object_path (DBusConnection *connection,
3076 const DBusObjectPathVTable *vtable,
3081 _dbus_return_val_if_fail (connection != NULL, FALSE);
3082 _dbus_return_val_if_fail (path != NULL, FALSE);
3083 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3084 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3086 CONNECTION_LOCK (connection);
3088 retval = _dbus_object_tree_register (connection->objects,
3093 CONNECTION_UNLOCK (connection);
3099 * Registers a fallback handler for a given subsection of the object
3100 * hierarchy. The given vtable handles messages at or below the given
3101 * path. You can use this to establish a default message handling
3102 * policy for a whole "subdirectory."
3104 * @param connection the connection
3105 * @param path #NULL-terminated array of path elements
3106 * @param vtable the virtual table
3107 * @param user_data data to pass to functions in the vtable
3108 * @returns #FALSE if not enough memory
3111 dbus_connection_register_fallback (DBusConnection *connection,
3113 const DBusObjectPathVTable *vtable,
3118 _dbus_return_val_if_fail (connection != NULL, FALSE);
3119 _dbus_return_val_if_fail (path != NULL, FALSE);
3120 _dbus_return_val_if_fail (path[0] != NULL, FALSE);
3121 _dbus_return_val_if_fail (vtable != NULL, FALSE);
3123 CONNECTION_LOCK (connection);
3125 retval = _dbus_object_tree_register (connection->objects,
3130 CONNECTION_UNLOCK (connection);
3136 * Unregisters the handler registered with exactly the given path.
3137 * It's a bug to call this function for a path that isn't registered.
3138 * Can unregister both fallback paths and object paths.
3140 * @param connection the connection
3141 * @param path the #NULL-terminated array of path elements
3144 dbus_connection_unregister_object_path (DBusConnection *connection,
3147 _dbus_return_if_fail (connection != NULL);
3148 _dbus_return_if_fail (path != NULL);
3149 _dbus_return_if_fail (path[0] != NULL);
3151 CONNECTION_LOCK (connection);
3153 return _dbus_object_tree_unregister_and_unlock (connection->objects,
3158 * Lists the registered fallback handlers and object path handlers at
3159 * the given parent_path. The returned array should be freed with
3160 * dbus_free_string_array().
3162 * @param connection the connection
3163 * @param parent_path the path to list the child handlers of
3164 * @param child_entries returns #NULL-terminated array of children
3165 * @returns #FALSE if no memory to allocate the child entries
3168 dbus_connection_list_registered (DBusConnection *connection,
3169 const char **parent_path,
3170 char ***child_entries)
3172 _dbus_return_val_if_fail (connection != NULL, FALSE);
3173 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
3174 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
3176 CONNECTION_LOCK (connection);
3178 return _dbus_object_tree_list_registered_and_unlock (connection->objects,
3183 static DBusDataSlotAllocator slot_allocator;
3184 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
3187 * Allocates an integer ID to be used for storing application-specific
3188 * data on any DBusConnection. The allocated ID may then be used
3189 * with dbus_connection_set_data() and dbus_connection_get_data().
3190 * The passed-in slot must be initialized to -1, and is filled in
3191 * with the slot ID. If the passed-in slot is not -1, it's assumed
3192 * to be already allocated, and its refcount is incremented.
3194 * The allocated slot is global, i.e. all DBusConnection objects will
3195 * have a slot with the given integer ID reserved.
3197 * @param slot_p address of a global variable storing the slot
3198 * @returns #FALSE on failure (no memory)
3201 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
3203 return _dbus_data_slot_allocator_alloc (&slot_allocator,
3204 _DBUS_LOCK_NAME (connection_slots),
3209 * Deallocates a global ID for connection data slots.
3210 * dbus_connection_get_data() and dbus_connection_set_data() may no
3211 * longer be used with this slot. Existing data stored on existing
3212 * DBusConnection objects will be freed when the connection is
3213 * finalized, but may not be retrieved (and may only be replaced if
3214 * someone else reallocates the slot). When the refcount on the
3215 * passed-in slot reaches 0, it is set to -1.
3217 * @param slot_p address storing the slot to deallocate
3220 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
3222 _dbus_return_if_fail (*slot_p >= 0);
3224 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
3228 * Stores a pointer on a DBusConnection, along
3229 * with an optional function to be used for freeing
3230 * the data when the data is set again, or when
3231 * the connection is finalized. The slot number
3232 * must have been allocated with dbus_connection_allocate_data_slot().
3234 * @param connection the connection
3235 * @param slot the slot number
3236 * @param data the data to store
3237 * @param free_data_func finalizer function for the data
3238 * @returns #TRUE if there was enough memory to store the data
3241 dbus_connection_set_data (DBusConnection *connection,
3244 DBusFreeFunction free_data_func)
3246 DBusFreeFunction old_free_func;
3250 _dbus_return_val_if_fail (connection != NULL, FALSE);
3251 _dbus_return_val_if_fail (slot >= 0, FALSE);
3253 CONNECTION_LOCK (connection);
3255 retval = _dbus_data_slot_list_set (&slot_allocator,
3256 &connection->slot_list,
3257 slot, data, free_data_func,
3258 &old_free_func, &old_data);
3260 CONNECTION_UNLOCK (connection);
3264 /* Do the actual free outside the connection lock */
3266 (* old_free_func) (old_data);
3273 * Retrieves data previously set with dbus_connection_set_data().
3274 * The slot must still be allocated (must not have been freed).
3276 * @param connection the connection
3277 * @param slot the slot to get data from
3278 * @returns the data, or #NULL if not found
3281 dbus_connection_get_data (DBusConnection *connection,
3286 _dbus_return_val_if_fail (connection != NULL, NULL);
3288 CONNECTION_LOCK (connection);
3290 res = _dbus_data_slot_list_get (&slot_allocator,
3291 &connection->slot_list,
3294 CONNECTION_UNLOCK (connection);
3300 * This function sets a global flag for whether dbus_connection_new()
3301 * will set SIGPIPE behavior to SIG_IGN.
3303 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
3306 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
3308 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
3312 * Specifies the maximum size message this connection is allowed to
3313 * receive. Larger messages will result in disconnecting the
3316 * @param connection a #DBusConnection
3317 * @param size maximum message size the connection can receive, in bytes
3320 dbus_connection_set_max_message_size (DBusConnection *connection,
3323 _dbus_return_if_fail (connection != NULL);
3325 CONNECTION_LOCK (connection);
3326 _dbus_transport_set_max_message_size (connection->transport,
3328 CONNECTION_UNLOCK (connection);
3332 * Gets the value set by dbus_connection_set_max_message_size().
3334 * @param connection the connection
3335 * @returns the max size of a single message
3338 dbus_connection_get_max_message_size (DBusConnection *connection)
3342 _dbus_return_val_if_fail (connection != NULL, 0);
3344 CONNECTION_LOCK (connection);
3345 res = _dbus_transport_get_max_message_size (connection->transport);
3346 CONNECTION_UNLOCK (connection);
3351 * Sets the maximum total number of bytes that can be used for all messages
3352 * received on this connection. Messages count toward the maximum until
3353 * they are finalized. When the maximum is reached, the connection will
3354 * not read more data until some messages are finalized.
3356 * The semantics of the maximum are: if outstanding messages are
3357 * already above the maximum, additional messages will not be read.
3358 * The semantics are not: if the next message would cause us to exceed
3359 * the maximum, we don't read it. The reason is that we don't know the
3360 * size of a message until after we read it.
3362 * Thus, the max live messages size can actually be exceeded
3363 * by up to the maximum size of a single message.
3365 * Also, if we read say 1024 bytes off the wire in a single read(),
3366 * and that contains a half-dozen small messages, we may exceed the
3367 * size max by that amount. But this should be inconsequential.
3369 * This does imply that we can't call read() with a buffer larger
3370 * than we're willing to exceed this limit by.
3372 * @param connection the connection
3373 * @param size the maximum size in bytes of all outstanding messages
3376 dbus_connection_set_max_received_size (DBusConnection *connection,
3379 _dbus_return_if_fail (connection != NULL);
3381 CONNECTION_LOCK (connection);
3382 _dbus_transport_set_max_received_size (connection->transport,
3384 CONNECTION_UNLOCK (connection);
3388 * Gets the value set by dbus_connection_set_max_received_size().
3390 * @param connection the connection
3391 * @returns the max size of all live messages
3394 dbus_connection_get_max_received_size (DBusConnection *connection)
3398 _dbus_return_val_if_fail (connection != NULL, 0);
3400 CONNECTION_LOCK (connection);
3401 res = _dbus_transport_get_max_received_size (connection->transport);
3402 CONNECTION_UNLOCK (connection);
3407 * Gets the approximate size in bytes of all messages in the outgoing
3408 * message queue. The size is approximate in that you shouldn't use
3409 * it to decide how many bytes to read off the network or anything
3410 * of that nature, as optimizations may choose to tell small white lies
3411 * to avoid performance overhead.
3413 * @param connection the connection
3414 * @returns the number of bytes that have been queued up but not sent
3417 dbus_connection_get_outgoing_size (DBusConnection *connection)
3421 _dbus_return_val_if_fail (connection != NULL, 0);
3423 CONNECTION_LOCK (connection);
3424 res = _dbus_counter_get_value (connection->outgoing_counter);
3425 CONNECTION_UNLOCK (connection);