ref: 2e5cce691c7b3975ca028fd3dcd977cc714683ae
parent: 790b9816c5e0f05155e657edc5bd98f8a25780ca
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu May 21 19:00:12 EDT 2020
9gc: retry more quitely, more times. hide reconnects entirely
--- a/9gc.c
+++ b/9gc.c
@@ -38,12 +38,11 @@
uint8_t rdbuf[Msize];
uint8_t wrbuf[Msize];
uint32_t wroff;
- uint32_t wrend;
};
static const char *nick;
static int printjoin;
-static uint64_t chatoff;
+static uint64_t chatoff, skipuntil;
static uint8_t *
ctxread(C9ctx *ctx, uint32_t size, int *err)
@@ -53,20 +52,13 @@
C9aux *a;
a = ctx->aux;
- r = 0;
*err = 0;
for (n = 0; n < size; n += r) {
- errno = 0;
if ((r = read(a->f, a->rdbuf+n, size-n)) <= 0) {
- if (r == EINTR)
+ if (errno == EINTR)
continue;
- if (r == 0) {
- a->flags |= Eof;
- } else {
- a->flags |= Disconnected;
- *err = 1;
- perror("ctxread");
- }
+ a->flags = Disconnected;
+ close(a->f);
return NULL;
}
}
@@ -80,11 +72,8 @@
uint32_t n;
int w;
- if (a->wrend == 0)
- return 0;
- for (n = 0; n < a->wrend; n += w) {
- errno = 0;
- if ((w = write(a->f, a->wrbuf+n, a->wrend-n)) <= 0) {
+ for (n = 0; n < a->wroff; n += w) {
+ if ((w = write(a->f, a->wrbuf+n, a->wroff-n)) <= 0) {
if (errno == EINTR)
continue;
if (errno != EPIPE) /* remote end closed */
@@ -92,9 +81,7 @@
return -1;
}
}
- memmove(a->wrbuf, a->wrbuf+a->wrend, a->wroff-a->wrend);
- a->wroff = a->wroff - a->wrend;
- a->wrend = 0;
+ a->wroff = 0;
return 0;
}
@@ -119,10 +106,7 @@
static int
ctxend(C9ctx *ctx)
{
- C9aux *a;
-
- a = ctx->aux;
- a->wrend = a->wroff;
+ (void)ctx;
return 0;
}
@@ -152,10 +136,8 @@
return -1;
}
port++;
- if ((e = getaddrinfo(host, port, &hint, &r)) != 0) {
- fprintf(stderr, "%s: %s\n", s, gai_strerror(e));
+ if ((e = getaddrinfo(host, port, &hint, &r)) != 0)
return -1;
- }
f = -1;
for (a = r; a != NULL; a = a->ai_next) {
if ((f = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0)
@@ -167,8 +149,6 @@
}
}
freeaddrinfo(r);
- if (f < 0)
- fprintf(stderr, "%s: connection failed\n", s);
return f;
}
@@ -192,7 +172,8 @@
break;
case Rread:
- write(1, r->read.data, r->read.size);
+ if (chatoff >= skipuntil)
+ write(1, r->read.data, r->read.size);
chatoff += r->read.size;
/* fallthrough */
case Ropen:
@@ -200,7 +181,7 @@
c9write(ctx, &tag, Chatfid, 0, buf, snprintf(buf, sizeof(buf), "JOIN %s to chat\n", nick));
a->flags |= Joined;
}
- c9read(ctx, &tag, Chatfid, chatoff, Msize);
+ c9read(ctx, &tag, Chatfid, chatoff, chatoff < skipuntil ? skipuntil-chatoff : Msize);
break;
case Rerror:
@@ -253,13 +234,12 @@
exit(1);
a->flags = 0;
a->r = ctxchatR;
- a->wroff = a->wrend = 0;
+ a->wroff = 0;
c9version(ctx, &tag, Msize);
if (wrsend(a) != 0)
exit(1);
return;
}
-
}
fprintf(stderr, "chat hasn't been found in the registry\n");
exit(1);
@@ -311,9 +291,16 @@
{
C9aux *a;
C9tag tag;
+ int i;
- if ((a = srv("tcp!registry.9gridchan.org!6675")) == NULL)
+ for (i = 0; i < 10; i++) {
+ if ((a = srv("tcp!registry.9gridchan.org!6675")) != NULL)
+ break;
+ sleep(5);
+ }
+ if (a == NULL)
return NULL;
+
a->r = ctxregistryR;
c9version((C9ctx*)a, &tag, Msize);
wrsend(a);
@@ -433,9 +420,11 @@
while (chatrw(a) == 0 && wrsend(a) == 0);
if (a->flags & Disconnected) {
a->flags = 0;
- printf("reconnecting...\n");
+ skipuntil = chatoff;
+ chatoff = 0;
free(a);
} else {
+ printf("exiting\n");
exit(1);
}
}