|
|
1.1 ! root 1: /* with luck, protocol can be used in either direction */ ! 2: /* for now, all ascii, for readability */ ! 3: /* ! 4: x character x ! 5: nnn integer nnn, followed by blank ! 6: NEW: last digit is now a letter, no blank ! 7: snn abcdef string abcdef, length nn, term. by \n ! 8: NEW: use new integer encoding ! 9: ! 10: Changing the integer encoding reduced the time for data transfer in ! 11: /u/j/anim/sort/a1.i from 58 seconds to 42 seconds ! 12: */ ! 13: ! 14: char protobuf[1024]; ! 15: char *protop = protobuf; ! 16: ! 17: flushproto() ! 18: { ! 19: if (protop > protobuf) { ! 20: sendbuf(protobuf, protop); ! 21: protop = protobuf; ! 22: } ! 23: } ! 24: ! 25: send1char(c) /* send one character */ ! 26: int c; ! 27: { ! 28: if (protop >= protobuf + sizeof protobuf) ! 29: flushproto(); ! 30: *protop++ = c; ! 31: } ! 32: ! 33: readchar() /* read one character */ ! 34: { ! 35: int c; ! 36: ! 37: flushproto(); ! 38: return readbyte(); ! 39: } ! 40: ! 41: sendint(n) /* send an integer */ ! 42: int n; ! 43: { ! 44: char buf[20], *p; ! 45: ! 46: sprintf(buf, "%d", n); ! 47: for (p = buf; *(p+1); p++) ! 48: send1char(*p); ! 49: send1char(*p - '0' + 'a'); ! 50: } ! 51: ! 52: readint() /* read an integer */ ! 53: { ! 54: int n, c, sign; ! 55: ! 56: flushproto(); ! 57: while ((c = readbyte()) == ' ' || c == '\n') ! 58: ; ! 59: assert(c == '-' || isdigit(c) || (c >= 'a' && c <= 'j'), ! 60: "c is not a digit"); ! 61: if (c == '-') { ! 62: sign = -1; ! 63: c = readbyte(); ! 64: } else ! 65: sign = 1; ! 66: for (n = 0; c >= '0' && c <= '9'; c = readbyte()) ! 67: n = 10 * n + c - '0'; ! 68: assert(c >= 'a' && c <= 'j', "bad string terminator"); ! 69: n = 10 * n + c - 'a'; ! 70: return sign * n; ! 71: } ! 72: ! 73: sendstring(s) /* send a string */ ! 74: char *s; ! 75: { ! 76: send1char('s'); ! 77: sendint(strlen(s)); ! 78: while (*s) ! 79: send1char(*s++); ! 80: send1char('\n'); ! 81: } ! 82: ! 83: readstring(s) /* read string into s (better be big enough) */ ! 84: char *s; ! 85: { ! 86: int c, n, m; ! 87: char *p = s; ! 88: ! 89: flushproto(); ! 90: c = readbyte(); ! 91: assert(c == 's', "readstring: c == 's'"); ! 92: m = n = readint(); ! 93: while (n--) ! 94: *p++ = readbyte(); ! 95: *p = '\0'; ! 96: c = readbyte(); ! 97: assert(c == '\n', "c == '\\n'"); ! 98: return m; ! 99: } ! 100: ! 101: isdigit(c) ! 102: { ! 103: return c >= '0' && c <= '9'; ! 104: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.