1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-connection.h"
25 #include "dbus-list.h"
26 #include "dbus-timeout.h"
27 #include "dbus-transport.h"
28 #include "dbus-watch.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-list.h"
31 #include "dbus-hash.h"
32 #include "dbus-message-internal.h"
33 #include "dbus-threads.h"
36 * @defgroup DBusConnection DBusConnection
38 * @brief Connection to another application
40 * A DBusConnection represents a connection to another
41 * application. Messages can be sent and received via this connection.
43 * The connection maintains a queue of incoming messages and a queue
44 * of outgoing messages. dbus_connection_pop_message() and friends
45 * can be used to read incoming messages from the queue.
46 * Outgoing messages are automatically discarded as they are
47 * written to the network.
49 * In brief a DBusConnection is a message queue associated with some
50 * message transport mechanism such as a socket.
55 * @defgroup DBusConnectionInternals DBusConnection implementation details
56 * @ingroup DBusInternals
57 * @brief Implementation details of DBusConnection
62 /** default timeout value when waiting for a message reply */
63 #define DEFAULT_TIMEOUT_VALUE (15 * 1000)
65 /** Opaque typedef for DBusDataSlot */
66 typedef struct DBusDataSlot DBusDataSlot;
67 /** DBusDataSlot is used to store application data on the connection */
70 void *data; /**< The application data */
71 DBusFreeFunction free_data_func; /**< Free the application data */
75 * Implementation details of DBusConnection. All fields are private.
79 int refcount; /**< Reference count. */
81 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
82 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
84 int n_outgoing; /**< Length of outgoing queue. */
85 int n_incoming; /**< Length of incoming queue. */
87 DBusTransport *transport; /**< Object that sends/receives messages over network. */
88 DBusWatchList *watches; /**< Stores active watches. */
89 DBusTimeoutList *timeouts; /**< Stores active timeouts. */
91 DBusDisconnectFunction disconnect_function; /**< Callback on disconnect. */
92 void *disconnect_data; /**< Data for disconnect callback. */
93 DBusFreeFunction disconnect_free_data_function; /**< Free function for disconnect callback data. */
94 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
95 DBusList *filter_list; /**< List of filters. */
96 int filters_serial; /**< Increments when the list of filters is changed. */
97 int handlers_serial; /**< Increments when the handler table is changed. */
98 DBusDataSlot *data_slots; /**< Data slots */
99 int n_slots; /**< Slots allocated so far. */
101 DBusCounter *connection_counter; /**< Counter that we decrement when finalized */
103 int client_serial; /**< Client serial. Increments each time a message is sent */
104 unsigned int disconnect_notified : 1; /**< Already called disconnect_function */
107 static void _dbus_connection_free_data_slots (DBusConnection *connection);
110 * Adds a message to the incoming message queue, returning #FALSE
111 * if there's insufficient memory to queue the message.
113 * @param connection the connection.
114 * @param message the message to queue.
115 * @returns #TRUE on success.
118 _dbus_connection_queue_received_message (DBusConnection *connection,
119 DBusMessage *message)
121 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
123 if (!_dbus_list_append (&connection->incoming_messages,
127 dbus_message_ref (message);
128 connection->n_incoming += 1;
130 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
131 message, connection->n_incoming);
137 * Checks whether there are messages in the outgoing message queue.
139 * @param connection the connection.
140 * @returns #TRUE if the outgoing queue is non-empty.
143 _dbus_connection_have_messages_to_send (DBusConnection *connection)
145 return connection->outgoing_messages != NULL;
149 * Gets the next outgoing message. The message remains in the
150 * queue, and the caller does not own a reference to it.
152 * @param connection the connection.
153 * @returns the message to be sent.
156 _dbus_connection_get_message_to_send (DBusConnection *connection)
158 return _dbus_list_get_last (&connection->outgoing_messages);
162 * Notifies the connection that a message has been sent, so the
163 * message can be removed from the outgoing queue.
165 * @param connection the connection.
166 * @param message the message that was sent.
169 _dbus_connection_message_sent (DBusConnection *connection,
170 DBusMessage *message)
172 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
173 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
175 _dbus_list_pop_last (&connection->outgoing_messages);
176 dbus_message_unref (message);
178 connection->n_outgoing -= 1;
180 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
181 message, connection->n_outgoing);
183 if (connection->n_outgoing == 0)
184 _dbus_transport_messages_pending (connection->transport,
185 connection->n_outgoing);
189 * Adds a watch using the connection's DBusAddWatchFunction if
190 * available. Otherwise records the watch to be added when said
191 * function is available. Also re-adds the watch if the
192 * DBusAddWatchFunction changes. May fail due to lack of memory.
194 * @param connection the connection.
195 * @param watch the watch to add.
196 * @returns #TRUE on success.
199 _dbus_connection_add_watch (DBusConnection *connection,
202 if (connection->watches) /* null during finalize */
203 return _dbus_watch_list_add_watch (connection->watches,
210 * Removes a watch using the connection's DBusRemoveWatchFunction
211 * if available. It's an error to call this function on a watch
212 * that was not previously added.
214 * @param connection the connection.
215 * @param watch the watch to remove.
218 _dbus_connection_remove_watch (DBusConnection *connection,
221 if (connection->watches) /* null during finalize */
222 _dbus_watch_list_remove_watch (connection->watches,
227 * Tells the connection that the transport has been disconnected.
228 * Results in calling the application disconnect callback.
229 * Only has an effect the first time it's called.
231 * @param connection the connection
234 _dbus_connection_notify_disconnected (DBusConnection *connection)
236 if (connection->disconnect_function != NULL &&
237 !connection->disconnect_notified)
239 connection->disconnect_notified = TRUE;
240 dbus_connection_ref (connection);
241 (* connection->disconnect_function) (connection,
242 connection->disconnect_data);
243 dbus_connection_unref (connection);
248 * Queues incoming messages and sends outgoing messages for this
249 * connection, optionally blocking in the process. Each call to
250 * _dbus_connection_do_iteration() will call select() or poll() one
251 * time and then read or write data if possible.
253 * The purpose of this function is to be able to flush outgoing
254 * messages or queue up incoming messages without returning
255 * control to the application and causing reentrancy weirdness.
257 * The flags parameter allows you to specify whether to
258 * read incoming messages, write outgoing messages, or both,
259 * and whether to block if no immediate action is possible.
261 * The timeout_milliseconds parameter does nothing unless the
262 * iteration is blocking.
264 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
265 * wasn't specified, then it's impossible to block, even if
266 * you specify DBUS_ITERATION_BLOCK; in that case the function
267 * returns immediately.
269 * @param connection the connection.
270 * @param flags iteration flags.
271 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
274 _dbus_connection_do_iteration (DBusConnection *connection,
276 int timeout_milliseconds)
278 if (connection->n_outgoing == 0)
279 flags &= ~DBUS_ITERATION_DO_WRITING;
281 _dbus_transport_do_iteration (connection->transport,
282 flags, timeout_milliseconds);
286 * Creates a new connection for the given transport. A transport
287 * represents a message stream that uses some concrete mechanism, such
288 * as UNIX domain sockets. May return #NULL if insufficient
289 * memory exists to create the connection.
291 * @param transport the transport.
292 * @returns the new connection, or #NULL on failure.
295 _dbus_connection_new_for_transport (DBusTransport *transport)
297 DBusConnection *connection;
298 DBusWatchList *watch_list;
299 DBusTimeoutList *timeout_list;
300 DBusHashTable *handler_table;
304 handler_table = NULL;
306 watch_list = _dbus_watch_list_new ();
307 if (watch_list == NULL)
310 timeout_list = _dbus_timeout_list_new ();
311 if (timeout_list == NULL)
315 _dbus_hash_table_new (DBUS_HASH_STRING,
317 if (handler_table == NULL)
320 connection = dbus_new0 (DBusConnection, 1);
321 if (connection == NULL)
324 connection->refcount = 1;
325 connection->transport = transport;
326 connection->watches = watch_list;
327 connection->timeouts = timeout_list;
328 connection->handler_table = handler_table;
329 connection->filter_list = NULL;
331 connection->data_slots = NULL;
332 connection->n_slots = 0;
333 connection->client_serial = 1;
334 connection->disconnect_notified = FALSE;
336 _dbus_transport_ref (transport);
337 _dbus_transport_set_connection (transport, connection);
343 if (connection != NULL)
344 dbus_free (connection);
347 _dbus_hash_table_unref (handler_table);
350 _dbus_watch_list_free (watch_list);
356 _dbus_connection_get_next_client_serial (DBusConnection *connection)
360 serial = connection->client_serial++;
362 if (connection->client_serial < 0)
363 connection->client_serial = 1;
369 * Used to notify a connection when a DBusMessageHandler is
370 * destroyed, so the connection can drop any reference
373 * @param connection the connection
374 * @param handler the handler
377 _dbus_connection_handler_destroyed (DBusConnection *connection,
378 DBusMessageHandler *handler)
383 _dbus_hash_iter_init (connection->handler_table, &iter);
384 while (_dbus_hash_iter_next (&iter))
386 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
389 _dbus_hash_iter_remove_entry (&iter);
392 link = _dbus_list_get_first_link (&connection->filter_list);
395 DBusMessageHandler *h = link->data;
396 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
399 _dbus_list_remove_link (&connection->filter_list,
407 * Adds the counter used to count the number of open connections.
408 * Increments the counter by one, and saves it to be decremented
409 * again when this connection is finalized.
411 * @param connection a #DBusConnection
412 * @param counter counter that tracks number of connections
415 _dbus_connection_set_connection_counter (DBusConnection *connection,
416 DBusCounter *counter)
418 _dbus_assert (connection->connection_counter == NULL);
420 connection->connection_counter = counter;
421 _dbus_counter_ref (connection->connection_counter);
422 _dbus_counter_adjust (connection->connection_counter, 1);
428 * @addtogroup DBusConnection
434 * Opens a new connection to a remote address.
436 * @todo specify what the address parameter is. Right now
437 * it's just the name of a UNIX domain socket. It should be
438 * something more complex that encodes which transport to use.
440 * If the open fails, the function returns #NULL, and provides
441 * a reason for the failure in the result parameter. Pass
442 * #NULL for the result parameter if you aren't interested
443 * in the reason for failure.
445 * @param address the address.
446 * @param result address where a result code can be returned.
447 * @returns new connection, or #NULL on failure.
450 dbus_connection_open (const char *address,
451 DBusResultCode *result)
453 DBusConnection *connection;
454 DBusTransport *transport;
456 transport = _dbus_transport_open (address, result);
457 if (transport == NULL)
460 connection = _dbus_connection_new_for_transport (transport);
462 _dbus_transport_unref (transport);
464 if (connection == NULL)
466 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
474 * Increments the reference count of a DBusConnection.
476 * @param connection the connection.
479 dbus_connection_ref (DBusConnection *connection)
481 connection->refcount += 1;
485 * Decrements the reference count of a DBusConnection, and finalizes
486 * it if the count reaches zero. If a connection is still connected
487 * when it's finalized, it will be disconnected (that is, associated
488 * file handles will be closed).
490 * @param connection the connection.
493 dbus_connection_unref (DBusConnection *connection)
495 _dbus_assert (connection != NULL);
496 _dbus_assert (connection->refcount > 0);
498 connection->refcount -= 1;
499 if (connection->refcount == 0)
504 dbus_connection_disconnect (connection);
506 /* free disconnect data as a side effect */
507 dbus_connection_set_disconnect_function (connection,
510 if (connection->connection_counter != NULL)
512 /* subtract ourselves from the counter */
513 _dbus_counter_adjust (connection->connection_counter, - 1);
514 _dbus_counter_unref (connection->connection_counter);
515 connection->connection_counter = NULL;
518 _dbus_watch_list_free (connection->watches);
519 connection->watches = NULL;
521 _dbus_connection_free_data_slots (connection);
523 _dbus_hash_iter_init (connection->handler_table, &iter);
524 while (_dbus_hash_iter_next (&iter))
526 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
528 _dbus_message_handler_remove_connection (h, connection);
531 link = _dbus_list_get_first_link (&connection->filter_list);
534 DBusMessageHandler *h = link->data;
535 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
537 _dbus_message_handler_remove_connection (h, connection);
542 _dbus_hash_table_unref (connection->handler_table);
543 connection->handler_table = NULL;
545 _dbus_list_clear (&connection->filter_list);
547 _dbus_list_foreach (&connection->outgoing_messages,
548 (DBusForeachFunction) dbus_message_unref,
550 _dbus_list_clear (&connection->outgoing_messages);
552 _dbus_list_foreach (&connection->incoming_messages,
553 (DBusForeachFunction) dbus_message_unref,
555 _dbus_list_clear (&connection->incoming_messages);
557 _dbus_transport_unref (connection->transport);
559 dbus_free (connection);
564 * Closes the connection, so no further data can be sent or received.
565 * Any further attempts to send data will result in errors. This
566 * function does not affect the connection's reference count. It's
567 * safe to disconnect a connection more than once; all calls after the
568 * first do nothing. It's impossible to "reconnect" a connection, a
569 * new connection must be created.
571 * @param connection the connection.
574 dbus_connection_disconnect (DBusConnection *connection)
576 _dbus_transport_disconnect (connection->transport);
580 * Gets whether the connection is currently connected. All
581 * connections are connected when they are opened. A connection may
582 * become disconnected when the remote application closes its end, or
583 * exits; a connection may also be disconnected with
584 * dbus_connection_disconnect().
586 * @param connection the connection.
587 * @returns #TRUE if the connection is still alive.
590 dbus_connection_get_is_connected (DBusConnection *connection)
592 return _dbus_transport_get_is_connected (connection->transport);
596 * Gets whether the connection was authenticated. (Note that
597 * if the connection was authenticated then disconnected,
598 * this function still returns #TRUE)
600 * @param connection the connection
601 * @returns #TRUE if the connection was ever authenticated
604 dbus_connection_get_is_authenticated (DBusConnection *connection)
606 return _dbus_transport_get_is_authenticated (connection->transport);
610 * Adds a message to the outgoing message queue. Does not block to
611 * write the message to the network; that happens asynchronously. to
612 * force the message to be written, call dbus_connection_flush().
614 * If the function fails, it returns #FALSE and returns the
615 * reason for failure via the result parameter.
616 * The result parameter can be #NULL if you aren't interested
617 * in the reason for the failure.
619 * @param connection the connection.
620 * @param message the message to write.
621 * @param client_serial return location for client serial.
622 * @param result address where result code can be placed.
623 * @returns #TRUE on success.
626 dbus_connection_send_message (DBusConnection *connection,
627 DBusMessage *message,
628 dbus_int32_t *client_serial,
629 DBusResultCode *result)
634 if (!_dbus_list_prepend (&connection->outgoing_messages,
637 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
641 dbus_message_ref (message);
642 connection->n_outgoing += 1;
644 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
645 message, connection->n_outgoing);
647 /* Unlock the message, resetting its header. */
648 _dbus_message_unlock (message);
650 serial = _dbus_connection_get_next_client_serial (connection);
651 _dbus_message_set_client_serial (message, serial);
654 *client_serial = serial;
656 _dbus_message_lock (message);
658 if (connection->n_outgoing == 1)
659 _dbus_transport_messages_pending (connection->transport,
660 connection->n_outgoing);
666 * Queues a message to send, as with dbus_connection_send_message(),
667 * but also sets up a DBusMessageHandler to receive a reply to the
668 * message. If no reply is received in the given timeout_milliseconds,
669 * expires the pending reply and sends the DBusMessageHandler a
670 * synthetic error reply (generated in-process, not by the remote
671 * application) indicating that a timeout occurred.
673 * Reply handlers see their replies after message filters see them,
674 * but before message handlers added with
675 * dbus_connection_register_handler() see them, regardless of the
676 * reply message's name. Reply handlers are only handed a single
677 * message as a reply, after a reply has been seen the handler is
678 * removed. If a filter filters out the reply before the handler sees
679 * it, the handler is not removed but the timeout will immediately
680 * fire again. If a filter was dumb and kept removing the timeout
681 * reply then we'd get in an infinite loop.
683 * If #NULL is passed for the reply_handler, the timeout reply will
684 * still be generated and placed into the message queue, but no
685 * specific message handler will receive the reply.
687 * If -1 is passed for the timeout, a sane default timeout is used. -1
688 * is typically the best value for the timeout for this reason, unless
689 * you want a very short or very long timeout. There is no way to
690 * avoid a timeout entirely, other than passing INT_MAX for the
691 * timeout to postpone it indefinitely.
693 * @param connection the connection
694 * @param message the message to send
695 * @param reply_handler message handler expecting the reply, or #NULL
696 * @param timeout_milliseconds timeout in milliseconds or -1 for default
697 * @param result return location for result code
698 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
700 * @todo this function isn't implemented because we need message serials
701 * and other slightly more rich DBusMessage implementation in order to
702 * implement it. The basic idea will be to keep a hash of serials we're
703 * expecting a reply to, and also to add a way to tell GLib or Qt to
704 * install a timeout. Then install a timeout which is the shortest
705 * timeout of any pending reply.
709 dbus_connection_send_message_with_reply (DBusConnection *connection,
710 DBusMessage *message,
711 DBusMessageHandler *reply_handler,
712 int timeout_milliseconds,
713 DBusResultCode *result)
716 return dbus_connection_send_message (connection, message, NULL, result);
720 * Sends a message and blocks a certain time period while waiting for a reply.
721 * This function does not dispatch any message handlers until the main loop
722 * has been reached. This function is used to do non-reentrant "method calls."
723 * If a reply is received, it is returned, and removed from the incoming
724 * message queue. If it is not received, #NULL is returned and the
725 * result is set to #DBUS_RESULT_NO_REPLY. If something else goes
726 * wrong, result is set to whatever is appropriate, such as
727 * #DBUS_RESULT_NO_MEMORY.
729 * @todo I believe if we get EINTR or otherwise interrupt the
730 * do_iteration call in here, we won't block the required length of
731 * time. I think there probably has to be a loop: "while (!timeout_elapsed)
732 * { check_for_reply_in_queue(); iterate_with_remaining_timeout(); }"
734 * @param connection the connection
735 * @param message the message to send
736 * @param timeout_milliseconds timeout in milliseconds or -1 for default
737 * @param result return location for result code
738 * @returns the message that is the reply or #NULL with an error code if the
742 dbus_connection_send_message_with_reply_and_block (DBusConnection *connection,
743 DBusMessage *message,
744 int timeout_milliseconds,
745 DBusResultCode *result)
747 dbus_int32_t client_serial;
750 if (timeout_milliseconds == -1)
751 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
753 if (!dbus_connection_send_message (connection, message, &client_serial, result))
756 /* Flush message queue */
757 dbus_connection_flush (connection);
760 _dbus_connection_do_iteration (connection,
761 DBUS_ITERATION_DO_READING |
762 DBUS_ITERATION_BLOCK,
763 timeout_milliseconds);
765 /* Check if we've gotten a reply */
766 link = _dbus_list_get_first_link (&connection->incoming_messages);
770 DBusMessage *reply = link->data;
772 if (_dbus_message_get_reply_serial (reply) == client_serial)
774 _dbus_list_remove (&connection->incoming_messages, link);
775 dbus_message_ref (message);
778 *result = DBUS_RESULT_SUCCESS;
782 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
786 *result = DBUS_RESULT_NO_REPLY;
792 * Blocks until the outgoing message queue is empty.
794 * @param connection the connection.
797 dbus_connection_flush (DBusConnection *connection)
799 while (connection->n_outgoing > 0)
800 _dbus_connection_do_iteration (connection,
801 DBUS_ITERATION_DO_WRITING |
802 DBUS_ITERATION_BLOCK,
807 * Gets the number of messages in the incoming message queue.
809 * @param connection the connection.
810 * @returns the number of messages in the queue.
813 dbus_connection_get_n_messages (DBusConnection *connection)
815 return connection->n_incoming;
819 * Returns the first-received message from the incoming message queue,
820 * leaving it in the queue. The caller does not own a reference to the
821 * returned message. If the queue is empty, returns #NULL.
823 * @param connection the connection.
824 * @returns next message in the incoming queue.
827 dbus_connection_peek_message (DBusConnection *connection)
829 return _dbus_list_get_first (&connection->incoming_messages);
833 * Returns the first-received message from the incoming message queue,
834 * removing it from the queue. The caller owns a reference to the
835 * returned message. If the queue is empty, returns #NULL.
837 * @param connection the connection.
838 * @returns next message in the incoming queue.
841 dbus_connection_pop_message (DBusConnection *connection)
843 if (connection->n_incoming > 0)
845 DBusMessage *message;
847 message = _dbus_list_pop_first (&connection->incoming_messages);
848 connection->n_incoming -= 1;
850 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
851 message, connection->n_incoming);
860 * Pops the first-received message from the current incoming message
861 * queue, runs any handlers for it, then unrefs the message.
863 * @param connection the connection
864 * @returns #TRUE if the queue is not empty after dispatch
866 * @todo this function is not properly robust against reentrancy,
867 * that is, if handlers are added/removed while dispatching
868 * a message, things will get messed up.
871 dbus_connection_dispatch_message (DBusConnection *connection)
873 DBusMessage *message;
877 DBusHandlerResult result;
880 dbus_connection_ref (connection);
882 message = dbus_connection_pop_message (connection);
885 dbus_connection_unref (connection);
889 filter_serial = connection->filters_serial;
890 handler_serial = connection->handlers_serial;
892 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
894 link = _dbus_list_get_first_link (&connection->filter_list);
897 DBusMessageHandler *handler = link->data;
898 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
900 result = _dbus_message_handler_handle_message (handler, connection,
903 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
906 if (filter_serial != connection->filters_serial)
908 _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
915 name = dbus_message_get_name (message);
918 DBusMessageHandler *handler;
920 handler = _dbus_hash_table_lookup_string (connection->handler_table,
925 result = _dbus_message_handler_handle_message (handler, connection,
928 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
931 if (handler_serial != connection->handlers_serial)
933 _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
940 dbus_connection_unref (connection);
941 dbus_message_unref (message);
943 return connection->n_incoming > 0;
947 * Sets the disconnect handler function for the connection.
948 * Will be called exactly once, when the connection is
951 * @param connection the connection.
952 * @param disconnect_function the disconnect handler.
953 * @param data data to pass to the disconnect handler.
954 * @param free_data_function function to be called to free the data.
957 dbus_connection_set_disconnect_function (DBusConnection *connection,
958 DBusDisconnectFunction disconnect_function,
960 DBusFreeFunction free_data_function)
962 if (connection->disconnect_free_data_function != NULL)
963 (* connection->disconnect_free_data_function) (connection->disconnect_data);
965 connection->disconnect_function = disconnect_function;
966 connection->disconnect_data = data;
967 connection->disconnect_free_data_function = free_data_function;
971 * Sets the watch functions for the connection. These functions are
972 * responsible for making the application's main loop aware of file
973 * descriptors that need to be monitored for events, using select() or
974 * poll(). When using Qt, typically the DBusAddWatchFunction would
975 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
976 * could call g_io_add_watch(), or could be used as part of a more
979 * The DBusWatch can be queried for the file descriptor to watch using
980 * dbus_watch_get_fd(), and for the events to watch for using
981 * dbus_watch_get_flags(). The flags returned by
982 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
983 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
984 * all watches implicitly include a watch for hangups, errors, and
985 * other exceptional conditions.
987 * Once a file descriptor becomes readable or writable, or an exception
988 * occurs, dbus_connection_handle_watch() should be called to
989 * notify the connection of the file descriptor's condition.
991 * dbus_connection_handle_watch() cannot be called during the
992 * DBusAddWatchFunction, as the connection will not be ready to handle
995 * It is not allowed to reference a DBusWatch after it has been passed
996 * to remove_function.
998 * @param connection the connection.
999 * @param add_function function to begin monitoring a new descriptor.
1000 * @param remove_function function to stop monitoring a descriptor.
1001 * @param data data to pass to add_function and remove_function.
1002 * @param free_data_function function to be called to free the data.
1005 dbus_connection_set_watch_functions (DBusConnection *connection,
1006 DBusAddWatchFunction add_function,
1007 DBusRemoveWatchFunction remove_function,
1009 DBusFreeFunction free_data_function)
1011 /* ref connection for slightly better reentrancy */
1012 dbus_connection_ref (connection);
1014 _dbus_watch_list_set_functions (connection->watches,
1015 add_function, remove_function,
1016 data, free_data_function);
1018 /* drop our paranoid refcount */
1019 dbus_connection_unref (connection);
1023 * Sets the timeout functions for the connection. These functions are
1024 * responsible for making the application's main loop aware of timeouts.
1025 * When using Qt, typically the DBusAddTimeoutFunction would create a
1026 * QTimer. When using GLib, the DBusAddTimeoutFunction would call
1029 * The DBusTimeout can be queried for the timer interval using
1030 * dbus_timeout_get_interval.
1032 * Once a timeout occurs, dbus_timeout_handle should be call to invoke
1033 * the timeout's callback.
1036 dbus_connection_set_timeout_functions (DBusConnection *connection,
1037 DBusAddTimeoutFunction add_function,
1038 DBusRemoveTimeoutFunction remove_function,
1040 DBusFreeFunction free_data_function)
1042 /* ref connection for slightly better reentrancy */
1043 dbus_connection_ref (connection);
1045 _dbus_timeout_list_set_functions (connection->timeouts,
1046 add_function, remove_function,
1047 data, free_data_function);
1049 /* drop our paranoid refcount */
1050 dbus_connection_unref (connection);
1054 * Called to notify the connection when a previously-added watch
1055 * is ready for reading or writing, or has an exception such
1058 * @param connection the connection.
1059 * @param watch the watch.
1060 * @param condition the current condition of the file descriptors being watched.
1063 dbus_connection_handle_watch (DBusConnection *connection,
1065 unsigned int condition)
1067 _dbus_transport_handle_watch (connection->transport,
1072 * Adds a message filter. Filters are handlers that are run on
1073 * all incoming messages, prior to the normal handlers
1074 * registered with dbus_connection_register_handler().
1075 * Filters are run in the order that they were added.
1076 * The same handler can be added as a filter more than once, in
1077 * which case it will be run more than once.
1079 * @param connection the connection
1080 * @param handler the handler
1081 * @returns #TRUE on success, #FALSE if not enough memory.
1084 dbus_connection_add_filter (DBusConnection *connection,
1085 DBusMessageHandler *handler)
1087 if (!_dbus_message_handler_add_connection (handler, connection))
1090 if (!_dbus_list_append (&connection->filter_list,
1093 _dbus_message_handler_remove_connection (handler, connection);
1097 connection->filters_serial += 1;
1103 * Removes a previously-added message filter. It is a programming
1104 * error to call this function for a handler that has not
1105 * been added as a filter. If the given handler was added
1106 * more than once, only one instance of it will be removed
1107 * (the most recently-added instance).
1109 * @param connection the connection
1110 * @param handler the handler to remove
1114 dbus_connection_remove_filter (DBusConnection *connection,
1115 DBusMessageHandler *handler)
1117 if (!_dbus_list_remove_last (&connection->filter_list, handler))
1119 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
1123 _dbus_message_handler_remove_connection (handler, connection);
1125 connection->filters_serial += 1;
1129 * Registers a handler for a list of message names. A single handler
1130 * can be registered for any number of message names, but each message
1131 * name can only have one handler at a time. It's not allowed to call
1132 * this function with the name of a message that already has a
1133 * handler. If the function returns #FALSE, the handlers were not
1134 * registered due to lack of memory.
1136 * @param connection the connection
1137 * @param handler the handler
1138 * @param messages_to_handle the messages to handle
1139 * @param n_messages the number of message names in messages_to_handle
1140 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
1144 dbus_connection_register_handler (DBusConnection *connection,
1145 DBusMessageHandler *handler,
1146 const char **messages_to_handle,
1152 while (i < n_messages)
1157 key = _dbus_strdup (messages_to_handle[i]);
1161 if (!_dbus_hash_iter_lookup (connection->handler_table,
1169 if (_dbus_hash_iter_get_value (&iter) != NULL)
1171 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1172 messages_to_handle[i]);
1173 dbus_free (key); /* won't have replaced the old key with the new one */
1177 if (!_dbus_message_handler_add_connection (handler, connection))
1179 _dbus_hash_iter_remove_entry (&iter);
1180 /* key has freed on nuking the entry */
1184 _dbus_hash_iter_set_value (&iter, handler);
1186 connection->handlers_serial += 1;
1194 /* unregister everything registered so far,
1195 * so we don't fail partially
1197 dbus_connection_unregister_handler (connection,
1206 * Unregisters a handler for a list of message names. The handlers
1207 * must have been previously registered.
1209 * @param connection the connection
1210 * @param handler the handler
1211 * @param messages_to_handle the messages to handle
1212 * @param n_messages the number of message names in messages_to_handle
1216 dbus_connection_unregister_handler (DBusConnection *connection,
1217 DBusMessageHandler *handler,
1218 const char **messages_to_handle,
1224 while (i < n_messages)
1228 if (!_dbus_hash_iter_lookup (connection->handler_table,
1229 (char*) messages_to_handle[i], FALSE,
1232 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1233 messages_to_handle[i]);
1235 else if (_dbus_hash_iter_get_value (&iter) != handler)
1237 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1238 messages_to_handle[i]);
1242 _dbus_hash_iter_remove_entry (&iter);
1243 _dbus_message_handler_remove_connection (handler, connection);
1249 connection->handlers_serial += 1;
1252 static int *allocated_slots = NULL;
1253 static int n_allocated_slots = 0;
1254 static int n_used_slots = 0;
1255 static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
1258 * Allocates an integer ID to be used for storing application-specific
1259 * data on any DBusConnection. The allocated ID may then be used
1260 * with dbus_connection_set_data() and dbus_connection_get_data().
1261 * If allocation fails, -1 is returned.
1263 * @returns -1 on failure, otherwise the data slot ID
1266 dbus_connection_allocate_data_slot (void)
1270 if (!dbus_static_mutex_lock (&allocated_slots_lock))
1273 if (n_used_slots < n_allocated_slots)
1276 while (slot < n_allocated_slots)
1278 if (allocated_slots[slot] < 0)
1280 allocated_slots[slot] = slot;
1287 _dbus_assert (slot < n_allocated_slots);
1294 tmp = dbus_realloc (allocated_slots,
1295 sizeof (int) * (n_allocated_slots + 1));
1299 allocated_slots = tmp;
1300 slot = n_allocated_slots;
1301 n_allocated_slots += 1;
1303 allocated_slots[slot] = slot;
1306 _dbus_assert (slot >= 0);
1307 _dbus_assert (slot < n_allocated_slots);
1310 dbus_static_mutex_unlock (&allocated_slots_lock);
1315 * Deallocates a global ID for connection data slots.
1316 * dbus_connection_get_data() and dbus_connection_set_data()
1317 * may no longer be used with this slot.
1318 * Existing data stored on existing DBusConnection objects
1319 * will be freed when the connection is finalized,
1320 * but may not be retrieved (and may only be replaced
1321 * if someone else reallocates the slot).
1323 * @param slot the slot to deallocate
1326 dbus_connection_free_data_slot (int slot)
1328 dbus_static_mutex_lock (&allocated_slots_lock);
1330 _dbus_assert (slot < n_allocated_slots);
1331 _dbus_assert (allocated_slots[slot] == slot);
1333 allocated_slots[slot] = -1;
1336 if (n_used_slots == 0)
1338 dbus_free (allocated_slots);
1339 allocated_slots = NULL;
1340 n_allocated_slots = 0;
1343 dbus_static_mutex_unlock (&allocated_slots_lock);
1347 * Stores a pointer on a DBusConnection, along
1348 * with an optional function to be used for freeing
1349 * the data when the data is set again, or when
1350 * the connection is finalized. The slot number
1351 * must have been allocated with dbus_connection_allocate_data_slot().
1353 * @param connection the connection
1354 * @param slot the slot number
1355 * @param data the data to store
1356 * @param free_data_func finalizer function for the data
1357 * @returns #TRUE if there was enough memory to store the data
1360 dbus_connection_set_data (DBusConnection *connection,
1363 DBusFreeFunction free_data_func)
1365 _dbus_assert (slot < n_allocated_slots);
1366 _dbus_assert (allocated_slots[slot] == slot);
1368 if (slot >= connection->n_slots)
1373 tmp = dbus_realloc (connection->data_slots,
1374 sizeof (DBusDataSlot) * (slot + 1));
1378 connection->data_slots = tmp;
1379 i = connection->n_slots;
1380 connection->n_slots = slot + 1;
1381 while (i < connection->n_slots)
1383 connection->data_slots[i].data = NULL;
1384 connection->data_slots[i].free_data_func = NULL;
1389 _dbus_assert (slot < connection->n_slots);
1391 if (connection->data_slots[slot].free_data_func)
1392 (* connection->data_slots[slot].free_data_func) (connection->data_slots[slot].data);
1394 connection->data_slots[slot].data = data;
1395 connection->data_slots[slot].free_data_func = free_data_func;
1401 * Retrieves data previously set with dbus_connection_set_data().
1402 * The slot must still be allocated (must not have been freed).
1404 * @param connection the connection
1405 * @param slot the slot to get data from
1406 * @returns the data, or #NULL if not found
1409 dbus_connection_get_data (DBusConnection *connection,
1412 _dbus_assert (slot < n_allocated_slots);
1413 _dbus_assert (allocated_slots[slot] == slot);
1415 if (slot >= connection->n_slots)
1418 return connection->data_slots[slot].data;
1422 _dbus_connection_free_data_slots (DBusConnection *connection)
1427 while (i < connection->n_slots)
1429 if (connection->data_slots[i].free_data_func)
1430 (* connection->data_slots[i].free_data_func) (connection->data_slots[i].data);
1431 connection->data_slots[i].data = NULL;
1432 connection->data_slots[i].free_data_func = NULL;
1436 dbus_free (connection->data_slots);
1437 connection->data_slots = NULL;
1438 connection->n_slots = 0;
1442 * Specifies the maximum size message this connection is allowed to
1443 * receive. Larger messages will result in disconnecting the
1446 * @param connection a #DBusConnection
1447 * @param size maximum message size the connection can receive, in bytes
1450 dbus_connection_set_max_message_size (DBusConnection *connection,
1453 _dbus_transport_set_max_message_size (connection->transport,
1458 * Gets the value set by dbus_connection_set_max_message_size().
1460 * @param connection the connection
1461 * @returns the max size of a single message
1464 dbus_connection_get_max_message_size (DBusConnection *connection)
1466 return _dbus_transport_get_max_message_size (connection->transport);
1470 * Sets the maximum total number of bytes that can be used for all messages
1471 * received on this connection. Messages count toward the maximum until
1472 * they are finalized. When the maximum is reached, the connection will
1473 * not read more data until some messages are finalized.
1475 * The semantics of the maximum are: if outstanding messages are
1476 * already above the maximum, additional messages will not be read.
1477 * The semantics are not: if the next message would cause us to exceed
1478 * the maximum, we don't read it. The reason is that we don't know the
1479 * size of a message until after we read it.
1481 * Thus, the max live messages size can actually be exceeded
1482 * by up to the maximum size of a single message.
1484 * Also, if we read say 1024 bytes off the wire in a single read(),
1485 * and that contains a half-dozen small messages, we may exceed the
1486 * size max by that amount. But this should be inconsequential.
1488 * @param connection the connection
1489 * @param size the maximum size in bytes of all outstanding messages
1492 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
1495 _dbus_transport_set_max_live_messages_size (connection->transport,
1500 * Gets the value set by dbus_connection_set_max_live_messages_size().
1502 * @param connection the connection
1503 * @returns the max size of all live messages
1506 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
1508 return _dbus_transport_get_max_live_messages_size (connection->transport);