100 err_t line_proto_create (struct line_proto **lp_ptr, transport_t *transport, size_t buf_size, |
100 err_t line_proto_create (struct line_proto **lp_ptr, transport_t *transport, size_t buf_size, |
101 const struct line_proto_callbacks *callbacks, void *cb_arg, error_t *err) |
101 const struct line_proto_callbacks *callbacks, void *cb_arg, error_t *err) |
102 { |
102 { |
103 struct line_proto *lp; |
103 struct line_proto *lp; |
104 |
104 |
|
105 // alloc |
|
106 if ((lp = calloc(1, sizeof(*lp))) == NULL) |
|
107 return SET_ERROR(err, ERR_CALLOC); |
|
108 |
105 // store |
109 // store |
106 lp->transport = transport; |
110 lp->transport = transport; |
107 lp->buf_len = buf_size; |
111 lp->buf_len = buf_size; |
108 lp->callbacks = *callbacks; |
112 lp->callbacks = *callbacks; |
109 lp->cb_arg = cb_arg; |
113 lp->cb_arg = cb_arg; |
110 |
114 |
111 // allocate struct and buffers |
115 // allocate buffers |
112 if ( |
116 if ( |
113 (lp = calloc(1, sizeof(*lp))) == NULL |
117 (lp->in_buf = malloc(buf_size)) == NULL |
114 || (lp->in_buf = malloc(buf_size)) == NULL |
|
115 || (lp->out_buf = malloc(buf_size)) == NULL |
118 || (lp->out_buf = malloc(buf_size)) == NULL |
116 ) |
119 ) |
117 JUMP_SET_ERROR(err, ERR_CALLOC); |
120 JUMP_SET_ERROR(err, ERR_CALLOC); |
118 |
121 |
119 // setup the transport |
122 // setup the transport |
279 int line_proto_flush (struct line_proto *lp) |
281 int line_proto_flush (struct line_proto *lp) |
280 { |
282 { |
281 int ret; |
283 int ret; |
282 size_t ret_len; |
284 size_t ret_len; |
283 |
285 |
|
286 assert(lp->out_offset); |
|
287 |
284 // try and write the line |
288 // try and write the line |
285 if ((ret = transport_write(lp->transport, lp->out_buf, lp->out_offset, &lp->err)) < 0) |
289 if ((ret = transport_write(lp->transport, lp->out_buf, lp->out_offset, &lp->err)) < 0) |
286 return -ERROR_CODE(&lp->err); |
290 return -ERROR_CODE(&lp->err); |
287 |
291 |
288 ret_len = ret; |
292 ret_len = ret; |