src/line_proto.c
changeset 12 4147fae232d9
parent 11 14e79683c48c
child 13 ca16f3a8f3b7
equal deleted inserted replaced
11:14e79683c48c 12:4147fae232d9
   152 {
   152 {
   153     // offset to recv() new data into, offset to _parse_line hint, offset to next line from _parse_line
   153     // offset to recv() new data into, offset to _parse_line hint, offset to next line from _parse_line
   154     size_t recv_offset = 0, peek_offset = 0, next_offset = 0;
   154     size_t recv_offset = 0, peek_offset = 0, next_offset = 0;
   155     int ret;
   155     int ret;
   156 
   156 
   157     // adjust offset from previous data
   157     // adjust offset to beyond previous data (as will be moved next)
   158     recv_offset = lp->tail_len;
   158     recv_offset = lp->tail_len;
   159 
   159 
   160     // move trailing data from previous line to front of buffer
   160     // move trailing data from previous line to front of buffer
   161     if (lp->tail_offset) {
   161     if (lp->tail_offset) {
   162         // move to front
   162         // move to front
   181         // ensure there's enough space for the rest of the line
   181         // ensure there's enough space for the rest of the line
   182         // XXX: this must be an error, not an assert
   182         // XXX: this must be an error, not an assert
   183         assert(recv_offset < lp->buf_len);
   183         assert(recv_offset < lp->buf_len);
   184         
   184         
   185         // otherwise, read more data
   185         // otherwise, read more data
   186         if ((ret = sock_stream_read(lp->sock, lp->buf + recv_offset, lp->buf_len - recv_offset)) < 0) {
   186         if ((ret = sock_stream_read(lp->sock, lp->buf + recv_offset, lp->buf_len - recv_offset)) < 0)
   187             // we can special-case EAGAIN, as it's expected
   187             // store and return NULL on errors
   188             if (MATCH_ERROR(sock_stream_error(lp->sock), ERR_READ, EAGAIN)) {
   188             RETURN_SET_ERROR_INFO(&lp->err, sock_stream_error(lp->sock));
   189                 // return a NULL *line_ptr
   189 
   190                 *line_ptr = NULL;
   190         // EAGAIN?
   191                 break;
   191         if (ret == 0) {
   192 
   192             // return a NULL *line_ptr
   193             } else {
   193             *line_ptr = NULL;
   194                 // store and return NULL on errors
   194             break;
   195                 RETURN_SET_ERROR_INFO(&lp->err, sock_stream_error(lp->sock));
       
   196 
       
   197             }
       
   198         }
   195         }
   199 
       
   200         // EOF?
       
   201         if (ret == 0)
       
   202             return SET_ERROR(&lp->err, ERR_READ_EOF);
       
   203         
   196         
   204         // update recv_offset
   197         // update recv_offset
   205         recv_offset += ret;
   198         recv_offset += ret;
   206 
   199 
   207     } while (1);
   200     } while (1);