1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include <transport.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct client
{
bool connected, received;
const char *expected;
struct transport_handle h;
struct io
{
FILE *in, *out;
} io;
};
struct server
{
struct transport_handle h;
struct io io;
const char *expected;
bool received;
};
static int io_write(const void *const buf, const size_t n,
const struct io *const io)
{
FILE *const f = io->out;
const size_t w = fwrite(buf, 1, n, f);
return ferror(f) ? -1 : w;
}
static int io_read(void *const buf, const size_t n,
const struct io *const io)
{
FILE *const f = io->in;
const size_t w = fread(buf, 1, n, f);
return ferror(f) ? -1 : w;
}
static int client_write(const void *const buf, const size_t n, void *const arg)
{
struct client *const c = arg;
return io_write(buf, n, &c->io);
}
static int client_read(void *const buf, const size_t n, void *const arg)
{
struct client *const c = arg;
return io_read(buf, n, &c->io);
}
static void client_received(const struct transport_event *const ev,
void *const arg)
{
struct client *const c = arg;
if (ev->common.type == TRANSPORT_EVENT_TYPE_CONNECT)
c->connected = true;
else if (ev->common.type == TRANSPORT_EVENT_TYPE_DATA)
{
const struct transport_event_data *const d = &ev->u.data;
if (!strncmp(d->buf, c->expected, d->n))
c->received = true;
}
}
static int server_write(const void *const buf, const size_t n, void *const arg)
{
struct server *const s = arg;
return io_write(buf, n, &s->io);
}
static int server_read(void *const buf, const size_t n, void *const arg)
{
struct server *const s = arg;
return io_read(buf, n, &s->io);
}
static void server_received(const struct transport_event *const ev,
void *const arg)
{
struct server *const s = arg;
if (ev->common.type == TRANSPORT_EVENT_TYPE_DATA)
{
const struct transport_event_data *const d = &ev->u.data;
if (!strncmp(d->buf, s->expected, d->n))
s->received = true;
}
}
static int loop(struct server *const s, struct client *const c)
{
if (transport_update(&s->h)
|| !freopen("client", "w+b", s->io.in))
return -1;
rewind(s->io.out);
if (transport_update(&c->h)
|| !freopen("server", "w+b", c->io.in))
return -1;
rewind(c->io.out);
return 0;
}
int main(void)
{
int ret = EXIT_FAILURE;
FILE *const cf = fopen("client", "w+b"),
*const sf = fopen("server", "w+b");
if (!cf)
{
perror("fopen(3) client");
goto end;
}
else if (!sf)
{
perror("fopen(3) server");
goto end;
}
struct client c =
{
.h =
{
.cfg =
{
.arg = &c,
.read = client_read,
.write = client_write,
.received = client_received
}
},
.io =
{
.in = sf,
.out = cf
},
.expected = "hi there!"
};
struct server s =
{
.h =
{
.cfg =
{
.arg = &s,
.read = server_read,
.write = server_write,
.received = server_received,
}
},
.io =
{
.in = cf,
.out = sf
},
.expected = "hello"
};
if (transport_connect(&c.h))
goto end;
while (!c.connected)
if (loop(&s, &c))
goto end;
if (transport_send(&c.h, s.expected, strlen(s.expected)))
goto end;
while (!s.received)
if (loop(&s, &c))
goto end;
if (transport_send(&s.h, c.expected, strlen(c.expected)))
goto end;
while (!c.received)
if (loop(&s, &c))
goto end;
ret = EXIT_SUCCESS;
end:
if (cf)
fclose(cf);
if (sf)
fclose(sf);
return ret;
}
|