- (*msg)[result] = '\0';
-
- DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
+ /* Process found message */
+ if(nulfound == 1) {
+ char *newbuf;
+
+ if(i == 0) {
+ /* problem */
+ }
+ *msg = strndup(*recv_buf, i);
+
+ /* Remove processed message from buffer */
+ newbuf = (char *) malloc(*recv_buf_size - (i+1));
+ memcpy(newbuf, *recv_buf + (i+1), *recv_buf_size - (i+1));
+ free(*recv_buf);
+ *recv_buf = newbuf;
+ *recv_buf_size -= (i+1);
+ *recv_buf_alloc -= (i+1);
+
+ return 1;
+ }
+
+ /* Receive a chunk from the fd */
+ if(*recv_buf_alloc - *recv_buf_size < RECV_INCREMENT) {
+ *recv_buf_alloc += RECV_INCREMENT - (*recv_buf_alloc - *recv_buf_size);
+ *recv_buf = (char *) realloc(*recv_buf, *recv_buf_alloc);
+ }
+
+ result = recv(fd, *recv_buf+*recv_buf_size, RECV_INCREMENT, 0);
+ if(result == -1) {
+ if(errno == ECONNRESET) {
+ *recv_buf_size = 0;
+ return 0;
+ }
+ /* real error */
+ PERROR("recv");
+ return -1;
+ }
+ if(result == 0) {
+ return 0;
+ }
+ *recv_buf_size += result;
+
+ /* Go back to the beginning to check if there is a full message in the buffer */
+ }