1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-connection.h"
25 #include "dbus-list.h"
26 #include "dbus-timeout.h"
27 #include "dbus-transport.h"
28 #include "dbus-watch.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-list.h"
31 #include "dbus-hash.h"
32 #include "dbus-message-internal.h"
33 #include "dbus-message-handler.h"
34 #include "dbus-threads.h"
35 #include "dbus-protocol.h"
36 #include "dbus-dataslot.h"
39 * @defgroup DBusConnection DBusConnection
41 * @brief Connection to another application
43 * A DBusConnection represents a connection to another
44 * application. Messages can be sent and received via this connection.
46 * The connection maintains a queue of incoming messages and a queue
47 * of outgoing messages. dbus_connection_pop_message() and friends
48 * can be used to read incoming messages from the queue.
49 * Outgoing messages are automatically discarded as they are
50 * written to the network.
52 * In brief a DBusConnection is a message queue associated with some
53 * message transport mechanism such as a socket.
58 * @defgroup DBusConnectionInternals DBusConnection implementation details
59 * @ingroup DBusInternals
60 * @brief Implementation details of DBusConnection
65 /** default timeout value when waiting for a message reply */
66 #define DEFAULT_TIMEOUT_VALUE (15 * 1000)
68 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
71 * Implementation details of DBusConnection. All fields are private.
75 int refcount; /**< Reference count. */
77 DBusMutex *mutex; /**< Lock on the entire DBusConnection */
79 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */
80 DBusCondVar *dispatch_cond; /**< Protects dispatch() */
82 dbus_bool_t io_path_acquired; /**< Protects transport io path */
83 DBusCondVar *io_path_cond; /**< Protects transport io path */
85 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
86 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
88 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
89 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
91 int n_outgoing; /**< Length of outgoing queue. */
92 int n_incoming; /**< Length of incoming queue. */
94 DBusTransport *transport; /**< Object that sends/receives messages over network. */
95 DBusWatchList *watches; /**< Stores active watches. */
96 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
98 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
99 DBusList *filter_list; /**< List of filters. */
101 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
103 DBusHashTable *pending_replies; /**< Hash of message serials and their message handlers. */
104 DBusCounter *connection_counter; /**< Counter that we decrement when finalized */
106 int client_serial; /**< Client serial. Increments each time a message is sent */
107 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */
109 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */
110 void *wakeup_main_data; /**< Application data for wakeup_main_function */
111 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */
116 DBusConnection *connection;
117 DBusMessageHandler *handler;
118 DBusTimeout *timeout;
121 DBusList *timeout_link; /* Preallocated timeout response */
123 dbus_bool_t timeout_added;
124 dbus_bool_t connection_added;
127 static void reply_handler_data_free (ReplyHandlerData *data);
129 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
130 DBusTimeout *timeout);
131 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
134 * Acquires the connection lock.
136 * @param connection the connection.
139 _dbus_connection_lock (DBusConnection *connection)
141 dbus_mutex_lock (connection->mutex);
145 * Releases the connection lock.
147 * @param connection the connection.
150 _dbus_connection_unlock (DBusConnection *connection)
152 dbus_mutex_unlock (connection->mutex);
156 * Wakes up the main loop if it is sleeping
157 * Needed if we're e.g. queueing outgoing messages
158 * on a thread while the mainloop sleeps.
160 * @param connection the connection.
163 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
165 if (connection->wakeup_main_function)
166 (*connection->wakeup_main_function) (connection->wakeup_main_data);
170 * Adds a message to the incoming message queue, returning #FALSE
171 * if there's insufficient memory to queue the message.
172 * Does not take over refcount of the message.
174 * @param connection the connection.
175 * @param message the message to queue.
176 * @returns #TRUE on success.
179 _dbus_connection_queue_received_message (DBusConnection *connection,
180 DBusMessage *message)
184 link = _dbus_list_alloc_link (message);
188 dbus_message_ref (message);
189 _dbus_connection_queue_received_message_link (connection, link);
195 * Adds a message-containing list link to the incoming message queue,
196 * taking ownership of the link and the message's current refcount.
197 * Cannot fail due to lack of memory.
199 * @param connection the connection.
200 * @param link the message link to queue.
203 _dbus_connection_queue_received_message_link (DBusConnection *connection,
206 ReplyHandlerData *reply_handler_data;
207 dbus_int32_t reply_serial;
208 DBusMessage *message;
210 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
212 _dbus_list_append_link (&connection->incoming_messages,
214 message = link->data;
216 /* If this is a reply we're waiting on, remove timeout for it */
217 reply_serial = dbus_message_get_reply_serial (message);
218 if (reply_serial != -1)
220 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
222 if (reply_handler_data != NULL)
224 if (reply_handler_data->timeout_added)
225 _dbus_connection_remove_timeout_locked (connection,
226 reply_handler_data->timeout);
227 reply_handler_data->timeout_added = FALSE;
231 connection->n_incoming += 1;
233 _dbus_connection_wakeup_mainloop (connection);
235 _dbus_assert (dbus_message_get_name (message) != NULL);
236 _dbus_verbose ("Message %p (%s) added to incoming queue %p, %d incoming\n",
237 message, dbus_message_get_name (message),
239 connection->n_incoming);
243 * Adds a link + message to the incoming message queue.
244 * Can't fail. Takes ownership of both link and message.
246 * @param connection the connection.
247 * @param link the list node and message to queue.
249 * @todo This needs to wake up the mainloop if it is in
250 * a poll/select and this is a multithreaded app.
253 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
256 _dbus_list_append_link (&connection->incoming_messages, link);
258 connection->n_incoming += 1;
260 _dbus_connection_wakeup_mainloop (connection);
262 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
263 link->data, connection, connection->n_incoming);
268 * Checks whether there are messages in the outgoing message queue.
270 * @param connection the connection.
271 * @returns #TRUE if the outgoing queue is non-empty.
274 _dbus_connection_have_messages_to_send (DBusConnection *connection)
276 return connection->outgoing_messages != NULL;
280 * Gets the next outgoing message. The message remains in the
281 * queue, and the caller does not own a reference to it.
283 * @param connection the connection.
284 * @returns the message to be sent.
287 _dbus_connection_get_message_to_send (DBusConnection *connection)
289 return _dbus_list_get_last (&connection->outgoing_messages);
293 * Notifies the connection that a message has been sent, so the
294 * message can be removed from the outgoing queue.
296 * @param connection the connection.
297 * @param message the message that was sent.
300 _dbus_connection_message_sent (DBusConnection *connection,
301 DBusMessage *message)
303 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
304 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
306 _dbus_list_pop_last (&connection->outgoing_messages);
307 dbus_message_unref (message);
309 connection->n_outgoing -= 1;
311 _dbus_verbose ("Message %p removed from outgoing queue %p, %d left to send\n",
312 message, connection, connection->n_outgoing);
314 if (connection->n_outgoing == 0)
315 _dbus_transport_messages_pending (connection->transport,
316 connection->n_outgoing);
320 * Adds a watch using the connection's DBusAddWatchFunction if
321 * available. Otherwise records the watch to be added when said
322 * function is available. Also re-adds the watch if the
323 * DBusAddWatchFunction changes. May fail due to lack of memory.
325 * @param connection the connection.
326 * @param watch the watch to add.
327 * @returns #TRUE on success.
330 _dbus_connection_add_watch (DBusConnection *connection,
333 if (connection->watches) /* null during finalize */
334 return _dbus_watch_list_add_watch (connection->watches,
341 * Removes a watch using the connection's DBusRemoveWatchFunction
342 * if available. It's an error to call this function on a watch
343 * that was not previously added.
345 * @param connection the connection.
346 * @param watch the watch to remove.
349 _dbus_connection_remove_watch (DBusConnection *connection,
352 if (connection->watches) /* null during finalize */
353 _dbus_watch_list_remove_watch (connection->watches,
358 * Toggles a watch and notifies app via connection's
359 * DBusWatchToggledFunction if available. It's an error to call this
360 * function on a watch that was not previously added.
362 * @param connection the connection.
363 * @param watch the watch to toggle.
364 * @param enabled whether to enable or disable
367 _dbus_connection_toggle_watch (DBusConnection *connection,
371 if (connection->watches) /* null during finalize */
372 _dbus_watch_list_toggle_watch (connection->watches,
377 * Adds a timeout using the connection's DBusAddTimeoutFunction if
378 * available. Otherwise records the timeout to be added when said
379 * function is available. Also re-adds the timeout if the
380 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
381 * The timeout will fire repeatedly until removed.
383 * @param connection the connection.
384 * @param timeout the timeout to add.
385 * @returns #TRUE on success.
388 _dbus_connection_add_timeout (DBusConnection *connection,
389 DBusTimeout *timeout)
391 if (connection->timeouts) /* null during finalize */
392 return _dbus_timeout_list_add_timeout (connection->timeouts,
399 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
400 * if available. It's an error to call this function on a timeout
401 * that was not previously added.
403 * @param connection the connection.
404 * @param timeout the timeout to remove.
407 _dbus_connection_remove_timeout (DBusConnection *connection,
408 DBusTimeout *timeout)
410 if (connection->timeouts) /* null during finalize */
411 _dbus_timeout_list_remove_timeout (connection->timeouts,
416 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
417 DBusTimeout *timeout)
419 dbus_mutex_lock (connection->mutex);
420 _dbus_connection_remove_timeout (connection, timeout);
421 dbus_mutex_unlock (connection->mutex);
425 * Toggles a timeout and notifies app via connection's
426 * DBusTimeoutToggledFunction if available. It's an error to call this
427 * function on a timeout that was not previously added.
429 * @param connection the connection.
430 * @param timeout the timeout to toggle.
431 * @param enabled whether to enable or disable
434 _dbus_connection_toggle_timeout (DBusConnection *connection,
435 DBusTimeout *timeout,
438 if (connection->timeouts) /* null during finalize */
439 _dbus_timeout_list_toggle_timeout (connection->timeouts,
444 * Tells the connection that the transport has been disconnected.
445 * Results in posting a disconnect message on the incoming message
446 * queue. Only has an effect the first time it's called.
448 * @param connection the connection
451 _dbus_connection_notify_disconnected (DBusConnection *connection)
453 if (connection->disconnect_message_link)
455 /* We haven't sent the disconnect message already */
456 _dbus_connection_queue_synthesized_message_link (connection,
457 connection->disconnect_message_link);
458 connection->disconnect_message_link = NULL;
464 * Acquire the transporter I/O path. This must be done before
465 * doing any I/O in the transporter. May sleep and drop the
466 * connection mutex while waiting for the I/O path.
468 * @param connection the connection.
469 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
470 * @returns TRUE if the I/O path was acquired.
473 _dbus_connection_acquire_io_path (DBusConnection *connection,
474 int timeout_milliseconds)
476 dbus_bool_t res = TRUE;
478 if (connection->io_path_acquired)
480 if (timeout_milliseconds != -1)
481 res = dbus_condvar_wait_timeout (connection->io_path_cond,
483 timeout_milliseconds);
485 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
490 _dbus_assert (!connection->io_path_acquired);
492 connection->io_path_acquired = TRUE;
499 * Release the I/O path when you're done with it. Only call
500 * after you've acquired the I/O. Wakes up at most one thread
501 * currently waiting to acquire the I/O path.
503 * @param connection the connection.
506 _dbus_connection_release_io_path (DBusConnection *connection)
508 _dbus_assert (connection->io_path_acquired);
510 connection->io_path_acquired = FALSE;
511 dbus_condvar_wake_one (connection->io_path_cond);
516 * Queues incoming messages and sends outgoing messages for this
517 * connection, optionally blocking in the process. Each call to
518 * _dbus_connection_do_iteration() will call select() or poll() one
519 * time and then read or write data if possible.
521 * The purpose of this function is to be able to flush outgoing
522 * messages or queue up incoming messages without returning
523 * control to the application and causing reentrancy weirdness.
525 * The flags parameter allows you to specify whether to
526 * read incoming messages, write outgoing messages, or both,
527 * and whether to block if no immediate action is possible.
529 * The timeout_milliseconds parameter does nothing unless the
530 * iteration is blocking.
532 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
533 * wasn't specified, then it's impossible to block, even if
534 * you specify DBUS_ITERATION_BLOCK; in that case the function
535 * returns immediately.
537 * @param connection the connection.
538 * @param flags iteration flags.
539 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
542 _dbus_connection_do_iteration (DBusConnection *connection,
544 int timeout_milliseconds)
546 if (connection->n_outgoing == 0)
547 flags &= ~DBUS_ITERATION_DO_WRITING;
549 if (_dbus_connection_acquire_io_path (connection,
550 (flags & DBUS_ITERATION_BLOCK)?timeout_milliseconds:0))
552 _dbus_transport_do_iteration (connection->transport,
553 flags, timeout_milliseconds);
554 _dbus_connection_release_io_path (connection);
559 * Creates a new connection for the given transport. A transport
560 * represents a message stream that uses some concrete mechanism, such
561 * as UNIX domain sockets. May return #NULL if insufficient
562 * memory exists to create the connection.
564 * @param transport the transport.
565 * @returns the new connection, or #NULL on failure.
568 _dbus_connection_new_for_transport (DBusTransport *transport)
570 DBusConnection *connection;
571 DBusWatchList *watch_list;
572 DBusTimeoutList *timeout_list;
573 DBusHashTable *handler_table, *pending_replies;
575 DBusCondVar *message_returned_cond;
576 DBusCondVar *dispatch_cond;
577 DBusCondVar *io_path_cond;
578 DBusList *disconnect_link;
579 DBusMessage *disconnect_message;
583 handler_table = NULL;
584 pending_replies = NULL;
587 message_returned_cond = NULL;
588 dispatch_cond = NULL;
590 disconnect_link = NULL;
591 disconnect_message = NULL;
593 watch_list = _dbus_watch_list_new ();
594 if (watch_list == NULL)
597 timeout_list = _dbus_timeout_list_new ();
598 if (timeout_list == NULL)
602 _dbus_hash_table_new (DBUS_HASH_STRING,
604 if (handler_table == NULL)
608 _dbus_hash_table_new (DBUS_HASH_INT,
609 NULL, (DBusFreeFunction)reply_handler_data_free);
610 if (pending_replies == NULL)
613 connection = dbus_new0 (DBusConnection, 1);
614 if (connection == NULL)
617 mutex = dbus_mutex_new ();
621 message_returned_cond = dbus_condvar_new ();
622 if (message_returned_cond == NULL)
625 dispatch_cond = dbus_condvar_new ();
626 if (dispatch_cond == NULL)
629 io_path_cond = dbus_condvar_new ();
630 if (io_path_cond == NULL)
633 disconnect_message = dbus_message_new (NULL, DBUS_MESSAGE_LOCAL_DISCONNECT);
634 if (disconnect_message == NULL)
637 disconnect_link = _dbus_list_alloc_link (disconnect_message);
638 if (disconnect_link == NULL)
641 if (_dbus_modify_sigpipe)
642 _dbus_disable_sigpipe ();
644 connection->refcount = 1;
645 connection->mutex = mutex;
646 connection->dispatch_cond = dispatch_cond;
647 connection->io_path_cond = io_path_cond;
648 connection->message_returned_cond = message_returned_cond;
649 connection->transport = transport;
650 connection->watches = watch_list;
651 connection->timeouts = timeout_list;
652 connection->handler_table = handler_table;
653 connection->pending_replies = pending_replies;
654 connection->filter_list = NULL;
656 _dbus_data_slot_list_init (&connection->slot_list);
658 connection->client_serial = 1;
660 connection->disconnect_message_link = disconnect_link;
662 if (!_dbus_transport_set_connection (transport, connection))
665 _dbus_transport_ref (transport);
670 if (disconnect_message != NULL)
671 dbus_message_unref (disconnect_message);
673 if (disconnect_link != NULL)
674 _dbus_list_free_link (disconnect_link);
676 if (io_path_cond != NULL)
677 dbus_condvar_free (io_path_cond);
679 if (dispatch_cond != NULL)
680 dbus_condvar_free (dispatch_cond);
682 if (message_returned_cond != NULL)
683 dbus_condvar_free (message_returned_cond);
686 dbus_mutex_free (mutex);
688 if (connection != NULL)
689 dbus_free (connection);
692 _dbus_hash_table_unref (handler_table);
695 _dbus_hash_table_unref (pending_replies);
698 _dbus_watch_list_free (watch_list);
701 _dbus_timeout_list_free (timeout_list);
707 _dbus_connection_get_next_client_serial (DBusConnection *connection)
711 serial = connection->client_serial++;
713 if (connection->client_serial < 0)
714 connection->client_serial = 1;
720 * Used to notify a connection when a DBusMessageHandler is
721 * destroyed, so the connection can drop any reference
722 * to the handler. This is a private function, but still
723 * takes the connection lock. Don't call it with the lock held.
725 * @todo needs to check in pending_replies too.
727 * @param connection the connection
728 * @param handler the handler
731 _dbus_connection_handler_destroyed_locked (DBusConnection *connection,
732 DBusMessageHandler *handler)
737 dbus_mutex_lock (connection->mutex);
739 _dbus_hash_iter_init (connection->handler_table, &iter);
740 while (_dbus_hash_iter_next (&iter))
742 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
745 _dbus_hash_iter_remove_entry (&iter);
748 link = _dbus_list_get_first_link (&connection->filter_list);
751 DBusMessageHandler *h = link->data;
752 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
755 _dbus_list_remove_link (&connection->filter_list,
760 dbus_mutex_unlock (connection->mutex);
764 * Adds the counter used to count the number of open connections.
765 * Increments the counter by one, and saves it to be decremented
766 * again when this connection is finalized.
768 * @param connection a #DBusConnection
769 * @param counter counter that tracks number of connections
772 _dbus_connection_set_connection_counter (DBusConnection *connection,
773 DBusCounter *counter)
775 _dbus_assert (connection->connection_counter == NULL);
777 connection->connection_counter = counter;
778 _dbus_counter_ref (connection->connection_counter);
779 _dbus_counter_adjust (connection->connection_counter, 1);
785 * @addtogroup DBusConnection
791 * Opens a new connection to a remote address.
793 * @todo specify what the address parameter is. Right now
794 * it's just the name of a UNIX domain socket. It should be
795 * something more complex that encodes which transport to use.
797 * If the open fails, the function returns #NULL, and provides
798 * a reason for the failure in the result parameter. Pass
799 * #NULL for the result parameter if you aren't interested
800 * in the reason for failure.
802 * @param address the address.
803 * @param error address where an error can be returned.
804 * @returns new connection, or #NULL on failure.
807 dbus_connection_open (const char *address,
810 DBusConnection *connection;
811 DBusTransport *transport;
813 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
815 transport = _dbus_transport_open (address, error);
816 if (transport == NULL)
818 _DBUS_ASSERT_ERROR_IS_SET (error);
822 connection = _dbus_connection_new_for_transport (transport);
824 _dbus_transport_unref (transport);
826 if (connection == NULL)
828 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
836 * Increments the reference count of a DBusConnection.
838 * @param connection the connection.
841 dbus_connection_ref (DBusConnection *connection)
843 dbus_mutex_lock (connection->mutex);
844 _dbus_assert (connection->refcount > 0);
846 connection->refcount += 1;
847 dbus_mutex_unlock (connection->mutex);
851 * Increments the reference count of a DBusConnection.
852 * Requires that the caller already holds the connection lock.
854 * @param connection the connection.
857 _dbus_connection_ref_unlocked (DBusConnection *connection)
859 _dbus_assert (connection->refcount > 0);
860 connection->refcount += 1;
864 /* This is run without the mutex held, but after the last reference
865 * to the connection has been dropped we should have no thread-related
869 _dbus_connection_last_unref (DBusConnection *connection)
874 _dbus_verbose ("Finalizing connection %p\n", connection);
876 _dbus_assert (connection->refcount == 0);
878 /* You have to disconnect the connection before unref:ing it. Otherwise
879 * you won't get the disconnected message.
881 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
883 if (connection->connection_counter != NULL)
885 /* subtract ourselves from the counter */
886 _dbus_counter_adjust (connection->connection_counter, - 1);
887 _dbus_counter_unref (connection->connection_counter);
888 connection->connection_counter = NULL;
891 _dbus_watch_list_free (connection->watches);
892 connection->watches = NULL;
894 _dbus_timeout_list_free (connection->timeouts);
895 connection->timeouts = NULL;
897 /* calls out to application code... */
898 _dbus_data_slot_list_free (&connection->slot_list);
900 _dbus_hash_iter_init (connection->handler_table, &iter);
901 while (_dbus_hash_iter_next (&iter))
903 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
905 _dbus_message_handler_remove_connection (h, connection);
908 link = _dbus_list_get_first_link (&connection->filter_list);
911 DBusMessageHandler *h = link->data;
912 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
914 _dbus_message_handler_remove_connection (h, connection);
919 _dbus_hash_table_unref (connection->handler_table);
920 connection->handler_table = NULL;
922 _dbus_hash_table_unref (connection->pending_replies);
923 connection->pending_replies = NULL;
925 _dbus_list_clear (&connection->filter_list);
927 _dbus_list_foreach (&connection->outgoing_messages,
928 (DBusForeachFunction) dbus_message_unref,
930 _dbus_list_clear (&connection->outgoing_messages);
932 _dbus_list_foreach (&connection->incoming_messages,
933 (DBusForeachFunction) dbus_message_unref,
935 _dbus_list_clear (&connection->incoming_messages);
937 _dbus_transport_unref (connection->transport);
939 if (connection->disconnect_message_link)
941 DBusMessage *message = connection->disconnect_message_link->data;
942 dbus_message_unref (message);
943 _dbus_list_free_link (connection->disconnect_message_link);
946 dbus_condvar_free (connection->dispatch_cond);
947 dbus_condvar_free (connection->io_path_cond);
948 dbus_condvar_free (connection->message_returned_cond);
950 dbus_mutex_free (connection->mutex);
952 dbus_free (connection);
956 * Decrements the reference count of a DBusConnection, and finalizes
957 * it if the count reaches zero. It is a bug to drop the last reference
958 * to a connection that has not been disconnected.
960 * @todo in practice it can be quite tricky to never unref a connection
961 * that's still connected; maybe there's some way we could avoid
964 * @param connection the connection.
967 dbus_connection_unref (DBusConnection *connection)
969 dbus_bool_t last_unref;
971 dbus_mutex_lock (connection->mutex);
973 _dbus_assert (connection != NULL);
974 _dbus_assert (connection->refcount > 0);
976 connection->refcount -= 1;
977 last_unref = (connection->refcount == 0);
980 printf ("unref() connection %p count = %d\n", connection, connection->refcount);
983 dbus_mutex_unlock (connection->mutex);
986 _dbus_connection_last_unref (connection);
990 * Closes the connection, so no further data can be sent or received.
991 * Any further attempts to send data will result in errors. This
992 * function does not affect the connection's reference count. It's
993 * safe to disconnect a connection more than once; all calls after the
994 * first do nothing. It's impossible to "reconnect" a connection, a
995 * new connection must be created.
997 * @param connection the connection.
1000 dbus_connection_disconnect (DBusConnection *connection)
1002 dbus_mutex_lock (connection->mutex);
1003 _dbus_transport_disconnect (connection->transport);
1004 dbus_mutex_unlock (connection->mutex);
1008 * Gets whether the connection is currently connected. All
1009 * connections are connected when they are opened. A connection may
1010 * become disconnected when the remote application closes its end, or
1011 * exits; a connection may also be disconnected with
1012 * dbus_connection_disconnect().
1014 * @param connection the connection.
1015 * @returns #TRUE if the connection is still alive.
1018 dbus_connection_get_is_connected (DBusConnection *connection)
1022 dbus_mutex_lock (connection->mutex);
1023 res = _dbus_transport_get_is_connected (connection->transport);
1024 dbus_mutex_unlock (connection->mutex);
1030 * Gets whether the connection was authenticated. (Note that
1031 * if the connection was authenticated then disconnected,
1032 * this function still returns #TRUE)
1034 * @param connection the connection
1035 * @returns #TRUE if the connection was ever authenticated
1038 dbus_connection_get_is_authenticated (DBusConnection *connection)
1042 dbus_mutex_lock (connection->mutex);
1043 res = _dbus_transport_get_is_authenticated (connection->transport);
1044 dbus_mutex_unlock (connection->mutex);
1050 * Preallocates resources needed to send a message, allowing the message
1051 * to be sent without the possibility of memory allocation failure.
1052 * Allows apps to create a future guarantee that they can send
1053 * a message regardless of memory shortages.
1055 * @param connection the connection we're preallocating for.
1056 * @returns the preallocated resources, or #NULL
1058 DBusPreallocatedSend*
1059 dbus_connection_preallocate_send (DBusConnection *connection)
1061 /* we store "connection" in the link just to enforce via
1062 * assertion that preallocated links are only used
1063 * with the connection they were created for.
1065 return (DBusPreallocatedSend*) _dbus_list_alloc_link (connection);
1069 * Frees preallocated message-sending resources from
1070 * dbus_connection_preallocate_send(). Should only
1071 * be called if the preallocated resources are not used
1072 * to send a message.
1074 * @param connection the connection
1075 * @param preallocated the resources
1078 dbus_connection_free_preallocated_send (DBusConnection *connection,
1079 DBusPreallocatedSend *preallocated)
1081 DBusList *link = (DBusList*) preallocated;
1082 _dbus_assert (link->data == connection);
1083 _dbus_list_free_link (link);
1087 * Sends a message using preallocated resources. This function cannot fail.
1088 * It works identically to dbus_connection_send() in other respects.
1089 * Preallocated resources comes from dbus_connection_preallocate_send().
1090 * This function "consumes" the preallocated resources, they need not
1091 * be freed separately.
1093 * @param connection the connection
1094 * @param preallocated the preallocated resources
1095 * @param message the message to send
1096 * @param client_serial return location for client serial assigned to the message
1099 dbus_connection_send_preallocated (DBusConnection *connection,
1100 DBusPreallocatedSend *preallocated,
1101 DBusMessage *message,
1102 dbus_int32_t *client_serial)
1104 DBusList *link = (DBusList*) preallocated;
1105 dbus_int32_t serial;
1107 _dbus_assert (link->data == connection);
1108 _dbus_assert (dbus_message_get_name (message) != NULL);
1110 dbus_mutex_lock (connection->mutex);
1112 link->data = message;
1113 _dbus_list_prepend_link (&connection->outgoing_messages,
1116 dbus_message_ref (message);
1117 connection->n_outgoing += 1;
1119 _dbus_verbose ("Message %p (%s) added to outgoing queue %p, %d pending to send\n",
1121 dbus_message_get_name (message),
1123 connection->n_outgoing);
1125 if (dbus_message_get_serial (message) == -1)
1127 serial = _dbus_connection_get_next_client_serial (connection);
1128 _dbus_message_set_serial (message, serial);
1132 *client_serial = dbus_message_get_serial (message);
1134 _dbus_message_lock (message);
1136 if (connection->n_outgoing == 1)
1137 _dbus_transport_messages_pending (connection->transport,
1138 connection->n_outgoing);
1140 _dbus_connection_wakeup_mainloop (connection);
1142 dbus_mutex_unlock (connection->mutex);
1146 * Adds a message to the outgoing message queue. Does not block to
1147 * write the message to the network; that happens asynchronously. To
1148 * force the message to be written, call dbus_connection_flush().
1149 * Because this only queues the message, the only reason it can
1150 * fail is lack of memory. Even if the connection is disconnected,
1151 * no error will be returned.
1153 * If the function fails due to lack of memory, it returns #FALSE.
1154 * The function will never fail for other reasons; even if the
1155 * connection is disconnected, you can queue an outgoing message,
1156 * though obviously it won't be sent.
1158 * @param connection the connection.
1159 * @param message the message to write.
1160 * @param client_serial return location for client serial.
1161 * @returns #TRUE on success.
1164 dbus_connection_send (DBusConnection *connection,
1165 DBusMessage *message,
1166 dbus_int32_t *client_serial)
1168 DBusPreallocatedSend *preallocated;
1170 preallocated = dbus_connection_preallocate_send (connection);
1171 if (preallocated == NULL)
1177 dbus_connection_send_preallocated (connection, preallocated, message, client_serial);
1183 reply_handler_timeout (void *data)
1185 DBusConnection *connection;
1186 ReplyHandlerData *reply_handler_data = data;
1188 connection = reply_handler_data->connection;
1190 dbus_mutex_lock (connection->mutex);
1191 if (reply_handler_data->timeout_link)
1193 _dbus_connection_queue_synthesized_message_link (connection,
1194 reply_handler_data->timeout_link);
1195 reply_handler_data->timeout_link = NULL;
1198 _dbus_connection_remove_timeout (connection,
1199 reply_handler_data->timeout);
1200 reply_handler_data->timeout_added = FALSE;
1202 dbus_mutex_unlock (connection->mutex);
1208 reply_handler_data_free (ReplyHandlerData *data)
1213 if (data->timeout_added)
1214 _dbus_connection_remove_timeout_locked (data->connection,
1217 if (data->connection_added)
1218 _dbus_message_handler_remove_connection (data->handler,
1221 if (data->timeout_link)
1223 dbus_message_unref ((DBusMessage *)data->timeout_link->data);
1224 _dbus_list_free_link (data->timeout_link);
1227 dbus_message_handler_unref (data->handler);
1233 * Queues a message to send, as with dbus_connection_send_message(),
1234 * but also sets up a DBusMessageHandler to receive a reply to the
1235 * message. If no reply is received in the given timeout_milliseconds,
1236 * expires the pending reply and sends the DBusMessageHandler a
1237 * synthetic error reply (generated in-process, not by the remote
1238 * application) indicating that a timeout occurred.
1240 * Reply handlers see their replies after message filters see them,
1241 * but before message handlers added with
1242 * dbus_connection_register_handler() see them, regardless of the
1243 * reply message's name. Reply handlers are only handed a single
1244 * message as a reply, after one reply has been seen the handler is
1245 * removed. If a filter filters out the reply before the handler sees
1246 * it, the reply is immediately timed out and a timeout error reply is
1247 * generated. If a filter removes the timeout error reply then the
1248 * reply handler will never be called. Filters should not do this.
1250 * If #NULL is passed for the reply_handler, the timeout reply will
1251 * still be generated and placed into the message queue, but no
1252 * specific message handler will receive the reply.
1254 * If -1 is passed for the timeout, a sane default timeout is used. -1
1255 * is typically the best value for the timeout for this reason, unless
1256 * you want a very short or very long timeout. There is no way to
1257 * avoid a timeout entirely, other than passing INT_MAX for the
1258 * timeout to postpone it indefinitely.
1260 * @param connection the connection
1261 * @param message the message to send
1262 * @param reply_handler message handler expecting the reply, or #NULL
1263 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1264 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1268 dbus_connection_send_with_reply (DBusConnection *connection,
1269 DBusMessage *message,
1270 DBusMessageHandler *reply_handler,
1271 int timeout_milliseconds)
1273 DBusTimeout *timeout;
1274 ReplyHandlerData *data;
1276 DBusList *reply_link;
1277 dbus_int32_t serial = -1;
1279 if (timeout_milliseconds == -1)
1280 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1282 data = dbus_new0 (ReplyHandlerData, 1);
1287 timeout = _dbus_timeout_new (timeout_milliseconds, reply_handler_timeout,
1292 reply_handler_data_free (data);
1296 dbus_mutex_lock (connection->mutex);
1299 if (!_dbus_connection_add_timeout (connection, timeout))
1301 reply_handler_data_free (data);
1302 _dbus_timeout_unref (timeout);
1303 dbus_mutex_unlock (connection->mutex);
1307 /* The connection now owns the reference to the timeout. */
1308 _dbus_timeout_unref (timeout);
1310 data->timeout_added = TRUE;
1311 data->timeout = timeout;
1312 data->connection = connection;
1314 if (!_dbus_message_handler_add_connection (reply_handler, connection))
1316 dbus_mutex_unlock (connection->mutex);
1317 reply_handler_data_free (data);
1320 data->connection_added = TRUE;
1322 /* Assign a serial to the message */
1323 if (dbus_message_get_serial (message) == -1)
1325 serial = _dbus_connection_get_next_client_serial (connection);
1326 _dbus_message_set_serial (message, serial);
1329 data->handler = reply_handler;
1330 data->serial = serial;
1332 dbus_message_handler_ref (reply_handler);
1334 reply = dbus_message_new_error_reply (message, DBUS_ERROR_NO_REPLY,
1335 "No reply within specified time");
1338 dbus_mutex_unlock (connection->mutex);
1339 reply_handler_data_free (data);
1343 reply_link = _dbus_list_alloc_link (reply);
1346 dbus_mutex_unlock (connection->mutex);
1347 dbus_message_unref (reply);
1348 reply_handler_data_free (data);
1352 data->timeout_link = reply_link;
1354 /* Insert the serial in the pending replies hash. */
1355 if (!_dbus_hash_table_insert_int (connection->pending_replies, serial, data))
1357 dbus_mutex_unlock (connection->mutex);
1358 reply_handler_data_free (data);
1362 dbus_mutex_unlock (connection->mutex);
1364 if (!dbus_connection_send (connection, message, NULL))
1366 /* This will free the handler data too */
1367 _dbus_hash_table_remove_int (connection->pending_replies, serial);
1376 check_for_reply_unlocked (DBusConnection *connection,
1377 dbus_int32_t client_serial)
1381 link = _dbus_list_get_first_link (&connection->incoming_messages);
1383 while (link != NULL)
1385 DBusMessage *reply = link->data;
1387 if (dbus_message_get_reply_serial (reply) == client_serial)
1389 _dbus_list_remove_link (&connection->incoming_messages, link);
1390 connection->n_incoming -= 1;
1391 dbus_message_ref (reply);
1394 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1401 * Sends a message and blocks a certain time period while waiting for a reply.
1402 * This function does not dispatch any message handlers until the main loop
1403 * has been reached. This function is used to do non-reentrant "method calls."
1404 * If a reply is received, it is returned, and removed from the incoming
1405 * message queue. If it is not received, #NULL is returned and the
1406 * error is set to #DBUS_ERROR_NO_REPLY. If something else goes
1407 * wrong, result is set to whatever is appropriate, such as
1408 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED.
1410 * @todo could use performance improvements (it keeps scanning
1411 * the whole message queue for example) and has thread issues,
1412 * see comments in source
1414 * @param connection the connection
1415 * @param message the message to send
1416 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1417 * @param error return location for error message
1418 * @returns the message that is the reply or #NULL with an error code if the
1422 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
1423 DBusMessage *message,
1424 int timeout_milliseconds,
1427 dbus_int32_t client_serial;
1428 long start_tv_sec, start_tv_usec;
1429 long end_tv_sec, end_tv_usec;
1430 long tv_sec, tv_usec;
1431 DBusDispatchStatus status;
1433 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1435 if (timeout_milliseconds == -1)
1436 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1438 /* it would probably seem logical to pass in _DBUS_INT_MAX
1439 * for infinite timeout, but then math below would get
1440 * all overflow-prone, so smack that down.
1442 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1443 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1445 if (!dbus_connection_send (connection, message, &client_serial))
1447 _DBUS_SET_OOM (error);
1453 /* Flush message queue */
1454 dbus_connection_flush (connection);
1456 dbus_mutex_lock (connection->mutex);
1458 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1459 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1460 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1461 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1462 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1464 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %d from %ld sec %ld usec to %ld sec %ld usec\n",
1465 timeout_milliseconds,
1467 start_tv_sec, start_tv_usec,
1468 end_tv_sec, end_tv_usec);
1470 /* Now we wait... */
1471 /* THREAD TODO: This is busted. What if a dispatch() or pop_message
1472 * gets the message before we do?
1474 /* always block at least once as we know we don't have the reply yet */
1475 _dbus_connection_do_iteration (connection,
1476 DBUS_ITERATION_DO_READING |
1477 DBUS_ITERATION_BLOCK,
1478 timeout_milliseconds);
1482 /* queue messages and get status */
1483 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1485 if (status == DBUS_DISPATCH_DATA_REMAINS)
1489 reply = check_for_reply_unlocked (connection, client_serial);
1492 dbus_mutex_unlock (connection->mutex);
1494 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply %s\n",
1495 dbus_message_get_name (reply));
1501 _dbus_get_current_time (&tv_sec, &tv_usec);
1503 if (tv_sec < start_tv_sec)
1504 ; /* clock set backward, bail out */
1505 else if (connection->disconnect_message_link == NULL)
1506 ; /* we're disconnected, bail out */
1507 else if (tv_sec < end_tv_sec ||
1508 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1510 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1511 (end_tv_usec - tv_usec) / 1000;
1512 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
1513 _dbus_assert (timeout_milliseconds >= 0);
1515 if (status == DBUS_DISPATCH_NEED_MEMORY)
1517 /* Try sleeping a bit, as we aren't sure we need to block for reading,
1518 * we may already have a reply in the buffer and just can't process
1521 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
1523 if (timeout_milliseconds < 100)
1524 ; /* just busy loop */
1525 else if (timeout_milliseconds <= 1000)
1526 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
1528 _dbus_sleep_milliseconds (1000);
1532 /* block again, we don't have the reply buffered yet. */
1533 _dbus_connection_do_iteration (connection,
1534 DBUS_ITERATION_DO_READING |
1535 DBUS_ITERATION_BLOCK,
1536 timeout_milliseconds);
1539 goto recheck_status;
1542 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
1543 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
1545 if (dbus_connection_get_is_connected (connection))
1546 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
1548 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
1550 dbus_mutex_unlock (connection->mutex);
1556 * Blocks until the outgoing message queue is empty.
1558 * @param connection the connection.
1561 dbus_connection_flush (DBusConnection *connection)
1563 /* We have to specify DBUS_ITERATION_DO_READING here
1564 * because otherwise we could have two apps deadlock
1565 * if they are both doing a flush(), and the kernel
1569 dbus_mutex_lock (connection->mutex);
1570 while (connection->n_outgoing > 0)
1571 _dbus_connection_do_iteration (connection,
1572 DBUS_ITERATION_DO_READING |
1573 DBUS_ITERATION_DO_WRITING |
1574 DBUS_ITERATION_BLOCK,
1576 dbus_mutex_unlock (connection->mutex);
1579 /* Call with mutex held. Will drop it while waiting and re-acquire
1583 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
1585 _dbus_assert (connection->message_borrowed != NULL);
1587 while (connection->message_borrowed != NULL)
1588 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
1592 * Returns the first-received message from the incoming message queue,
1593 * leaving it in the queue. If the queue is empty, returns #NULL.
1595 * The caller does not own a reference to the returned message, and must
1596 * either return it using dbus_connection_return_message or keep it after
1597 * calling dbus_connection_steal_borrowed_message. No one can get at the
1598 * message while its borrowed, so return it as quickly as possible and
1599 * don't keep a reference to it after returning it. If you need to keep
1600 * the message, make a copy of it.
1602 * @param connection the connection.
1603 * @returns next message in the incoming queue.
1606 dbus_connection_borrow_message (DBusConnection *connection)
1608 DBusMessage *message;
1609 DBusDispatchStatus status;
1611 /* this is called for the side effect that it queues
1612 * up any messages from the transport
1614 status = dbus_connection_get_dispatch_status (connection);
1615 if (status != DBUS_DISPATCH_DATA_REMAINS)
1618 dbus_mutex_lock (connection->mutex);
1620 if (connection->message_borrowed != NULL)
1621 _dbus_connection_wait_for_borrowed (connection);
1623 message = _dbus_list_get_first (&connection->incoming_messages);
1626 connection->message_borrowed = message;
1628 dbus_mutex_unlock (connection->mutex);
1636 dbus_connection_return_message (DBusConnection *connection,
1637 DBusMessage *message)
1639 dbus_mutex_lock (connection->mutex);
1641 _dbus_assert (message == connection->message_borrowed);
1643 connection->message_borrowed = NULL;
1644 dbus_condvar_wake_all (connection->message_returned_cond);
1646 dbus_mutex_unlock (connection->mutex);
1653 dbus_connection_steal_borrowed_message (DBusConnection *connection,
1654 DBusMessage *message)
1656 DBusMessage *pop_message;
1658 dbus_mutex_lock (connection->mutex);
1660 _dbus_assert (message == connection->message_borrowed);
1662 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
1663 _dbus_assert (message == pop_message);
1665 connection->n_incoming -= 1;
1667 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
1668 message, connection->n_incoming);
1670 connection->message_borrowed = NULL;
1671 dbus_condvar_wake_all (connection->message_returned_cond);
1673 dbus_mutex_unlock (connection->mutex);
1676 /* See dbus_connection_pop_message, but requires the caller to own
1677 * the lock before calling. May drop the lock while running.
1680 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
1682 if (connection->message_borrowed != NULL)
1683 _dbus_connection_wait_for_borrowed (connection);
1685 if (connection->n_incoming > 0)
1689 link = _dbus_list_pop_first_link (&connection->incoming_messages);
1690 connection->n_incoming -= 1;
1692 _dbus_verbose ("Message %p removed from incoming queue %p, %d incoming\n",
1693 link->data, connection, connection->n_incoming);
1701 /* See dbus_connection_pop_message, but requires the caller to own
1702 * the lock before calling. May drop the lock while running.
1705 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
1709 link = _dbus_connection_pop_message_link_unlocked (connection);
1713 DBusMessage *message;
1715 message = link->data;
1717 _dbus_list_free_link (link);
1727 * Returns the first-received message from the incoming message queue,
1728 * removing it from the queue. The caller owns a reference to the
1729 * returned message. If the queue is empty, returns #NULL.
1731 * @param connection the connection.
1732 * @returns next message in the incoming queue.
1735 dbus_connection_pop_message (DBusConnection *connection)
1737 DBusMessage *message;
1738 DBusDispatchStatus status;
1740 /* this is called for the side effect that it queues
1741 * up any messages from the transport
1743 status = dbus_connection_get_dispatch_status (connection);
1744 if (status != DBUS_DISPATCH_DATA_REMAINS)
1747 dbus_mutex_lock (connection->mutex);
1749 message = _dbus_connection_pop_message_unlocked (connection);
1751 dbus_mutex_unlock (connection->mutex);
1757 * Acquire the dispatcher. This must be done before dispatching
1758 * messages in order to guarantee the right order of
1759 * message delivery. May sleep and drop the connection mutex
1760 * while waiting for the dispatcher.
1762 * @param connection the connection.
1765 _dbus_connection_acquire_dispatch (DBusConnection *connection)
1767 if (connection->dispatch_acquired)
1768 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
1769 _dbus_assert (!connection->dispatch_acquired);
1771 connection->dispatch_acquired = TRUE;
1775 * Release the dispatcher when you're done with it. Only call
1776 * after you've acquired the dispatcher. Wakes up at most one
1777 * thread currently waiting to acquire the dispatcher.
1779 * @param connection the connection.
1782 _dbus_connection_release_dispatch (DBusConnection *connection)
1784 _dbus_assert (connection->dispatch_acquired);
1786 connection->dispatch_acquired = FALSE;
1787 dbus_condvar_wake_one (connection->dispatch_cond);
1791 _dbus_connection_failed_pop (DBusConnection *connection,
1792 DBusList *message_link)
1794 _dbus_list_prepend_link (&connection->incoming_messages,
1796 connection->n_incoming += 1;
1799 static DBusDispatchStatus
1800 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
1802 if (connection->n_incoming > 0)
1803 return DBUS_DISPATCH_DATA_REMAINS;
1804 else if (!_dbus_transport_queue_messages (connection->transport))
1805 return DBUS_DISPATCH_NEED_MEMORY;
1808 DBusDispatchStatus status;
1810 status = _dbus_transport_get_dispatch_status (connection->transport);
1812 if (status != DBUS_DISPATCH_COMPLETE)
1814 else if (connection->n_incoming > 0)
1815 return DBUS_DISPATCH_DATA_REMAINS;
1817 return DBUS_DISPATCH_COMPLETE;
1822 * Gets the current state (what we would currently return
1823 * from dbus_connection_dispatch()) but doesn't actually
1824 * dispatch any messages.
1826 * @param connection the connection.
1827 * @returns current dispatch status
1830 dbus_connection_get_dispatch_status (DBusConnection *connection)
1832 DBusDispatchStatus status;
1834 dbus_mutex_lock (connection->mutex);
1836 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1838 dbus_mutex_unlock (connection->mutex);
1844 * Processes data buffered while handling watches, queueing zero or
1845 * more incoming messages. Then pops the first-received message from
1846 * the current incoming message queue, runs any handlers for it, and
1847 * unrefs the message. Returns a status indicating whether messages/data
1848 * remain, more memory is needed, or all data has been processed.
1850 * @param connection the connection
1851 * @returns dispatch status
1854 dbus_connection_dispatch (DBusConnection *connection)
1856 DBusMessageHandler *handler;
1857 DBusMessage *message;
1858 DBusList *link, *filter_list_copy, *message_link;
1859 DBusHandlerResult result;
1860 ReplyHandlerData *reply_handler_data;
1862 dbus_int32_t reply_serial;
1863 DBusDispatchStatus status;
1865 status = dbus_connection_get_dispatch_status (connection);
1866 if (status != DBUS_DISPATCH_DATA_REMAINS)
1869 dbus_mutex_lock (connection->mutex);
1871 /* We need to ref the connection since the callback could potentially
1872 * drop the last ref to it
1874 _dbus_connection_ref_unlocked (connection);
1876 _dbus_connection_acquire_dispatch (connection);
1878 /* This call may drop the lock during the execution (if waiting for
1879 * borrowed messages to be returned) but the order of message
1880 * dispatch if several threads call dispatch() is still
1881 * protected by the lock, since only one will get the lock, and that
1882 * one will finish the message dispatching
1884 message_link = _dbus_connection_pop_message_link_unlocked (connection);
1885 if (message_link == NULL)
1887 /* another thread dispatched our stuff */
1889 _dbus_connection_release_dispatch (connection);
1890 dbus_mutex_unlock (connection->mutex);
1892 status = dbus_connection_get_dispatch_status (connection);
1894 dbus_connection_unref (connection);
1899 message = message_link->data;
1901 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
1903 reply_serial = dbus_message_get_reply_serial (message);
1904 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
1907 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
1909 _dbus_connection_release_dispatch (connection);
1910 dbus_mutex_unlock (connection->mutex);
1911 _dbus_connection_failed_pop (connection, message_link);
1912 dbus_connection_unref (connection);
1913 return DBUS_DISPATCH_NEED_MEMORY;
1916 _dbus_list_foreach (&filter_list_copy,
1917 (DBusForeachFunction)dbus_message_handler_ref,
1920 /* We're still protected from dispatch() reentrancy here
1921 * since we acquired the dispatcher
1923 dbus_mutex_unlock (connection->mutex);
1925 link = _dbus_list_get_first_link (&filter_list_copy);
1926 while (link != NULL)
1928 DBusMessageHandler *handler = link->data;
1929 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
1931 _dbus_verbose (" running filter on message %p\n", message);
1932 result = _dbus_message_handler_handle_message (handler, connection,
1935 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1941 _dbus_list_foreach (&filter_list_copy,
1942 (DBusForeachFunction)dbus_message_handler_unref,
1944 _dbus_list_clear (&filter_list_copy);
1946 dbus_mutex_lock (connection->mutex);
1948 /* Did a reply we were waiting on get filtered? */
1949 if (reply_handler_data && result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1951 /* Queue the timeout immediately! */
1952 if (reply_handler_data->timeout_link)
1954 _dbus_connection_queue_synthesized_message_link (connection,
1955 reply_handler_data->timeout_link);
1956 reply_handler_data->timeout_link = NULL;
1960 /* We already queued the timeout? Then it was filtered! */
1961 _dbus_warn ("The timeout error with reply serial %d was filtered, so the reply handler will never be called.\n", reply_serial);
1965 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1968 if (reply_handler_data)
1970 dbus_mutex_unlock (connection->mutex);
1972 _dbus_verbose (" running reply handler on message %p\n", message);
1974 result = _dbus_message_handler_handle_message (reply_handler_data->handler,
1975 connection, message);
1976 reply_handler_data_free (reply_handler_data);
1977 dbus_mutex_lock (connection->mutex);
1981 name = dbus_message_get_name (message);
1984 handler = _dbus_hash_table_lookup_string (connection->handler_table,
1986 if (handler != NULL)
1988 /* We're still protected from dispatch() reentrancy here
1989 * since we acquired the dispatcher
1991 dbus_mutex_unlock (connection->mutex);
1993 _dbus_verbose (" running app handler on message %p\n", message);
1995 result = _dbus_message_handler_handle_message (handler, connection,
1997 dbus_mutex_lock (connection->mutex);
1998 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
2003 _dbus_verbose (" done dispatching %p (%s)\n", message,
2004 dbus_message_get_name (message));
2007 _dbus_connection_release_dispatch (connection);
2008 dbus_mutex_unlock (connection->mutex);
2009 _dbus_list_free_link (message_link);
2010 dbus_message_unref (message); /* don't want the message to count in max message limits
2011 * in computing dispatch status
2014 status = dbus_connection_get_dispatch_status (connection);
2016 dbus_connection_unref (connection);
2022 * Sets the watch functions for the connection. These functions are
2023 * responsible for making the application's main loop aware of file
2024 * descriptors that need to be monitored for events, using select() or
2025 * poll(). When using Qt, typically the DBusAddWatchFunction would
2026 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
2027 * could call g_io_add_watch(), or could be used as part of a more
2028 * elaborate GSource. Note that when a watch is added, it may
2031 * The DBusWatchToggledFunction notifies the application that the
2032 * watch has been enabled or disabled. Call dbus_watch_get_enabled()
2033 * to check this. A disabled watch should have no effect, and enabled
2034 * watch should be added to the main loop. This feature is used
2035 * instead of simply adding/removing the watch because
2036 * enabling/disabling can be done without memory allocation. The
2037 * toggled function may be NULL if a main loop re-queries
2038 * dbus_watch_get_enabled() every time anyway.
2040 * The DBusWatch can be queried for the file descriptor to watch using
2041 * dbus_watch_get_fd(), and for the events to watch for using
2042 * dbus_watch_get_flags(). The flags returned by
2043 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
2044 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
2045 * all watches implicitly include a watch for hangups, errors, and
2046 * other exceptional conditions.
2048 * Once a file descriptor becomes readable or writable, or an exception
2049 * occurs, dbus_connection_handle_watch() should be called to
2050 * notify the connection of the file descriptor's condition.
2052 * dbus_connection_handle_watch() cannot be called during the
2053 * DBusAddWatchFunction, as the connection will not be ready to handle
2056 * It is not allowed to reference a DBusWatch after it has been passed
2057 * to remove_function.
2059 * If #FALSE is returned due to lack of memory, the failure may be due
2060 * to a #FALSE return from the new add_function. If so, the
2061 * add_function may have been called successfully one or more times,
2062 * but the remove_function will also have been called to remove any
2063 * successful adds. i.e. if #FALSE is returned the net result
2064 * should be that dbus_connection_set_watch_functions() has no effect,
2065 * but the add_function and remove_function may have been called.
2067 * @todo We need to drop the lock when we call the
2068 * add/remove/toggled functions which can be a side effect
2069 * of setting the watch functions.
2071 * @param connection the connection.
2072 * @param add_function function to begin monitoring a new descriptor.
2073 * @param remove_function function to stop monitoring a descriptor.
2074 * @param toggled_function function to notify of enable/disable
2075 * @param data data to pass to add_function and remove_function.
2076 * @param free_data_function function to be called to free the data.
2077 * @returns #FALSE on failure (no memory)
2080 dbus_connection_set_watch_functions (DBusConnection *connection,
2081 DBusAddWatchFunction add_function,
2082 DBusRemoveWatchFunction remove_function,
2083 DBusWatchToggledFunction toggled_function,
2085 DBusFreeFunction free_data_function)
2089 dbus_mutex_lock (connection->mutex);
2090 /* ref connection for slightly better reentrancy */
2091 _dbus_connection_ref_unlocked (connection);
2093 /* FIXME this can call back into user code, and we need to drop the
2094 * connection lock when it does.
2096 retval = _dbus_watch_list_set_functions (connection->watches,
2097 add_function, remove_function,
2099 data, free_data_function);
2101 dbus_mutex_unlock (connection->mutex);
2102 /* drop our paranoid refcount */
2103 dbus_connection_unref (connection);
2109 * Sets the timeout functions for the connection. These functions are
2110 * responsible for making the application's main loop aware of timeouts.
2111 * When using Qt, typically the DBusAddTimeoutFunction would create a
2112 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
2115 * The DBusTimeoutToggledFunction notifies the application that the
2116 * timeout has been enabled or disabled. Call
2117 * dbus_timeout_get_enabled() to check this. A disabled timeout should
2118 * have no effect, and enabled timeout should be added to the main
2119 * loop. This feature is used instead of simply adding/removing the
2120 * timeout because enabling/disabling can be done without memory
2121 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used
2122 * to enable and disable. The toggled function may be NULL if a main
2123 * loop re-queries dbus_timeout_get_enabled() every time anyway.
2125 * The DBusTimeout can be queried for the timer interval using
2126 * dbus_timeout_get_interval(). dbus_timeout_handle() should
2127 * be called repeatedly, each time the interval elapses, starting
2128 * after it has elapsed once. The timeout stops firing when
2129 * it is removed with the given remove_function.
2131 * @param connection the connection.
2132 * @param add_function function to add a timeout.
2133 * @param remove_function function to remove a timeout.
2134 * @param toggled_function function to notify of enable/disable
2135 * @param data data to pass to add_function and remove_function.
2136 * @param free_data_function function to be called to free the data.
2137 * @returns #FALSE on failure (no memory)
2140 dbus_connection_set_timeout_functions (DBusConnection *connection,
2141 DBusAddTimeoutFunction add_function,
2142 DBusRemoveTimeoutFunction remove_function,
2143 DBusTimeoutToggledFunction toggled_function,
2145 DBusFreeFunction free_data_function)
2149 dbus_mutex_lock (connection->mutex);
2150 /* ref connection for slightly better reentrancy */
2151 _dbus_connection_ref_unlocked (connection);
2153 retval = _dbus_timeout_list_set_functions (connection->timeouts,
2154 add_function, remove_function,
2156 data, free_data_function);
2158 dbus_mutex_unlock (connection->mutex);
2159 /* drop our paranoid refcount */
2160 dbus_connection_unref (connection);
2166 * Sets the mainloop wakeup function for the connection. Thi function is
2167 * responsible for waking up the main loop (if its sleeping) when some some
2168 * change has happened to the connection that the mainloop needs to reconsiders
2169 * (e.g. a message has been queued for writing).
2170 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
2171 * When using GLib, it would call g_main_context_wakeup().
2174 * @param connection the connection.
2175 * @param wakeup_main_function function to wake up the mainloop
2176 * @param data data to pass wakeup_main_function
2177 * @param free_data_function function to be called to free the data.
2180 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
2181 DBusWakeupMainFunction wakeup_main_function,
2183 DBusFreeFunction free_data_function)
2186 DBusFreeFunction old_free_data;
2188 dbus_mutex_lock (connection->mutex);
2189 old_data = connection->wakeup_main_data;
2190 old_free_data = connection->free_wakeup_main_data;
2192 connection->wakeup_main_function = wakeup_main_function;
2193 connection->wakeup_main_data = data;
2194 connection->free_wakeup_main_data = free_data_function;
2196 dbus_mutex_unlock (connection->mutex);
2198 /* Callback outside the lock */
2200 (*old_free_data) (old_data);
2204 * Called to notify the connection when a previously-added watch
2205 * is ready for reading or writing, or has an exception such
2208 * If this function returns #FALSE, then the file descriptor may still
2209 * be ready for reading or writing, but more memory is needed in order
2210 * to do the reading or writing. If you ignore the #FALSE return, your
2211 * application may spin in a busy loop on the file descriptor until
2212 * memory becomes available, but nothing more catastrophic should
2215 * @param connection the connection.
2216 * @param watch the watch.
2217 * @param condition the current condition of the file descriptors being watched.
2218 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory
2221 dbus_connection_handle_watch (DBusConnection *connection,
2223 unsigned int condition)
2227 dbus_mutex_lock (connection->mutex);
2228 _dbus_connection_acquire_io_path (connection, -1);
2229 retval = _dbus_transport_handle_watch (connection->transport,
2231 _dbus_connection_release_io_path (connection);
2232 dbus_mutex_unlock (connection->mutex);
2238 * Gets the UNIX user ID of the connection if any.
2239 * Returns #TRUE if the uid is filled in.
2240 * Always returns #FALSE on non-UNIX platforms.
2241 * Always returns #FALSE prior to authenticating the
2244 * @param connection the connection
2245 * @param uid return location for the user ID
2246 * @returns #TRUE if uid is filled in with a valid user ID
2249 dbus_connection_get_unix_user (DBusConnection *connection,
2254 dbus_mutex_lock (connection->mutex);
2256 if (!_dbus_transport_get_is_authenticated (connection->transport))
2259 result = _dbus_transport_get_unix_user (connection->transport,
2261 dbus_mutex_unlock (connection->mutex);
2267 * Sets a predicate function used to determine whether a given user ID
2268 * is allowed to connect. When an incoming connection has
2269 * authenticated with a particular user ID, this function is called;
2270 * if it returns #TRUE, the connection is allowed to proceed,
2271 * otherwise the connection is disconnected.
2273 * If the function is set to #NULL (as it is by default), then
2274 * only the same UID as the server process will be allowed to
2277 * @param connection the connection
2278 * @param function the predicate
2279 * @param data data to pass to the predicate
2280 * @param free_data_function function to free the data
2283 dbus_connection_set_unix_user_function (DBusConnection *connection,
2284 DBusAllowUnixUserFunction function,
2286 DBusFreeFunction free_data_function)
2288 void *old_data = NULL;
2289 DBusFreeFunction old_free_function = NULL;
2291 dbus_mutex_lock (connection->mutex);
2292 _dbus_transport_set_unix_user_function (connection->transport,
2293 function, data, free_data_function,
2294 &old_data, &old_free_function);
2295 dbus_mutex_unlock (connection->mutex);
2297 if (old_free_function != NULL)
2298 (* old_free_function) (old_data);
2302 * Adds a message filter. Filters are handlers that are run on
2303 * all incoming messages, prior to the normal handlers
2304 * registered with dbus_connection_register_handler().
2305 * Filters are run in the order that they were added.
2306 * The same handler can be added as a filter more than once, in
2307 * which case it will be run more than once.
2308 * Filters added during a filter callback won't be run on the
2309 * message being processed.
2311 * @param connection the connection
2312 * @param handler the handler
2313 * @returns #TRUE on success, #FALSE if not enough memory.
2316 dbus_connection_add_filter (DBusConnection *connection,
2317 DBusMessageHandler *handler)
2319 dbus_mutex_lock (connection->mutex);
2320 if (!_dbus_message_handler_add_connection (handler, connection))
2322 dbus_mutex_unlock (connection->mutex);
2326 if (!_dbus_list_append (&connection->filter_list,
2329 _dbus_message_handler_remove_connection (handler, connection);
2330 dbus_mutex_unlock (connection->mutex);
2334 dbus_mutex_unlock (connection->mutex);
2339 * Removes a previously-added message filter. It is a programming
2340 * error to call this function for a handler that has not
2341 * been added as a filter. If the given handler was added
2342 * more than once, only one instance of it will be removed
2343 * (the most recently-added instance).
2345 * @param connection the connection
2346 * @param handler the handler to remove
2350 dbus_connection_remove_filter (DBusConnection *connection,
2351 DBusMessageHandler *handler)
2353 dbus_mutex_lock (connection->mutex);
2354 if (!_dbus_list_remove_last (&connection->filter_list, handler))
2356 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
2357 dbus_mutex_unlock (connection->mutex);
2361 _dbus_message_handler_remove_connection (handler, connection);
2363 dbus_mutex_unlock (connection->mutex);
2367 * Registers a handler for a list of message names. A single handler
2368 * can be registered for any number of message names, but each message
2369 * name can only have one handler at a time. It's not allowed to call
2370 * this function with the name of a message that already has a
2371 * handler. If the function returns #FALSE, the handlers were not
2372 * registered due to lack of memory.
2374 * @todo the messages_to_handle arg may be more convenient if it's a
2375 * single string instead of an array. Though right now MessageHandler
2376 * is sort of designed to say be associated with an entire object with
2377 * multiple methods, that's why for example the connection only
2378 * weakrefs it. So maybe the "manual" API should be different.
2380 * @param connection the connection
2381 * @param handler the handler
2382 * @param messages_to_handle the messages to handle
2383 * @param n_messages the number of message names in messages_to_handle
2384 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
2388 dbus_connection_register_handler (DBusConnection *connection,
2389 DBusMessageHandler *handler,
2390 const char **messages_to_handle,
2395 dbus_mutex_lock (connection->mutex);
2397 while (i < n_messages)
2402 key = _dbus_strdup (messages_to_handle[i]);
2406 if (!_dbus_hash_iter_lookup (connection->handler_table,
2414 if (_dbus_hash_iter_get_value (&iter) != NULL)
2416 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
2417 messages_to_handle[i]);
2418 dbus_free (key); /* won't have replaced the old key with the new one */
2422 if (!_dbus_message_handler_add_connection (handler, connection))
2424 _dbus_hash_iter_remove_entry (&iter);
2425 /* key has freed on nuking the entry */
2429 _dbus_hash_iter_set_value (&iter, handler);
2434 dbus_mutex_unlock (connection->mutex);
2438 /* unregister everything registered so far,
2439 * so we don't fail partially
2441 dbus_connection_unregister_handler (connection,
2446 dbus_mutex_unlock (connection->mutex);
2451 * Unregisters a handler for a list of message names. The handlers
2452 * must have been previously registered.
2454 * @param connection the connection
2455 * @param handler the handler
2456 * @param messages_to_handle the messages to handle
2457 * @param n_messages the number of message names in messages_to_handle
2461 dbus_connection_unregister_handler (DBusConnection *connection,
2462 DBusMessageHandler *handler,
2463 const char **messages_to_handle,
2468 dbus_mutex_lock (connection->mutex);
2470 while (i < n_messages)
2474 if (!_dbus_hash_iter_lookup (connection->handler_table,
2475 (char*) messages_to_handle[i], FALSE,
2478 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
2479 messages_to_handle[i]);
2481 else if (_dbus_hash_iter_get_value (&iter) != handler)
2483 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
2484 messages_to_handle[i]);
2488 _dbus_hash_iter_remove_entry (&iter);
2489 _dbus_message_handler_remove_connection (handler, connection);
2495 dbus_mutex_unlock (connection->mutex);
2498 static DBusDataSlotAllocator slot_allocator;
2499 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
2502 * Allocates an integer ID to be used for storing application-specific
2503 * data on any DBusConnection. The allocated ID may then be used
2504 * with dbus_connection_set_data() and dbus_connection_get_data().
2505 * If allocation fails, -1 is returned. Again, the allocated
2506 * slot is global, i.e. all DBusConnection objects will
2507 * have a slot with the given integer ID reserved.
2509 * @returns -1 on failure, otherwise the data slot ID
2512 dbus_connection_allocate_data_slot (void)
2514 return _dbus_data_slot_allocator_alloc (&slot_allocator,
2515 _DBUS_LOCK_NAME (connection_slots));
2519 * Deallocates a global ID for connection data slots.
2520 * dbus_connection_get_data() and dbus_connection_set_data()
2521 * may no longer be used with this slot.
2522 * Existing data stored on existing DBusConnection objects
2523 * will be freed when the connection is finalized,
2524 * but may not be retrieved (and may only be replaced
2525 * if someone else reallocates the slot).
2527 * @param slot the slot to deallocate
2530 dbus_connection_free_data_slot (int slot)
2532 _dbus_data_slot_allocator_free (&slot_allocator, slot);
2536 * Stores a pointer on a DBusConnection, along
2537 * with an optional function to be used for freeing
2538 * the data when the data is set again, or when
2539 * the connection is finalized. The slot number
2540 * must have been allocated with dbus_connection_allocate_data_slot().
2542 * @param connection the connection
2543 * @param slot the slot number
2544 * @param data the data to store
2545 * @param free_data_func finalizer function for the data
2546 * @returns #TRUE if there was enough memory to store the data
2549 dbus_connection_set_data (DBusConnection *connection,
2552 DBusFreeFunction free_data_func)
2554 DBusFreeFunction old_free_func;
2558 dbus_mutex_lock (connection->mutex);
2560 retval = _dbus_data_slot_list_set (&slot_allocator,
2561 &connection->slot_list,
2562 slot, data, free_data_func,
2563 &old_free_func, &old_data);
2565 dbus_mutex_unlock (connection->mutex);
2569 /* Do the actual free outside the connection lock */
2571 (* old_free_func) (old_data);
2578 * Retrieves data previously set with dbus_connection_set_data().
2579 * The slot must still be allocated (must not have been freed).
2581 * @param connection the connection
2582 * @param slot the slot to get data from
2583 * @returns the data, or #NULL if not found
2586 dbus_connection_get_data (DBusConnection *connection,
2591 dbus_mutex_lock (connection->mutex);
2593 res = _dbus_data_slot_list_get (&slot_allocator,
2594 &connection->slot_list,
2597 dbus_mutex_unlock (connection->mutex);
2603 * This function sets a global flag for whether dbus_connection_new()
2604 * will set SIGPIPE behavior to SIG_IGN.
2606 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
2609 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
2611 _dbus_modify_sigpipe = will_modify_sigpipe;
2615 * Specifies the maximum size message this connection is allowed to
2616 * receive. Larger messages will result in disconnecting the
2619 * @param connection a #DBusConnection
2620 * @param size maximum message size the connection can receive, in bytes
2623 dbus_connection_set_max_message_size (DBusConnection *connection,
2626 dbus_mutex_lock (connection->mutex);
2627 _dbus_transport_set_max_message_size (connection->transport,
2629 dbus_mutex_unlock (connection->mutex);
2633 * Gets the value set by dbus_connection_set_max_message_size().
2635 * @param connection the connection
2636 * @returns the max size of a single message
2639 dbus_connection_get_max_message_size (DBusConnection *connection)
2642 dbus_mutex_lock (connection->mutex);
2643 res = _dbus_transport_get_max_message_size (connection->transport);
2644 dbus_mutex_unlock (connection->mutex);
2649 * Sets the maximum total number of bytes that can be used for all messages
2650 * received on this connection. Messages count toward the maximum until
2651 * they are finalized. When the maximum is reached, the connection will
2652 * not read more data until some messages are finalized.
2654 * The semantics of the maximum are: if outstanding messages are
2655 * already above the maximum, additional messages will not be read.
2656 * The semantics are not: if the next message would cause us to exceed
2657 * the maximum, we don't read it. The reason is that we don't know the
2658 * size of a message until after we read it.
2660 * Thus, the max live messages size can actually be exceeded
2661 * by up to the maximum size of a single message.
2663 * Also, if we read say 1024 bytes off the wire in a single read(),
2664 * and that contains a half-dozen small messages, we may exceed the
2665 * size max by that amount. But this should be inconsequential.
2667 * This does imply that we can't call read() with a buffer larger
2668 * than we're willing to exceed this limit by.
2670 * @param connection the connection
2671 * @param size the maximum size in bytes of all outstanding messages
2674 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
2677 dbus_mutex_lock (connection->mutex);
2678 _dbus_transport_set_max_live_messages_size (connection->transport,
2680 dbus_mutex_unlock (connection->mutex);
2684 * Gets the value set by dbus_connection_set_max_live_messages_size().
2686 * @param connection the connection
2687 * @returns the max size of all live messages
2690 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
2693 dbus_mutex_lock (connection->mutex);
2694 res = _dbus_transport_get_max_live_messages_size (connection->transport);
2695 dbus_mutex_unlock (connection->mutex);