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_message */
80 DBusCondVar *dispatch_cond; /**< Protects dispatch_message */
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);
133 * Acquires the connection lock.
135 * @param connection the connection.
138 _dbus_connection_lock (DBusConnection *connection)
140 dbus_mutex_lock (connection->mutex);
144 * Releases the connection lock.
146 * @param connection the connection.
149 _dbus_connection_unlock (DBusConnection *connection)
151 dbus_mutex_unlock (connection->mutex);
155 * Wakes up the main loop if it is sleeping
156 * Needed if we're e.g. queueing outgoing messages
157 * on a thread while the mainloop sleeps.
159 * @param connection the connection.
162 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
164 if (connection->wakeup_main_function)
165 (*connection->wakeup_main_function) (connection->wakeup_main_data);
169 * Adds a message to the incoming message queue, returning #FALSE
170 * if there's insufficient memory to queue the message.
172 * @param connection the connection.
173 * @param message the message to queue.
174 * @returns #TRUE on success.
177 _dbus_connection_queue_received_message (DBusConnection *connection,
178 DBusMessage *message)
180 ReplyHandlerData *reply_handler_data;
181 dbus_int32_t reply_serial;
183 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
185 if (!_dbus_list_append (&connection->incoming_messages,
189 /* If this is a reply we're waiting on, remove timeout for it */
190 reply_serial = _dbus_message_get_reply_serial (message);
191 if (reply_serial != -1)
193 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
195 if (reply_handler_data != NULL)
197 if (reply_handler_data->timeout_added)
198 _dbus_connection_remove_timeout_locked (connection,
199 reply_handler_data->timeout);
200 reply_handler_data->timeout_added = FALSE;
204 dbus_message_ref (message);
205 connection->n_incoming += 1;
207 _dbus_connection_wakeup_mainloop (connection);
209 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
210 message, connection->n_incoming);
216 * Adds a link + message to the incoming message queue.
217 * Can't fail. Takes ownership of both link and message.
219 * @param connection the connection.
220 * @param link the list node and message to queue.
222 * @todo This needs to wake up the mainloop if it is in
223 * a poll/select and this is a multithreaded app.
226 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
229 _dbus_list_append_link (&connection->incoming_messages, link);
231 connection->n_incoming += 1;
233 _dbus_connection_wakeup_mainloop (connection);
235 _dbus_verbose ("Incoming synthesized message %p added to queue, %d incoming\n",
236 link->data, connection->n_incoming);
241 * Checks whether there are messages in the outgoing message queue.
243 * @param connection the connection.
244 * @returns #TRUE if the outgoing queue is non-empty.
247 _dbus_connection_have_messages_to_send (DBusConnection *connection)
249 return connection->outgoing_messages != NULL;
253 * Gets the next outgoing message. The message remains in the
254 * queue, and the caller does not own a reference to it.
256 * @param connection the connection.
257 * @returns the message to be sent.
260 _dbus_connection_get_message_to_send (DBusConnection *connection)
262 return _dbus_list_get_last (&connection->outgoing_messages);
266 * Notifies the connection that a message has been sent, so the
267 * message can be removed from the outgoing queue.
269 * @param connection the connection.
270 * @param message the message that was sent.
273 _dbus_connection_message_sent (DBusConnection *connection,
274 DBusMessage *message)
276 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
277 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
279 _dbus_list_pop_last (&connection->outgoing_messages);
280 dbus_message_unref (message);
282 connection->n_outgoing -= 1;
284 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
285 message, connection->n_outgoing);
287 if (connection->n_outgoing == 0)
288 _dbus_transport_messages_pending (connection->transport,
289 connection->n_outgoing);
293 * Adds a watch using the connection's DBusAddWatchFunction if
294 * available. Otherwise records the watch to be added when said
295 * function is available. Also re-adds the watch if the
296 * DBusAddWatchFunction changes. May fail due to lack of memory.
298 * @param connection the connection.
299 * @param watch the watch to add.
300 * @returns #TRUE on success.
303 _dbus_connection_add_watch (DBusConnection *connection,
306 if (connection->watches) /* null during finalize */
307 return _dbus_watch_list_add_watch (connection->watches,
314 * Removes a watch using the connection's DBusRemoveWatchFunction
315 * if available. It's an error to call this function on a watch
316 * that was not previously added.
318 * @param connection the connection.
319 * @param watch the watch to remove.
322 _dbus_connection_remove_watch (DBusConnection *connection,
325 if (connection->watches) /* null during finalize */
326 _dbus_watch_list_remove_watch (connection->watches,
331 * Adds a timeout using the connection's DBusAddTimeoutFunction if
332 * available. Otherwise records the timeout to be added when said
333 * function is available. Also re-adds the timeout if the
334 * DBusAddTimeoutFunction changes. May fail due to lack of memory.
336 * @param connection the connection.
337 * @param timeout the timeout to add.
338 * @returns #TRUE on success.
341 _dbus_connection_add_timeout (DBusConnection *connection,
342 DBusTimeout *timeout)
344 if (connection->timeouts) /* null during finalize */
345 return _dbus_timeout_list_add_timeout (connection->timeouts,
352 * Removes a timeout using the connection's DBusRemoveTimeoutFunction
353 * if available. It's an error to call this function on a timeout
354 * that was not previously added.
356 * @param connection the connection.
357 * @param timeout the timeout to remove.
360 _dbus_connection_remove_timeout (DBusConnection *connection,
361 DBusTimeout *timeout)
363 if (connection->timeouts) /* null during finalize */
364 _dbus_timeout_list_remove_timeout (connection->timeouts,
369 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
370 DBusTimeout *timeout)
372 dbus_mutex_lock (connection->mutex);
373 _dbus_connection_remove_timeout (connection, timeout);
374 dbus_mutex_unlock (connection->mutex);
379 * Tells the connection that the transport has been disconnected.
380 * Results in posting a disconnect message on the incoming message
381 * queue. Only has an effect the first time it's called.
383 * @param connection the connection
386 _dbus_connection_notify_disconnected (DBusConnection *connection)
388 if (connection->disconnect_message_link)
390 /* We haven't sent the disconnect message already */
391 _dbus_connection_queue_synthesized_message_link (connection,
392 connection->disconnect_message_link);
393 connection->disconnect_message_link = NULL;
399 * Acquire the transporter I/O path. This must be done before
400 * doing any I/O in the transporter. May sleep and drop the
401 * connection mutex while waiting for the I/O path.
403 * @param connection the connection.
404 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
405 * @returns TRUE if the I/O path was acquired.
408 _dbus_connection_acquire_io_path (DBusConnection *connection,
409 int timeout_milliseconds)
411 dbus_bool_t res = TRUE;
413 if (connection->io_path_acquired)
415 if (timeout_milliseconds != -1)
416 res = dbus_condvar_wait_timeout (connection->io_path_cond,
418 timeout_milliseconds);
420 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
425 _dbus_assert (!connection->io_path_acquired);
427 connection->io_path_acquired = TRUE;
434 * Release the I/O path when you're done with it. Only call
435 * after you've acquired the I/O. Wakes up at most one thread
436 * currently waiting to acquire the I/O path.
438 * @param connection the connection.
441 _dbus_connection_release_io_path (DBusConnection *connection)
443 _dbus_assert (connection->io_path_acquired);
445 connection->io_path_acquired = FALSE;
446 dbus_condvar_wake_one (connection->io_path_cond);
451 * Queues incoming messages and sends outgoing messages for this
452 * connection, optionally blocking in the process. Each call to
453 * _dbus_connection_do_iteration() will call select() or poll() one
454 * time and then read or write data if possible.
456 * The purpose of this function is to be able to flush outgoing
457 * messages or queue up incoming messages without returning
458 * control to the application and causing reentrancy weirdness.
460 * The flags parameter allows you to specify whether to
461 * read incoming messages, write outgoing messages, or both,
462 * and whether to block if no immediate action is possible.
464 * The timeout_milliseconds parameter does nothing unless the
465 * iteration is blocking.
467 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
468 * wasn't specified, then it's impossible to block, even if
469 * you specify DBUS_ITERATION_BLOCK; in that case the function
470 * returns immediately.
472 * @param connection the connection.
473 * @param flags iteration flags.
474 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
477 _dbus_connection_do_iteration (DBusConnection *connection,
479 int timeout_milliseconds)
481 if (connection->n_outgoing == 0)
482 flags &= ~DBUS_ITERATION_DO_WRITING;
484 if (_dbus_connection_acquire_io_path (connection,
485 (flags & DBUS_ITERATION_BLOCK)?timeout_milliseconds:0))
487 _dbus_transport_do_iteration (connection->transport,
488 flags, timeout_milliseconds);
489 _dbus_connection_release_io_path (connection);
494 * Creates a new connection for the given transport. A transport
495 * represents a message stream that uses some concrete mechanism, such
496 * as UNIX domain sockets. May return #NULL if insufficient
497 * memory exists to create the connection.
499 * @param transport the transport.
500 * @returns the new connection, or #NULL on failure.
503 _dbus_connection_new_for_transport (DBusTransport *transport)
505 DBusConnection *connection;
506 DBusWatchList *watch_list;
507 DBusTimeoutList *timeout_list;
508 DBusHashTable *handler_table, *pending_replies;
510 DBusCondVar *message_returned_cond;
511 DBusCondVar *dispatch_cond;
512 DBusCondVar *io_path_cond;
513 DBusList *disconnect_link;
514 DBusMessage *disconnect_message;
518 handler_table = NULL;
519 pending_replies = NULL;
522 message_returned_cond = NULL;
523 dispatch_cond = NULL;
525 disconnect_link = NULL;
526 disconnect_message = NULL;
528 watch_list = _dbus_watch_list_new ();
529 if (watch_list == NULL)
532 timeout_list = _dbus_timeout_list_new ();
533 if (timeout_list == NULL)
537 _dbus_hash_table_new (DBUS_HASH_STRING,
539 if (handler_table == NULL)
543 _dbus_hash_table_new (DBUS_HASH_INT,
544 NULL, (DBusFreeFunction)reply_handler_data_free);
545 if (pending_replies == NULL)
548 connection = dbus_new0 (DBusConnection, 1);
549 if (connection == NULL)
552 mutex = dbus_mutex_new ();
556 message_returned_cond = dbus_condvar_new ();
557 if (message_returned_cond == NULL)
560 dispatch_cond = dbus_condvar_new ();
561 if (dispatch_cond == NULL)
564 io_path_cond = dbus_condvar_new ();
565 if (io_path_cond == NULL)
568 disconnect_message = dbus_message_new (NULL, DBUS_MESSAGE_LOCAL_DISCONNECT);
569 if (disconnect_message == NULL)
572 disconnect_link = _dbus_list_alloc_link (disconnect_message);
573 if (disconnect_link == NULL)
576 if (_dbus_modify_sigpipe)
577 _dbus_disable_sigpipe ();
579 connection->refcount = 1;
580 connection->mutex = mutex;
581 connection->dispatch_cond = dispatch_cond;
582 connection->io_path_cond = io_path_cond;
583 connection->message_returned_cond = message_returned_cond;
584 connection->transport = transport;
585 connection->watches = watch_list;
586 connection->timeouts = timeout_list;
587 connection->handler_table = handler_table;
588 connection->pending_replies = pending_replies;
589 connection->filter_list = NULL;
591 _dbus_data_slot_list_init (&connection->slot_list);
593 connection->client_serial = 1;
595 connection->disconnect_message_link = disconnect_link;
597 _dbus_transport_ref (transport);
598 _dbus_transport_set_connection (transport, connection);
603 if (disconnect_message != NULL)
604 dbus_message_unref (disconnect_message);
606 if (disconnect_link != NULL)
607 _dbus_list_free_link (disconnect_link);
609 if (io_path_cond != NULL)
610 dbus_condvar_free (io_path_cond);
612 if (dispatch_cond != NULL)
613 dbus_condvar_free (dispatch_cond);
615 if (message_returned_cond != NULL)
616 dbus_condvar_free (message_returned_cond);
619 dbus_mutex_free (mutex);
621 if (connection != NULL)
622 dbus_free (connection);
625 _dbus_hash_table_unref (handler_table);
628 _dbus_hash_table_unref (pending_replies);
631 _dbus_watch_list_free (watch_list);
634 _dbus_timeout_list_free (timeout_list);
640 _dbus_connection_get_next_client_serial (DBusConnection *connection)
644 serial = connection->client_serial++;
646 if (connection->client_serial < 0)
647 connection->client_serial = 1;
653 * Used to notify a connection when a DBusMessageHandler is
654 * destroyed, so the connection can drop any reference
655 * to the handler. This is a private function, but still
656 * takes the connection lock. Don't call it with the lock held.
658 * @todo needs to check in pending_replies too.
660 * @param connection the connection
661 * @param handler the handler
664 _dbus_connection_handler_destroyed_locked (DBusConnection *connection,
665 DBusMessageHandler *handler)
670 dbus_mutex_lock (connection->mutex);
672 _dbus_hash_iter_init (connection->handler_table, &iter);
673 while (_dbus_hash_iter_next (&iter))
675 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
678 _dbus_hash_iter_remove_entry (&iter);
681 link = _dbus_list_get_first_link (&connection->filter_list);
684 DBusMessageHandler *h = link->data;
685 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
688 _dbus_list_remove_link (&connection->filter_list,
693 dbus_mutex_unlock (connection->mutex);
697 * Adds the counter used to count the number of open connections.
698 * Increments the counter by one, and saves it to be decremented
699 * again when this connection is finalized.
701 * @param connection a #DBusConnection
702 * @param counter counter that tracks number of connections
705 _dbus_connection_set_connection_counter (DBusConnection *connection,
706 DBusCounter *counter)
708 _dbus_assert (connection->connection_counter == NULL);
710 connection->connection_counter = counter;
711 _dbus_counter_ref (connection->connection_counter);
712 _dbus_counter_adjust (connection->connection_counter, 1);
718 * @addtogroup DBusConnection
724 * Opens a new connection to a remote address.
726 * @todo specify what the address parameter is. Right now
727 * it's just the name of a UNIX domain socket. It should be
728 * something more complex that encodes which transport to use.
730 * If the open fails, the function returns #NULL, and provides
731 * a reason for the failure in the result parameter. Pass
732 * #NULL for the result parameter if you aren't interested
733 * in the reason for failure.
735 * @param address the address.
736 * @param result address where a result code can be returned.
737 * @returns new connection, or #NULL on failure.
740 dbus_connection_open (const char *address,
741 DBusResultCode *result)
743 DBusConnection *connection;
744 DBusTransport *transport;
746 transport = _dbus_transport_open (address, result);
747 if (transport == NULL)
750 connection = _dbus_connection_new_for_transport (transport);
752 _dbus_transport_unref (transport);
754 if (connection == NULL)
756 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
764 * Increments the reference count of a DBusConnection.
766 * @param connection the connection.
769 dbus_connection_ref (DBusConnection *connection)
771 dbus_mutex_lock (connection->mutex);
772 _dbus_assert (connection->refcount > 0);
774 connection->refcount += 1;
775 dbus_mutex_unlock (connection->mutex);
779 * Increments the reference count of a DBusConnection.
780 * Requires that the caller already holds the connection lock.
782 * @param connection the connection.
785 _dbus_connection_ref_unlocked (DBusConnection *connection)
787 _dbus_assert (connection->refcount > 0);
788 connection->refcount += 1;
792 /* This is run without the mutex held, but after the last reference
793 * to the connection has been dropped we should have no thread-related
797 _dbus_connection_last_unref (DBusConnection *connection)
802 /* You have to disconnect the connection before unref:ing it. Otherwise
803 * you won't get the disconnected message.
805 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
807 if (connection->connection_counter != NULL)
809 /* subtract ourselves from the counter */
810 _dbus_counter_adjust (connection->connection_counter, - 1);
811 _dbus_counter_unref (connection->connection_counter);
812 connection->connection_counter = NULL;
815 _dbus_watch_list_free (connection->watches);
816 connection->watches = NULL;
818 _dbus_timeout_list_free (connection->timeouts);
819 connection->timeouts = NULL;
821 /* calls out to application code... */
822 _dbus_data_slot_list_free (&connection->slot_list);
824 _dbus_hash_iter_init (connection->handler_table, &iter);
825 while (_dbus_hash_iter_next (&iter))
827 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
829 _dbus_message_handler_remove_connection (h, connection);
832 link = _dbus_list_get_first_link (&connection->filter_list);
835 DBusMessageHandler *h = link->data;
836 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
838 _dbus_message_handler_remove_connection (h, connection);
843 _dbus_hash_table_unref (connection->handler_table);
844 connection->handler_table = NULL;
846 _dbus_hash_table_unref (connection->pending_replies);
847 connection->pending_replies = NULL;
849 _dbus_list_clear (&connection->filter_list);
851 _dbus_list_foreach (&connection->outgoing_messages,
852 (DBusForeachFunction) dbus_message_unref,
854 _dbus_list_clear (&connection->outgoing_messages);
856 _dbus_list_foreach (&connection->incoming_messages,
857 (DBusForeachFunction) dbus_message_unref,
859 _dbus_list_clear (&connection->incoming_messages);
861 _dbus_transport_unref (connection->transport);
863 if (connection->disconnect_message_link)
865 DBusMessage *message = connection->disconnect_message_link->data;
866 dbus_message_unref (message);
867 _dbus_list_free_link (connection->disconnect_message_link);
870 dbus_condvar_free (connection->dispatch_cond);
871 dbus_condvar_free (connection->io_path_cond);
872 dbus_condvar_free (connection->message_returned_cond);
874 dbus_mutex_free (connection->mutex);
876 dbus_free (connection);
880 * Decrements the reference count of a DBusConnection, and finalizes
881 * it if the count reaches zero. It is a bug to drop the last reference
882 * to a connection that has not been disconnected.
884 * @param connection the connection.
887 dbus_connection_unref (DBusConnection *connection)
889 dbus_bool_t last_unref;
891 dbus_mutex_lock (connection->mutex);
893 _dbus_assert (connection != NULL);
894 _dbus_assert (connection->refcount > 0);
896 connection->refcount -= 1;
897 last_unref = (connection->refcount == 0);
899 dbus_mutex_unlock (connection->mutex);
902 _dbus_connection_last_unref (connection);
906 * Closes the connection, so no further data can be sent or received.
907 * Any further attempts to send data will result in errors. This
908 * function does not affect the connection's reference count. It's
909 * safe to disconnect a connection more than once; all calls after the
910 * first do nothing. It's impossible to "reconnect" a connection, a
911 * new connection must be created.
913 * @param connection the connection.
916 dbus_connection_disconnect (DBusConnection *connection)
918 dbus_mutex_lock (connection->mutex);
919 _dbus_transport_disconnect (connection->transport);
920 dbus_mutex_unlock (connection->mutex);
924 * Gets whether the connection is currently connected. All
925 * connections are connected when they are opened. A connection may
926 * become disconnected when the remote application closes its end, or
927 * exits; a connection may also be disconnected with
928 * dbus_connection_disconnect().
930 * @param connection the connection.
931 * @returns #TRUE if the connection is still alive.
934 dbus_connection_get_is_connected (DBusConnection *connection)
938 dbus_mutex_lock (connection->mutex);
939 res = _dbus_transport_get_is_connected (connection->transport);
940 dbus_mutex_unlock (connection->mutex);
946 * Gets whether the connection was authenticated. (Note that
947 * if the connection was authenticated then disconnected,
948 * this function still returns #TRUE)
950 * @param connection the connection
951 * @returns #TRUE if the connection was ever authenticated
954 dbus_connection_get_is_authenticated (DBusConnection *connection)
958 dbus_mutex_lock (connection->mutex);
959 res = _dbus_transport_get_is_authenticated (connection->transport);
960 dbus_mutex_unlock (connection->mutex);
966 * Adds a message to the outgoing message queue. Does not block to
967 * write the message to the network; that happens asynchronously. to
968 * force the message to be written, call dbus_connection_flush().
970 * If the function fails, it returns #FALSE and returns the
971 * reason for failure via the result parameter.
972 * The result parameter can be #NULL if you aren't interested
973 * in the reason for the failure.
975 * @param connection the connection.
976 * @param message the message to write.
977 * @param client_serial return location for client serial.
978 * @param result address where result code can be placed.
979 * @returns #TRUE on success.
982 dbus_connection_send_message (DBusConnection *connection,
983 DBusMessage *message,
984 dbus_int32_t *client_serial,
985 DBusResultCode *result)
990 dbus_mutex_lock (connection->mutex);
992 if (!_dbus_list_prepend (&connection->outgoing_messages,
995 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
996 dbus_mutex_unlock (connection->mutex);
1000 dbus_message_ref (message);
1001 connection->n_outgoing += 1;
1003 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
1004 message, connection->n_outgoing);
1006 if (_dbus_message_get_client_serial (message) == -1)
1008 serial = _dbus_connection_get_next_client_serial (connection);
1009 _dbus_message_set_client_serial (message, serial);
1013 *client_serial = _dbus_message_get_client_serial (message);
1015 _dbus_message_lock (message);
1017 if (connection->n_outgoing == 1)
1018 _dbus_transport_messages_pending (connection->transport,
1019 connection->n_outgoing);
1021 _dbus_connection_wakeup_mainloop (connection);
1023 dbus_mutex_unlock (connection->mutex);
1029 reply_handler_timeout (void *data)
1031 DBusConnection *connection;
1032 ReplyHandlerData *reply_handler_data = data;
1034 connection = reply_handler_data->connection;
1036 dbus_mutex_lock (connection->mutex);
1037 if (reply_handler_data->timeout_link)
1039 _dbus_connection_queue_synthesized_message_link (connection,
1040 reply_handler_data->timeout_link);
1041 reply_handler_data->timeout_link = NULL;
1044 _dbus_connection_remove_timeout (connection,
1045 reply_handler_data->timeout);
1046 reply_handler_data->timeout_added = FALSE;
1048 dbus_mutex_unlock (connection->mutex);
1052 reply_handler_data_free (ReplyHandlerData *data)
1057 if (data->timeout_added)
1058 _dbus_connection_remove_timeout_locked (data->connection,
1061 if (data->connection_added)
1062 _dbus_message_handler_remove_connection (data->handler,
1065 if (data->timeout_link)
1067 dbus_message_unref ((DBusMessage *)data->timeout_link->data);
1068 _dbus_list_free_link (data->timeout_link);
1071 dbus_message_handler_unref (data->handler);
1077 * Queues a message to send, as with dbus_connection_send_message(),
1078 * but also sets up a DBusMessageHandler to receive a reply to the
1079 * message. If no reply is received in the given timeout_milliseconds,
1080 * expires the pending reply and sends the DBusMessageHandler a
1081 * synthetic error reply (generated in-process, not by the remote
1082 * application) indicating that a timeout occurred.
1084 * Reply handlers see their replies after message filters see them,
1085 * but before message handlers added with
1086 * dbus_connection_register_handler() see them, regardless of the
1087 * reply message's name. Reply handlers are only handed a single
1088 * message as a reply, after one reply has been seen the handler is
1089 * removed. If a filter filters out the reply before the handler sees
1090 * it, the reply is immediately timed out and a timeout error reply is
1091 * generated. If a filter removes the timeout error reply then the
1092 * reply handler will never be called. Filters should not do this.
1094 * If #NULL is passed for the reply_handler, the timeout reply will
1095 * still be generated and placed into the message queue, but no
1096 * specific message handler will receive the reply.
1098 * If -1 is passed for the timeout, a sane default timeout is used. -1
1099 * is typically the best value for the timeout for this reason, unless
1100 * you want a very short or very long timeout. There is no way to
1101 * avoid a timeout entirely, other than passing INT_MAX for the
1102 * timeout to postpone it indefinitely.
1104 * @todo I think we should rename this function family
1105 * dbus_connection_send(), send_with_reply(), etc. (i.e.
1106 * drop the "message" part), the names are too long.
1108 * @param connection the connection
1109 * @param message the message to send
1110 * @param reply_handler message handler expecting the reply, or #NULL
1111 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1112 * @param result return location for result code
1113 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
1117 dbus_connection_send_message_with_reply (DBusConnection *connection,
1118 DBusMessage *message,
1119 DBusMessageHandler *reply_handler,
1120 int timeout_milliseconds,
1121 DBusResultCode *result)
1123 DBusTimeout *timeout;
1124 ReplyHandlerData *data;
1126 DBusList *reply_link;
1127 dbus_int32_t serial = -1;
1129 if (timeout_milliseconds == -1)
1130 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1132 data = dbus_new0 (ReplyHandlerData, 1);
1136 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1140 timeout = _dbus_timeout_new (timeout_milliseconds, reply_handler_timeout,
1145 reply_handler_data_free (data);
1146 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1150 dbus_mutex_lock (connection->mutex);
1153 if (!_dbus_connection_add_timeout (connection, timeout))
1155 reply_handler_data_free (data);
1156 _dbus_timeout_unref (timeout);
1157 dbus_mutex_unlock (connection->mutex);
1159 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1163 /* The connection now owns the reference to the timeout. */
1164 _dbus_timeout_unref (timeout);
1166 data->timeout_added = TRUE;
1167 data->timeout = timeout;
1168 data->connection = connection;
1170 if (!_dbus_message_handler_add_connection (reply_handler, connection))
1172 dbus_mutex_unlock (connection->mutex);
1173 reply_handler_data_free (data);
1175 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1178 data->connection_added = TRUE;
1180 /* Assign a serial to the message */
1181 if (_dbus_message_get_client_serial (message) == -1)
1183 serial = _dbus_connection_get_next_client_serial (connection);
1184 _dbus_message_set_client_serial (message, serial);
1187 data->handler = reply_handler;
1188 data->serial = serial;
1190 dbus_message_handler_ref (reply_handler);
1192 reply = dbus_message_new_error_reply (message, DBUS_ERROR_NO_REPLY,
1193 "No reply within specified time");
1196 dbus_mutex_unlock (connection->mutex);
1197 reply_handler_data_free (data);
1199 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1203 reply_link = _dbus_list_alloc_link (reply);
1206 dbus_mutex_unlock (connection->mutex);
1207 dbus_message_unref (reply);
1208 reply_handler_data_free (data);
1210 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1214 data->timeout_link = reply_link;
1216 /* Insert the serial in the pending replies hash. */
1217 if (!_dbus_hash_table_insert_int (connection->pending_replies, serial, data))
1219 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
1220 dbus_mutex_unlock (connection->mutex);
1221 reply_handler_data_free (data);
1226 dbus_mutex_unlock (connection->mutex);
1228 if (!dbus_connection_send_message (connection, message, NULL, result))
1230 /* This will free the handler data too */
1231 _dbus_hash_table_remove_int (connection->pending_replies, serial);
1235 dbus_set_result (result, DBUS_RESULT_SUCCESS);
1240 * Sends a message and blocks a certain time period while waiting for a reply.
1241 * This function does not dispatch any message handlers until the main loop
1242 * has been reached. This function is used to do non-reentrant "method calls."
1243 * If a reply is received, it is returned, and removed from the incoming
1244 * message queue. If it is not received, #NULL is returned and the
1245 * result is set to #DBUS_RESULT_NO_REPLY. If something else goes
1246 * wrong, result is set to whatever is appropriate, such as
1247 * #DBUS_RESULT_NO_MEMORY.
1249 * @todo could use performance improvements (it keeps scanning
1250 * the whole message queue for example) and has thread issues,
1251 * see comments in source
1253 * @param connection the connection
1254 * @param message the message to send
1255 * @param timeout_milliseconds timeout in milliseconds or -1 for default
1256 * @param result return location for result code
1257 * @returns the message that is the reply or #NULL with an error code if the
1261 dbus_connection_send_message_with_reply_and_block (DBusConnection *connection,
1262 DBusMessage *message,
1263 int timeout_milliseconds,
1264 DBusResultCode *result)
1266 dbus_int32_t client_serial;
1268 long start_tv_sec, start_tv_usec;
1269 long end_tv_sec, end_tv_usec;
1270 long tv_sec, tv_usec;
1272 if (timeout_milliseconds == -1)
1273 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
1275 /* it would probably seem logical to pass in _DBUS_INT_MAX
1276 * for infinite timeout, but then math below would get
1277 * all overflow-prone, so smack that down.
1279 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
1280 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
1282 if (!dbus_connection_send_message (connection, message, &client_serial, result))
1287 /* Flush message queue */
1288 dbus_connection_flush (connection);
1290 dbus_mutex_lock (connection->mutex);
1292 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
1293 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
1294 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
1295 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
1296 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
1298 _dbus_verbose ("will block %d milliseconds from %ld sec %ld usec to %ld sec %ld usec\n",
1299 timeout_milliseconds,
1300 start_tv_sec, start_tv_usec,
1301 end_tv_sec, end_tv_usec);
1303 /* Now we wait... */
1304 /* THREAD TODO: This is busted. What if a dispatch_message or pop_message
1305 * gets the message before we do?
1309 _dbus_connection_do_iteration (connection,
1310 DBUS_ITERATION_DO_READING |
1311 DBUS_ITERATION_BLOCK,
1312 timeout_milliseconds);
1314 /* Check if we've gotten a reply */
1315 link = _dbus_list_get_first_link (&connection->incoming_messages);
1317 while (link != NULL)
1319 DBusMessage *reply = link->data;
1321 if (_dbus_message_get_reply_serial (reply) == client_serial)
1323 _dbus_list_remove (&connection->incoming_messages, link);
1324 dbus_message_ref (reply);
1327 *result = DBUS_RESULT_SUCCESS;
1329 dbus_mutex_unlock (connection->mutex);
1332 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
1335 _dbus_get_current_time (&tv_sec, &tv_usec);
1337 if (tv_sec < start_tv_sec)
1338 ; /* clock set backward, bail out */
1339 else if (connection->disconnect_message_link == NULL)
1340 ; /* we're disconnected, bail out */
1341 else if (tv_sec < end_tv_sec ||
1342 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
1344 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
1345 (end_tv_usec - tv_usec) / 1000;
1346 _dbus_verbose ("%d milliseconds remain\n", timeout_milliseconds);
1347 _dbus_assert (timeout_milliseconds > 0);
1349 goto block_again; /* not expired yet */
1352 if (dbus_connection_get_is_connected (connection))
1353 dbus_set_result (result, DBUS_RESULT_NO_REPLY);
1355 dbus_set_result (result, DBUS_RESULT_DISCONNECTED);
1357 dbus_mutex_unlock (connection->mutex);
1363 * Blocks until the outgoing message queue is empty.
1365 * @param connection the connection.
1368 dbus_connection_flush (DBusConnection *connection)
1370 dbus_mutex_lock (connection->mutex);
1371 while (connection->n_outgoing > 0)
1372 _dbus_connection_do_iteration (connection,
1373 DBUS_ITERATION_DO_WRITING |
1374 DBUS_ITERATION_BLOCK,
1376 dbus_mutex_unlock (connection->mutex);
1380 * Gets the number of messages in the incoming message queue.
1382 * @param connection the connection.
1383 * @returns the number of messages in the queue.
1386 dbus_connection_get_n_messages (DBusConnection *connection)
1390 dbus_mutex_lock (connection->mutex);
1391 res = connection->n_incoming;
1392 dbus_mutex_unlock (connection->mutex);
1397 /* Call with mutex held. Will drop it while waiting and re-acquire
1401 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
1403 _dbus_assert (connection->message_borrowed != NULL);
1405 while (connection->message_borrowed != NULL)
1406 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
1410 * Returns the first-received message from the incoming message queue,
1411 * leaving it in the queue. If the queue is empty, returns #NULL.
1413 * The caller does not own a reference to the returned message, and must
1414 * either return it using dbus_connection_return_message or keep it after
1415 * calling dbus_connection_steal_borrowed_message. No one can get at the
1416 * message while its borrowed, so return it as quickly as possible and
1417 * don't keep a reference to it after returning it. If you need to keep
1418 * the message, make a copy of it.
1420 * @param connection the connection.
1421 * @returns next message in the incoming queue.
1424 dbus_connection_borrow_message (DBusConnection *connection)
1426 DBusMessage *message;
1428 dbus_mutex_lock (connection->mutex);
1430 if (connection->message_borrowed != NULL)
1431 _dbus_connection_wait_for_borrowed (connection);
1433 message = _dbus_list_get_first (&connection->incoming_messages);
1436 connection->message_borrowed = message;
1438 dbus_mutex_unlock (connection->mutex);
1446 dbus_connection_return_message (DBusConnection *connection,
1447 DBusMessage *message)
1449 dbus_mutex_lock (connection->mutex);
1451 _dbus_assert (message == connection->message_borrowed);
1453 connection->message_borrowed = NULL;
1454 dbus_condvar_wake_all (connection->message_returned_cond);
1456 dbus_mutex_unlock (connection->mutex);
1463 dbus_connection_steal_borrowed_message (DBusConnection *connection,
1464 DBusMessage *message)
1466 DBusMessage *pop_message;
1468 dbus_mutex_lock (connection->mutex);
1470 _dbus_assert (message == connection->message_borrowed);
1472 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
1473 _dbus_assert (message == pop_message);
1475 connection->n_incoming -= 1;
1477 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
1478 message, connection->n_incoming);
1480 connection->message_borrowed = NULL;
1481 dbus_condvar_wake_all (connection->message_returned_cond);
1483 dbus_mutex_unlock (connection->mutex);
1487 /* See dbus_connection_pop_message, but requires the caller to own
1488 * the lock before calling. May drop the lock while running.
1491 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
1493 if (connection->message_borrowed != NULL)
1494 _dbus_connection_wait_for_borrowed (connection);
1496 if (connection->n_incoming > 0)
1498 DBusMessage *message;
1500 message = _dbus_list_pop_first (&connection->incoming_messages);
1501 connection->n_incoming -= 1;
1503 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
1504 message, connection->n_incoming);
1514 * Returns the first-received message from the incoming message queue,
1515 * removing it from the queue. The caller owns a reference to the
1516 * returned message. If the queue is empty, returns #NULL.
1518 * @param connection the connection.
1519 * @returns next message in the incoming queue.
1522 dbus_connection_pop_message (DBusConnection *connection)
1524 DBusMessage *message;
1525 dbus_mutex_lock (connection->mutex);
1527 message = _dbus_connection_pop_message_unlocked (connection);
1529 dbus_mutex_unlock (connection->mutex);
1535 * Acquire the dispatcher. This must be done before dispatching
1536 * messages in order to guarantee the right order of
1537 * message delivery. May sleep and drop the connection mutex
1538 * while waiting for the dispatcher.
1540 * @param connection the connection.
1543 _dbus_connection_acquire_dispatch (DBusConnection *connection)
1545 if (connection->dispatch_acquired)
1546 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
1547 _dbus_assert (!connection->dispatch_acquired);
1549 connection->dispatch_acquired = TRUE;
1553 * Release the dispatcher when you're done with it. Only call
1554 * after you've acquired the dispatcher. Wakes up at most one
1555 * thread currently waiting to acquire the dispatcher.
1557 * @param connection the connection.
1560 _dbus_connection_release_dispatch (DBusConnection *connection)
1562 _dbus_assert (connection->dispatch_acquired);
1564 connection->dispatch_acquired = FALSE;
1565 dbus_condvar_wake_one (connection->dispatch_cond);
1569 _dbus_connection_failed_pop (DBusConnection *connection,
1570 DBusList *message_link)
1572 _dbus_list_prepend_link (&connection->incoming_messages,
1574 connection->n_incoming += 1;
1578 * Pops the first-received message from the current incoming message
1579 * queue, runs any handlers for it, then unrefs the message.
1581 * @param connection the connection
1582 * @returns #TRUE if the queue is not empty after dispatch
1585 dbus_connection_dispatch_message (DBusConnection *connection)
1587 DBusMessageHandler *handler;
1588 DBusMessage *message;
1589 DBusList *link, *filter_list_copy, *message_link;
1590 DBusHandlerResult result;
1591 ReplyHandlerData *reply_handler_data;
1593 dbus_int32_t reply_serial;
1595 /* Preallocate link so we can put the message back on failure */
1596 message_link = _dbus_list_alloc_link (NULL);
1597 if (message_link == NULL)
1600 dbus_mutex_lock (connection->mutex);
1602 /* We need to ref the connection since the callback could potentially
1603 * drop the last ref to it */
1604 _dbus_connection_ref_unlocked (connection);
1606 _dbus_connection_acquire_dispatch (connection);
1608 /* This call may drop the lock during the execution (if waiting for
1609 * borrowed messages to be returned) but the order of message
1610 * dispatch if several threads call dispatch_message is still
1611 * protected by the lock, since only one will get the lock, and that
1612 * one will finish the message dispatching
1614 message = _dbus_connection_pop_message_unlocked (connection);
1615 if (message == NULL)
1617 _dbus_connection_release_dispatch (connection);
1618 dbus_mutex_unlock (connection->mutex);
1619 dbus_connection_unref (connection);
1623 message_link->data = message;
1625 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
1627 reply_serial = _dbus_message_get_reply_serial (message);
1628 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
1631 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
1633 _dbus_connection_release_dispatch (connection);
1634 dbus_mutex_unlock (connection->mutex);
1635 _dbus_connection_failed_pop (connection, message_link);
1636 dbus_connection_unref (connection);
1640 _dbus_list_foreach (&filter_list_copy,
1641 (DBusForeachFunction)dbus_message_handler_ref,
1644 /* We're still protected from dispatch_message reentrancy here
1645 * since we acquired the dispatcher
1647 dbus_mutex_unlock (connection->mutex);
1649 link = _dbus_list_get_first_link (&filter_list_copy);
1650 while (link != NULL)
1652 DBusMessageHandler *handler = link->data;
1653 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
1655 result = _dbus_message_handler_handle_message (handler, connection,
1658 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1664 _dbus_list_foreach (&filter_list_copy,
1665 (DBusForeachFunction)dbus_message_handler_unref,
1667 _dbus_list_clear (&filter_list_copy);
1669 dbus_mutex_lock (connection->mutex);
1671 /* Did a reply we were waiting on get filtered? */
1672 if (reply_handler_data && result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1674 /* Queue the timeout immediately! */
1675 if (reply_handler_data->timeout_link)
1677 _dbus_connection_queue_synthesized_message_link (connection,
1678 reply_handler_data->timeout_link);
1679 reply_handler_data->timeout_link = NULL;
1683 /* We already queued the timeout? Then it was filtered! */
1684 _dbus_warn ("The timeout error with reply serial %d was filtered, so the reply handler will never be called.\n", reply_serial);
1688 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1691 if (reply_handler_data)
1693 dbus_mutex_unlock (connection->mutex);
1694 result = _dbus_message_handler_handle_message (reply_handler_data->handler,
1695 connection, message);
1696 reply_handler_data_free (reply_handler_data);
1697 dbus_mutex_lock (connection->mutex);
1701 name = dbus_message_get_name (message);
1704 handler = _dbus_hash_table_lookup_string (connection->handler_table,
1706 if (handler != NULL)
1708 /* We're still protected from dispatch_message reentrancy here
1709 * since we acquired the dispatcher */
1710 dbus_mutex_unlock (connection->mutex);
1711 result = _dbus_message_handler_handle_message (handler, connection,
1713 dbus_mutex_lock (connection->mutex);
1714 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
1720 _dbus_connection_release_dispatch (connection);
1721 dbus_mutex_unlock (connection->mutex);
1722 _dbus_list_free_link (message_link);
1723 dbus_connection_unref (connection);
1724 dbus_message_unref (message);
1726 return connection->n_incoming > 0;
1730 * Sets the watch functions for the connection. These functions are
1731 * responsible for making the application's main loop aware of file
1732 * descriptors that need to be monitored for events, using select() or
1733 * poll(). When using Qt, typically the DBusAddWatchFunction would
1734 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
1735 * could call g_io_add_watch(), or could be used as part of a more
1736 * elaborate GSource.
1738 * The DBusWatch can be queried for the file descriptor to watch using
1739 * dbus_watch_get_fd(), and for the events to watch for using
1740 * dbus_watch_get_flags(). The flags returned by
1741 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
1742 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
1743 * all watches implicitly include a watch for hangups, errors, and
1744 * other exceptional conditions.
1746 * Once a file descriptor becomes readable or writable, or an exception
1747 * occurs, dbus_connection_handle_watch() should be called to
1748 * notify the connection of the file descriptor's condition.
1750 * dbus_connection_handle_watch() cannot be called during the
1751 * DBusAddWatchFunction, as the connection will not be ready to handle
1754 * It is not allowed to reference a DBusWatch after it has been passed
1755 * to remove_function.
1757 * @param connection the connection.
1758 * @param add_function function to begin monitoring a new descriptor.
1759 * @param remove_function function to stop monitoring a descriptor.
1760 * @param data data to pass to add_function and remove_function.
1761 * @param free_data_function function to be called to free the data.
1764 dbus_connection_set_watch_functions (DBusConnection *connection,
1765 DBusAddWatchFunction add_function,
1766 DBusRemoveWatchFunction remove_function,
1768 DBusFreeFunction free_data_function)
1770 dbus_mutex_lock (connection->mutex);
1771 /* ref connection for slightly better reentrancy */
1772 _dbus_connection_ref_unlocked (connection);
1774 _dbus_watch_list_set_functions (connection->watches,
1775 add_function, remove_function,
1776 data, free_data_function);
1778 dbus_mutex_unlock (connection->mutex);
1779 /* drop our paranoid refcount */
1780 dbus_connection_unref (connection);
1784 * Sets the timeout functions for the connection. These functions are
1785 * responsible for making the application's main loop aware of timeouts.
1786 * When using Qt, typically the DBusAddTimeoutFunction would create a
1787 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
1790 * The DBusTimeout can be queried for the timer interval using
1791 * dbus_timeout_get_interval.
1793 * Once a timeout occurs, dbus_timeout_handle should be called to invoke
1794 * the timeout's callback.
1796 * @param connection the connection.
1797 * @param add_function function to add a timeout.
1798 * @param remove_function function to remove a timeout.
1799 * @param data data to pass to add_function and remove_function.
1800 * @param free_data_function function to be called to free the data.
1803 dbus_connection_set_timeout_functions (DBusConnection *connection,
1804 DBusAddTimeoutFunction add_function,
1805 DBusRemoveTimeoutFunction remove_function,
1807 DBusFreeFunction free_data_function)
1809 dbus_mutex_lock (connection->mutex);
1810 /* ref connection for slightly better reentrancy */
1811 _dbus_connection_ref_unlocked (connection);
1813 _dbus_timeout_list_set_functions (connection->timeouts,
1814 add_function, remove_function,
1815 data, free_data_function);
1817 dbus_mutex_unlock (connection->mutex);
1818 /* drop our paranoid refcount */
1819 dbus_connection_unref (connection);
1823 * Sets the mainloop wakeup function for the connection. Thi function is
1824 * responsible for waking up the main loop (if its sleeping) when some some
1825 * change has happened to the connection that the mainloop needs to reconsiders
1826 * (e.g. a message has been queued for writing).
1827 * When using Qt, this typically results in a call to QEventLoop::wakeUp().
1828 * When using GLib, it would call g_main_context_wakeup().
1831 * @param connection the connection.
1832 * @param wakeup_main_function function to wake up the mainloop
1833 * @param data data to pass wakeup_main_function
1834 * @param free_data_function function to be called to free the data.
1837 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
1838 DBusWakeupMainFunction wakeup_main_function,
1840 DBusFreeFunction free_data_function)
1843 DBusFreeFunction old_free_data;
1845 dbus_mutex_lock (connection->mutex);
1846 old_data = connection->wakeup_main_data;
1847 old_free_data = connection->free_wakeup_main_data;
1849 connection->wakeup_main_function = wakeup_main_function;
1850 connection->wakeup_main_data = data;
1851 connection->free_wakeup_main_data = free_data_function;
1853 dbus_mutex_unlock (connection->mutex);
1855 /* Callback outside the lock */
1857 (*old_free_data) (old_data);
1861 * Called to notify the connection when a previously-added watch
1862 * is ready for reading or writing, or has an exception such
1865 * @param connection the connection.
1866 * @param watch the watch.
1867 * @param condition the current condition of the file descriptors being watched.
1870 dbus_connection_handle_watch (DBusConnection *connection,
1872 unsigned int condition)
1874 dbus_mutex_lock (connection->mutex);
1875 _dbus_connection_acquire_io_path (connection, -1);
1876 _dbus_transport_handle_watch (connection->transport,
1878 _dbus_connection_release_io_path (connection);
1879 dbus_mutex_unlock (connection->mutex);
1883 * Adds a message filter. Filters are handlers that are run on
1884 * all incoming messages, prior to the normal handlers
1885 * registered with dbus_connection_register_handler().
1886 * Filters are run in the order that they were added.
1887 * The same handler can be added as a filter more than once, in
1888 * which case it will be run more than once.
1889 * Filters added during a filter callback won't be run on the
1890 * message being processed.
1892 * @param connection the connection
1893 * @param handler the handler
1894 * @returns #TRUE on success, #FALSE if not enough memory.
1897 dbus_connection_add_filter (DBusConnection *connection,
1898 DBusMessageHandler *handler)
1900 dbus_mutex_lock (connection->mutex);
1901 if (!_dbus_message_handler_add_connection (handler, connection))
1903 dbus_mutex_unlock (connection->mutex);
1907 if (!_dbus_list_append (&connection->filter_list,
1910 _dbus_message_handler_remove_connection (handler, connection);
1911 dbus_mutex_unlock (connection->mutex);
1915 dbus_mutex_unlock (connection->mutex);
1920 * Removes a previously-added message filter. It is a programming
1921 * error to call this function for a handler that has not
1922 * been added as a filter. If the given handler was added
1923 * more than once, only one instance of it will be removed
1924 * (the most recently-added instance).
1926 * @param connection the connection
1927 * @param handler the handler to remove
1931 dbus_connection_remove_filter (DBusConnection *connection,
1932 DBusMessageHandler *handler)
1934 dbus_mutex_lock (connection->mutex);
1935 if (!_dbus_list_remove_last (&connection->filter_list, handler))
1937 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
1938 dbus_mutex_unlock (connection->mutex);
1942 _dbus_message_handler_remove_connection (handler, connection);
1944 dbus_mutex_unlock (connection->mutex);
1948 * Registers a handler for a list of message names. A single handler
1949 * can be registered for any number of message names, but each message
1950 * name can only have one handler at a time. It's not allowed to call
1951 * this function with the name of a message that already has a
1952 * handler. If the function returns #FALSE, the handlers were not
1953 * registered due to lack of memory.
1955 * @todo the messages_to_handle arg may be more convenient if it's a
1956 * single string instead of an array. Though right now MessageHandler
1957 * is sort of designed to say be associated with an entire object with
1958 * multiple methods, that's why for example the connection only
1959 * weakrefs it. So maybe the "manual" API should be different.
1961 * @param connection the connection
1962 * @param handler the handler
1963 * @param messages_to_handle the messages to handle
1964 * @param n_messages the number of message names in messages_to_handle
1965 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
1969 dbus_connection_register_handler (DBusConnection *connection,
1970 DBusMessageHandler *handler,
1971 const char **messages_to_handle,
1976 dbus_mutex_lock (connection->mutex);
1978 while (i < n_messages)
1983 key = _dbus_strdup (messages_to_handle[i]);
1987 if (!_dbus_hash_iter_lookup (connection->handler_table,
1995 if (_dbus_hash_iter_get_value (&iter) != NULL)
1997 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1998 messages_to_handle[i]);
1999 dbus_free (key); /* won't have replaced the old key with the new one */
2003 if (!_dbus_message_handler_add_connection (handler, connection))
2005 _dbus_hash_iter_remove_entry (&iter);
2006 /* key has freed on nuking the entry */
2010 _dbus_hash_iter_set_value (&iter, handler);
2015 dbus_mutex_unlock (connection->mutex);
2019 /* unregister everything registered so far,
2020 * so we don't fail partially
2022 dbus_connection_unregister_handler (connection,
2027 dbus_mutex_unlock (connection->mutex);
2032 * Unregisters a handler for a list of message names. The handlers
2033 * must have been previously registered.
2035 * @param connection the connection
2036 * @param handler the handler
2037 * @param messages_to_handle the messages to handle
2038 * @param n_messages the number of message names in messages_to_handle
2042 dbus_connection_unregister_handler (DBusConnection *connection,
2043 DBusMessageHandler *handler,
2044 const char **messages_to_handle,
2049 dbus_mutex_lock (connection->mutex);
2051 while (i < n_messages)
2055 if (!_dbus_hash_iter_lookup (connection->handler_table,
2056 (char*) messages_to_handle[i], FALSE,
2059 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
2060 messages_to_handle[i]);
2062 else if (_dbus_hash_iter_get_value (&iter) != handler)
2064 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
2065 messages_to_handle[i]);
2069 _dbus_hash_iter_remove_entry (&iter);
2070 _dbus_message_handler_remove_connection (handler, connection);
2076 dbus_mutex_unlock (connection->mutex);
2079 static DBusDataSlotAllocator slot_allocator;
2082 * Initialize the mutex used for #DBusConnection data
2083 * slot reservations.
2085 * @returns the mutex
2088 _dbus_connection_slots_init_lock (void)
2090 if (!_dbus_data_slot_allocator_init (&slot_allocator))
2093 return slot_allocator.lock;
2097 * Allocates an integer ID to be used for storing application-specific
2098 * data on any DBusConnection. The allocated ID may then be used
2099 * with dbus_connection_set_data() and dbus_connection_get_data().
2100 * If allocation fails, -1 is returned. Again, the allocated
2101 * slot is global, i.e. all DBusConnection objects will
2102 * have a slot with the given integer ID reserved.
2104 * @returns -1 on failure, otherwise the data slot ID
2107 dbus_connection_allocate_data_slot (void)
2109 return _dbus_data_slot_allocator_alloc (&slot_allocator);
2113 * Deallocates a global ID for connection data slots.
2114 * dbus_connection_get_data() and dbus_connection_set_data()
2115 * may no longer be used with this slot.
2116 * Existing data stored on existing DBusConnection objects
2117 * will be freed when the connection is finalized,
2118 * but may not be retrieved (and may only be replaced
2119 * if someone else reallocates the slot).
2121 * @param slot the slot to deallocate
2124 dbus_connection_free_data_slot (int slot)
2126 _dbus_data_slot_allocator_free (&slot_allocator, slot);
2130 * Stores a pointer on a DBusConnection, along
2131 * with an optional function to be used for freeing
2132 * the data when the data is set again, or when
2133 * the connection is finalized. The slot number
2134 * must have been allocated with dbus_connection_allocate_data_slot().
2136 * @param connection the connection
2137 * @param slot the slot number
2138 * @param data the data to store
2139 * @param free_data_func finalizer function for the data
2140 * @returns #TRUE if there was enough memory to store the data
2143 dbus_connection_set_data (DBusConnection *connection,
2146 DBusFreeFunction free_data_func)
2148 DBusFreeFunction old_free_func;
2152 dbus_mutex_lock (connection->mutex);
2154 retval = _dbus_data_slot_list_set (&slot_allocator,
2155 &connection->slot_list,
2156 slot, data, free_data_func,
2157 &old_free_func, &old_data);
2159 dbus_mutex_unlock (connection->mutex);
2163 /* Do the actual free outside the connection lock */
2165 (* old_free_func) (old_data);
2172 * Retrieves data previously set with dbus_connection_set_data().
2173 * The slot must still be allocated (must not have been freed).
2175 * @param connection the connection
2176 * @param slot the slot to get data from
2177 * @returns the data, or #NULL if not found
2180 dbus_connection_get_data (DBusConnection *connection,
2185 dbus_mutex_lock (connection->mutex);
2187 res = _dbus_data_slot_list_get (&slot_allocator,
2188 &connection->slot_list,
2191 dbus_mutex_unlock (connection->mutex);
2197 * This function sets a global flag for whether dbus_connection_new()
2198 * will set SIGPIPE behavior to SIG_IGN.
2200 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN
2203 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
2205 _dbus_modify_sigpipe = will_modify_sigpipe;
2209 * Specifies the maximum size message this connection is allowed to
2210 * receive. Larger messages will result in disconnecting the
2213 * @param connection a #DBusConnection
2214 * @param size maximum message size the connection can receive, in bytes
2217 dbus_connection_set_max_message_size (DBusConnection *connection,
2220 dbus_mutex_lock (connection->mutex);
2221 _dbus_transport_set_max_message_size (connection->transport,
2223 dbus_mutex_unlock (connection->mutex);
2227 * Gets the value set by dbus_connection_set_max_message_size().
2229 * @param connection the connection
2230 * @returns the max size of a single message
2233 dbus_connection_get_max_message_size (DBusConnection *connection)
2236 dbus_mutex_lock (connection->mutex);
2237 res = _dbus_transport_get_max_message_size (connection->transport);
2238 dbus_mutex_unlock (connection->mutex);
2243 * Sets the maximum total number of bytes that can be used for all messages
2244 * received on this connection. Messages count toward the maximum until
2245 * they are finalized. When the maximum is reached, the connection will
2246 * not read more data until some messages are finalized.
2248 * The semantics of the maximum are: if outstanding messages are
2249 * already above the maximum, additional messages will not be read.
2250 * The semantics are not: if the next message would cause us to exceed
2251 * the maximum, we don't read it. The reason is that we don't know the
2252 * size of a message until after we read it.
2254 * Thus, the max live messages size can actually be exceeded
2255 * by up to the maximum size of a single message.
2257 * Also, if we read say 1024 bytes off the wire in a single read(),
2258 * and that contains a half-dozen small messages, we may exceed the
2259 * size max by that amount. But this should be inconsequential.
2261 * @param connection the connection
2262 * @param size the maximum size in bytes of all outstanding messages
2265 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
2268 dbus_mutex_lock (connection->mutex);
2269 _dbus_transport_set_max_live_messages_size (connection->transport,
2271 dbus_mutex_unlock (connection->mutex);
2275 * Gets the value set by dbus_connection_set_max_live_messages_size().
2277 * @param connection the connection
2278 * @returns the max size of all live messages
2281 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
2284 dbus_mutex_lock (connection->mutex);
2285 res = _dbus_transport_get_max_live_messages_size (connection->transport);
2286 dbus_mutex_unlock (connection->mutex);