2003-01-26 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-connection.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.c DBusConnection object
3  *
4  * Copyright (C) 2002, 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
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"
34
35 /**
36  * @defgroup DBusConnection DBusConnection
37  * @ingroup  DBus
38  * @brief Connection to another application
39  *
40  * A DBusConnection represents a connection to another
41  * application. Messages can be sent and received via this connection.
42  *
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.
48  *
49  * In brief a DBusConnection is a message queue associated with some
50  * message transport mechanism such as a socket.
51  * 
52  */
53
54 /**
55  * @defgroup DBusConnectionInternals DBusConnection implementation details
56  * @ingroup  DBusInternals
57  * @brief Implementation details of DBusConnection
58  *
59  * @{
60  */
61
62 /** default timeout value when waiting for a message reply */
63 #define DEFAULT_TIMEOUT_VALUE (15 * 1000)
64
65 /** Opaque typedef for DBusDataSlot */
66 typedef struct DBusDataSlot DBusDataSlot;
67 /** DBusDataSlot is used to store application data on the connection */
68 struct DBusDataSlot
69 {
70   void *data;                      /**< The application data */
71   DBusFreeFunction free_data_func; /**< Free the application data */
72 };
73
74 /**
75  * Implementation details of DBusConnection. All fields are private.
76  */
77 struct DBusConnection
78 {
79   int refcount; /**< Reference count. */
80
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. */
83
84   int n_outgoing;              /**< Length of outgoing queue. */
85   int n_incoming;              /**< Length of incoming queue. */
86   
87   DBusTransport *transport;    /**< Object that sends/receives messages over network. */
88   DBusWatchList *watches;      /**< Stores active watches. */
89   DBusTimeoutList *timeouts;   /**< Stores active timeouts. */
90   
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. */
100
101   DBusCounter *connection_counter; /**< Counter that we decrement when finalized */
102   
103   int client_serial;            /**< Client serial. Increments each time a message is sent  */
104   unsigned int disconnect_notified : 1; /**< Already called disconnect_function */
105 };
106
107 static void _dbus_connection_free_data_slots (DBusConnection *connection);
108
109 /**
110  * Adds a message to the incoming message queue, returning #FALSE
111  * if there's insufficient memory to queue the message.
112  *
113  * @param connection the connection.
114  * @param message the message to queue.
115  * @returns #TRUE on success.
116  */
117 dbus_bool_t
118 _dbus_connection_queue_received_message (DBusConnection *connection,
119                                          DBusMessage    *message)
120 {
121   _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
122   
123   if (!_dbus_list_append (&connection->incoming_messages,
124                           message))
125     return FALSE;
126   
127   dbus_message_ref (message);
128   connection->n_incoming += 1;
129
130   _dbus_verbose ("Incoming message %p added to queue, %d incoming\n",
131                  message, connection->n_incoming);
132   
133   return TRUE;
134 }
135
136 /**
137  * Checks whether there are messages in the outgoing message queue.
138  *
139  * @param connection the connection.
140  * @returns #TRUE if the outgoing queue is non-empty.
141  */
142 dbus_bool_t
143 _dbus_connection_have_messages_to_send (DBusConnection *connection)
144 {
145   return connection->outgoing_messages != NULL;
146 }
147
148 /**
149  * Gets the next outgoing message. The message remains in the
150  * queue, and the caller does not own a reference to it.
151  *
152  * @param connection the connection.
153  * @returns the message to be sent.
154  */ 
155 DBusMessage*
156 _dbus_connection_get_message_to_send (DBusConnection *connection)
157 {
158   return _dbus_list_get_last (&connection->outgoing_messages);
159 }
160
161 /**
162  * Notifies the connection that a message has been sent, so the
163  * message can be removed from the outgoing queue.
164  *
165  * @param connection the connection.
166  * @param message the message that was sent.
167  */
168 void
169 _dbus_connection_message_sent (DBusConnection *connection,
170                                DBusMessage    *message)
171 {
172   _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
173   _dbus_assert (message == _dbus_list_get_last (&connection->outgoing_messages));
174   
175   _dbus_list_pop_last (&connection->outgoing_messages);
176   dbus_message_unref (message);
177   
178   connection->n_outgoing -= 1;
179
180   _dbus_verbose ("Message %p removed from outgoing queue, %d left to send\n",
181                  message, connection->n_outgoing);
182   
183   if (connection->n_outgoing == 0)
184     _dbus_transport_messages_pending (connection->transport,
185                                       connection->n_outgoing);  
186 }
187
188 /**
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.
193  *
194  * @param connection the connection.
195  * @param watch the watch to add.
196  * @returns #TRUE on success.
197  */
198 dbus_bool_t
199 _dbus_connection_add_watch (DBusConnection *connection,
200                             DBusWatch      *watch)
201 {
202   if (connection->watches) /* null during finalize */
203     return _dbus_watch_list_add_watch (connection->watches,
204                                        watch);
205   else
206     return FALSE;
207 }
208
209 /**
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.
213  *
214  * @param connection the connection.
215  * @param watch the watch to remove.
216  */
217 void
218 _dbus_connection_remove_watch (DBusConnection *connection,
219                                DBusWatch      *watch)
220 {
221   if (connection->watches) /* null during finalize */
222     _dbus_watch_list_remove_watch (connection->watches,
223                                    watch);
224 }
225
226 /**
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.
230  *
231  * @param connection the connection
232  */
233 void
234 _dbus_connection_notify_disconnected (DBusConnection *connection)
235 {
236   if (connection->disconnect_function != NULL &&
237       !connection->disconnect_notified)
238     {
239       connection->disconnect_notified = TRUE;
240       dbus_connection_ref (connection);
241       (* connection->disconnect_function) (connection,
242                                            connection->disconnect_data);
243       dbus_connection_unref (connection);
244     }
245 }
246
247 /**
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.
252  *
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.
256  *
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.
260  *
261  * The timeout_milliseconds parameter does nothing unless the
262  * iteration is blocking.
263  *
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.
268  * 
269  * @param connection the connection.
270  * @param flags iteration flags.
271  * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
272  */
273 void
274 _dbus_connection_do_iteration (DBusConnection *connection,
275                                unsigned int    flags,
276                                int             timeout_milliseconds)
277 {
278   if (connection->n_outgoing == 0)
279     flags &= ~DBUS_ITERATION_DO_WRITING;
280   
281   _dbus_transport_do_iteration (connection->transport,
282                                 flags, timeout_milliseconds);
283 }
284
285 /**
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.
290  *
291  * @param transport the transport.
292  * @returns the new connection, or #NULL on failure.
293  */
294 DBusConnection*
295 _dbus_connection_new_for_transport (DBusTransport *transport)
296 {
297   DBusConnection *connection;
298   DBusWatchList *watch_list;
299   DBusTimeoutList *timeout_list;
300   DBusHashTable *handler_table;
301   
302   watch_list = NULL;
303   connection = NULL;
304   handler_table = NULL;
305   timeout_list = NULL;
306   
307   watch_list = _dbus_watch_list_new ();
308   if (watch_list == NULL)
309     goto error;
310
311   timeout_list = _dbus_timeout_list_new ();
312   if (timeout_list == NULL)
313     goto error;
314   
315   handler_table =
316     _dbus_hash_table_new (DBUS_HASH_STRING,
317                           dbus_free, NULL);
318   if (handler_table == NULL)
319     goto error;
320   
321   connection = dbus_new0 (DBusConnection, 1);
322   if (connection == NULL)
323     goto error;
324   
325   connection->refcount = 1;
326   connection->transport = transport;
327   connection->watches = watch_list;
328   connection->timeouts = timeout_list;
329   connection->handler_table = handler_table;
330   connection->filter_list = NULL;
331
332   connection->data_slots = NULL;
333   connection->n_slots = 0;
334   connection->client_serial = 1;
335   connection->disconnect_notified = FALSE;
336   
337   _dbus_transport_ref (transport);
338   _dbus_transport_set_connection (transport, connection);
339   
340   return connection;
341   
342  error:
343
344   if (connection != NULL)
345     dbus_free (connection);
346
347   if (handler_table)
348     _dbus_hash_table_unref (handler_table);
349   
350   if (watch_list)
351     _dbus_watch_list_free (watch_list);
352
353   if (timeout_list)
354     _dbus_timeout_list_free (timeout_list);
355   
356   return NULL;
357 }
358
359 static dbus_int32_t
360 _dbus_connection_get_next_client_serial (DBusConnection *connection)
361 {
362   int serial;
363
364   serial = connection->client_serial++;
365
366   if (connection->client_serial < 0)
367     connection->client_serial = 1;
368   
369   return serial;
370 }
371
372 /**
373  * Used to notify a connection when a DBusMessageHandler is
374  * destroyed, so the connection can drop any reference
375  * to the handler.
376  *
377  * @param connection the connection
378  * @param handler the handler
379  */
380 void
381 _dbus_connection_handler_destroyed (DBusConnection     *connection,
382                                     DBusMessageHandler *handler)
383 {
384   DBusHashIter iter;
385   DBusList *link;
386
387   _dbus_hash_iter_init (connection->handler_table, &iter);
388   while (_dbus_hash_iter_next (&iter))
389     {
390       DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
391
392       if (h == handler)
393         _dbus_hash_iter_remove_entry (&iter);
394     }
395
396   link = _dbus_list_get_first_link (&connection->filter_list);
397   while (link != NULL)
398     {
399       DBusMessageHandler *h = link->data;
400       DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
401
402       if (h == handler)
403         _dbus_list_remove_link (&connection->filter_list,
404                                 link);
405       
406       link = next;
407     }
408 }
409
410 /**
411  * Adds the counter used to count the number of open connections.
412  * Increments the counter by one, and saves it to be decremented
413  * again when this connection is finalized.
414  *
415  * @param connection a #DBusConnection
416  * @param counter counter that tracks number of connections
417  */
418 void
419 _dbus_connection_set_connection_counter (DBusConnection *connection,
420                                          DBusCounter    *counter)
421 {
422   _dbus_assert (connection->connection_counter == NULL);
423   
424   connection->connection_counter = counter;
425   _dbus_counter_ref (connection->connection_counter);
426   _dbus_counter_adjust (connection->connection_counter, 1);
427 }
428
429 /** @} */
430
431 /**
432  * @addtogroup DBusConnection
433  *
434  * @{
435  */
436
437 /**
438  * Opens a new connection to a remote address.
439  *
440  * @todo specify what the address parameter is. Right now
441  * it's just the name of a UNIX domain socket. It should be
442  * something more complex that encodes which transport to use.
443  *
444  * If the open fails, the function returns #NULL, and provides
445  * a reason for the failure in the result parameter. Pass
446  * #NULL for the result parameter if you aren't interested
447  * in the reason for failure.
448  * 
449  * @param address the address.
450  * @param result address where a result code can be returned.
451  * @returns new connection, or #NULL on failure.
452  */
453 DBusConnection*
454 dbus_connection_open (const char     *address,
455                       DBusResultCode *result)
456 {
457   DBusConnection *connection;
458   DBusTransport *transport;
459   
460   transport = _dbus_transport_open (address, result);
461   if (transport == NULL)
462     return NULL;
463   
464   connection = _dbus_connection_new_for_transport (transport);
465
466   _dbus_transport_unref (transport);
467   
468   if (connection == NULL)
469     {
470       dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
471       return NULL;
472     }
473   
474   return connection;
475 }
476
477 /**
478  * Increments the reference count of a DBusConnection.
479  *
480  * @param connection the connection.
481  */
482 void
483 dbus_connection_ref (DBusConnection *connection)
484 {
485   connection->refcount += 1;
486 }
487
488 /**
489  * Decrements the reference count of a DBusConnection, and finalizes
490  * it if the count reaches zero.  If a connection is still connected
491  * when it's finalized, it will be disconnected (that is, associated
492  * file handles will be closed).
493  *
494  * @param connection the connection.
495  */
496 void
497 dbus_connection_unref (DBusConnection *connection)
498 {
499   _dbus_assert (connection != NULL);
500   _dbus_assert (connection->refcount > 0);
501   
502   connection->refcount -= 1;
503   if (connection->refcount == 0)
504     {
505       DBusHashIter iter;
506       DBusList *link;
507
508       dbus_connection_disconnect (connection);
509       
510       /* free disconnect data as a side effect */
511       dbus_connection_set_disconnect_function (connection,
512                                                NULL, NULL, NULL);
513
514       if (connection->connection_counter != NULL)
515         {
516           /* subtract ourselves from the counter */
517           _dbus_counter_adjust (connection->connection_counter, - 1);
518           _dbus_counter_unref (connection->connection_counter);
519           connection->connection_counter = NULL;
520         }
521       
522       _dbus_watch_list_free (connection->watches);
523       connection->watches = NULL;
524
525       _dbus_timeout_list_free (connection->timeouts);
526       connection->timeouts = NULL;
527       
528       _dbus_connection_free_data_slots (connection);
529       
530       _dbus_hash_iter_init (connection->handler_table, &iter);
531       while (_dbus_hash_iter_next (&iter))
532         {
533           DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
534           
535           _dbus_message_handler_remove_connection (h, connection);
536         }
537
538       link = _dbus_list_get_first_link (&connection->filter_list);
539       while (link != NULL)
540         {
541           DBusMessageHandler *h = link->data;
542           DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
543           
544           _dbus_message_handler_remove_connection (h, connection);
545           
546           link = next;
547         }
548       
549       _dbus_hash_table_unref (connection->handler_table);
550       connection->handler_table = NULL;
551
552       _dbus_list_clear (&connection->filter_list);
553       
554       _dbus_list_foreach (&connection->outgoing_messages,
555                           (DBusForeachFunction) dbus_message_unref,
556                           NULL);
557       _dbus_list_clear (&connection->outgoing_messages);
558
559       _dbus_list_foreach (&connection->incoming_messages,
560                           (DBusForeachFunction) dbus_message_unref,
561                           NULL);
562       _dbus_list_clear (&connection->incoming_messages);
563       
564       _dbus_transport_unref (connection->transport);
565       
566       dbus_free (connection);
567     }
568 }
569
570 /**
571  * Closes the connection, so no further data can be sent or received.
572  * Any further attempts to send data will result in errors.  This
573  * function does not affect the connection's reference count.  It's
574  * safe to disconnect a connection more than once; all calls after the
575  * first do nothing. It's impossible to "reconnect" a connection, a
576  * new connection must be created.
577  *
578  * @param connection the connection.
579  */
580 void
581 dbus_connection_disconnect (DBusConnection *connection)
582 {
583   _dbus_transport_disconnect (connection->transport);
584 }
585
586 /**
587  * Gets whether the connection is currently connected.  All
588  * connections are connected when they are opened.  A connection may
589  * become disconnected when the remote application closes its end, or
590  * exits; a connection may also be disconnected with
591  * dbus_connection_disconnect().
592  *
593  * @param connection the connection.
594  * @returns #TRUE if the connection is still alive.
595  */
596 dbus_bool_t
597 dbus_connection_get_is_connected (DBusConnection *connection)
598 {
599   return _dbus_transport_get_is_connected (connection->transport);
600 }
601
602 /**
603  * Gets whether the connection was authenticated. (Note that
604  * if the connection was authenticated then disconnected,
605  * this function still returns #TRUE)
606  *
607  * @param connection the connection
608  * @returns #TRUE if the connection was ever authenticated
609  */
610 dbus_bool_t
611 dbus_connection_get_is_authenticated (DBusConnection *connection)
612 {
613   return _dbus_transport_get_is_authenticated (connection->transport);
614 }
615
616 /**
617  * Adds a message to the outgoing message queue. Does not block to
618  * write the message to the network; that happens asynchronously. to
619  * force the message to be written, call dbus_connection_flush().
620  *
621  * If the function fails, it returns #FALSE and returns the
622  * reason for failure via the result parameter.
623  * The result parameter can be #NULL if you aren't interested
624  * in the reason for the failure.
625  * 
626  * @param connection the connection.
627  * @param message the message to write.
628  * @param client_serial return location for client serial.
629  * @param result address where result code can be placed.
630  * @returns #TRUE on success.
631  */
632 dbus_bool_t
633 dbus_connection_send_message (DBusConnection *connection,
634                               DBusMessage    *message,
635                               dbus_int32_t   *client_serial,                          
636                               DBusResultCode *result)
637
638 {
639   dbus_int32_t serial;
640   
641   if (!_dbus_list_prepend (&connection->outgoing_messages,
642                            message))
643     {
644       dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
645       return FALSE;
646     }
647
648   dbus_message_ref (message);
649   connection->n_outgoing += 1;
650
651   _dbus_verbose ("Message %p added to outgoing queue, %d pending to send\n",
652                  message, connection->n_outgoing);
653
654   if (_dbus_message_get_client_serial (message) == -1)
655     {
656       serial = _dbus_connection_get_next_client_serial (connection);
657       _dbus_message_set_client_serial (message, serial);
658     }
659   
660   if (client_serial)
661     *client_serial = _dbus_message_get_client_serial (message);
662   
663   _dbus_message_lock (message);
664   
665   if (connection->n_outgoing == 1)
666     _dbus_transport_messages_pending (connection->transport,
667                                       connection->n_outgoing);
668
669   return TRUE;
670 }
671
672 /**
673  * Queues a message to send, as with dbus_connection_send_message(),
674  * but also sets up a DBusMessageHandler to receive a reply to the
675  * message. If no reply is received in the given timeout_milliseconds,
676  * expires the pending reply and sends the DBusMessageHandler a
677  * synthetic error reply (generated in-process, not by the remote
678  * application) indicating that a timeout occurred.
679  *
680  * Reply handlers see their replies after message filters see them,
681  * but before message handlers added with
682  * dbus_connection_register_handler() see them, regardless of the
683  * reply message's name. Reply handlers are only handed a single
684  * message as a reply, after a reply has been seen the handler is
685  * removed. If a filter filters out the reply before the handler sees
686  * it, the handler is not removed but the timeout will immediately
687  * fire again. If a filter was dumb and kept removing the timeout
688  * reply then we'd get in an infinite loop.
689  * 
690  * If #NULL is passed for the reply_handler, the timeout reply will
691  * still be generated and placed into the message queue, but no
692  * specific message handler will receive the reply.
693  *
694  * If -1 is passed for the timeout, a sane default timeout is used. -1
695  * is typically the best value for the timeout for this reason, unless
696  * you want a very short or very long timeout.  There is no way to
697  * avoid a timeout entirely, other than passing INT_MAX for the
698  * timeout to postpone it indefinitely.
699  * 
700  * @param connection the connection
701  * @param message the message to send
702  * @param reply_handler message handler expecting the reply, or #NULL
703  * @param timeout_milliseconds timeout in milliseconds or -1 for default
704  * @param result return location for result code
705  * @returns #TRUE if the message is successfully queued, #FALSE if no memory.
706  *
707  * @todo this function isn't implemented because we need message serials
708  * and other slightly more rich DBusMessage implementation in order to
709  * implement it. The basic idea will be to keep a hash of serials we're
710  * expecting a reply to, and also to add a way to tell GLib or Qt to
711  * install a timeout. Then install a timeout which is the shortest
712  * timeout of any pending reply.
713  *
714  */
715 dbus_bool_t
716 dbus_connection_send_message_with_reply (DBusConnection     *connection,
717                                          DBusMessage        *message,
718                                          DBusMessageHandler *reply_handler,
719                                          int                 timeout_milliseconds,
720                                          DBusResultCode     *result)
721 {
722   /* FIXME */
723   return dbus_connection_send_message (connection, message, NULL, result);
724 }
725
726 /**
727  * Sends a message and blocks a certain time period while waiting for a reply.
728  * This function does not dispatch any message handlers until the main loop
729  * has been reached. This function is used to do non-reentrant "method calls."
730  * If a reply is received, it is returned, and removed from the incoming
731  * message queue. If it is not received, #NULL is returned and the
732  * result is set to #DBUS_RESULT_NO_REPLY. If something else goes
733  * wrong, result is set to whatever is appropriate, such as
734  * #DBUS_RESULT_NO_MEMORY.
735  *
736  * @todo I believe if we get EINTR or otherwise interrupt the
737  * do_iteration call in here, we won't block the required length of
738  * time. I think there probably has to be a loop: "while (!timeout_elapsed)
739  * { check_for_reply_in_queue(); iterate_with_remaining_timeout(); }"
740  *
741  * @param connection the connection
742  * @param message the message to send
743  * @param timeout_milliseconds timeout in milliseconds or -1 for default
744  * @param result return location for result code
745  * @returns the message that is the reply or #NULL with an error code if the
746  * function fails.
747  */
748 DBusMessage *
749 dbus_connection_send_message_with_reply_and_block (DBusConnection     *connection,
750                                                    DBusMessage        *message,
751                                                    int                 timeout_milliseconds,
752                                                    DBusResultCode     *result)
753 {
754   dbus_int32_t client_serial;
755   DBusList *link;
756
757   if (timeout_milliseconds == -1)
758     timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
759   
760   if (!dbus_connection_send_message (connection, message, &client_serial, result))
761     return NULL;
762
763   /* Flush message queue */
764   dbus_connection_flush (connection);
765   
766   /* Now we wait... */
767   _dbus_connection_do_iteration (connection,
768                                  DBUS_ITERATION_DO_READING |
769                                  DBUS_ITERATION_BLOCK,
770                                  timeout_milliseconds);
771
772   /* Check if we've gotten a reply */
773   link = _dbus_list_get_first_link (&connection->incoming_messages);
774
775   while (link != NULL)
776     {
777       DBusMessage *reply = link->data;
778
779       if (_dbus_message_get_reply_serial (reply) == client_serial)
780         {
781           _dbus_list_remove (&connection->incoming_messages, link);
782           dbus_message_ref (message);
783
784           if (result)
785             *result = DBUS_RESULT_SUCCESS;
786           
787           return reply;
788         }
789       link = _dbus_list_get_next_link (&connection->incoming_messages, link);
790     }
791
792   if (result)
793     *result = DBUS_RESULT_NO_REPLY;
794   
795   return NULL;
796 }
797
798 /**
799  * Blocks until the outgoing message queue is empty.
800  *
801  * @param connection the connection.
802  */
803 void
804 dbus_connection_flush (DBusConnection *connection)
805 {
806   while (connection->n_outgoing > 0)
807     _dbus_connection_do_iteration (connection,
808                                    DBUS_ITERATION_DO_WRITING |
809                                    DBUS_ITERATION_BLOCK,
810                                    -1);
811 }
812
813 /**
814  * Gets the number of messages in the incoming message queue.
815  *
816  * @param connection the connection.
817  * @returns the number of messages in the queue.
818  */
819 int
820 dbus_connection_get_n_messages (DBusConnection *connection)
821 {
822   return connection->n_incoming;
823 }
824
825 /**
826  * Returns the first-received message from the incoming message queue,
827  * leaving it in the queue. The caller does not own a reference to the
828  * returned message. If the queue is empty, returns #NULL.
829  *
830  * @param connection the connection.
831  * @returns next message in the incoming queue.
832  */
833 DBusMessage*
834 dbus_connection_peek_message  (DBusConnection *connection)
835 {
836   return _dbus_list_get_first (&connection->incoming_messages);
837 }
838
839 /**
840  * Returns the first-received message from the incoming message queue,
841  * removing it from the queue. The caller owns a reference to the
842  * returned message. If the queue is empty, returns #NULL.
843  *
844  * @param connection the connection.
845  * @returns next message in the incoming queue.
846  */
847 DBusMessage*
848 dbus_connection_pop_message (DBusConnection *connection)
849 {
850   if (connection->n_incoming > 0)
851     {
852       DBusMessage *message;
853
854       message = _dbus_list_pop_first (&connection->incoming_messages);
855       connection->n_incoming -= 1;
856
857       _dbus_verbose ("Incoming message %p removed from queue, %d incoming\n",
858                      message, connection->n_incoming);
859
860       return message;
861     }
862   else
863     return NULL;
864 }
865
866 /**
867  * Pops the first-received message from the current incoming message
868  * queue, runs any handlers for it, then unrefs the message.
869  *
870  * @param connection the connection
871  * @returns #TRUE if the queue is not empty after dispatch
872  *
873  * @todo this function is not properly robust against reentrancy,
874  * that is, if handlers are added/removed while dispatching
875  * a message, things will get messed up.
876  */
877 dbus_bool_t
878 dbus_connection_dispatch_message (DBusConnection *connection)
879 {
880   DBusMessage *message;
881   int filter_serial;
882   int handler_serial;
883   DBusList *link;
884   DBusHandlerResult result;
885   const char *name;
886   
887   dbus_connection_ref (connection);
888   
889   message = dbus_connection_pop_message (connection);
890   if (message == NULL)
891     {
892       dbus_connection_unref (connection);
893       return FALSE;
894     }
895
896   filter_serial = connection->filters_serial;
897   handler_serial = connection->handlers_serial;
898
899   result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
900   
901   link = _dbus_list_get_first_link (&connection->filter_list);
902   while (link != NULL)
903     {
904       DBusMessageHandler *handler = link->data;
905       DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
906       
907       result = _dbus_message_handler_handle_message (handler, connection,
908                                                      message);
909
910       if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
911         goto out;
912
913       if (filter_serial != connection->filters_serial)
914         {
915           _dbus_warn ("Message filters added or removed while dispatching filters - not currently supported!\n");
916           goto out;
917         }
918       
919       link = next;
920     }
921
922   name = dbus_message_get_name (message);
923   if (name != NULL)
924     {
925       DBusMessageHandler *handler;
926       
927       handler = _dbus_hash_table_lookup_string (connection->handler_table,
928                                                 name);
929       if (handler != NULL)
930         {
931
932           result = _dbus_message_handler_handle_message (handler, connection,
933                                                          message);
934       
935           if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
936             goto out;
937           
938           if (handler_serial != connection->handlers_serial)
939             {
940               _dbus_warn ("Message handlers added or removed while dispatching handlers - not currently supported!\n");
941               goto out;
942             }
943         }
944     }
945
946  out:
947   dbus_connection_unref (connection);
948   dbus_message_unref (message);
949   
950   return connection->n_incoming > 0;
951 }
952
953 /**
954  * Sets the disconnect handler function for the connection.
955  * Will be called exactly once, when the connection is
956  * disconnected.
957  * 
958  * @param connection the connection.
959  * @param disconnect_function the disconnect handler.
960  * @param data data to pass to the disconnect handler.
961  * @param free_data_function function to be called to free the data.
962  */
963 void
964 dbus_connection_set_disconnect_function  (DBusConnection              *connection,
965                                           DBusDisconnectFunction       disconnect_function,
966                                           void                        *data,
967                                           DBusFreeFunction             free_data_function)
968 {
969   if (connection->disconnect_free_data_function != NULL)
970     (* connection->disconnect_free_data_function) (connection->disconnect_data);
971
972   connection->disconnect_function = disconnect_function;
973   connection->disconnect_data = data;
974   connection->disconnect_free_data_function = free_data_function;
975 }
976
977 /**
978  * Sets the watch functions for the connection. These functions are
979  * responsible for making the application's main loop aware of file
980  * descriptors that need to be monitored for events, using select() or
981  * poll(). When using Qt, typically the DBusAddWatchFunction would
982  * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction
983  * could call g_io_add_watch(), or could be used as part of a more
984  * elaborate GSource.
985  *
986  * The DBusWatch can be queried for the file descriptor to watch using
987  * dbus_watch_get_fd(), and for the events to watch for using
988  * dbus_watch_get_flags(). The flags returned by
989  * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and
990  * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR;
991  * all watches implicitly include a watch for hangups, errors, and
992  * other exceptional conditions.
993  *
994  * Once a file descriptor becomes readable or writable, or an exception
995  * occurs, dbus_connection_handle_watch() should be called to
996  * notify the connection of the file descriptor's condition.
997  *
998  * dbus_connection_handle_watch() cannot be called during the
999  * DBusAddWatchFunction, as the connection will not be ready to handle
1000  * that watch yet.
1001  * 
1002  * It is not allowed to reference a DBusWatch after it has been passed
1003  * to remove_function.
1004  * 
1005  * @param connection the connection.
1006  * @param add_function function to begin monitoring a new descriptor.
1007  * @param remove_function function to stop monitoring a descriptor.
1008  * @param data data to pass to add_function and remove_function.
1009  * @param free_data_function function to be called to free the data.
1010  */
1011 void
1012 dbus_connection_set_watch_functions (DBusConnection              *connection,
1013                                      DBusAddWatchFunction         add_function,
1014                                      DBusRemoveWatchFunction      remove_function,
1015                                      void                        *data,
1016                                      DBusFreeFunction             free_data_function)
1017 {
1018   /* ref connection for slightly better reentrancy */
1019   dbus_connection_ref (connection);
1020   
1021   _dbus_watch_list_set_functions (connection->watches,
1022                                   add_function, remove_function,
1023                                   data, free_data_function);
1024   
1025   /* drop our paranoid refcount */
1026   dbus_connection_unref (connection);
1027 }
1028
1029 /**
1030  * Sets the timeout functions for the connection. These functions are
1031  * responsible for making the application's main loop aware of timeouts.
1032  * When using Qt, typically the DBusAddTimeoutFunction would create a
1033  * QTimer. When using GLib, the DBusAddTimeoutFunction would call
1034  * g_timeout_add.
1035  *
1036  * The DBusTimeout can be queried for the timer interval using
1037  * dbus_timeout_get_interval.
1038  *
1039  * Once a timeout occurs, dbus_timeout_handle should be call to invoke
1040  * the timeout's callback.
1041  */
1042 void
1043 dbus_connection_set_timeout_functions   (DBusConnection            *connection,
1044                                          DBusAddTimeoutFunction     add_function,
1045                                          DBusRemoveTimeoutFunction  remove_function,
1046                                          void                      *data,
1047                                          DBusFreeFunction           free_data_function)
1048 {
1049   /* ref connection for slightly better reentrancy */
1050   dbus_connection_ref (connection);
1051   
1052   _dbus_timeout_list_set_functions (connection->timeouts,
1053                                     add_function, remove_function,
1054                                     data, free_data_function);
1055   
1056   /* drop our paranoid refcount */
1057   dbus_connection_unref (connection);  
1058 }
1059
1060 /**
1061  * Called to notify the connection when a previously-added watch
1062  * is ready for reading or writing, or has an exception such
1063  * as a hangup.
1064  *
1065  * @param connection the connection.
1066  * @param watch the watch.
1067  * @param condition the current condition of the file descriptors being watched.
1068  */
1069 void
1070 dbus_connection_handle_watch (DBusConnection              *connection,
1071                               DBusWatch                   *watch,
1072                               unsigned int                 condition)
1073 {
1074   _dbus_transport_handle_watch (connection->transport,
1075                                 watch, condition);
1076 }
1077
1078 /**
1079  * Adds a message filter. Filters are handlers that are run on
1080  * all incoming messages, prior to the normal handlers
1081  * registered with dbus_connection_register_handler().
1082  * Filters are run in the order that they were added.
1083  * The same handler can be added as a filter more than once, in
1084  * which case it will be run more than once.
1085  *
1086  * @param connection the connection
1087  * @param handler the handler
1088  * @returns #TRUE on success, #FALSE if not enough memory.
1089  */
1090 dbus_bool_t
1091 dbus_connection_add_filter (DBusConnection      *connection,
1092                             DBusMessageHandler  *handler)
1093 {
1094   if (!_dbus_message_handler_add_connection (handler, connection))
1095     return FALSE;
1096
1097   if (!_dbus_list_append (&connection->filter_list,
1098                           handler))
1099     {
1100       _dbus_message_handler_remove_connection (handler, connection);
1101       return FALSE;
1102     }
1103
1104   connection->filters_serial += 1;
1105   
1106   return TRUE;
1107 }
1108
1109 /**
1110  * Removes a previously-added message filter. It is a programming
1111  * error to call this function for a handler that has not
1112  * been added as a filter. If the given handler was added
1113  * more than once, only one instance of it will be removed
1114  * (the most recently-added instance).
1115  *
1116  * @param connection the connection
1117  * @param handler the handler to remove
1118  *
1119  */
1120 void
1121 dbus_connection_remove_filter (DBusConnection      *connection,
1122                                DBusMessageHandler  *handler)
1123 {
1124   if (!_dbus_list_remove_last (&connection->filter_list, handler))
1125     {
1126       _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
1127       return;
1128     }
1129
1130   _dbus_message_handler_remove_connection (handler, connection);
1131
1132   connection->filters_serial += 1;
1133 }
1134
1135 /**
1136  * Registers a handler for a list of message names. A single handler
1137  * can be registered for any number of message names, but each message
1138  * name can only have one handler at a time. It's not allowed to call
1139  * this function with the name of a message that already has a
1140  * handler. If the function returns #FALSE, the handlers were not
1141  * registered due to lack of memory.
1142  * 
1143  * @param connection the connection
1144  * @param handler the handler
1145  * @param messages_to_handle the messages to handle
1146  * @param n_messages the number of message names in messages_to_handle
1147  * @returns #TRUE on success, #FALSE if no memory or another handler already exists
1148  * 
1149  **/
1150 dbus_bool_t
1151 dbus_connection_register_handler (DBusConnection     *connection,
1152                                   DBusMessageHandler *handler,
1153                                   const char        **messages_to_handle,
1154                                   int                 n_messages)
1155 {
1156   int i;
1157
1158   i = 0;
1159   while (i < n_messages)
1160     {
1161       DBusHashIter iter;
1162       char *key;
1163
1164       key = _dbus_strdup (messages_to_handle[i]);
1165       if (key == NULL)
1166         goto failed;
1167       
1168       if (!_dbus_hash_iter_lookup (connection->handler_table,
1169                                    key, TRUE,
1170                                    &iter))
1171         {
1172           dbus_free (key);
1173           goto failed;
1174         }
1175
1176       if (_dbus_hash_iter_get_value (&iter) != NULL)
1177         {
1178           _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
1179                       messages_to_handle[i]);
1180           dbus_free (key); /* won't have replaced the old key with the new one */
1181           goto failed;
1182         }
1183
1184       if (!_dbus_message_handler_add_connection (handler, connection))
1185         {
1186           _dbus_hash_iter_remove_entry (&iter);
1187           /* key has freed on nuking the entry */
1188           goto failed;
1189         }
1190       
1191       _dbus_hash_iter_set_value (&iter, handler);
1192
1193       connection->handlers_serial += 1;
1194       
1195       ++i;
1196     }
1197   
1198   return TRUE;
1199   
1200  failed:
1201   /* unregister everything registered so far,
1202    * so we don't fail partially
1203    */
1204   dbus_connection_unregister_handler (connection,
1205                                       handler,
1206                                       messages_to_handle,
1207                                       i);
1208
1209   return FALSE;
1210 }
1211
1212 /**
1213  * Unregisters a handler for a list of message names. The handlers
1214  * must have been previously registered.
1215  *
1216  * @param connection the connection
1217  * @param handler the handler
1218  * @param messages_to_handle the messages to handle
1219  * @param n_messages the number of message names in messages_to_handle
1220  * 
1221  **/
1222 void
1223 dbus_connection_unregister_handler (DBusConnection     *connection,
1224                                     DBusMessageHandler *handler,
1225                                     const char        **messages_to_handle,
1226                                     int                 n_messages)
1227 {
1228   int i;
1229
1230   i = 0;
1231   while (i < n_messages)
1232     {
1233       DBusHashIter iter;
1234
1235       if (!_dbus_hash_iter_lookup (connection->handler_table,
1236                                    (char*) messages_to_handle[i], FALSE,
1237                                    &iter))
1238         {
1239           _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
1240                       messages_to_handle[i]);
1241         }
1242       else if (_dbus_hash_iter_get_value (&iter) != handler)
1243         {
1244           _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
1245                       messages_to_handle[i]);
1246         }
1247       else
1248         {
1249           _dbus_hash_iter_remove_entry (&iter);
1250           _dbus_message_handler_remove_connection (handler, connection);
1251         }
1252
1253       ++i;
1254     }
1255
1256   connection->handlers_serial += 1;
1257 }
1258
1259 static int *allocated_slots = NULL;
1260 static int  n_allocated_slots = 0;
1261 static int  n_used_slots = 0;
1262 static DBusStaticMutex allocated_slots_lock = DBUS_STATIC_MUTEX_INIT;
1263
1264 /**
1265  * Allocates an integer ID to be used for storing application-specific
1266  * data on any DBusConnection. The allocated ID may then be used
1267  * with dbus_connection_set_data() and dbus_connection_get_data().
1268  * If allocation fails, -1 is returned.
1269  *
1270  * @returns -1 on failure, otherwise the data slot ID
1271  */
1272 int
1273 dbus_connection_allocate_data_slot (void)
1274 {
1275   int slot;
1276   
1277   if (!dbus_static_mutex_lock (&allocated_slots_lock))
1278     return -1;
1279
1280   if (n_used_slots < n_allocated_slots)
1281     {
1282       slot = 0;
1283       while (slot < n_allocated_slots)
1284         {
1285           if (allocated_slots[slot] < 0)
1286             {
1287               allocated_slots[slot] = slot;
1288               n_used_slots += 1;
1289               break;
1290             }
1291           ++slot;
1292         }
1293
1294       _dbus_assert (slot < n_allocated_slots);
1295     }
1296   else
1297     {
1298       int *tmp;
1299       
1300       slot = -1;
1301       tmp = dbus_realloc (allocated_slots,
1302                           sizeof (int) * (n_allocated_slots + 1));
1303       if (tmp == NULL)
1304         goto out;
1305
1306       allocated_slots = tmp;
1307       slot = n_allocated_slots;
1308       n_allocated_slots += 1;
1309       n_used_slots += 1;
1310       allocated_slots[slot] = slot;
1311     }
1312
1313   _dbus_assert (slot >= 0);
1314   _dbus_assert (slot < n_allocated_slots);
1315   
1316  out:
1317   dbus_static_mutex_unlock (&allocated_slots_lock);
1318   return slot;
1319 }
1320
1321 /**
1322  * Deallocates a global ID for connection data slots.
1323  * dbus_connection_get_data() and dbus_connection_set_data()
1324  * may no longer be used with this slot.
1325  * Existing data stored on existing DBusConnection objects
1326  * will be freed when the connection is finalized,
1327  * but may not be retrieved (and may only be replaced
1328  * if someone else reallocates the slot).
1329  *
1330  * @param slot the slot to deallocate
1331  */
1332 void
1333 dbus_connection_free_data_slot (int slot)
1334 {
1335   dbus_static_mutex_lock (&allocated_slots_lock);
1336
1337   _dbus_assert (slot < n_allocated_slots);
1338   _dbus_assert (allocated_slots[slot] == slot);
1339   
1340   allocated_slots[slot] = -1;
1341   n_used_slots -= 1;
1342
1343   if (n_used_slots == 0)
1344     {
1345       dbus_free (allocated_slots);
1346       allocated_slots = NULL;
1347       n_allocated_slots = 0;
1348     }
1349   
1350   dbus_static_mutex_unlock (&allocated_slots_lock);
1351 }
1352
1353 /**
1354  * Stores a pointer on a DBusConnection, along
1355  * with an optional function to be used for freeing
1356  * the data when the data is set again, or when
1357  * the connection is finalized. The slot number
1358  * must have been allocated with dbus_connection_allocate_data_slot().
1359  *
1360  * @param connection the connection
1361  * @param slot the slot number
1362  * @param data the data to store
1363  * @param free_data_func finalizer function for the data
1364  * @returns #TRUE if there was enough memory to store the data
1365  */
1366 dbus_bool_t
1367 dbus_connection_set_data (DBusConnection   *connection,
1368                           int               slot,
1369                           void             *data,
1370                           DBusFreeFunction  free_data_func)
1371 {
1372   _dbus_assert (slot < n_allocated_slots);
1373   _dbus_assert (allocated_slots[slot] == slot);
1374   
1375   if (slot >= connection->n_slots)
1376     {
1377       DBusDataSlot *tmp;
1378       int i;
1379       
1380       tmp = dbus_realloc (connection->data_slots,
1381                           sizeof (DBusDataSlot) * (slot + 1));
1382       if (tmp == NULL)
1383         return FALSE;
1384       
1385       connection->data_slots = tmp;
1386       i = connection->n_slots;
1387       connection->n_slots = slot + 1;
1388       while (i < connection->n_slots)
1389         {
1390           connection->data_slots[i].data = NULL;
1391           connection->data_slots[i].free_data_func = NULL;
1392           ++i;
1393         }
1394     }
1395
1396   _dbus_assert (slot < connection->n_slots);
1397   
1398   if (connection->data_slots[slot].free_data_func)
1399     (* connection->data_slots[slot].free_data_func) (connection->data_slots[slot].data);
1400
1401   connection->data_slots[slot].data = data;
1402   connection->data_slots[slot].free_data_func = free_data_func;
1403
1404   return TRUE;
1405 }
1406
1407 /**
1408  * Retrieves data previously set with dbus_connection_set_data().
1409  * The slot must still be allocated (must not have been freed).
1410  *
1411  * @param connection the connection
1412  * @param slot the slot to get data from
1413  * @returns the data, or #NULL if not found
1414  */
1415 void*
1416 dbus_connection_get_data (DBusConnection   *connection,
1417                           int               slot)
1418 {
1419   _dbus_assert (slot < n_allocated_slots);
1420   _dbus_assert (allocated_slots[slot] == slot);
1421
1422   if (slot >= connection->n_slots)
1423     return NULL;
1424
1425   return connection->data_slots[slot].data;
1426 }
1427
1428 static void
1429 _dbus_connection_free_data_slots (DBusConnection *connection)
1430 {
1431   int i;
1432
1433   i = 0;
1434   while (i < connection->n_slots)
1435     {
1436       if (connection->data_slots[i].free_data_func)
1437         (* connection->data_slots[i].free_data_func) (connection->data_slots[i].data);
1438       connection->data_slots[i].data = NULL;
1439       connection->data_slots[i].free_data_func = NULL;
1440       ++i;
1441     }
1442
1443   dbus_free (connection->data_slots);
1444   connection->data_slots = NULL;
1445   connection->n_slots = 0;
1446 }
1447
1448 /**
1449  * Specifies the maximum size message this connection is allowed to
1450  * receive. Larger messages will result in disconnecting the
1451  * connection.
1452  * 
1453  * @param connection a #DBusConnection
1454  * @param size maximum message size the connection can receive, in bytes
1455  */
1456 void
1457 dbus_connection_set_max_message_size (DBusConnection *connection,
1458                                       long            size)
1459 {
1460   _dbus_transport_set_max_message_size (connection->transport,
1461                                         size);
1462 }
1463
1464 /**
1465  * Gets the value set by dbus_connection_set_max_message_size().
1466  *
1467  * @param connection the connection
1468  * @returns the max size of a single message
1469  */
1470 long
1471 dbus_connection_get_max_message_size (DBusConnection *connection)
1472 {
1473   return _dbus_transport_get_max_message_size (connection->transport);
1474 }
1475
1476 /**
1477  * Sets the maximum total number of bytes that can be used for all messages
1478  * received on this connection. Messages count toward the maximum until
1479  * they are finalized. When the maximum is reached, the connection will
1480  * not read more data until some messages are finalized.
1481  *
1482  * The semantics of the maximum are: if outstanding messages are
1483  * already above the maximum, additional messages will not be read.
1484  * The semantics are not: if the next message would cause us to exceed
1485  * the maximum, we don't read it. The reason is that we don't know the
1486  * size of a message until after we read it.
1487  *
1488  * Thus, the max live messages size can actually be exceeded
1489  * by up to the maximum size of a single message.
1490  * 
1491  * Also, if we read say 1024 bytes off the wire in a single read(),
1492  * and that contains a half-dozen small messages, we may exceed the
1493  * size max by that amount. But this should be inconsequential.
1494  *
1495  * @param connection the connection
1496  * @param size the maximum size in bytes of all outstanding messages
1497  */
1498 void
1499 dbus_connection_set_max_live_messages_size (DBusConnection *connection,
1500                                             long            size)
1501 {
1502   _dbus_transport_set_max_live_messages_size (connection->transport,
1503                                               size);
1504 }
1505
1506 /**
1507  * Gets the value set by dbus_connection_set_max_live_messages_size().
1508  *
1509  * @param connection the connection
1510  * @returns the max size of all live messages
1511  */
1512 long
1513 dbus_connection_get_max_live_messages_size (DBusConnection *connection)
1514 {
1515   return _dbus_transport_get_max_live_messages_size (connection->transport);
1516 }
1517
1518 /** @} */