src/line_proto.c
changeset 20 d9c4c2980a0d
parent 19 8c80580ccde9
child 21 0911d0b828d4
equal deleted inserted replaced
19:8c80580ccde9 20:d9c4c2980a0d
    93     return SUCCESS;
    93     return SUCCESS;
    94 }
    94 }
    95 
    95 
    96 struct sock_stream_callbacks line_proto_sock_stream_callbacks = {
    96 struct sock_stream_callbacks line_proto_sock_stream_callbacks = {
    97     .on_read    = &line_proto_on_read,
    97     .on_read    = &line_proto_on_read,
       
    98     .on_write   = &line_proto_on_write,
    98 };
    99 };
    99 
   100 
   100 err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size,
   101 err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size,
   101         line_proto_read_cb cb_func, void *cb_arg, struct error_info *err)
   102         line_proto_read_cb cb_func, void *cb_arg, struct error_info *err)
   102 {
   103 {
   119     // initialize event-based stuff
   120     // initialize event-based stuff
   120     if (
   121     if (
   121             sock_stream_event_init(sock, &line_proto_sock_stream_callbacks, lp)
   122             sock_stream_event_init(sock, &line_proto_sock_stream_callbacks, lp)
   122         ||  line_proto_schedule_events(lp, EV_READ)
   123         ||  line_proto_schedule_events(lp, EV_READ)
   123     )
   124     )
   124         return ERROR_CODE(&lp->err);
   125         JUMP_SET_ERROR_INFO(err, &lp->err);
   125 
   126 
   126     // return
   127     // return
   127     *lp_ptr = lp;
   128     *lp_ptr = lp;
   128 
   129 
   129     return SUCCESS;
   130     return SUCCESS;
   130 
   131 
   131 error:
   132 error:
   132     if (lp) {
   133     if (lp) {
   133         free(lp->in_buf);
   134         free(lp->in_buf);
   134         free(lp->out_buf);
   135         free(lp->out_buf);
       
   136 
       
   137         // XXX: handle sock init errors
   135     }
   138     }
   136 
   139 
   137     free(lp);
   140     free(lp);
       
   141 
       
   142     return ERROR_CODE(err);
   138 }
   143 }
   139 
   144 
   140 /*
   145 /*
   141  * This looks for a full '\r\n' terminated line at the beginning of the given buffer. If found, the \r\n will be
   146  * This looks for a full '\r\n' terminated line at the beginning of the given buffer. If found, the \r\n will be
   142  * replaced with a '\0', and the offset to the beginning of the next line returned. If not found, zero is returned
   147  * replaced with a '\0', and the offset to the beginning of the next line returned. If not found, zero is returned