changeset 103:f7bec3f7181d

Change connection creation API to specify incoming buffer size.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 21 Jun 2014 05:11:06 +0300
parents 6ca407bfbeaf
children ec5a5e573885
files th_network.c th_network.h
diffstat 2 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/th_network.c	Sat Jun 21 02:41:29 2014 +0300
+++ b/th_network.c	Sat Jun 21 05:11:06 2014 +0300
@@ -109,7 +109,8 @@
 
 th_conn_t * th_conn_new(
     void (*errfunc)(th_conn_t *conn, int err, const char *msg),
-    void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg))
+    void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg),
+    ssize_t bufsize)
 {
     th_conn_t *conn = th_malloc0(sizeof(th_conn_t));
 
@@ -119,6 +120,13 @@
     conn->errfunc = errfunc;
     conn->msgfunc = msgfunc;
 
+    conn->bufsize = (bufsize <= 0) ? TH_CONNBUF_SIZE : bufsize;
+    if ((conn->buf = th_malloc(conn->bufsize)) == NULL)
+    {
+        th_free(conn);
+        return NULL;
+    }
+
     return conn;
 }
 
@@ -693,7 +701,7 @@
     }
     else if (FD_ISSET(conn->socket, &tmpfds))
     {
-        conn->got_bytes = recv(conn->socket, conn->in_ptr, TH_CONNBUF_SIZE - conn->total_bytes, 0);
+        conn->got_bytes = recv(conn->socket, conn->in_ptr, conn->bufsize - conn->total_bytes, 0);
         if (conn->got_bytes < 0)
         {
             int err = th_errno_to_error(th_get_socket_errno());
--- a/th_network.h	Sat Jun 21 02:41:29 2014 +0300
+++ b/th_network.h	Sat Jun 21 05:11:06 2014 +0300
@@ -109,9 +109,8 @@
     int status;
 
     // Data buffer
-    char buf[TH_CONNBUF_SIZE + 16];
-    char *ptr, *in_ptr;
-    ssize_t got_bytes, total_bytes;
+    char *buf, *ptr, *in_ptr;
+    ssize_t bufsize, got_bytes, total_bytes;
 
     void *node;
 } th_conn_t;
@@ -123,7 +122,8 @@
 struct hostent *th_resolve_host(th_conn_t *conn, const char *name);
 th_conn_t * th_conn_new(
     void (*errfunc)(th_conn_t *conn, int err, const char *msg),
-    void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg));
+    void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg),
+    ssize_t bufsize);
 
 void        th_conn_err(th_conn_t *conn, int err, const char *fmt, ...);
 void        th_conn_msg(th_conn_t *conn, int loglevel, const char *fmt, ...);