--- deliantra/server/socket/lowlevel.C 2012/11/06 03:45:17 1.87 +++ deliantra/server/socket/lowlevel.C 2012/11/06 15:11:16 1.88 @@ -202,7 +202,7 @@ && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING); } -// HACK: some commands currently should be executed +// HACK: some commands currently should be executed // even when the player is frozen. this hack detects // those commands. it should be folded into may_execute, // but kept seperate to emphasise the hack aspect, i.e. @@ -359,7 +359,7 @@ if (ws_version) { - if (ws_inbuf_len + 4096 > ws_inbuf_alloc) + if (ws_inbuf_len + 2048 > ws_inbuf_alloc) ws_inbuf = (uint8 *)realloc (ws_inbuf, ws_inbuf_alloc += 4096); int len = read (fd, ws_inbuf + ws_inbuf_len, ws_inbuf_alloc - ws_inbuf_len); @@ -423,7 +423,21 @@ switch (o) { case 1: // utf-8 - //TODO + { + uint8 *a = inbuf + inbuf_len; + uint8 *b = a; + uint8 *c = a + l; + + for (; a < c; ++a, ++b) + { + *b = *a; + + if (*a >= 0x80) + *b = (a [0] & 0x1f) << 6 | (a [1] & 0x3f), ++a; + } + + l -= a - b; + } break; case 2: // binary break; @@ -446,7 +460,7 @@ destroy (); return; } - + amount = l; } else @@ -556,16 +570,57 @@ if (ws_version == 8) { - uint8 hdr [4] = { 0x81, 126, sl.length () >> 8, sl.length () }; - // TODO: utf-8 encoding - send (hdr, 4); - send (sl.buf_ + sl.hdrlen, sl.cur - sl.buf_ - sl.hdrlen); + static uint8 buf [MAXSOCKBUF * 2 + 4]; + + uint8 *b = buf + 4; + for (uint8 *a = sl.buf_ + sl.hdrlen; a < sl.cur; ++a) + { + if (*a < 0x80) + *b++ = *a; + else + { + *b++ = 0xc0 | ((*a >> 6) & 0x1f); + *b++ = 0x80 | ( *a & 0x3f); + } + } + + assert (b - buf < sizeof (buf)); + + int l = b - (buf + 4); + + if (l < 126) + { + buf [2] = 0x81; + buf [3] = l; + + send (buf + 2, l + 2); + } + else + { + buf [0] = 0x81; + buf [1] = 126; + buf [2] = l >> 8; + buf [3] = l; + + send (buf, l + 4); + } } else if (ws_version == 13) { - uint8 hdr [4] = { 0x82, 126, sl.length () >> 8, sl.length () }; - send (hdr, 4); - send (sl.buf_ + sl.hdrlen, sl.cur - sl.buf_ - sl.hdrlen); + int l = sl.length (); + + if (l < 126) + { + uint8 hdr [] = { 0x82, 126, l >> 8, l }; + send (hdr, sizeof (hdr)); + } + else + { + uint8 hdr [] = { 0x82, l }; + send (hdr, sizeof (hdr)); + } + + send (sl.buf_ + sl.hdrlen, l); } else {