author | Tero Marttila <terom@fixme.fi> |
Sun, 22 Feb 2009 10:16:28 +0200 | |
changeset 10 | 9fe218576d13 |
parent 9 | 4c4c906cc649 |
child 11 | 14e79683c48c |
permissions | -rw-r--r-- |
1
cf0e1bb6bcab
a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
|
cf0e1bb6bcab
a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
#include "sock_internal.h" |
2
a834f0559939
working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
3 |
#include "sock_gnutls.h" |
a834f0559939
working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
4 |
|
3 | 5 |
#include <assert.h> |
6 |
||
9
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
7 |
// global sock_stream_ctx instance |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
8 |
struct sock_stream_ctx _sock_stream_ctx; |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
9 |
|
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
10 |
err_t sock_init (struct event_base *ev_base, struct error_info *err) |
2
a834f0559939
working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
11 |
{ |
9
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
12 |
// store ev_base |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
13 |
_sock_stream_ctx.ev_base = ev_base; |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
14 |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
15 |
// XXX: just call these all directly for now |
5 | 16 |
if (sock_gnutls_global_init(err)) |
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
17 |
return ERROR_CODE(err); |
3 | 18 |
|
4
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
19 |
// done |
a3ca0f97a075
change ERROR_* to use pointers again, and implement error_info for sock_init
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
20 |
return SUCCESS; |
2
a834f0559939
working SSL using gnutls - a bit of a painful process
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
21 |
} |
1
cf0e1bb6bcab
a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
|
3 | 23 |
void sock_stream_init (struct sock_stream *sock, struct sock_stream_type *type) |
24 |
{ |
|
25 |
// be strict |
|
26 |
assert(sock->type == NULL); |
|
27 |
||
28 |
// store type |
|
29 |
sock->type = type; |
|
30 |
} |
|
31 |
||
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:
9
diff
changeset
|
32 |
int sock_stream_read (struct sock_stream *sock, void *buf, size_t 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:
9
diff
changeset
|
33 |
{ |
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:
9
diff
changeset
|
34 |
err_t 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:
9
diff
changeset
|
35 |
|
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:
9
diff
changeset
|
36 |
// proxy off to method 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:
9
diff
changeset
|
37 |
if ((err = sock->type->methods.read(sock, 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:
9
diff
changeset
|
38 |
return 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:
9
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:
9
diff
changeset
|
40 |
// return updated bytes-read 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:
9
diff
changeset
|
41 |
return 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:
9
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:
9
diff
changeset
|
43 |
|
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:
9
diff
changeset
|
44 |
int sock_stream_write (struct sock_stream *sock, const void *buf, size_t 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:
9
diff
changeset
|
45 |
{ |
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:
9
diff
changeset
|
46 |
err_t 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:
9
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:
9
diff
changeset
|
48 |
// proxy off to method 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:
9
diff
changeset
|
49 |
if ((err = sock->type->methods.write(sock, 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:
9
diff
changeset
|
50 |
return 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:
9
diff
changeset
|
51 |
|
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:
9
diff
changeset
|
52 |
// return updated bytes-written 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:
9
diff
changeset
|
53 |
return 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:
9
diff
changeset
|
54 |
} |
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:
9
diff
changeset
|
55 |
|
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:
9
diff
changeset
|
56 |
err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg) |
9
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
57 |
{ |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
58 |
// store |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
59 |
sock->cb_info = callbacks; |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
60 |
sock->cb_arg = 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:
9
diff
changeset
|
61 |
|
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:
9
diff
changeset
|
62 |
// run method |
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:
9
diff
changeset
|
63 |
return sock->type->methods.event_init(sock); |
9
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
64 |
} |
4c4c906cc649
add sock_stream_callbacks and ev_base
Tero Marttila <terom@fixme.fi>
parents:
8
diff
changeset
|
65 |
|
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:
9
diff
changeset
|
66 |
err_t sock_stream_event_enable (struct sock_stream *sock, short mask) |
1
cf0e1bb6bcab
a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
{ |
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:
9
diff
changeset
|
68 |
// run method |
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:
9
diff
changeset
|
69 |
return sock->type->methods.event_enable(sock, mask); |
1
cf0e1bb6bcab
a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
} |
cf0e1bb6bcab
a fancy socket abstraction layer, with TCP, next, SSL. Also, .hgignore
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
|
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
72 |
const struct error_info* sock_stream_error (struct sock_stream *sock) |
3 | 73 |
{ |
8
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
74 |
// return pointer |
be88e543c8ff
split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
75 |
return SOCK_ERR(sock); |
3 | 76 |
} |