aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-16 00:46:06 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-16 00:46:06 -0400
commit5e1ceee3576438b769acc461afe00f1108ca9f58 (patch)
treea8fef7af9a172f9ca7e33bedfe5f65b58cb4c943
parent05c9a4fa2756c0713e5dc56a69531cdadb4f411d (diff)
downloadpython-termcast-server-5e1ceee3576438b769acc461afe00f1108ca9f58.tar.gz
python-termcast-server-5e1ceee3576438b769acc461afe00f1108ca9f58.zip
properly handle split packets
-rw-r--r--termcast.py22
1 files changed, 9 insertions, 13 deletions
diff --git a/termcast.py b/termcast.py
index 23c4b62..76ee3f3 100644
--- a/termcast.py
+++ b/termcast.py
@@ -14,37 +14,33 @@ class Handler(object):
self.rows = rows
self.cols = cols
self.buf = b''
+ self.prev_read = b''
self.vt = vt100.vt100(rows, cols)
def process(self, data):
- # XXX this will break if the data is split between reads, but i don't
- # know if there's anything we can do about this, because we'll have
- # already processed the beginning of it if that happens. we might need
- # some better framing which allows us to detect the start of an out of
- # band section even when the end hasn't been received yet
+ to_process = self.prev_read + data
+ processed = self.vt.process(to_process)
+ self.prev_read = to_process[processed:]
+
+ self.buf += data
+
extra_data = {}
while True:
- m = extra_data_re.search(data)
+ m = extra_data_re.search(self.buf)
if m is None:
break
extra_data_json = m.group(1)
extra_data = json.loads(extra_data_json.decode('utf-8'))
- data = data[:m.start(0)] + data[m.end(0):]
+ self.buf = self.buf[:m.start(0)] + self.buf[m.end(0):]
if "geometry" in extra_data:
self.rows = extra_data["geometry"][1]
self.cols = extra_data["geometry"][0]
self.vt.set_window_size(self.rows, self.cols)
- self.buf += data
-
clear = self.buf.rfind(b"\033[2J")
if clear != -1:
self.buf = self.buf[clear + 4:]
- # XXX need to check how much was actually processed here, to avoid
- # breaking when escape sequences or utf-8 sequences are split across
- # reads
- self.vt.process(data)
self.idle_since = time.time()
def get_term(self):