updates.
[silc.git] / lib / silccore / silcsockconn.c
index 3262cfc4153f63b81b6d4ddc049a3e6f8b3693a1..5111980b8bb2e6a447554ea75276f5bce3c9716a 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.3  2001/02/11 14:09:34  priikone
- *     Code auditing weekend results and fixes committing.
- *
- * Revision 1.2  2000/07/05 06:06:35  priikone
- *     Global cosmetic change.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 
+/* Heartbeat context */
+struct SilcSocketConnectionHB {
+  uint32 heartbeat;
+  SilcSocketConnectionHBCb hb_callback;
+  void *hb_context;
+  void *timeout_queue;
+  SilcTask hb_task;
+  SilcSocketConnection sock;
+};
+
 /* Allocates a new socket connection object. The allocated object is 
    returned to the new_socket argument. */
 
@@ -43,7 +40,7 @@ void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
   SILC_LOG_DEBUG(("Allocating new socket connection object"));
 
   /* Set the pointers. Incoming and outgoing data buffers
-     are allocated by the server when they are first used. */
+     are allocated by the application when they are first used. */
   *new_socket = silc_calloc(1, sizeof(**new_socket));
   (*new_socket)->sock = sock;
   (*new_socket)->type = type;
@@ -52,15 +49,89 @@ void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
   (*new_socket)->flags = 0;
   (*new_socket)->inbuf = NULL;
   (*new_socket)->outbuf = NULL;
+  (*new_socket)->users++;
 }
 
 /* Free's the Socket connection object. */
 
 void silc_socket_free(SilcSocketConnection sock)
 {
-  if (sock) {
+  sock->users--;
+  SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users + 1,
+                 sock->users));
+  if (sock->users < 1) {
     silc_buffer_free(sock->inbuf);
     silc_buffer_free(sock->outbuf);
+    if (sock->hb) {
+      silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
+      silc_free(sock->hb->hb_context);
+      silc_free(sock->hb);
+    }
+
+    memset(sock, 'F', sizeof(*sock));
     silc_free(sock);
   }
 }
+
+/* Increase the reference counter. */
+
+SilcSocketConnection silc_socket_dup(SilcSocketConnection sock)
+{
+  sock->users++;
+  SILC_LOG_DEBUG(("Socket %p refcnt %d->%d", sock, sock->users - 1,
+                 sock->users));
+  return sock;
+}
+
+/* Internal timeout callback to perform heartbeat */
+
+SILC_TASK_CALLBACK(silc_socket_heartbeat)
+{
+  SilcSocketConnectionHB hb = (SilcSocketConnectionHB)context;
+
+  if (!hb->heartbeat)
+    return;
+
+  if (hb->hb_callback)
+    hb->hb_callback(hb->sock, hb->hb_context);
+
+  hb->hb_task = silc_task_register(hb->timeout_queue, hb->sock->sock, 
+                                  silc_socket_heartbeat,
+                                  context, hb->heartbeat, 0,
+                                  SILC_TASK_TIMEOUT,
+                                  SILC_TASK_PRI_LOW);
+}
+
+/* Sets the heartbeat timeout and prepares the socket for performing
+   heartbeat in `heartbeat' intervals (seconds). The `hb_context' is
+   allocated by the application and will be sent as argument to the
+   `hb_callback' function that is called when the `heartbeat' timeout
+   expires.  The callback `hb_context' won't be touched by the library
+   but will be freed automatically when calling silc_socket_free.  The
+   `timeout_queue' is the application's scheduler timeout queue. */
+
+void silc_socket_set_heartbeat(SilcSocketConnection sock, 
+                              uint32 heartbeat,
+                              void *hb_context,
+                              SilcSocketConnectionHBCb hb_callback,
+                              void *timeout_queue)
+{
+
+  if (sock->hb) {
+    silc_task_unregister(sock->hb->timeout_queue, sock->hb->hb_task);
+    silc_free(sock->hb->hb_context);
+    silc_free(sock->hb);
+  }
+
+  sock->hb = silc_calloc(1, sizeof(*sock->hb));
+  sock->hb->heartbeat = heartbeat;
+  sock->hb->hb_context = hb_context;
+  sock->hb->hb_callback = hb_callback;
+  sock->hb->timeout_queue = timeout_queue;
+  sock->hb->sock = sock;
+  sock->hb->hb_task = silc_task_register(timeout_queue, sock->sock,
+                                         silc_socket_heartbeat,
+                                         (void *)sock->hb, heartbeat, 0,
+                                         SILC_TASK_TIMEOUT,
+                                         SILC_TASK_PRI_LOW);
+}