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
|
#include <u.h>
#include <libc.h>
#include <thread.h>
#include "dat.h"
#include "fns.h"
int debug;
void
threadnetin(void *arg)
{
uchar buf[256];
int fd, n;
double x, v;
Ioproc *io;
fd = *((int*)arg);
io = ioproc();
while((n = ioread(io, fd, buf, sizeof buf)) > 0){
unpack(buf, n, "dd", &x, &v);
n = snprint((char *)buf, sizeof buf, "state: x=%g v=%g\n", x, v);
if(iowrite(io, 1, buf, n) != n)
fprint(2, "iowrite: %r\n");
}
closeioproc(io);
}
void
threadnetout(void *arg)
{
char buf[256];
int fd, n;
Ioproc *io;
fd = *((int*)arg);
io = ioproc();
while((n = ioread(io, 0, buf, sizeof buf)) > 0)
if(iowrite(io, fd, buf, n) != n)
fprint(2, "iowrite: %r\n");
closeioproc(io);
}
void
usage(void)
{
fprint(2, "usage: %s [-d] server\n", argv0);
threadexitsall("usage");
}
void
threadmain(int argc, char *argv[])
{
char *server;
int fd;
ARGBEGIN{
case 'd':
debug++;
break;
default:
usage();
}ARGEND;
if(argc != 1)
usage();
server = argv[0];
fd = dial(server, nil, nil, nil);
if(fd < 0)
sysfatal("dial: %r");
threadcreate(threadnetin, &fd, 4096);
threadcreate(threadnetout, &fd, 4096);
threadexits(nil);
}
|