comparison 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
comparison
equal deleted inserted replaced
167:b0b63d164702 168:2e4850ece456
19 #include <sys/types.h> 19 #include <sys/types.h>
20 #include <time.h> 20 #include <time.h>
21 #include <errno.h> 21 #include <errno.h>
22 #include "th_string.h" 22 #include "th_string.h"
23 23
24 #define SET_BUFSIZE (4096) 24 #define NN_TMPBUF_SIZE (4096)
25 #define SET_ALLOC_SIZE (128) 25 #define NN_ALLOC_SIZE (128)
26 26 #define NN_CONNBUF_SIZE (64 * 1024)
27 #define NN_NUM_BUCKETS (256)
28 #define NN_DELAY_USEC (15 * 250)
27 29
28 typedef struct { 30 typedef struct {
29 char **data; 31 char **data;
30 size_t n, size; 32 size_t n, size;
31 } nn_ringbuf_t; 33 } nn_ringbuf_t;
42 time_t lastspoke, joined; 44 time_t lastspoke, joined;
43 struct _nn_user_t *next; 45 struct _nn_user_t *next;
44 } nn_user_t; 46 } nn_user_t;
45 47
46 48
47 #define NN_NUM_BUCKETS (256)
48 49
49 typedef struct { 50 typedef struct {
50 nn_user_t *buckets[NN_NUM_BUCKETS]; 51 nn_user_t *buckets[NN_NUM_BUCKETS];
51 } nn_userhash_t; 52 } nn_userhash_t;
52 53
54 enum {
55 NN_CONN_UNINIT = 0,
56 NN_CONN_OPEN,
57 NN_CONN_CLOSED
58 };
53 59
54 #ifdef __WIN32 60 typedef struct _nn_conn_t {
55 const char *hstrerror(int err); 61 int socket;
56 #endif 62 int port;
57 int nn_get_socket_errno(void); 63 struct in_addr address;
58 const char *nn_get_socket_errstr(int err); 64 fd_set sockfds;
65
66 void (*errfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
67 void (*msgfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
68
69 int err;
70 int status;
71
72 char buf[NN_CONNBUF_SIZE + 16];
73 char *ptr;
74 ssize_t got;
75 } nn_conn_t;
76
77
78 const char *nn_get_errstr(int err);
59 BOOL nn_network_init(void); 79 BOOL nn_network_init(void);
60 void nn_network_close(void); 80 void nn_network_close(void);
61 81
62 int nn_open_connection(struct in_addr *addr, const int port); 82 nn_conn_t * nn_conn_open(struct in_addr *addr, const int port);
63 void nn_close_connection(const int sock); 83 void nn_conn_close(nn_conn_t *);
64 BOOL nn_send_to_socket(const int sock, char *buf, const size_t bufLen); 84 BOOL nn_conn_pull(nn_conn_t *);
65 BOOL nn_send_msg(const int sock, const char *user, const char *fmt, ...); 85 BOOL nn_conn_send_buf(nn_conn_t *, const char *buf, const size_t len);
86 BOOL nn_conn_send_msg(nn_conn_t *, const char *user, const char *fmt, ...);
87 BOOL nn_conn_check(nn_conn_t *);
88
66 89
67 nn_userhash_t *nn_userhash_new(void); 90 nn_userhash_t *nn_userhash_new(void);
68 nn_user_t * nn_userhash_foreach(const nn_userhash_t *, int (*func)(const nn_user_t *)); 91 nn_user_t * nn_userhash_foreach(const nn_userhash_t *, int (*func)(const nn_user_t *));
69 nn_user_t * nn_user_match(const nn_userhash_t *list, const char *str, const char *current, BOOL again); 92 nn_user_t * nn_user_match(const nn_userhash_t *list, const char *str, const char *current, BOOL again);
70 int nn_userhash_insert(nn_userhash_t *, const char *name); 93 int nn_userhash_insert(nn_userhash_t *, const char *name);
72 void nn_userhash_free(nn_userhash_t *); 95 void nn_userhash_free(nn_userhash_t *);
73 void nn_user_free(nn_user_t *); 96 void nn_user_free(nn_user_t *);
74 void nn_user_free_list(nn_user_t *); 97 void nn_user_free_list(nn_user_t *);
75 nn_user_t * nn_user_copy(const nn_user_t *src); 98 nn_user_t * nn_user_copy(const nn_user_t *src);
76 nn_user_t * nn_user_find(const nn_userhash_t *list, const char *name); 99 nn_user_t * nn_user_find(const nn_userhash_t *list, const char *name);
100
77 101
78 char * nn_encode_str1(const char *str); 102 char * nn_encode_str1(const char *str);
79 char * nn_decode_str1(const char *str); 103 char * nn_decode_str1(const char *str);
80 char * nn_encode_str2(const char *str); 104 char * nn_encode_str2(const char *str);
81 char * nn_decode_str2(const char *str); 105 char * nn_decode_str2(const char *str);
86 110
87 nn_ringbuf_t * nn_ringbuf_new(const size_t size); 111 nn_ringbuf_t * nn_ringbuf_new(const size_t size);
88 void nn_ringbuf_free(nn_ringbuf_t *buf); 112 void nn_ringbuf_free(nn_ringbuf_t *buf);
89 void nn_ringbuf_add(nn_ringbuf_t *buf, const char *str); 113 void nn_ringbuf_add(nn_ringbuf_t *buf, const char *str);
90 114
115
91 int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch); 116 int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch);
92 int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch); 117 int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch);
93 int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos); 118 int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos);
94 void nn_editbuf_clear(nn_editbuf_t *buf); 119 void nn_editbuf_clear(nn_editbuf_t *buf);
95 nn_editbuf_t * nn_editbuf_new(ssize_t n); 120 nn_editbuf_t * nn_editbuf_new(ssize_t n);