updates. New data types.
[silc.git] / lib / silccore / silcsockconn.h
index 291a5a46e798546d964f12d42421446cc8bb98e5..f77ea997c38d2ee8e33af7f66bc1095104716f6f 100644 (file)
 #ifndef SILCSOCKCONN_H
 #define SILCSOCKCONN_H
 
+/* Forward declarations */
+typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
+typedef struct SilcSocketConnectionHB *SilcSocketConnectionHB;
+
 /* Socket types. These identifies the socket connection. */
 typedef enum {
   SILC_SOCKET_TYPE_UNKNOWN = 0,
@@ -36,6 +40,12 @@ typedef enum {
 #define SILC_SF_DISCONNECTING 3
 #define SILC_SF_DISCONNECTED 4
 
+/* Heartbeat callback function. This is the function in the application
+   that this library will call when it is time to send the keepalive
+   packet SILC_PACKET_HEARTBEAT. */
+typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
+                                        void *context);
+
 /* 
    SILC Socket Connection object.
 
@@ -70,15 +80,20 @@ typedef enum {
        Protocol object for the socket. Currently only one protocol can be
        executing at a time for a particular socket.
 
-   unsigned int flags
+   uint32 flags
 
        Socket flags that indicate the status of the socket. This can
        indicate several different status that can affect the use of the
        socket object.
 
+   int users
+
+       Reference counter. When allocated it is set to one (1) and it won't
+       be freed until it hits zero (0).
+
    char *hostname
    char *ip
-   unsigned short port
+   uint16 port
 
        Resolved hostname, IP address and port of the connection who owns
        this object.
@@ -91,30 +106,45 @@ typedef enum {
        inbuf buffer and outgoing data after encryption is put to the outbuf
        buffer.
 
+  SilcSocketConnectionHB hb
+
+       The heartbeat context.  If NULL, heartbeat is not performed.
+
 */
-typedef struct {
+struct SilcSocketConnectionStruct {
   int sock;
   SilcSocketType type;
   void *user_data;
   SilcProtocol protocol;
-  unsigned int flags;
+  uint32 flags;
+  int users;
 
   char *hostname;
   char *ip;
-  unsigned short port;
+  uint16 port;
 
   SilcBuffer inbuf;
   SilcBuffer outbuf;
-} SilcSocketConnectionObject;
 
-typedef SilcSocketConnectionObject *SilcSocketConnection;
+  SilcSocketConnectionHB hb;
+};
+
+/* Heartbeat context */
+struct SilcSocketConnectionHB {
+  uint32 heartbeat;
+  SilcSocketConnectionHBCb hb_callback;
+  void *hb_context;
+  void *timeout_queue;
+  SilcTask hb_task;
+  SilcSocketConnection sock;
+};
 
 /* Macros */
 
 /* Generic manipulation of flags */
 #define SF_SET(x, f) (x)->flags |= (1L << (f))
 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
-#define SF_IS(x, f) (x)->flags & (1L << (f))
+#define SF_IS(x, f) ((x)->flags & (1L << (f)))
 
 /* Setting/Unsetting flags */
 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
@@ -136,5 +166,11 @@ typedef SilcSocketConnectionObject *SilcSocketConnection;
 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
                       SilcSocketConnection *new_socket);
 void silc_socket_free(SilcSocketConnection sock);
+SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
+void silc_socket_set_heartbeat(SilcSocketConnection sock, 
+                              uint32 heartbeat,
+                              void *hb_context,
+                              SilcSocketConnectionHBCb hb_callback,
+                              void *timeout_queue);
 
 #endif