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