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