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-transport.h"
27 #include "dbus-watch.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-list.h"
30 #include "dbus-hash.h"
31 #include "dbus-message-internal.h"
32 #include "dbus-threads.h"
35 * @defgroup DBusConnection DBusConnection
37 * @brief Connection to another application
39 * A DBusConnection represents a connection to another
40 * application. Messages can be sent and received via this connection.
42 * The connection maintains a queue of incoming messages and a queue
43 * of outgoing messages. dbus_connection_pop_message() and friends
44 * can be used to read incoming messages from the queue.
45 * Outgoing messages are automatically discarded as they are
46 * written to the network.
48 * In brief a DBusConnection is a message queue associated with some
49 * message transport mechanism such as a socket.
54 * @defgroup DBusConnectionInternals DBusConnection implementation details
55 * @ingroup DBusInternals
56 * @brief Implementation details of DBusConnection
61 /** Opaque typedef for DBusDataSlot */
62 typedef struct DBusDataSlot DBusDataSlot;
63 /** DBusDataSlot is used to store application data on the connection */
66 void *data; /**< The application data */
67 DBusFreeFunction free_data_func; /**< Free the application data */
71 * Implementation details of DBusConnection. All fields are private.
75 int refcount; /**< Reference count. */
77 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
78 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
80 int n_outgoing; /**< Length of outgoing queue. */
81 int n_incoming; /**< Length of incoming queue. */
83 DBusTransport *transport; /**< Object that sends/receives messages over network. */
84 DBusWatchList *watches; /**< Stores active watches. */
86 DBusDisconnectFunction disconnect_function; /**< Callback on disconnect. */
87 void *disconnect_data; /**< Data for disconnect callback. */
88 DBusFreeFunction disconnect_free_data_function; /**< Free function for disconnect callback data. */
89 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
90 DBusList *filter_list; /**< List of filters. */
91 int filters_serial; /**< Increments when the list of filters is changed. */
92 int handlers_serial; /**< Increments when the handler table is changed. */
93 DBusDataSlot *data_slots; /**< Data slots */
94 int n_slots; /**< Slots allocated so far. */
96 DBusCounter *connection_counter; /**< Counter that we decrement when finalized */
98 int client_serial; /**< Client serial. Increments each time a message is sent */
99 unsigned int disconnect_notified : 1; /**< Already called disconnect_function */
102 static void _dbus_connection_free_data_slots (DBusConnection *connection);
105 * Adds a message to the incoming message queue, returning #FALSE
106 * if there's insufficient memory to queue the message.
108 * @param connection the connection.
109 * @param message the message to queue.
110 * @returns #TRUE on success.
113 _dbus_connection_queue_received_message (DBusConnection *connection,
114 DBusMessage *message)
116 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
118 if (!_dbus_list_append (&connection->incoming_messages,
122 dbus_message_ref (message);
123 connection->n_incoming += 1;
125 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
126 message, connection->n_incoming);
132 * Checks whether there are messages in the outgoing message queue.
134 * @param connection the connection.
135 * @returns #TRUE if the outgoing queue is non-empty.
138 _dbus_connection_have_messages_to_send (DBusConnection *connection)
140 return connection->outgoing_messages != NULL;
144 * Gets the next outgoing message. The message remains in the
145 * queue, and the caller does not own a reference to it.
147 * @param connection the connection.
148 * @returns the message to be sent.
151 _dbus_connection_get_message_to_send (DBusConnection *connection)
153 return _dbus_list_get_last (&connection->outgoing_messages);
157 * Notifies the connection that a message has been sent, so the
158 * message can be removed from the outgoing queue.
160 * @param connection the connection.
161 * @param message the message that was sent.
164 _dbus_connection_message_sent (DBusConnection *connection,
165 DBusMessage *message)
167 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
168 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
170 _dbus_list_pop_last (&connection->outgoing_messages);
171 dbus_message_unref (message);
173 connection->n_outgoing -= 1;
175 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
176 message, connection->n_outgoing);
178 if (connection->n_outgoing == 0)
179 _dbus_transport_messages_pending (connection->transport,
180 connection->n_outgoing);
184 * Adds a watch using the connection's DBusAddWatchFunction if
185 * available. Otherwise records the watch to be added when said
186 * function is available. Also re-adds the watch if the
187 * DBusAddWatchFunction changes. May fail due to lack of memory.
189 * @param connection the connection.
190 * @param watch the watch to add.
191 * @returns #TRUE on success.
194 _dbus_connection_add_watch (DBusConnection *connection,
197 if (connection->watches) /* null during finalize */
198 return _dbus_watch_list_add_watch (connection->watches,
205 * Removes a watch using the connection's DBusRemoveWatchFunction
206 * if available. It's an error to call this function on a watch
207 * that was not previously added.
209 * @param connection the connection.
210 * @param watch the watch to remove.
213 _dbus_connection_remove_watch (DBusConnection *connection,
216 if (connection->watches) /* null during finalize */
217 _dbus_watch_list_remove_watch (connection->watches,
222 * Tells the connection that the transport has been disconnected.
223 * Results in calling the application disconnect callback.
224 * Only has an effect the first time it's called.
226 * @param connection the connection
229 _dbus_connection_notify_disconnected (DBusConnection *connection)
231 if (connection->disconnect_function != NULL &&
232 !connection->disconnect_notified)
234 connection->disconnect_notified = TRUE;
235 dbus_connection_ref (connection);
236 (* connection->disconnect_function) (connection,
237 connection->disconnect_data);
238 dbus_connection_unref (connection);
243 * Queues incoming messages and sends outgoing messages for this
244 * connection, optionally blocking in the process. Each call to
245 * _dbus_connection_do_iteration() will call select() or poll() one
246 * time and then read or write data if possible.
248 * The purpose of this function is to be able to flush outgoing
249 * messages or queue up incoming messages without returning
250 * control to the application and causing reentrancy weirdness.
252 * The flags parameter allows you to specify whether to
253 * read incoming messages, write outgoing messages, or both,
254 * and whether to block if no immediate action is possible.
256 * The timeout_milliseconds parameter does nothing unless the
257 * iteration is blocking.
259 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
260 * wasn't specified, then it's impossible to block, even if
261 * you specify DBUS_ITERATION_BLOCK; in that case the function
262 * returns immediately.
264 * @param connection the connection.
265 * @param flags iteration flags.
266 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
269 _dbus_connection_do_iteration (DBusConnection *connection,
271 int timeout_milliseconds)
273 if (connection->n_outgoing == 0)
274 flags &= ~DBUS_ITERATION_DO_WRITING;
276 _dbus_transport_do_iteration (connection->transport,
277 flags, timeout_milliseconds);
281 * Creates a new connection for the given transport. A transport
282 * represents a message stream that uses some concrete mechanism, such
283 * as UNIX domain sockets. May return #NULL if insufficient
284 * memory exists to create the connection.
286 * @param transport the transport.
287 * @returns the new connection, or #NULL on failure.
290 _dbus_connection_new_for_transport (DBusTransport *transport)
292 DBusConnection *connection;
293 DBusWatchList *watch_list;
294 DBusHashTable *handler_table;
298 handler_table = NULL;
300 watch_list = _dbus_watch_list_new ();
301 if (watch_list == NULL)
305 _dbus_hash_table_new (DBUS_HASH_STRING,
307 if (handler_table == NULL)
310 connection = dbus_new0 (DBusConnection, 1);
311 if (connection == NULL)
314 connection->refcount = 1;
315 connection->transport = transport;
316 connection->watches = watch_list;
317 connection->handler_table = handler_table;
318 connection->filter_list = NULL;
320 connection->data_slots = NULL;
321 connection->n_slots = 0;
322 connection->client_serial = 1;
323 connection->disconnect_notified = FALSE;
325 _dbus_transport_ref (transport);
326 _dbus_transport_set_connection (transport, connection);
332 if (connection != NULL)
333 dbus_free (connection);
336 _dbus_hash_table_unref (handler_table);
339 _dbus_watch_list_free (watch_list);
345 _dbus_connection_get_next_client_serial (DBusConnection *connection)
349 serial = connection->client_serial++;
351 if (connection->client_serial < 0)
352 connection->client_serial = 1;
358 * Used to notify a connection when a DBusMessageHandler is
359 * destroyed, so the connection can drop any reference
362 * @param connection the connection
363 * @param handler the handler
366 _dbus_connection_handler_destroyed (DBusConnection *connection,
367 DBusMessageHandler *handler)
372 _dbus_hash_iter_init (connection->handler_table, &iter);
373 while (_dbus_hash_iter_next (&iter))
375 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
378 _dbus_hash_iter_remove_entry (&iter);
381 link = _dbus_list_get_first_link (&connection->filter_list);
384 DBusMessageHandler *h = link->data;
385 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
388 _dbus_list_remove_link (&connection->filter_list,
396 * Adds the counter used to count the number of open connections.
397 * Increments the counter by one, and saves it to be decremented
398 * again when this connection is finalized.
400 * @param connection a #DBusConnection
401 * @param counter counter that tracks number of connections
404 _dbus_connection_set_connection_counter (DBusConnection *connection,
405 DBusCounter *counter)
407 _dbus_assert (connection->connection_counter == NULL);
409 connection->connection_counter = counter;
410 _dbus_counter_ref (connection->connection_counter);
411 _dbus_counter_adjust (connection->connection_counter, 1);
417 * @addtogroup DBusConnection
423 * Opens a new connection to a remote address.
425 * @todo specify what the address parameter is. Right now
426 * it's just the name of a UNIX domain socket. It should be
427 * something more complex that encodes which transport to use.
429 * If the open fails, the function returns #NULL, and provides
430 * a reason for the failure in the result parameter. Pass
431 * #NULL for the result parameter if you aren't interested
432 * in the reason for failure.
434 * @param address the address.
435 * @param result address where a result code can be returned.
436 * @returns new connection, or #NULL on failure.
439 dbus_connection_open (const char *address,
440 DBusResultCode *result)
442 DBusConnection *connection;
443 DBusTransport *transport;
445 transport = _dbus_transport_open (address, result);
446 if (transport == NULL)
449 connection = _dbus_connection_new_for_transport (transport);
451 _dbus_transport_unref (transport);
453 if (connection == NULL)
455 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
463 * Increments the reference count of a DBusConnection.
465 * @param connection the connection.
468 dbus_connection_ref (DBusConnection *connection)
470 connection->refcount += 1;
474 * Decrements the reference count of a DBusConnection, and finalizes
475 * it if the count reaches zero. If a connection is still connected
476 * when it's finalized, it will be disconnected (that is, associated
477 * file handles will be closed).
479 * @param connection the connection.
482 dbus_connection_unref (DBusConnection *connection)
484 _dbus_assert (connection != NULL);
485 _dbus_assert (connection->refcount > 0);
487 connection->refcount -= 1;
488 if (connection->refcount == 0)
493 dbus_connection_disconnect (connection);
495 /* free disconnect data as a side effect */
496 dbus_connection_set_disconnect_function (connection,
499 if (connection->connection_counter != NULL)
501 /* subtract ourselves from the counter */
502 _dbus_counter_adjust (connection->connection_counter, - 1);
503 _dbus_counter_unref (connection->connection_counter);
504 connection->connection_counter = NULL;
507 _dbus_watch_list_free (connection->watches);
508 connection->watches = NULL;
510 _dbus_connection_free_data_slots (connection);
512 _dbus_hash_iter_init (connection->handler_table, &iter);
513 while (_dbus_hash_iter_next (&iter))
515 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
517 _dbus_message_handler_remove_connection (h, connection);
520 link = _dbus_list_get_first_link (&connection->filter_list);
523 DBusMessageHandler *h = link->data;
524 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
526 _dbus_message_handler_remove_connection (h, connection);
531 _dbus_hash_table_unref (connection->handler_table);
532 connection->handler_table = NULL;
534 _dbus_list_clear (&connection->filter_list);
536 _dbus_list_foreach (&connection->outgoing_messages,
537 (DBusForeachFunction) dbus_message_unref,
539 _dbus_list_clear (&connection->outgoing_messages);
541 _dbus_list_foreach (&connection->incoming_messages,
542 (DBusForeachFunction) dbus_message_unref,
544 _dbus_list_clear (&connection->incoming_messages);
546 _dbus_transport_unref (connection->transport);
548 dbus_free (connection);
553 * Closes the connection, so no further data can be sent or received.
554 * Any further attempts to send data will result in errors. This
555 * function does not affect the connection's reference count. It's
556 * safe to disconnect a connection more than once; all calls after the
557 * first do nothing. It's impossible to "reconnect" a connection, a
558 * new connection must be created.
560 * @param connection the connection.
563 dbus_connection_disconnect (DBusConnection *connection)
565 _dbus_transport_disconnect (connection->transport);
569 * Gets whether the connection is currently connected. All
570 * connections are connected when they are opened. A connection may
571 * become disconnected when the remote application closes its end, or
572 * exits; a connection may also be disconnected with
573 * dbus_connection_disconnect().
575 * @param connection the connection.
576 * @returns #TRUE if the connection is still alive.
579 dbus_connection_get_is_connected (DBusConnection *connection)
581 return _dbus_transport_get_is_connected (connection->transport);
585 * Gets whether the connection was authenticated. (Note that
586 * if the connection was authenticated then disconnected,
587 * this function still returns #TRUE)
589 * @param connection the connection
590 * @returns #TRUE if the connection was ever authenticated
593 dbus_connection_get_is_authenticated (DBusConnection *connection)
595 return _dbus_transport_get_is_authenticated (connection->transport);
599 * Adds a message to the outgoing message queue. Does not block to
600 * write the message to the network; that happens asynchronously. to
601 * force the message to be written, call dbus_connection_flush().
603 * If the function fails, it returns #FALSE and returns the
604 * reason for failure via the result parameter.
605 * The result parameter can be #NULL if you aren't interested
606 * in the reason for the failure.
608 * @param connection the connection.
609 * @param message the message to write.
610 * @param result address where result code can be placed.
611 * @returns #TRUE on success.
614 dbus_connection_send_message (DBusConnection *connection,
615 DBusMessage *message,
616 DBusResultCode *result)
618 if (!_dbus_list_prepend (&connection->outgoing_messages,
621 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
625 dbus_message_ref (message);
626 connection->n_outgoing += 1;
628 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
629 message, connection->n_outgoing);
631 _dbus_message_set_client_serial (message, _dbus_connection_get_next_client_serial (connection));
632 _dbus_message_lock (message);
634 if (connection->n_outgoing == 1)
635 _dbus_transport_messages_pending (connection->transport,
636 connection->n_outgoing);
642 * Queues a message to send, as with dbus_connection_send_message(),
643 * but also sets up a DBusMessageHandler to receive a reply to the
644 * message. If no reply is received in the given timeout_milliseconds,
645 * expires the pending reply and sends the DBusMessageHandler a
646 * synthetic error reply (generated in-process, not by the remote
647 * application) indicating that a timeout occurred.
649 * Reply handlers see their replies after message filters see them,
650 * but before message handlers added with
651 * dbus_connection_register_handler() see them, regardless of the
652 * reply message's name. Reply handlers are only handed a single
653 * message as a reply, after a reply has been seen the handler is
654 * removed. If a filter filters out the reply before the handler sees
655 * it, the handler is not removed but the timeout will immediately
656 * fire again. If a filter was dumb and kept removing the timeout
657 * reply then we'd get in an infinite loop.
659 * If #NULL is passed for the reply_handler, the timeout reply will
660 * still be generated and placed into the message queue, but no
661 * specific message handler will receive the reply.
663 * If -1 is passed for the timeout, a sane default timeout is used. -1
664 * is typically the best value for the timeout for this reason, unless
665 * you want a very short or very long timeout. There is no way to
666 * avoid a timeout entirely, other than passing INT_MAX for the
667 * timeout to postpone it indefinitely.
669 * @param connection the connection
670 * @param message the message to send
671 * @param reply_handler message handler expecting the reply, or #NULL
672 * @param timeout_milliseconds timeout in milliseconds or -1 for default
673 * @param result return location for result code
674 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
676 * @todo this function isn't implemented because we need message serials
677 * and other slightly more rich DBusMessage implementation in order to
678 * implement it. The basic idea will be to keep a hash of serials we're
679 * expecting a reply to, and also to add a way to tell GLib or Qt to
680 * install a timeout. Then install a timeout which is the shortest
681 * timeout of any pending reply.
683 * @todo implement non-reentrant "block for reply" variant. i.e. send
684 * a message, block until we get a reply, then pull reply out of
685 * message queue and return it, *without dispatching any handlers for
686 * any other messages* - used for non-reentrant "method calls" We can
687 * block properly for this using _dbus_connection_do_iteration().
691 dbus_connection_send_message_with_reply (DBusConnection *connection,
692 DBusMessage *message,
693 DBusMessageHandler *reply_handler,
694 int timeout_milliseconds,
695 DBusResultCode *result)
698 return dbus_connection_send_message (connection, message, result);
702 * Blocks until the outgoing message queue is empty.
704 * @param connection the connection.
707 dbus_connection_flush (DBusConnection *connection)
709 while (connection->n_outgoing > 0)
710 _dbus_connection_do_iteration (connection,
711 DBUS_ITERATION_DO_WRITING |
712 DBUS_ITERATION_BLOCK,
717 * Gets the number of messages in the incoming message queue.
719 * @param connection the connection.
720 * @returns the number of messages in the queue.
723 dbus_connection_get_n_messages (DBusConnection *connection)
725 return connection->n_incoming;
729 * Returns the first-received message from the incoming message queue,
730 * leaving it in the queue. The caller does not own a reference to the
731 * returned message. If the queue is empty, returns #NULL.
733 * @param connection the connection.
734 * @returns next message in the incoming queue.
737 dbus_connection_peek_message (DBusConnection *connection)
739 return _dbus_list_get_first (&connection->incoming_messages);
743 * Returns the first-received message from the incoming message queue,
744 * removing it from the queue. The caller owns a reference to the
745 * returned message. If the queue is empty, returns #NULL.
747 * @param connection the connection.
748 * @returns next message in the incoming queue.
751 dbus_connection_pop_message (DBusConnection *connection)
753 if (connection->n_incoming > 0)
755 DBusMessage *message;
757 message = _dbus_list_pop_first (&connection->incoming_messages);
758 connection->n_incoming -= 1;
760 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
761 message, connection->n_incoming);
770 * Pops the first-received message from the current incoming message
771 * queue, runs any handlers for it, then unrefs the message.
773 * @param connection the connection
774 * @returns #TRUE if the queue is not empty after dispatch
776 * @todo this function is not properly robust against reentrancy,
777 * that is, if handlers are added/removed while dispatching
778 * a message, things will get messed up.
781 dbus_connection_dispatch_message (DBusConnection *connection)
783 DBusMessage *message;
787 DBusHandlerResult result;
790 dbus_connection_ref (connection);
792 message = dbus_connection_pop_message (connection);
795 dbus_connection_unref (connection);
799 filter_serial = connection->filters_serial;
800 handler_serial = connection->handlers_serial;
802 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
804 link = _dbus_list_get_first_link (&connection->filter_list);
807 DBusMessageHandler *handler = link->data;
808 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
810 result = _dbus_message_handler_handle_message (handler, connection,
813 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
816 if (filter_serial != connection->filters_serial)
818 _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
825 name = dbus_message_get_name (message);
828 DBusMessageHandler *handler;
830 handler = _dbus_hash_table_lookup_string (connection->handler_table,
835 result = _dbus_message_handler_handle_message (handler, connection,
838 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
841 if (handler_serial != connection->handlers_serial)
843 _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
850 dbus_connection_unref (connection);
851 dbus_message_unref (message);
853 return connection->n_incoming > 0;
857 * Sets the disconnect handler function for the connection.
858 * Will be called exactly once, when the connection is
861 * @param connection the connection.
862 * @param disconnect_function the disconnect handler.
863 * @param data data to pass to the disconnect handler.
864 * @param free_data_function function to be called to free the data.
867 dbus_connection_set_disconnect_function (DBusConnection *connection,
868 DBusDisconnectFunction disconnect_function,
870 DBusFreeFunction free_data_function)
872 if (connection->disconnect_free_data_function != NULL)
873 (* connection->disconnect_free_data_function) (connection->disconnect_data);
875 connection->disconnect_function = disconnect_function;
876 connection->disconnect_data = data;
877 connection->disconnect_free_data_function = free_data_function;
881 * Sets the watch functions for the connection. These functions are
882 * responsible for making the application's main loop aware of file
883 * descriptors that need to be monitored for events, using select() or
884 * poll(). When using Qt, typically the DBusAddWatchFunction would
885 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
886 * could call g_io_add_watch(), or could be used as part of a more
889 * The DBusWatch can be queried for the file descriptor to watch using
890 * dbus_watch_get_fd(), and for the events to watch for using
891 * dbus_watch_get_flags(). The flags returned by
892 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
893 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
894 * all watches implicitly include a watch for hangups, errors, and
895 * other exceptional conditions.
897 * Once a file descriptor becomes readable or writable, or an exception
898 * occurs, dbus_connection_handle_watch() should be called to
899 * notify the connection of the file descriptor's condition.
901 * dbus_connection_handle_watch() cannot be called during the
902 * DBusAddWatchFunction, as the connection will not be ready to handle
905 * It is not allowed to reference a DBusWatch after it has been passed
906 * to remove_function.
908 * @param connection the connection.
909 * @param add_function function to begin monitoring a new descriptor.
910 * @param remove_function function to stop monitoring a descriptor.
911 * @param data data to pass to add_function and remove_function.
912 * @param free_data_function function to be called to free the data.
915 dbus_connection_set_watch_functions (DBusConnection *connection,
916 DBusAddWatchFunction add_function,
917 DBusRemoveWatchFunction remove_function,
919 DBusFreeFunction free_data_function)
921 /* ref connection for slightly better reentrancy */
922 dbus_connection_ref (connection);
924 _dbus_watch_list_set_functions (connection->watches,
925 add_function, remove_function,
926 data, free_data_function);
928 /* drop our paranoid refcount */
929 dbus_connection_unref (connection);
933 * Called to notify the connection when a previously-added watch
934 * is ready for reading or writing, or has an exception such
937 * @param connection the connection.
938 * @param watch the watch.
939 * @param condition the current condition of the file descriptors being watched.
942 dbus_connection_handle_watch (DBusConnection *connection,
944 unsigned int condition)
946 _dbus_transport_handle_watch (connection->transport,
951 * Adds a message filter. Filters are handlers that are run on
952 * all incoming messages, prior to the normal handlers
953 * registered with dbus_connection_register_handler().
954 * Filters are run in the order that they were added.
955 * The same handler can be added as a filter more than once, in
956 * which case it will be run more than once.
958 * @param connection the connection
959 * @param handler the handler
960 * @returns #TRUE on success, #FALSE if not enough memory.
963 dbus_connection_add_filter (DBusConnection *connection,
964 DBusMessageHandler *handler)
966 if (!_dbus_message_handler_add_connection (handler, connection))
969 if (!_dbus_list_append (&connection->filter_list,
972 _dbus_message_handler_remove_connection (handler, connection);
976 connection->filters_serial += 1;
982 * Removes a previously-added message filter. It is a programming
983 * error to call this function for a handler that has not
984 * been added as a filter. If the given handler was added
985 * more than once, only one instance of it will be removed
986 * (the most recently-added instance).
988 * @param connection the connection
989 * @param handler the handler to remove
993 dbus_connection_remove_filter (DBusConnection *connection,
994 DBusMessageHandler *handler)
996 if (!_dbus_list_remove_last (&connection->filter_list, handler))
998 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
1002 _dbus_message_handler_remove_connection (handler, connection);
1004 connection->filters_serial += 1;
1008 * Registers a handler for a list of message names. A single handler
1009 * can be registered for any number of message names, but each message
1010 * name can only have one handler at a time. It's not allowed to call
1011 * this function with the name of a message that already has a
1012 * handler. If the function returns #FALSE, the handlers were not
1013 * registered due to lack of memory.
1015 * @param connection the connection
1016 * @param handler the handler
1017 * @param messages_to_handle the messages to handle
1018 * @param n_messages the number of message names in messages_to_handle
1019 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
1023 dbus_connection_register_handler (DBusConnection *connection,
1024 DBusMessageHandler *handler,
1025 const char **messages_to_handle,
1031 while (i < n_messages)
1036 key = _dbus_strdup (messages_to_handle[i]);
1040 if (!_dbus_hash_iter_lookup (connection->handler_table,
1048 if (_dbus_hash_iter_get_value (&iter) != NULL)
1050 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1051 messages_to_handle[i]);
1052 dbus_free (key); /* won't have replaced the old key with the new one */
1056 if (!_dbus_message_handler_add_connection (handler, connection))
1058 _dbus_hash_iter_remove_entry (&iter);
1059 /* key has freed on nuking the entry */
1063 _dbus_hash_iter_set_value (&iter, handler);
1065 connection->handlers_serial += 1;
1073 /* unregister everything registered so far,
1074 * so we don't fail partially
1076 dbus_connection_unregister_handler (connection,
1085 * Unregisters a handler for a list of message names. The handlers
1086 * must have been previously registered.
1088 * @param connection the connection
1089 * @param handler the handler
1090 * @param messages_to_handle the messages to handle
1091 * @param n_messages the number of message names in messages_to_handle
1095 dbus_connection_unregister_handler (DBusConnection *connection,
1096 DBusMessageHandler *handler,
1097 const char **messages_to_handle,
1103 while (i < n_messages)
1107 if (!_dbus_hash_iter_lookup (connection->handler_table,
1108 (char*) messages_to_handle[i], FALSE,
1111 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1112 messages_to_handle[i]);
1114 else if (_dbus_hash_iter_get_value (&iter) != handler)
1116 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1117 messages_to_handle[i]);
1121 _dbus_hash_iter_remove_entry (&iter);
1122 _dbus_message_handler_remove_connection (handler, connection);
1128 connection->handlers_serial += 1;
1131 static int *allocated_slots = NULL;
1132 static int n_allocated_slots = 0;
1133 static int n_used_slots = 0;
1134 static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
1137 * Allocates an integer ID to be used for storing application-specific
1138 * data on any DBusConnection. The allocated ID may then be used
1139 * with dbus_connection_set_data() and dbus_connection_get_data().
1140 * If allocation fails, -1 is returned.
1142 * @returns -1 on failure, otherwise the data slot ID
1145 dbus_connection_allocate_data_slot (void)
1149 if (!dbus_static_mutex_lock (&allocated_slots_lock))
1152 if (n_used_slots < n_allocated_slots)
1155 while (slot < n_allocated_slots)
1157 if (allocated_slots[slot] < 0)
1159 allocated_slots[slot] = slot;
1166 _dbus_assert (slot < n_allocated_slots);
1173 tmp = dbus_realloc (allocated_slots,
1174 sizeof (int) * (n_allocated_slots + 1));
1178 allocated_slots = tmp;
1179 slot = n_allocated_slots;
1180 n_allocated_slots += 1;
1182 allocated_slots[slot] = slot;
1185 _dbus_assert (slot >= 0);
1186 _dbus_assert (slot < n_allocated_slots);
1189 dbus_static_mutex_unlock (&allocated_slots_lock);
1194 * Deallocates a global ID for connection data slots.
1195 * dbus_connection_get_data() and dbus_connection_set_data()
1196 * may no longer be used with this slot.
1197 * Existing data stored on existing DBusConnection objects
1198 * will be freed when the connection is finalized,
1199 * but may not be retrieved (and may only be replaced
1200 * if someone else reallocates the slot).
1202 * @param slot the slot to deallocate
1205 dbus_connection_free_data_slot (int slot)
1207 dbus_static_mutex_lock (&allocated_slots_lock);
1209 _dbus_assert (slot < n_allocated_slots);
1210 _dbus_assert (allocated_slots[slot] == slot);
1212 allocated_slots[slot] = -1;
1215 if (n_used_slots == 0)
1217 dbus_free (allocated_slots);
1218 allocated_slots = NULL;
1219 n_allocated_slots = 0;
1222 dbus_static_mutex_unlock (&allocated_slots_lock);
1226 * Stores a pointer on a DBusConnection, along
1227 * with an optional function to be used for freeing
1228 * the data when the data is set again, or when
1229 * the connection is finalized. The slot number
1230 * must have been allocated with dbus_connection_allocate_data_slot().
1232 * @param connection the connection
1233 * @param slot the slot number
1234 * @param data the data to store
1235 * @param free_data_func finalizer function for the data
1236 * @returns #TRUE if there was enough memory to store the data
1239 dbus_connection_set_data (DBusConnection *connection,
1242 DBusFreeFunction free_data_func)
1244 _dbus_assert (slot < n_allocated_slots);
1245 _dbus_assert (allocated_slots[slot] == slot);
1247 if (slot >= connection->n_slots)
1252 tmp = dbus_realloc (connection->data_slots,
1253 sizeof (DBusDataSlot) * (slot + 1));
1257 connection->data_slots = tmp;
1258 i = connection->n_slots;
1259 connection->n_slots = slot + 1;
1260 while (i < connection->n_slots)
1262 connection->data_slots[i].data = NULL;
1263 connection->data_slots[i].free_data_func = NULL;
1268 _dbus_assert (slot < connection->n_slots);
1270 if (connection->data_slots[slot].free_data_func)
1271 (* connection->data_slots[slot].free_data_func) (connection->data_slots[slot].data);
1273 connection->data_slots[slot].data = data;
1274 connection->data_slots[slot].free_data_func = free_data_func;
1280 * Retrieves data previously set with dbus_connection_set_data().
1281 * The slot must still be allocated (must not have been freed).
1283 * @param connection the connection
1284 * @param slot the slot to get data from
1285 * @returns the data, or #NULL if not found
1288 dbus_connection_get_data (DBusConnection *connection,
1291 _dbus_assert (slot < n_allocated_slots);
1292 _dbus_assert (allocated_slots[slot] == slot);
1294 if (slot >= connection->n_slots)
1297 return connection->data_slots[slot].data;
1301 _dbus_connection_free_data_slots (DBusConnection *connection)
1306 while (i < connection->n_slots)
1308 if (connection->data_slots[i].free_data_func)
1309 (* connection->data_slots[i].free_data_func) (connection->data_slots[i].data);
1310 connection->data_slots[i].data = NULL;
1311 connection->data_slots[i].free_data_func = NULL;
1315 dbus_free (connection->data_slots);
1316 connection->data_slots = NULL;
1317 connection->n_slots = 0;
1321 * Specifies the maximum size message this connection is allowed to
1322 * receive. Larger messages will result in disconnecting the
1325 * @param connection a #DBusConnection
1326 * @param size maximum message size the connection can receive, in bytes
1329 dbus_connection_set_max_message_size (DBusConnection *connection,
1332 _dbus_transport_set_max_message_size (connection->transport,
1337 * Gets the value set by dbus_connection_set_max_message_size().
1339 * @param connection the connection
1340 * @returns the max size of a single message
1343 dbus_connection_get_max_message_size (DBusConnection *connection)
1345 return _dbus_transport_get_max_message_size (connection->transport);
1349 * Sets the maximum total number of bytes that can be used for all messages
1350 * received on this connection. Messages count toward the maximum until
1351 * they are finalized. When the maximum is reached, the connection will
1352 * not read more data until some messages are finalized.
1354 * The semantics of the maximum are: if outstanding messages are
1355 * already above the maximum, additional messages will not be read.
1356 * The semantics are not: if the next message would cause us to exceed
1357 * the maximum, we don't read it. The reason is that we don't know the
1358 * size of a message until after we read it.
1360 * Thus, the max live messages size can actually be exceeded
1361 * by up to the maximum size of a single message.
1363 * Also, if we read say 1024 bytes off the wire in a single read(),
1364 * and that contains a half-dozen small messages, we may exceed the
1365 * size max by that amount. But this should be inconsequential.
1367 * @param connection the connection
1368 * @param size the maximum size in bytes of all outstanding messages
1371 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
1374 _dbus_transport_set_max_live_messages_size (connection->transport,
1379 * Gets the value set by dbus_connection_set_max_live_messages_size().
1381 * @param connection the connection
1382 * @returns the max size of all live messages
1385 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
1387 return _dbus_transport_get_max_live_messages_size (connection->transport);