src/line_proto.c
changeset 32 ae66e9ae4afb
parent 28 9c1050bc8709
child 33 e5139b339b18
equal deleted inserted replaced
31:98ea2bd59195 32:ae66e9ae4afb
    30 
    30 
    31     /* Last error */
    31     /* Last error */
    32     struct error_info err;
    32     struct error_info err;
    33 
    33 
    34     /* Callback info */
    34     /* Callback info */
    35     line_proto_read_cb cb_read;
    35     struct line_proto_callbacks callbacks;
    36     void *cb_arg;
    36     void *cb_arg;
    37 };
    37 };
    38 
    38 
    39 // function prototypes
    39 // function prototypes
    40 static err_t line_proto_schedule_events (struct line_proto *lp, short what);
    40 static err_t line_proto_schedule_events (struct line_proto *lp, short what);
    58             // XXX: un-fatalize
    58             // XXX: un-fatalize
    59             FATAL_ERROR(&lp->err, "line_proto_recv");
    59             FATAL_ERROR(&lp->err, "line_proto_recv");
    60         
    60         
    61         // got a line?
    61         // got a line?
    62         if (line)
    62         if (line)
    63             lp->cb_read(line, lp->cb_arg);
    63             lp->callbacks.on_line(line, lp->cb_arg);
    64 
    64 
    65     } while (line);
    65     } while (line);
    66 
    66 
    67     // reschedule
    67     // reschedule
    68     if (line_proto_schedule_events(lp, EV_READ))
    68     if (line_proto_schedule_events(lp, EV_READ))
   100     .on_read    = &line_proto_on_read,
   100     .on_read    = &line_proto_on_read,
   101     .on_write   = &line_proto_on_write,
   101     .on_write   = &line_proto_on_write,
   102 };
   102 };
   103 
   103 
   104 err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size,
   104 err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size,
   105         line_proto_read_cb cb_func, void *cb_arg, struct error_info *err)
   105         const struct line_proto_callbacks *callbacks, void *cb_arg, struct error_info *err)
   106 {
   106 {
   107     struct line_proto *lp;
   107     struct line_proto *lp;
   108 
   108 
   109     // allocate struct and buffers
   109     // allocate struct and buffers
   110     if (
   110     if (
   115         JUMP_SET_ERROR(err, ERR_CALLOC);
   115         JUMP_SET_ERROR(err, ERR_CALLOC);
   116 
   116 
   117     // store
   117     // store
   118     lp->sock = sock;
   118     lp->sock = sock;
   119     lp->buf_len = buf_size;
   119     lp->buf_len = buf_size;
   120     lp->cb_read = cb_func;
   120     lp->callbacks = *callbacks;
   121     lp->cb_arg = cb_arg;
   121     lp->cb_arg = cb_arg;
   122 
   122 
   123     // initialize event-based stuff
   123     // initialize event-based stuff
   124     if (
   124     if (
   125             sock_stream_event_init(sock, &line_proto_sock_stream_callbacks, lp)
   125             sock_stream_event_init(sock, &line_proto_sock_stream_callbacks, lp)