1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
4 * Copyright (C) 2002 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"
34 * @defgroup DBusConnection DBusConnection
36 * @brief Connection to another application
38 * A DBusConnection represents a connection to another
39 * application. Messages can be sent and received via this connection.
41 * The connection maintains a queue of incoming messages and a queue
42 * of outgoing messages. dbus_connection_pop_message() and friends
43 * can be used to read incoming messages from the queue.
44 * Outgoing messages are automatically discarded as they are
45 * written to the network.
47 * In brief a DBusConnection is a message queue associated with some
48 * message transport mechanism such as a socket.
53 * @defgroup DBusConnectionInternals DBusConnection implementation details
54 * @ingroup DBusInternals
55 * @brief Implementation details of DBusConnection
61 * Implementation details of DBusConnection. All fields are private.
65 int refcount; /**< Reference count. */
67 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
68 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
70 int n_outgoing; /**< Length of outgoing queue. */
71 int n_incoming; /**< Length of incoming queue. */
73 DBusTransport *transport; /**< Object that sends/receives messages over network. */
74 DBusWatchList *watches; /**< Stores active watches. */
76 DBusConnectionErrorFunction error_function; /**< Callback for errors. */
77 void *error_data; /**< Data for error callback. */
78 DBusFreeFunction error_free_data_function; /**< Free function for error callback data. */
79 DBusHashTable *handler_table; /**< Table of registered DBusMessageHandler */
80 DBusList *filter_list; /**< List of filters. */
81 int filters_serial; /**< Increments when the list of filters is changed. */
82 int handlers_serial; /**< Increments when the handler table is changed. */
86 * Adds a message to the incoming message queue, returning #FALSE
87 * if there's insufficient memory to queue the message.
89 * @param connection the connection.
90 * @param message the message to queue.
91 * @returns #TRUE on success.
94 _dbus_connection_queue_received_message (DBusConnection *connection,
97 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
99 if (!_dbus_list_append (&connection->incoming_messages,
103 dbus_message_ref (message);
104 connection->n_incoming += 1;
106 _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
107 message, connection->n_incoming);
113 * Checks whether there are messages in the outgoing message queue.
115 * @param connection the connection.
116 * @returns #TRUE if the outgoing queue is non-empty.
119 _dbus_connection_have_messages_to_send (DBusConnection *connection)
121 return connection->outgoing_messages != NULL;
125 * Gets the next outgoing message. The message remanins in the
126 * queue, and the caller does not own a reference to it.
128 * @param connection the connection.
129 * @returns the message to be sent.
132 _dbus_connection_get_message_to_send (DBusConnection *connection)
134 return _dbus_list_get_last (&connection->outgoing_messages);
138 * Notifies the connection that a message has been sent, so the
139 * message can be removed from the outgoing queue.
141 * @param connection the connection.
142 * @param message the message that was sent.
145 _dbus_connection_message_sent (DBusConnection *connection,
146 DBusMessage *message)
148 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
149 _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
151 _dbus_list_pop_last (&connection->outgoing_messages);
152 dbus_message_unref (message);
154 connection->n_outgoing -= 1;
156 _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
157 message, connection->n_outgoing);
159 if (connection->n_outgoing == 0)
160 _dbus_transport_messages_pending (connection->transport,
161 connection->n_outgoing);
165 * Adds a watch using the connection's DBusAddWatchFunction if
166 * available. Otherwise records the watch to be added when said
167 * function is available. Also re-adds the watch if the
168 * DBusAddWatchFunction changes. May fail due to lack of memory.
170 * @param connection the connection.
171 * @param watch the watch to add.
172 * @returns #TRUE on success.
175 _dbus_connection_add_watch (DBusConnection *connection,
178 if (connection->watches) /* null during finalize */
179 return _dbus_watch_list_add_watch (connection->watches,
186 * Removes a watch using the connection's DBusRemoveWatchFunction
187 * if available. It's an error to call this function on a watch
188 * that was not previously added.
190 * @param connection the connection.
191 * @param watch the watch to remove.
194 _dbus_connection_remove_watch (DBusConnection *connection,
197 if (connection->watches) /* null during finalize */
198 _dbus_watch_list_remove_watch (connection->watches,
203 handle_error (DBusConnection *connection,
204 DBusResultCode result)
206 if (result != DBUS_RESULT_SUCCESS &&
207 connection->error_function != NULL)
209 dbus_connection_ref (connection);
210 (* connection->error_function) (connection, result,
211 connection->error_data);
212 dbus_connection_unref (connection);
217 set_result_handled (DBusConnection *connection,
218 DBusResultCode *result_address,
219 DBusResultCode result)
221 dbus_set_result (result_address, result);
222 handle_error (connection, result);
226 * Reports a transport error to the connection. Typically
227 * results in an application error callback being invoked.
229 * @param connection the connection.
230 * @param result_code the error code.
233 _dbus_connection_transport_error (DBusConnection *connection,
234 DBusResultCode result_code)
236 handle_error (connection, result_code);
240 * Queues incoming messages and sends outgoing messages for this
241 * connection, optionally blocking in the process. Each call to
242 * _dbus_connection_do_iteration() will call select() or poll() one
243 * time and then read or write data if possible.
245 * The purpose of this function is to be able to flush outgoing
246 * messages or queue up incoming messages without returning
247 * control to the application and causing reentrancy weirdness.
249 * The flags parameter allows you to specify whether to
250 * read incoming messages, write outgoing messages, or both,
251 * and whether to block if no immediate action is possible.
253 * The timeout_milliseconds parameter does nothing unless the
254 * iteration is blocking.
256 * If there are no outgoing messages and DBUS_ITERATION_DO_READING
257 * wasn't specified, then it's impossible to block, even if
258 * you specify DBUS_ITERATION_BLOCK; in that case the function
259 * returns immediately.
261 * @param connection the connection.
262 * @param flags iteration flags.
263 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
266 _dbus_connection_do_iteration (DBusConnection *connection,
268 int timeout_milliseconds)
270 if (connection->n_outgoing == 0)
271 flags &= ~DBUS_ITERATION_DO_WRITING;
273 _dbus_transport_do_iteration (connection->transport,
274 flags, timeout_milliseconds);
278 * Creates a new connection for the given transport. A transport
279 * represents a message stream that uses some concrete mechanism, such
280 * as UNIX domain sockets. May return #NULL if insufficient
281 * memory exists to create the connection.
283 * @param transport the transport.
284 * @returns the new connection, or #NULL on failure.
287 _dbus_connection_new_for_transport (DBusTransport *transport)
289 DBusConnection *connection;
290 DBusWatchList *watch_list;
291 DBusHashTable *handler_table;
295 handler_table = NULL;
297 watch_list = _dbus_watch_list_new ();
298 if (watch_list == NULL)
302 _dbus_hash_table_new (DBUS_HASH_STRING,
304 if (handler_table == NULL)
307 connection = dbus_new0 (DBusConnection, 1);
308 if (connection == NULL)
311 connection->refcount = 1;
312 connection->transport = transport;
313 connection->watches = watch_list;
314 connection->handler_table = handler_table;
315 connection->filter_list = NULL;
317 _dbus_transport_ref (transport);
318 _dbus_transport_set_connection (transport, connection);
324 if (connection != NULL)
325 dbus_free (connection);
328 _dbus_hash_table_unref (handler_table);
331 _dbus_watch_list_free (watch_list);
337 * Used to notify a connection when a DBusMessageHandler is
338 * destroyed, so the connection can drop any reference
341 * @param connection the connection
342 * @param handler the handler
345 _dbus_connection_handler_destroyed (DBusConnection *connection,
346 DBusMessageHandler *handler)
351 _dbus_hash_iter_init (connection->handler_table, &iter);
352 while (_dbus_hash_iter_next (&iter))
354 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
357 _dbus_hash_iter_remove_entry (&iter);
360 link = _dbus_list_get_first_link (&connection->filter_list);
363 DBusMessageHandler *h = link->data;
364 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
367 _dbus_list_remove_link (&connection->filter_list,
377 * @addtogroup DBusConnection
383 * Opens a new connection to a remote address.
385 * @todo specify what the address parameter is. Right now
386 * it's just the name of a UNIX domain socket. It should be
387 * something more complex that encodes which transport to use.
389 * If the open fails, the function returns #NULL, and provides
390 * a reason for the failure in the result parameter. Pass
391 * #NULL for the result parameter if you aren't interested
392 * in the reason for failure.
394 * @param address the address.
395 * @param result address where a result code can be returned.
396 * @returns new connection, or #NULL on failure.
399 dbus_connection_open (const char *address,
400 DBusResultCode *result)
402 DBusConnection *connection;
403 DBusTransport *transport;
405 transport = _dbus_transport_open (address, result);
406 if (transport == NULL)
409 connection = _dbus_connection_new_for_transport (transport);
411 _dbus_transport_unref (transport);
413 if (connection == NULL)
415 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
423 * Increments the reference count of a DBusConnection.
425 * @param connection the connection.
428 dbus_connection_ref (DBusConnection *connection)
430 connection->refcount += 1;
434 * Decrements the reference count of a DBusConnection, and finalizes
435 * it if the count reaches zero. If a connection is still connected
436 * when it's finalized, it will be disconnected (that is, associated
437 * file handles will be closed).
439 * @param connection the connection.
442 dbus_connection_unref (DBusConnection *connection)
444 _dbus_assert (connection != NULL);
445 _dbus_assert (connection->refcount > 0);
447 connection->refcount -= 1;
448 if (connection->refcount == 0)
453 /* free error data as a side effect */
454 dbus_connection_set_error_function (connection,
457 _dbus_watch_list_free (connection->watches);
458 connection->watches = NULL;
460 _dbus_hash_iter_init (connection->handler_table, &iter);
461 while (_dbus_hash_iter_next (&iter))
463 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
465 _dbus_message_handler_remove_connection (h, connection);
468 link = _dbus_list_get_first_link (&connection->filter_list);
471 DBusMessageHandler *h = link->data;
472 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
474 _dbus_message_handler_remove_connection (h, connection);
479 _dbus_hash_table_unref (connection->handler_table);
480 connection->handler_table = NULL;
482 _dbus_list_clear (&connection->filter_list);
484 _dbus_list_foreach (&connection->outgoing_messages,
485 (DBusForeachFunction) dbus_message_unref,
487 _dbus_list_clear (&connection->outgoing_messages);
489 _dbus_list_foreach (&connection->incoming_messages,
490 (DBusForeachFunction) dbus_message_unref,
492 _dbus_list_clear (&connection->incoming_messages);
494 _dbus_transport_unref (connection->transport);
496 dbus_free (connection);
501 * Closes the connection, so no further data can be sent or received.
502 * Any further attempts to send data will result in errors. This
503 * function does not affect the connection's reference count. It's
504 * safe to disconnect a connection more than once; all calls after the
505 * first do nothing. It's impossible to "reconnect" a connection, a
506 * new connection must be created.
508 * @param connection the connection.
511 dbus_connection_disconnect (DBusConnection *connection)
513 _dbus_transport_disconnect (connection->transport);
517 * Gets whether the connection is currently connected. All
518 * connections are connected when they are opened. A connection may
519 * become disconnected when the remote application closes its end, or
520 * exits; a connection may also be disconnected with
521 * dbus_connection_disconnect().
523 * @param connection the connection.
524 * @returns #TRUE if the connection is still alive.
527 dbus_connection_get_is_connected (DBusConnection *connection)
529 return _dbus_transport_get_is_connected (connection->transport);
533 * Adds a message to the outgoing message queue. Does not block to
534 * write the message to the network; that happens asynchronously. to
535 * force the message to be written, call dbus_connection_flush().
537 * If the function fails, it returns #FALSE and returns the
538 * reason for failure via the result parameter.
539 * The result parameter can be #NULL if you aren't interested
540 * in the reason for the failure.
542 * @param connection the connection.
543 * @param message the message to write.
544 * @param result address where result code can be placed.
545 * @returns #TRUE on success.
548 dbus_connection_send_message (DBusConnection *connection,
549 DBusMessage *message,
550 DBusResultCode *result)
552 if (!_dbus_list_prepend (&connection->outgoing_messages,
555 set_result_handled (connection, result, DBUS_RESULT_NO_MEMORY);
559 dbus_message_ref (message);
560 connection->n_outgoing += 1;
562 _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
563 message, connection->n_outgoing);
565 _dbus_message_lock (message);
567 if (connection->n_outgoing == 1)
568 _dbus_transport_messages_pending (connection->transport,
569 connection->n_outgoing);
575 * Queues a message to send, as with dbus_connection_send_message(),
576 * but also sets up a DBusMessageHandler to receive a reply to the
577 * message. If no reply is received in the given timeout_milliseconds,
578 * expires the pending reply and sends the DBusMessageHandler a
579 * synthetic error reply (generated in-process, not by the remote
580 * application) indicating that a timeout occurred.
582 * Reply handlers see their replies after message filters see them,
583 * but before message handlers added with
584 * dbus_connection_register_handler() see them, regardless of the
585 * reply message's name. Reply handlers are only handed a single
586 * message as a reply, after a reply has been seen the handler is
587 * removed. If a filter filters out the reply before the handler sees
588 * it, the handler is not removed but the timeout will immediately
589 * fire again. If a filter was dumb and kept removing the timeout
590 * reply then we'd get in an infinite loop.
592 * If #NULL is passed for the reply_handler, the timeout reply will
593 * still be generated and placed into the message queue, but no
594 * specific message handler will receive the reply.
596 * If -1 is passed for the timeout, a sane default timeout is used. -1
597 * is typically the best value for the timeout for this reason, unless
598 * you want a very short or very long timeout. There is no way to
599 * avoid a timeout entirely, other than passing INT_MAX for the
600 * timeout to postpone it indefinitely.
602 * @param connection the connection
603 * @param message the message to send
604 * @param reply_handler message handler expecting the reply, or #NULL
605 * @param timeout_milliseconds timeout in milliseconds or -1 for default
606 * @param result return location for result code
607 * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
609 * @todo this function isn't implemented because we need message serials
610 * and other slightly more rich DBusMessage implementation in order to
611 * implement it. The basic idea will be to keep a hash of serials we're
612 * expecting a reply to, and also to add a way to tell GLib or Qt to
613 * install a timeout. Then install a timeout which is the shortest
614 * timeout of any pending reply.
616 * @todo implement non-reentrant "block for reply" variant. i.e. send
617 * a message, block until we get a reply, then pull reply out of
618 * message queue and return it, *without dispatching any handlers for
619 * any other messages* - used for non-reentrant "method calls" We can
620 * block properly for this using _dbus_connection_do_iteration().
624 dbus_connection_send_message_with_reply (DBusConnection *connection,
625 DBusMessage *message,
626 DBusMessageHandler *reply_handler,
627 int timeout_milliseconds,
628 DBusResultCode *result)
631 return dbus_connection_send_message (connection, message, result);
635 * Blocks until the outgoing message queue is empty.
637 * @param connection the connection.
640 dbus_connection_flush (DBusConnection *connection)
642 while (connection->n_outgoing > 0)
643 _dbus_connection_do_iteration (connection,
644 DBUS_ITERATION_DO_WRITING |
645 DBUS_ITERATION_BLOCK,
650 * Gets the number of messages in the incoming message queue.
652 * @param connection the connection.
653 * @returns the number of messages in the queue.
656 dbus_connection_get_n_messages (DBusConnection *connection)
658 return connection->n_incoming;
662 * Returns the first-received message from the incoming message queue,
663 * leaving it in the queue. The caller does not own a reference to the
664 * returned message. If the queue is empty, returns #NULL.
666 * @param connection the connection.
667 * @returns next message in the incoming queue.
670 dbus_connection_peek_message (DBusConnection *connection)
672 return _dbus_list_get_first (&connection->incoming_messages);
676 * Returns the first-received message from the incoming message queue,
677 * removing it from the queue. The caller owns a reference to the
678 * returned message. If the queue is empty, returns #NULL.
680 * @param connection the connection.
681 * @returns next message in the incoming queue.
684 dbus_connection_pop_message (DBusConnection *connection)
686 if (connection->n_incoming > 0)
688 DBusMessage *message;
690 message = _dbus_list_pop_first (&connection->incoming_messages);
691 connection->n_incoming -= 1;
693 _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
694 message, connection->n_incoming);
703 * Pops the first-received message from the current incoming message
704 * queue, runs any handlers for it, then unrefs the message.
706 * @param connection the connection
707 * @returns #TRUE if the queue is not empty after dispatch
709 * @todo this function is not properly robust against reentrancy,
710 * that is, if handlers are added/removed while dispatching
711 * a message, things will get messed up.
714 dbus_connection_dispatch_message (DBusConnection *connection)
716 DBusMessage *message;
720 DBusHandlerResult result;
723 dbus_connection_ref (connection);
725 message = dbus_connection_pop_message (connection);
728 dbus_connection_unref (connection);
732 filter_serial = connection->filters_serial;
733 handler_serial = connection->handlers_serial;
735 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
737 link = _dbus_list_get_first_link (&connection->filter_list);
740 DBusMessageHandler *handler = link->data;
741 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
743 result = _dbus_message_handler_handle_message (handler, connection,
746 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
749 if (filter_serial != connection->filters_serial)
751 _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
758 name = dbus_message_get_name (message);
761 DBusMessageHandler *handler;
763 handler = _dbus_hash_table_lookup_string (connection->handler_table,
768 result = _dbus_message_handler_handle_message (handler, connection,
771 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
774 if (handler_serial != connection->handlers_serial)
776 _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
783 dbus_connection_unref (connection);
784 dbus_message_unref (message);
786 return connection->n_incoming > 0;
790 * Sets the error handler function for the connection.
792 * @param connection the connection.
793 * @param error_function the error handler.
794 * @param data data to pass to the error handler.
795 * @param free_data_function function to be called to free the data.
798 dbus_connection_set_error_function (DBusConnection *connection,
799 DBusConnectionErrorFunction error_function,
801 DBusFreeFunction free_data_function)
803 if (connection->error_free_data_function != NULL)
804 (* connection->error_free_data_function) (connection->error_data);
806 connection->error_function = error_function;
807 connection->error_data = data;
808 connection->error_free_data_function = free_data_function;
812 * Sets the watch functions for the connection. These functions are
813 * responsible for making the application's main loop aware of file
814 * descriptors that need to be monitored for events, using select() or
815 * poll(). When using Qt, typically the DBusAddWatchFunction would
816 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
817 * could call g_io_add_watch(), or could be used as part of a more
820 * The DBusWatch can be queried for the file descriptor to watch using
821 * dbus_watch_get_fd(), and for the events to watch for using
822 * dbus_watch_get_flags(). The flags returned by
823 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
824 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
825 * all watches implicitly include a watch for hangups, errors, and
826 * other exceptional conditions.
828 * Once a file descriptor becomes readable or writable, or an exception
829 * occurs, dbus_connection_handle_watch() should be called to
830 * notify the connection of the file descriptor's condition.
832 * dbus_connection_handle_watch() cannot be called during the
833 * DBusAddWatchFunction, as the connection will not be ready to handle
836 * It is not allowed to reference a DBusWatch after it has been passed
837 * to remove_function.
839 * @param connection the connection.
840 * @param add_function function to begin monitoring a new descriptor.
841 * @param remove_function function to stop monitoring a descriptor.
842 * @param data data to pass to add_function and remove_function.
843 * @param free_data_function function to be called to free the data.
846 dbus_connection_set_watch_functions (DBusConnection *connection,
847 DBusAddWatchFunction add_function,
848 DBusRemoveWatchFunction remove_function,
850 DBusFreeFunction free_data_function)
852 /* ref connection for slightly better reentrancy */
853 dbus_connection_ref (connection);
855 _dbus_watch_list_set_functions (connection->watches,
856 add_function, remove_function,
857 data, free_data_function);
859 /* drop our paranoid refcount */
860 dbus_connection_unref (connection);
864 * Called to notify the connection when a previously-added watch
865 * is ready for reading or writing, or has an exception such
868 * @param connection the connection.
869 * @param watch the watch.
870 * @param condition the current condition of the file descriptors being watched.
873 dbus_connection_handle_watch (DBusConnection *connection,
875 unsigned int condition)
877 _dbus_transport_handle_watch (connection->transport,
882 * Adds a message filter. Filters are handlers that are run on
883 * all incoming messages, prior to the normal handlers
884 * registered with dbus_connection_register_handler().
885 * Filters are run in the order that they were added.
886 * The same handler can be added as a filter more than once, in
887 * which case it will be run more than once.
889 * @param connection the connection
890 * @param handler the handler
891 * @returns #TRUE on success, #FALSE if not enough memory.
894 dbus_connection_add_filter (DBusConnection *connection,
895 DBusMessageHandler *handler)
897 if (!_dbus_message_handler_add_connection (handler, connection))
900 if (!_dbus_list_append (&connection->filter_list,
903 _dbus_message_handler_remove_connection (handler, connection);
907 connection->filters_serial += 1;
913 * Removes a previously-added message filter. It is a programming
914 * error to call this function for a handler that has not
915 * been added as a filter. If the given handler was added
916 * more than once, only one instance of it will be removed
917 * (the most recently-added instance).
919 * @param connection the connection
920 * @param handler the handler to remove
924 dbus_connection_remove_filter (DBusConnection *connection,
925 DBusMessageHandler *handler)
927 if (!_dbus_list_remove_last (&connection->filter_list, handler))
929 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
933 _dbus_message_handler_remove_connection (handler, connection);
935 connection->filters_serial += 1;
939 * Registers a handler for a list of message names. A single handler
940 * can be registered for any number of message names, but each message
941 * name can only have one handler at a time. It's not allowed to call
942 * this function with the name of a message that already has a
943 * handler. If the function returns #FALSE, the handlers were not
944 * registered due to lack of memory.
946 * @param connection the connection
947 * @param handler the handler
948 * @param messages_to_handle the messages to handle
949 * @param n_messages the number of message names in messages_to_handle
950 * @returns #TRUE on success, #FALSE if no memory or another handler already exists
954 dbus_connection_register_handler (DBusConnection *connection,
955 DBusMessageHandler *handler,
956 const char **messages_to_handle,
962 while (i < n_messages)
967 key = _dbus_strdup (messages_to_handle[i]);
971 if (!_dbus_hash_iter_lookup (connection->handler_table,
979 if (_dbus_hash_iter_get_value (&iter) != NULL)
981 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
982 messages_to_handle[i]);
983 dbus_free (key); /* won't have replaced the old key with the new one */
987 if (!_dbus_message_handler_add_connection (handler, connection))
989 _dbus_hash_iter_remove_entry (&iter);
990 /* key has freed on nuking the entry */
994 _dbus_hash_iter_set_value (&iter, handler);
996 connection->handlers_serial += 1;
1004 /* unregister everything registered so far,
1005 * so we don't fail partially
1007 dbus_connection_unregister_handler (connection,
1016 * Unregisters a handler for a list of message names. The handlers
1017 * must have been previously registered.
1019 * @param connection the connection
1020 * @param handler the handler
1021 * @param messages_to_handle the messages to handle
1022 * @param n_messages the number of message names in messages_to_handle
1026 dbus_connection_unregister_handler (DBusConnection *connection,
1027 DBusMessageHandler *handler,
1028 const char **messages_to_handle,
1034 while (i < n_messages)
1038 if (!_dbus_hash_iter_lookup (connection->handler_table,
1039 (char*) messages_to_handle[i], FALSE,
1042 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1043 messages_to_handle[i]);
1045 else if (_dbus_hash_iter_get_value (&iter) != handler)
1047 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1048 messages_to_handle[i]);
1052 _dbus_hash_iter_remove_entry (&iter);
1053 _dbus_message_handler_remove_connection (handler, connection);
1059 connection->handlers_serial += 1;