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