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