7 #define NS(x) ((vlong)x)
8 #define US(x) (NS(x) * 1000LL)
9 #define MS(x) (US(x) * 1000LL)
10 #define S(x) (MS(x) * 1000LL)
18 Timer = 0, // Alt channels.
20 Maxto = 24 * 3600, // A full day to reconnect.
25 uchar nb[4]; // Number of data bytes in this message
26 uchar msg[4]; // Message number
27 uchar acked[4]; // Number of messages acked
35 static Channel *unsent;
36 static Channel *unacked;
37 static Channel *empty;
43 static char *dialstring;
44 static int maxto = Maxto;
45 static char *Logname = "aan";
47 static int reader = -1;
52 { nil, nil, CHANRCV }, // timer
53 { nil, nil, CHANRCV }, // unsent
54 { nil, nil, CHANEND },
57 static void fromnet(void*);
58 static void fromclient(void*);
59 static int reconnect(int);
60 static void synchronize(void);
61 static void showmsg(int, char *, Buf *);
62 static int writen(int, uchar *, int);
63 static void dmessage(int, char *, ...);
64 static void timerproc(void *);
69 fprint(2, "Usage: %s [-cd] [-m maxto] dialstring|netdir\n", argv0);
75 catch(void *, char *s)
77 if (!strcmp(s, "alarm")) {
78 syslog(0, Logname, "Timed out while waiting for reconnect, exiting...");
93 sysfatal("Cannot allocate memory; pc=%lux", pc);
99 threadmain(int argc, char **argv)
115 maxto = (int)strtol(EARGF(usage()), nil, 0);
128 if ((p = strstr(devdir, "/local")) != nil)
132 dialstring = argv[0];
135 int fd = open("#c/cons", OWRITE|OCEXEC);
139 fmtinstall('F', fcallfmt);
144 * Set up initial connection. use short timeout
145 * of 60 seconds so we wont hang arround for too
146 * long if there is some general connection problem
149 netfd = reconnect(60);
151 unsent = chancreate(sizeof(Buf *), Nbuf);
152 unacked = chancreate(sizeof(Buf *), Nbuf);
153 empty = chancreate(sizeof(Buf *), Nbuf);
154 timer = chancreate(sizeof(uchar *), 1);
155 if(unsent == nil || unacked == nil || empty == nil || timer == nil)
156 sysfatal("Cannot allocate channels");
158 for (i = 0; i < Nbuf; i++)
159 sendp(empty, emalloc(sizeof(Buf)));
161 reader = proccreate(fromnet, nil, Stacksize);
163 sysfatal("Cannot start fromnet; %r");
165 if (proccreate(fromclient, nil, Stacksize) < 0)
166 sysfatal("Cannot start fromclient; %r");
168 if (proccreate(timerproc, timer, Stacksize) < 0)
169 sysfatal("Cannot start timerproc; %r");
172 a[Unsent].c = unsent;
176 synctime = nsec() + Synctime;
180 if (netfd < 0 || failed) {
181 // Wait for the netreader to die.
183 dmessage(1, "main; waiting for netreader to die\n");
188 // the reader died; reestablish the world.
189 netfd = reconnect(maxto);
196 if (netfd < 0 || nsec() < synctime)
200 PBIT32(hdr.acked, inmsg);
203 if (writen(netfd, (uchar *)&hdr, Hdrsz) < 0) {
204 dmessage(2, "main; writen failed; %r\n");
210 syslog(0, Logname, "connection seems hung up...");
214 synctime = nsec() + Synctime;
223 PBIT32(b->hdr.acked, inmsg);
225 if (writen(netfd, (uchar *)&b->hdr, Hdrsz) < 0) {
226 dmessage(2, "main; writen failed; %r\n");
230 n = GBIT32(b->hdr.nb);
231 if (writen(netfd, b->buf, n) < 0) {
232 dmessage(2, "main; writen failed; %r\n");
241 syslog(0, Logname, "exiting...");
253 threadsetname("fromclient");
257 n = read(0, b->buf, Bufsize);
260 dmessage(2, "fromclient; Cannot read 9P message; %r\n");
262 dmessage(2, "fromclient; Client terminated\n");
265 PBIT32(b->hdr.nb, n);
266 PBIT32(b->hdr.msg, outmsg);
267 showmsg(1, "fromclient", b);
276 extern void _threadnote(void *, char *);
277 static int lastacked;
278 int n, m, len, acked;
283 threadsetname("fromnet");
285 b = emalloc(sizeof(Buf));
290 dmessage(1, "fromnet; waiting for connection... (inmsg %d)\n", inmsg);
295 len = readn(netfd, (uchar *)&b->hdr, Hdrsz);
298 dmessage(1, "fromnet; (hdr) network failure; %r\n");
300 dmessage(1, "fromnet; (hdr) network closed\n");
305 lostsync = 0; // reset timeout
306 n = GBIT32(b->hdr.nb);
307 m = GBIT32(b->hdr.msg);
308 acked = GBIT32(b->hdr.acked);
309 dmessage(2, "fromnet: Got message, size %d, nb %d, msg %d, acked %d, lastacked %d\n",
310 len, n, m, acked, lastacked);
314 dmessage(1, "fromnet; network closed\n");
321 dmessage(1, "fromnet; message too big %d > %d\n", n, Bufsize);
325 len = readn(netfd, b->buf, n);
326 if (len <= 0 || len != n) {
328 dmessage(1, "fromnet; network closed\n");
330 dmessage(1, "fromnet; network failure; %r\n");
337 dmessage(1, "fromnet; skipping message %d, currently at %d\n", m, inmsg);
341 // Process the acked list.
342 while(lastacked != acked) {
346 m = GBIT32(rb->hdr.msg);
347 if (m != lastacked) {
348 dmessage(1, "fromnet; rb %p, msg %d, lastacked %d\n", rb, m, lastacked);
349 sysfatal("fromnet; bug");
351 PBIT32(rb->hdr.msg, -1);
357 showmsg(1, "fromnet", b);
359 if (writen(1, b->buf, len) < 0)
360 sysfatal("fromnet; cannot write to client; %r");
373 syslog(0, Logname, "dialing %s", dialstring);
375 while ((fd = dial(dialstring, nil, ldir, nil)) < 0) {
379 errstr(err, sizeof err);
380 if (strstr(err, "connection refused")) {
381 dmessage(1, "reconnect; server died...\n");
382 threadexitsall("server died...");
384 dmessage(1, "reconnect: dialed %s; %s\n", dialstring, err);
388 syslog(0, Logname, "reconnected to %s", dialstring);
391 syslog(0, Logname, "waiting for connection on %s", devdir);
393 if ((lcfd = listen(devdir, ldir)) < 0)
394 sysfatal("reconnect; cannot listen; %r");
395 if ((fd = accept(lcfd, ldir)) < 0)
396 sysfatal("reconnect; cannot accept; %r");
401 if(nci = getnetconninfo(ldir, fd)){
402 syslog(0, Logname, "connected from %s", nci->rsys);
403 threadsetname(client? "client %s %s" : "server %s %s", ldir, nci->rsys);
404 freenetconninfo(nci);
406 syslog(0, Logname, "connected");
418 // Ignore network errors here. If we fail during
419 // synchronization, the next alarm will pick up
422 tmp = chancreate(sizeof(Buf *), Nbuf);
423 while ((b = nbrecvp(unacked)) != nil) {
424 n = GBIT32(b->hdr.nb);
425 writen(netfd, (uchar *)&b->hdr, Hdrsz);
426 writen(netfd, b->buf, n);
434 showmsg(int level, char *s, Buf *b)
439 dmessage(level, "%s; b == nil\n", s);
442 n = GBIT32(b->hdr.nb);
443 dmessage(level, "%s; (len %d) %X %X %X %X %X %X %X %X %X (%p)\n", s, n,
444 b->buf[0], b->buf[1], b->buf[2],
445 b->buf[3], b->buf[4], b->buf[5],
446 b->buf[6], b->buf[7], b->buf[8], b);
450 writen(int fd, uchar *buf, int nb)
460 if ((n = write(fd, buf, nb)) < 0) {
461 dmessage(1, "writen; Write failed; %r\n");
464 dmessage(2, "writen: wrote %d bytes\n", n);
477 threadsetname("timer");
480 sleep((Synctime / MS(1)) >> 1);
481 sendp(timer, "timer");
486 dmessage(int level, char *fmt, ...)
494 vfprint(2, fmt, arg);