Unified · Split
| @@ -38,12 +38,11 @@ struct C9aux { · 37 unmodified lines | ||
| 38 | 38 | uint8_t rdbuf[Msize]; |
| 39 | 39 | uint8_t wrbuf[Msize]; |
| 40 | 40 | uint32_t wroff; |
| 41 | − uint32_t wrend; | |
| 42 | 41 | }; |
| 43 | 42 | |
| 44 | 43 | static const char *nick; |
| 45 | 44 | static int printjoin; |
| 46 | −static uint64_t chatoff; | |
| 45 | +static uint64_t chatoff, skipuntil; | |
| 47 | 46 | |
| 48 | 47 | static uint8_t * |
| 49 | 48 | ctxread(C9ctx *ctx, uint32_t size, int *err) |
| @@ -53,20 +52,13 @@ ctxread(C9ctx *ctx, uint32_t size, int *err) · 3 unmodified lines | ||
| 53 | 52 | C9aux *a; |
| 54 | 53 | |
| 55 | 54 | a = ctx->aux; |
| 56 | − r = 0; | |
| 57 | 55 | *err = 0; |
| 58 | 56 | for (n = 0; n < size; n += r) { |
| 59 | − errno = 0; | |
| 60 | 57 | if ((r = read(a->f, a->rdbuf+n, size-n)) <= 0) { |
| 61 | − if (r == EINTR) | |
| 58 | + if (errno == EINTR) | |
| 62 | 59 | 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); | |
| 70 | 62 | return NULL; |
| 71 | 63 | } |
| 72 | 64 | } |
| @@ -80,11 +72,8 @@ wrsend(C9aux *a) · 7 unmodified lines | ||
| 80 | 72 | uint32_t n; |
| 81 | 73 | int w; |
| 82 | 74 | |
| 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) { | |
| 88 | 77 | if (errno == EINTR) |
| 89 | 78 | continue; |
| 90 | 79 | if (errno != EPIPE) /* remote end closed */ |
| @@ -92,9 +81,7 @@ wrsend(C9aux *a) · 1 unmodified line | ||
| 92 | 81 | return -1; |
| 93 | 82 | } |
| 94 | 83 | } |
| 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; | |
| 98 | 85 | |
| 99 | 86 | return 0; |
| 100 | 87 | } |
| @@ -119,10 +106,7 @@ ctxbegin(C9ctx *ctx, uint32_t size) · 18 unmodified lines | ||
| 119 | 106 | static int |
| 120 | 107 | ctxend(C9ctx *ctx) |
| 121 | 108 | { |
| 122 | − C9aux *a; | |
| 123 | − | |
| 124 | − a = ctx->aux; | |
| 125 | − a->wrend = a->wroff; | |
| 109 | + (void)ctx; | |
| 126 | 110 | return 0; |
| 127 | 111 | } |
| 128 | 112 | |
| @@ -152,10 +136,8 @@ dial(char *s) · 23 unmodified lines | ||
| 152 | 136 | return -1; |
| 153 | 137 | } |
| 154 | 138 | 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) | |
| 157 | 140 | return -1; |
| 158 | − } | |
| 159 | 141 | f = -1; |
| 160 | 142 | for (a = r; a != NULL; a = a->ai_next) { |
| 161 | 143 | if ((f = socket(a->ai_family, a->ai_socktype, a->ai_protocol)) < 0) |
| @@ -167,8 +149,6 @@ dial(char *s) · 5 unmodified lines | ||
| 167 | 149 | } |
| 168 | 150 | } |
| 169 | 151 | freeaddrinfo(r); |
| 170 | − if (f < 0) | |
| 171 | − fprintf(stderr, "%s: connection failed\n", s); | |
| 172 | 152 | |
| 173 | 153 | return f; |
| 174 | 154 | } |
| @@ -192,7 +172,8 @@ ctxchatR(C9ctx *ctx, C9r *r) · 17 unmodified lines | ||
| 192 | 172 | break; |
| 193 | 173 | |
| 194 | 174 | case Rread: |
| 195 | − write(1, r->read.data, r->read.size); | |
| 175 | + if (chatoff >= skipuntil) | |
| 176 | + write(1, r->read.data, r->read.size); | |
| 196 | 177 | chatoff += r->read.size; |
| 197 | 178 | /* fallthrough */ |
| 198 | 179 | case Ropen: |
| @@ -200,7 +181,7 @@ ctxchatR(C9ctx *ctx, C9r *r) · 1 unmodified line | ||
| 200 | 181 | c9write(ctx, &tag, Chatfid, 0, buf, snprintf(buf, sizeof(buf), "JOIN %s to chat\n", nick)); |
| 201 | 182 | a->flags |= Joined; |
| 202 | 183 | } |
| 203 | − c9read(ctx, &tag, Chatfid, chatoff, Msize); | |
| 184 | + c9read(ctx, &tag, Chatfid, chatoff, chatoff < skipuntil ? skipuntil-chatoff : Msize); | |
| 204 | 185 | break; |
| 205 | 186 | |
| 206 | 187 | case Rerror: |
| @@ -253,13 +234,12 @@ ctxregistryR(C9ctx *ctx, C9r *r) · 46 unmodified lines | ||
| 253 | 234 | exit(1); |
| 254 | 235 | a->flags = 0; |
| 255 | 236 | a->r = ctxchatR; |
| 256 | − a->wroff = a->wrend = 0; | |
| 237 | + a->wroff = 0; | |
| 257 | 238 | c9version(ctx, &tag, Msize); |
| 258 | 239 | if (wrsend(a) != 0) |
| 259 | 240 | exit(1); |
| 260 | 241 | return; |
| 261 | 242 | } |
| 262 | − | |
| 263 | 243 | } |
| 264 | 244 | fprintf(stderr, "chat hasn't been found in the registry\n"); |
| 265 | 245 | exit(1); |
| @@ -311,9 +291,16 @@ registry(void) · 45 unmodified lines | ||
| 311 | 291 | { |
| 312 | 292 | C9aux *a; |
| 313 | 293 | C9tag tag; |
| 294 | + int i; | |
| 314 | 295 | |
| 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) | |
| 316 | 302 | return NULL; |
| 303 | + | |
| 317 | 304 | a->r = ctxregistryR; |
| 318 | 305 | c9version((C9ctx*)a, &tag, Msize); |
| 319 | 306 | wrsend(a); |
| @@ -433,9 +420,11 @@ main(int argc, char **argv) · 113 unmodified lines | ||
| 433 | 420 | while (chatrw(a) == 0 && wrsend(a) == 0); |
| 434 | 421 | if (a->flags & Disconnected) { |
| 435 | 422 | a->flags = 0; |
| 436 | − printf("reconnecting...\n"); | |
| 423 | + skipuntil = chatoff; | |
| 424 | + chatoff = 0; | |
| 437 | 425 | free(a); |
| 438 | 426 | } else { |
| 427 | + printf("exiting\n"); | |
| 439 | 428 | exit(1); |
| 440 | 429 | } |
| 441 | 430 | } |