diff libnnchat.h @ 168:2e4850ece456

Partially re-factor connection handling.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 14 Nov 2010 22:08:08 +0200
parents 741e45592522
children 7ba4e371e9a6
line wrap: on
line diff
--- a/libnnchat.h	Sat Nov 06 18:00:22 2010 +0200
+++ b/libnnchat.h	Sun Nov 14 22:08:08 2010 +0200
@@ -21,9 +21,11 @@
 #include <errno.h>
 #include "th_string.h"
 
-#define SET_BUFSIZE     (4096)
-#define SET_ALLOC_SIZE  (128)
-
+#define NN_TMPBUF_SIZE    (4096)
+#define NN_ALLOC_SIZE     (128)
+#define NN_CONNBUF_SIZE   (64 * 1024)
+#define NN_NUM_BUCKETS    (256)
+#define NN_DELAY_USEC     (15 * 250)
 
 typedef struct {
     char **data;
@@ -44,25 +46,46 @@
 } nn_user_t;
 
 
-#define NN_NUM_BUCKETS (256)
 
 typedef struct {
     nn_user_t *buckets[NN_NUM_BUCKETS];
 } nn_userhash_t;
 
+enum {
+    NN_CONN_UNINIT = 0,
+    NN_CONN_OPEN,
+    NN_CONN_CLOSED
+};
 
-#ifdef __WIN32
-const char *hstrerror(int err);
-#endif
-int         nn_get_socket_errno(void);
-const char *nn_get_socket_errstr(int err);
+typedef struct _nn_conn_t {
+    int socket;
+    int port;
+    struct in_addr address;
+    fd_set sockfds;
+
+    void (*errfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
+    void (*msgfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
+
+    int err;
+    int status;
+    
+    char buf[NN_CONNBUF_SIZE + 16];
+    char *ptr;
+    ssize_t got;
+} nn_conn_t;
+
+
+const char *nn_get_errstr(int err);
 BOOL        nn_network_init(void);
 void        nn_network_close(void);
 
-int         nn_open_connection(struct in_addr *addr, const int port);
-void        nn_close_connection(const int sock);
-BOOL        nn_send_to_socket(const int sock, char *buf, const size_t bufLen);
-BOOL        nn_send_msg(const int sock, const char *user, const char *fmt, ...);
+nn_conn_t * nn_conn_open(struct in_addr *addr, const int port);
+void        nn_conn_close(nn_conn_t *);
+BOOL        nn_conn_pull(nn_conn_t *);
+BOOL        nn_conn_send_buf(nn_conn_t *, const char *buf, const size_t len);
+BOOL        nn_conn_send_msg(nn_conn_t *, const char *user, const char *fmt, ...);
+BOOL        nn_conn_check(nn_conn_t *);
+
 
 nn_userhash_t *nn_userhash_new(void);
 nn_user_t * nn_userhash_foreach(const nn_userhash_t *, int (*func)(const nn_user_t *));
@@ -75,6 +98,7 @@
 nn_user_t * nn_user_copy(const nn_user_t *src);
 nn_user_t * nn_user_find(const nn_userhash_t *list, const char *name);
 
+
 char *      nn_encode_str1(const char *str);
 char *      nn_decode_str1(const char *str);
 char *      nn_encode_str2(const char *str);
@@ -88,6 +112,7 @@
 void        nn_ringbuf_free(nn_ringbuf_t *buf);
 void        nn_ringbuf_add(nn_ringbuf_t *buf, const char *str);
 
+
 int         nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch);
 int         nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch);
 int         nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos);