comparison th_network.c @ 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
comparison
equal deleted inserted replaced
102:6ca407bfbeaf 103:f7bec3f7181d
107 } 107 }
108 108
109 109
110 th_conn_t * th_conn_new( 110 th_conn_t * th_conn_new(
111 void (*errfunc)(th_conn_t *conn, int err, const char *msg), 111 void (*errfunc)(th_conn_t *conn, int err, const char *msg),
112 void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg)) 112 void (*msgfunc)(th_conn_t *conn, int loglevel, const char *msg),
113 ssize_t bufsize)
113 { 114 {
114 th_conn_t *conn = th_malloc0(sizeof(th_conn_t)); 115 th_conn_t *conn = th_malloc0(sizeof(th_conn_t));
115 116
116 if (conn == NULL) 117 if (conn == NULL)
117 return NULL; 118 return NULL;
118 119
119 conn->errfunc = errfunc; 120 conn->errfunc = errfunc;
120 conn->msgfunc = msgfunc; 121 conn->msgfunc = msgfunc;
122
123 conn->bufsize = (bufsize <= 0) ? TH_CONNBUF_SIZE : bufsize;
124 if ((conn->buf = th_malloc(conn->bufsize)) == NULL)
125 {
126 th_free(conn);
127 return NULL;
128 }
121 129
122 return conn; 130 return conn;
123 } 131 }
124 132
125 133
691 return TH_CONN_ERROR; 699 return TH_CONN_ERROR;
692 } 700 }
693 } 701 }
694 else if (FD_ISSET(conn->socket, &tmpfds)) 702 else if (FD_ISSET(conn->socket, &tmpfds))
695 { 703 {
696 conn->got_bytes = recv(conn->socket, conn->in_ptr, TH_CONNBUF_SIZE - conn->total_bytes, 0); 704 conn->got_bytes = recv(conn->socket, conn->in_ptr, conn->bufsize - conn->total_bytes, 0);
697 if (conn->got_bytes < 0) 705 if (conn->got_bytes < 0)
698 { 706 {
699 int err = th_errno_to_error(th_get_socket_errno()); 707 int err = th_errno_to_error(th_get_socket_errno());
700 th_conn_err(conn, err, "Error in recv: %s\n", th_error_str(err)); 708 th_conn_err(conn, err, "Error in recv: %s\n", th_error_str(err));
701 return TH_CONN_ERROR; 709 return TH_CONN_ERROR;