author | Tero Marttila <terom@fixme.fi> |
Sat, 28 Feb 2009 21:39:15 +0200 | |
changeset 17 | 5001564ac5fc |
parent 13 | ca16f3a8f3b7 |
child 18 | dedf137b504f |
permissions | -rw-r--r-- |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
#include "line_proto.h" |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
#include <string.h> |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
#include <assert.h> |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
|
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
8 |
#include <err.h> |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
9 |
|
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
/* |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
* Our state |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
*/ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
struct line_proto { |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
/* The sock_stream we read/write with */ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
struct sock_stream *sock; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
|
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
17 |
/* The incoming line buffer */ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
18 |
char *buf; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
19 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
20 |
/* Incoming buffer size */ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
21 |
size_t buf_len; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
22 |
|
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
/* Offset of trailing data in buf */ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
size_t tail_offset; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
/* Length of trailing data in buf, if any */ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
size_t tail_len; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
/* Last error */ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
struct error_info err; |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
31 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
32 |
/* Callback info */ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
33 |
line_proto_read_cb cb_read; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
34 |
void *cb_arg; |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
}; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
|
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
37 |
// function prototypes |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
38 |
static err_t line_proto_schedule_events (struct line_proto *lp, short what); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
39 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
40 |
/* |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
41 |
* Our sock_stream on_read handler |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
42 |
*/ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
43 |
static void line_proto_on_read (struct sock_stream *sock, void *arg) |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
44 |
{ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
45 |
struct line_proto *lp = arg; |
17 | 46 |
char *line; |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
47 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
48 |
// sanity-check |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
49 |
assert(lp->tail_offset < lp->buf_len); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
50 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
51 |
do { |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
52 |
// attempt to read a line |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
53 |
if (line_proto_read(lp, &line)) |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
54 |
// XXX: fail |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
55 |
errx(1, "line_proto_read: %s", error_msg(&lp->err)); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
56 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
57 |
// got a line? |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
58 |
if (line) |
11
14e79683c48c
working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents:
10
diff
changeset
|
59 |
lp->cb_read(line, lp->cb_arg); |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
60 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
61 |
} while (line); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
62 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
63 |
// reschedule |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
64 |
if (line_proto_schedule_events(lp, EV_READ)) |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
65 |
errx(1, "line_proto_schedule_events: %s", error_msg(&lp->err)); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
66 |
} |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
67 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
68 |
/* |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
69 |
* Schedule our sock_stream callback |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
70 |
*/ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
71 |
static err_t line_proto_schedule_events (struct line_proto *lp, short what) |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
72 |
{ |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
73 |
// just use sock_stream's interface |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
74 |
if (sock_stream_event_enable(lp->sock, what)) |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
75 |
RETURN_SET_ERROR_INFO(&lp->err, sock_stream_error(lp->sock)); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
76 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
77 |
return SUCCESS; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
78 |
} |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
79 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
80 |
struct sock_stream_callbacks line_proto_sock_stream_callbacks = { |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
81 |
.on_read = &line_proto_on_read, |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
82 |
}; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
83 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
84 |
err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size, |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
85 |
line_proto_read_cb cb_func, void *cb_arg, struct error_info *err) |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
{ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
struct line_proto *lp; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
// allocate |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
if ((lp = calloc(1, sizeof(*lp))) == NULL) |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
return SET_ERROR(err, ERR_CALLOC); |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
92 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
93 |
// allocate buffer |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
94 |
if ((lp->buf = malloc(buf_size)) == NULL) { |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
95 |
free(lp); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
96 |
return SET_ERROR(err, ERR_CALLOC); |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
97 |
} |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
// store |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
lp->sock = sock; |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
101 |
lp->buf_len = buf_size; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
102 |
lp->cb_read = cb_func; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
103 |
lp->cb_arg = cb_arg; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
104 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
105 |
// initialize event-based stuff |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
106 |
sock_stream_event_init(sock, &line_proto_sock_stream_callbacks, lp); |
11
14e79683c48c
working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents:
10
diff
changeset
|
107 |
line_proto_schedule_events(lp, EV_READ); |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
// return |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
*lp_ptr = lp; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
return SUCCESS; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
} |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
/* |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
* This looks for a full '\r\n' terminated line at the beginning of the given buffer. If found, the \r\n will be |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
* replaced with a '\0', and the offset to the beginning of the next line returned. If not found, zero is returned |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
118 |
* (which is never a valid next-line offset). |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
119 |
* |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
120 |
* The given \a hint is an hint as to the offset at which to start scanning, used for incremental invocations of this |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
* on the same buffer. |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
* |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
*/ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
int _parse_line (char *buf, size_t len, size_t *hint) { |
13
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
125 |
int i, next = 0; |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
// empty buffer -> nothing |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
128 |
if (len == 0) |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
129 |
return 0; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
|
13
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
131 |
// look for terminating '\r\n' or '\n' sequence |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
132 |
for (i = *hint; i < len; i++) { |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
133 |
// match this + next char? |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
134 |
if (i < len - 1 && buf[i] == '\r' && buf[i + 1] == '\n') { |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
135 |
next = i + 2; |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
136 |
break; |
13
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
137 |
|
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
138 |
} else if (buf[i] == '\n') { |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
139 |
next = i + 1; |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
140 |
break; |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
141 |
} |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
142 |
} |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
143 |
|
13
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
144 |
// searched the whole buffer? |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
145 |
if (i >= len) { |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
146 |
// do continue one char back, to keep any \r |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
147 |
*hint = len - 1; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
148 |
return 0; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
149 |
} |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
150 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
151 |
// mangle the newline off |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
152 |
buf[i] = '\0'; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
153 |
|
13
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
154 |
// return offset to next line, as set in loop based on delim |
ca16f3a8f3b7
add support for \n line endings as well
Tero Marttila <terom@fixme.fi>
parents:
12
diff
changeset
|
155 |
return next; |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
156 |
} |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
157 |
|
17 | 158 |
err_t line_proto_read (struct line_proto *lp, char **line_ptr) |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
159 |
{ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
160 |
// offset to recv() new data into, offset to _parse_line hint, offset to next line from _parse_line |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
161 |
size_t recv_offset = 0, peek_offset = 0, next_offset = 0; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
162 |
int ret; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
163 |
|
12
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
164 |
// adjust offset to beyond previous data (as will be moved next) |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
165 |
recv_offset = lp->tail_len; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
166 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
167 |
// move trailing data from previous line to front of buffer |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
168 |
if (lp->tail_offset) { |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
169 |
// move to front |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
170 |
memmove(lp->buf, lp->buf + lp->tail_offset, lp->tail_len); |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
171 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
172 |
// reset |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
173 |
lp->tail_offset = 0; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
174 |
lp->tail_len = 0; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
175 |
} |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
176 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
177 |
// readline loop |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
178 |
do { |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
179 |
// parse any line at the beginning of the buffer |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
180 |
if ((next_offset = _parse_line(lp->buf, recv_offset, &peek_offset)) > 0) { |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
181 |
// store a valid *line_ptr |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
182 |
*line_ptr = lp->buf; |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
183 |
|
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
184 |
// exit loop and return |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
break; |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
186 |
} |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
|
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
188 |
// ensure there's enough space for the rest of the line |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
189 |
// XXX: this must be an error, not an assert |
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
190 |
assert(recv_offset < lp->buf_len); |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
191 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
192 |
// otherwise, read more data |
12
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
193 |
if ((ret = sock_stream_read(lp->sock, lp->buf + recv_offset, lp->buf_len - recv_offset)) < 0) |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
194 |
// store and return NULL on errors |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
195 |
RETURN_SET_ERROR_INFO(&lp->err, sock_stream_error(lp->sock)); |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
196 |
|
12
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
197 |
// EAGAIN? |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
198 |
if (ret == 0) { |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
199 |
// return a NULL *line_ptr |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
200 |
*line_ptr = NULL; |
4147fae232d9
update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
201 |
break; |
10
9fe218576d13
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
202 |
} |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
203 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
204 |
// update recv_offset |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
205 |
recv_offset += ret; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
206 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
207 |
} while (1); |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
208 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
209 |
// update state for next call |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
210 |
lp->tail_offset = next_offset; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
211 |
lp->tail_len = recv_offset - next_offset; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
212 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
213 |
// ok |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
214 |
return SUCCESS; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
215 |
} |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
216 |
|
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
217 |
const struct error_info* line_proto_error (struct line_proto *lp) |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
218 |
{ |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
219 |
// return pointer |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
220 |
return &lp->err; |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
221 |
} |