History

9gc: retry more quitely, more times. hide reconnects entirely

Sigrid Haflínudóttir committed 6 years ago

Commit 2e5cce61 parent 790b9811 file · +24 −35Browse files

Unified · Split

9gc.c+24−35
@@ -38,12 +38,11 @@ struct C9aux { · 37 unmodified lines
3838 uint8_t rdbuf[Msize];
3939 uint8_t wrbuf[Msize];
4040 uint32_t wroff;
41− uint32_t wrend;
4241 };
4342
4443 static const char *nick;
4544 static int printjoin;
46−static uint64_t chatoff;
45+static uint64_t chatoff, skipuntil;
4746
4847 static uint8_t *
4948 ctxread(C9ctx *ctx, uint32_t size, int *err)
@@ -53,20 +52,13 @@ ctxread(C9ctx *ctx, uint32_t size, int *err) · 3 unmodified lines
5352 C9aux *a;
5453
5554 a = ctx->aux;
56− r = 0;
5755 *err = 0;
5856 for (n = 0; n < size; n += r) {
59− errno = 0;
6057 if ((r = read(a->f, a->rdbuf+n, size-n)) <= 0) {
61− if (r == EINTR)
58+ if (errno == EINTR)
6259 continue;
63− if (r == 0) {
64− a->flags |= Eof;
65− } else {
66− a->flags |= Disconnected;
67− *err = 1;
68− perror("ctxread");
69− }
60+ a->flags = Disconnected;
61+ close(a->f);
7062 return NULL;
7163 }
7264 }
@@ -80,11 +72,8 @@ wrsend(C9aux *a) · 7 unmodified lines
8072 uint32_t n;
8173 int w;
8274
83− if (a->wrend == 0)
84− return 0;
85− for (n = 0; n < a->wrend; n += w) {
86− errno = 0;
87− if ((w = write(a->f, a->wrbuf+n, a->wrend-n)) <= 0) {
75+ for (n = 0; n < a->wroff; n += w) {
76+ if ((w = write(a->f, a->wrbuf+n, a->wroff-n)) <= 0) {
8877 if (errno == EINTR)
8978 continue;
9079 if (errno != EPIPE) /* remote end closed */
@@ -92,9 +81,7 @@ wrsend(C9aux *a) · 1 unmodified line
9281 return -1;
9382 }
9483 }
95− memmove(a->wrbuf, a->wrbuf+a->wrend, a->wroff-a->wrend);
96− a->wroff = a->wroff - a->wrend;
97− a->wrend = 0;
84+ a->wroff = 0;
9885
9986 return 0;
10087 }
@@ -119,10 +106,7 @@ ctxbegin(C9ctx *ctx, uint32_t size) · 18 unmodified lines
119106 static int
120107 ctxend(C9ctx *ctx)
121108 {
122− C9aux *a;
123−
124− a = ctx->aux;
125− a->wrend = a->wroff;
109+ (void)ctx;
126110 return 0;
127111 }
128112
@@ -152,10 +136,8 @@ dial(char *s) · 23 unmodified lines
152136 return -1;
153137 }
154138 port++;
155− if ((e = getaddrinfo(host, port, &hint, &r)) != 0) {
156− fprintf(stderr, "%s: %s\n", s, gai_strerror(e));
139+ if ((e = getaddrinfo(host, port, &hint, &r)) != 0)
157140 return -1;
158− }
159141 f = -1;
160142 for (a = r; a != NULL; a = a->ai_next) {
161143 if ((f = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0)
@@ -167,8 +149,6 @@ dial(char *s) · 5 unmodified lines
167149 }
168150 }
169151 freeaddrinfo(r);
170− if (f < 0)
171− fprintf(stderr, "%s: connection failed\n", s);
172152
173153 return f;
174154 }
@@ -192,7 +172,8 @@ ctxchatR(C9ctx *ctx, C9r *r) · 17 unmodified lines
192172 break;
193173
194174 case Rread:
195− write(1, r->read.data, r->read.size);
175+ if (chatoff >= skipuntil)
176+ write(1, r->read.data, r->read.size);
196177 chatoff += r->read.size;
197178 /* fallthrough */
198179 case Ropen:
@@ -200,7 +181,7 @@ ctxchatR(C9ctx *ctx, C9r *r) · 1 unmodified line
200181 c9write(ctx, &tag, Chatfid, 0, buf, snprintf(buf, sizeof(buf), "JOIN %s to chat\n", nick));
201182 a->flags |= Joined;
202183 }
203− c9read(ctx, &tag, Chatfid, chatoff, Msize);
184+ c9read(ctx, &tag, Chatfid, chatoff, chatoff < skipuntil ? skipuntil-chatoff : Msize);
204185 break;
205186
206187 case Rerror:
@@ -253,13 +234,12 @@ ctxregistryR(C9ctx *ctx, C9r *r) · 46 unmodified lines
253234 exit(1);
254235 a->flags = 0;
255236 a->r = ctxchatR;
256− a->wroff = a->wrend = 0;
237+ a->wroff = 0;
257238 c9version(ctx, &tag, Msize);
258239 if (wrsend(a) != 0)
259240 exit(1);
260241 return;
261242 }
262−
263243 }
264244 fprintf(stderr, "chat hasn't been found in the registry\n");
265245 exit(1);
@@ -311,9 +291,16 @@ registry(void) · 45 unmodified lines
311291 {
312292 C9aux *a;
313293 C9tag tag;
294+ int i;
314295
315− if ((a = srv("tcp!registry.9gridchan.org!6675")) == NULL)
296+ for (i = 0; i < 10; i++) {
297+ if ((a = srv("tcp!registry.9gridchan.org!6675")) != NULL)
298+ break;
299+ sleep(5);
300+ }
301+ if (a == NULL)
316302 return NULL;
303+
317304 a->r = ctxregistryR;
318305 c9version((C9ctx*)a, &tag, Msize);
319306 wrsend(a);
@@ -433,9 +420,11 @@ main(int argc, char **argv) · 113 unmodified lines
433420 while (chatrw(a) == 0 && wrsend(a) == 0);
434421 if (a->flags & Disconnected) {
435422 a->flags = 0;
436− printf("reconnecting...\n");
423+ skipuntil = chatoff;
424+ chatoff = 0;
437425 free(a);
438426 } else {
427+ printf("exiting\n");
439428 exit(1);
440429 }
441430 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t