author | Tero Marttila <terom@fixme.fi> |
Thu, 21 May 2009 16:23:50 +0300 | |
branch | lua-threads |
changeset 207 | 3fa22abb5421 |
parent 196 | 873796250c60 |
permissions | -rw-r--r-- |
196
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#include "msg_proto.h" |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
#include <string.h> |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
#include <stdint.h> |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
#include <arpa/inet.h> |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
* I/O buffer |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
struct msg_buf { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
/** Buffer base pointer */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
char *base; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
/** Size of the buffer */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
size_t size; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
/** Current read/write offset */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
size_t off; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
}; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
* The minimum size used for any msg_buf::size related operation. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
#define MSG_BUF_MIN_SIZE 1024 |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
* Growth rate for size |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
#define MSG_BUF_GROW_RATE 2 |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
* Initialize a message buffer at the given initial size |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
err_t msg_buf_init (struct msg_buf *buf, size_t hint) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
// apply minimum size |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
if (hint < MSG_BUF_MIN_SIZE) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
hint = MSG_BUF_MIN_SIZE; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
// allocate the initial buffer |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
if ((buf->base = malloc(hint)) == NULL) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
return ERR_MEM; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
// set fields |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
buf->size = hint; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
buf->off = 0; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
* Grow the buffer if needed to fit the given capacity. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
err_t msg_buf_grow (struct msg_buf *buf, size_t size) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
char *tmp = buf->base; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
if (buf->size >= size) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
// nothing to do |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
// calculate new size |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
while (buf->size < size) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
buf->size *= MSG_BUF_GROW_RATE; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
// resize |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
if ((buf->base = realloc(buf->base, buf->size)) == NULL) { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
buf->base = tmp; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
return ERR_MEM; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
* Drain \a len bytes off the head of the buffer |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
err_t msg_buf_drain (struct msg_buf *buf, size_t len) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
// simple memmove |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
memmove(buf->base, buf->base + len, buf->off - len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
// update offfset |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
buf->off -= len; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
* Read into the buffer from a transport_t. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
* This will attempt to read \a len bytes onto the end of the buffer, growing it if needed to fit. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
* This may read/return more data than the given len. Use msg_buf_drain the remove the data from the buffer once you |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
* have used it. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
* Returns the number of new bytes read, zero for transport read buffer empty, -err_t for error. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
ssize_t msg_buf_read (struct msg_buf *buf, transport_t *transport, size_t len, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
ssize_t ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
// clamp size |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
if (len < MSG_BUF_MIN_SIZE) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
len = MSG_BUF_MIN_SIZE; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
// ensure space |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
if ((ERROR_CODE(err) = msg_buf_grow(buf, buf->off + len))) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
// read |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
if ((ret = transport_read(transport, buf->base + buf->off, len, err)) < 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
118 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
119 |
// no data left? |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
120 |
if (!ret) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
return 0; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
// update offset |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
buf->off += ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
125 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
return ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
128 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
129 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
return -ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
131 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
132 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
133 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
134 |
* Drives transport_write on the given data until all the given data is written, or zero is returned. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
135 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
136 |
* @param transport transport to write to |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
137 |
* @param data input data |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
138 |
* @param len number of bytes to write from data |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
139 |
* @param err returned error info |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
140 |
* @return number of bytes written (which may be zero or less than len), or -err_t. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
141 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
142 |
static ssize_t _transport_write_all (transport_t *transport, const char *data, size_t len, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
143 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
144 |
ssize_t ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
145 |
size_t written = 0; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
146 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
147 |
while (len) { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
148 |
// try and write out remaining data |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
149 |
if ((ret = transport_write(transport, data, len, err)) < 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
150 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
151 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
152 |
if (!ret) { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
153 |
// write buffer full |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
154 |
break; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
155 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
156 |
} else { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
157 |
// update and continue |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
158 |
written += ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
159 |
data += ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
160 |
len -= ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
161 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
162 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
163 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
164 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
165 |
return written; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
166 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
167 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
168 |
return -ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
169 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
170 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
171 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
172 |
* If the buffer is empty, this will attempt to write the given data directly using transport_write until either all |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
173 |
* the data is written (in which case nothing more needs to be done), or the transport won't accept any more writes, |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
174 |
* in which case the remaining data will be buffered. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
175 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
176 |
* If the buffer is not empty, then the given data will be added to the end of the buffer, since otherwise the order of |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
177 |
* data would be broken. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
178 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
179 |
* In either case, transport_write semantics garuntee that our buffer will either be empty, or an on_write will be |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
180 |
* pending on the transport. See msg_buf_flush() for how to handle transport_callbacks::on_write. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
181 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
182 |
err_t msg_buf_write (struct msg_buf *buf, transport_t *transport, const void *data_ptr, size_t len, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
183 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
184 |
ssize_t ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
const char *data = data_ptr; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
186 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
if (!buf->off) { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
188 |
// no data buffered, so we can try and write directly |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
189 |
if ((ret = _transport_write_all(transport, data, len, err)) < 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
190 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
191 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
192 |
// update written |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
193 |
data += ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
194 |
len -= ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
195 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
196 |
if (len == 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
197 |
// wrote it all |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
198 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
199 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
200 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
201 |
// ensure space |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
202 |
if ((ERROR_CODE(err) = msg_buf_grow(buf, buf->off + len))) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
203 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
204 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
205 |
// store |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
206 |
memcpy(buf->base + buf->off, data, len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
207 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
208 |
// update |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
209 |
buf->off += len; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
210 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
211 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
212 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
213 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
214 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
215 |
return ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
216 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
217 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
218 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
219 |
* Flush buffered write data to the transport, driving transport_write() until either all of our bufferd data has been |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
220 |
* written, or the transport will not accept any more. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
221 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
222 |
* In either case, transport_write semantics garuntee that our buffer will either be empty, or an on_write will be |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
223 |
* pending on the transport. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
224 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
225 |
err_t msg_buf_flush (struct msg_buf *buf, transport_t *transport, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
226 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
227 |
ssize_t ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
228 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
229 |
// write |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
230 |
if ((ret = _transport_write_all(transport, buf->base, buf->off, err)) < 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
231 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
232 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
233 |
if (ret) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
234 |
// unbuffer the written data |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
235 |
msg_buf_drain(buf, ret); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
236 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
237 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
238 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
239 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
240 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
241 |
return ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
242 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
243 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
244 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
245 |
* Deinitialize msg_buf to release allocated buffers |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
246 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
247 |
void msg_buf_deinit (struct msg_buf *buf) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
248 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
249 |
// release |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
250 |
free(buf->base); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
251 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
252 |
// reset |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
253 |
buf->base = NULL; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
254 |
buf->size = buf->off = 0; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
255 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
256 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
257 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
258 |
* Message header |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
259 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
260 |
struct msg_header { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
261 |
/** Message length, including header */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
262 |
uint16_t len; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
263 |
}; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
264 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
265 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
266 |
* Message header size |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
267 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
268 |
#define MSG_PROTO_HEADER_SIZE (sizeof(uint16_t)) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
269 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
270 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
271 |
* Our state struct |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
272 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
273 |
struct msg_proto { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
274 |
/** The transport */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
275 |
transport_t *transport; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
276 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
277 |
/** User callbacks */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
278 |
const struct msg_proto_callbacks *cb_tbl; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
279 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
280 |
/** User callback argument */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
281 |
void *cb_arg; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
282 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
283 |
/** Input buffer */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
284 |
struct msg_buf in; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
285 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
286 |
/** Output buffer */ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
287 |
struct msg_buf out; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
288 |
}; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
289 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
290 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
291 |
* Signal error to user |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
292 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
293 |
static void msg_proto_error (struct msg_proto *proto, const error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
294 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
295 |
// invoke user callback |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
296 |
proto->cb_tbl->on_error(proto, err, proto->cb_arg); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
297 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
298 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
299 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
300 |
* Attempt to read the current header from our input buffer. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
301 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
302 |
* Returns >0 for full header, 0 for incomplete header, -err_t for error. |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
303 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
304 |
static int msg_proto_peek_header (struct msg_proto *proto, struct msg_header *header, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
305 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
306 |
if (proto->in.off < MSG_PROTO_HEADER_SIZE) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
307 |
// not enough data for header |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
308 |
return 0; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
309 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
310 |
// read header |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
311 |
header->len = ntohs(*((uint16_t *) proto->in.base)); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
312 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
313 |
// bad header? |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
314 |
if (header->len < MSG_PROTO_HEADER_SIZE) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
315 |
JUMP_SET_ERROR_STR(err, ERR_MISC, "message_header::len"); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
316 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
317 |
// ok, got header |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
318 |
return 1; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
319 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
320 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
321 |
return -ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
322 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
323 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
324 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
325 |
* Recieved a message with the given header, and a pointer to the message data |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
326 |
* |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
327 |
* XXX: what to do if the user callback destroys the msg_proto? |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
328 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
329 |
static err_t msg_proto_on_msg (struct msg_proto *proto, struct msg_header *header, char *data, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
330 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
331 |
(void) err; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
332 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
333 |
// invoke user callback |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
334 |
proto->cb_tbl->on_msg(proto, data, header->len - MSG_PROTO_HEADER_SIZE, proto->cb_arg); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
335 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
336 |
// XXX: handle user errors |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
337 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
338 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
339 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
340 |
static void msg_proto_on_read (transport_t *transport, void *arg) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
341 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
342 |
struct msg_proto *proto = arg; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
343 |
struct msg_header header; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
344 |
ssize_t ret; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
345 |
error_t err; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
346 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
347 |
// we might be able to read more than one message per event |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
348 |
do { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
349 |
// try and read message length for incomplete message |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
350 |
if ((ret = msg_proto_peek_header(proto, &header, &err)) < 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
351 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
352 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
353 |
// need to read more data? |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
354 |
if (!ret || header.len > proto->in.off) { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
355 |
// msg_buf_read a minimum size, so passing a zero is OK |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
356 |
size_t to_read = ret ? header.len : 0; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
357 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
358 |
// read into our buffer |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
359 |
if ((ret = msg_buf_read(&proto->in, transport, to_read, &err)) < 0) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
360 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
361 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
362 |
} else { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
363 |
// handle full message |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
364 |
if (msg_proto_on_msg(proto, &header, proto->in.base + MSG_PROTO_HEADER_SIZE, &err)) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
365 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
366 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
367 |
// remove the data from the buffer |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
368 |
msg_buf_drain(&proto->in, header.len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
369 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
370 |
} while (ret); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
371 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
372 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
373 |
return; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
374 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
375 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
376 |
// notify user |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
377 |
msg_proto_error(proto, &err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
378 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
379 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
380 |
static void msg_proto_on_write (transport_t *transport, void *arg) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
381 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
382 |
struct msg_proto *proto = arg; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
383 |
error_t err; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
384 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
385 |
// flush |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
386 |
if (msg_buf_flush(&proto->out, transport, &err)) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
387 |
// notify user on transport errors |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
388 |
msg_proto_error(proto, &err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
389 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
390 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
391 |
static void msg_proto_on_error (transport_t *transport, const error_t *err, void *arg) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
392 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
393 |
struct msg_proto *proto = arg; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
394 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
395 |
(void) transport; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
396 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
397 |
// report to user |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
398 |
msg_proto_error(proto, err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
399 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
400 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
401 |
static const struct transport_callbacks msg_proto_transport_callbacks = { |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
402 |
.on_read = msg_proto_on_read, |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
403 |
.on_write = msg_proto_on_write, |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
404 |
.on_error = msg_proto_on_error, |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
405 |
}; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
406 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
407 |
err_t msg_proto_create (struct msg_proto **proto_ptr, transport_t *transport, const struct msg_proto_callbacks *cb_tbl, void *cb_arg, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
408 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
409 |
struct msg_proto *proto; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
410 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
411 |
// alloc |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
412 |
if ((proto = calloc(1, sizeof(*proto))) == NULL) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
413 |
return ERR_MEM; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
414 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
415 |
// store |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
416 |
proto->transport = transport; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
417 |
proto->cb_tbl = cb_tbl; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
418 |
proto->cb_arg = cb_arg; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
419 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
420 |
// init |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
421 |
if ( |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
422 |
(ERROR_CODE(err) = msg_buf_init(&proto->in, 0)) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
423 |
|| (ERROR_CODE(err) = msg_buf_init(&proto->out, 0)) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
424 |
) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
425 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
426 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
427 |
// setup transport |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
428 |
if ((ERROR_CODE(err) = transport_events(transport, TRANSPORT_READ | TRANSPORT_WRITE))) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
429 |
goto error; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
430 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
431 |
transport_set_callbacks(transport, &msg_proto_transport_callbacks, proto); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
432 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
433 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
434 |
*proto_ptr = proto; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
435 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
436 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
437 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
438 |
error: |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
439 |
// release |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
440 |
msg_proto_destroy(proto); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
441 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
442 |
return ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
443 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
444 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
445 |
/** |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
446 |
* Build and write out the data for the given header |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
447 |
*/ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
448 |
static err_t msg_proto_write_header (struct msg_proto *proto, const struct msg_header *header, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
449 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
450 |
char buf[MSG_PROTO_HEADER_SIZE]; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
451 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
452 |
// validate |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
453 |
if (header->len < MSG_PROTO_HEADER_SIZE) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
454 |
return SET_ERROR(err, ERR_MISC); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
455 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
456 |
// build |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
457 |
*((uint16_t *) buf) = htons(header->len); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
458 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
459 |
// write |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
460 |
return msg_buf_write(&proto->out, proto->transport, buf, sizeof(buf), err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
461 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
462 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
463 |
err_t msg_proto_send (struct msg_proto *proto, const void *data, size_t len, error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
464 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
465 |
struct msg_header header; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
466 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
467 |
// build header |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
468 |
header.len = MSG_PROTO_HEADER_SIZE + len; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
469 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
470 |
// write it |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
471 |
if ( |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
472 |
msg_proto_write_header(proto, &header, err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
473 |
|| msg_buf_write(&proto->out, proto->transport, data, len, err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
474 |
) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
475 |
return ERROR_CODE(err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
476 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
477 |
// ok |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
478 |
return SUCCESS; |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
479 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
480 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
481 |
void msg_proto_destroy (struct msg_proto *proto) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
482 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
483 |
// drop buffers |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
484 |
msg_buf_deinit(&proto->in); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
485 |
msg_buf_deinit(&proto->out); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
486 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
487 |
// kill transport |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
488 |
transport_destroy(proto->transport); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
489 |
|
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
490 |
// release ourself |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
491 |
free(proto); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
492 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
493 |