diff options
author | jaseg <code@jaseg.net> | 2016-01-02 00:48:14 +0100 |
---|---|---|
committer | jaseg <code@jaseg.net> | 2016-01-02 01:29:02 +0100 |
commit | fefb33736af4afddf539a1eff6d2d5d257d57f36 (patch) | |
tree | 57cc374251cc54a6c3c06fc6ac654effb6eb9e22 /host/crap.py | |
parent | 330e1eb20ec751256ea1cee7a6b100e1cb2d3a72 (diff) | |
download | matelight-fefb33736af4afddf539a1eff6d2d5d257d57f36.tar.gz matelight-fefb33736af4afddf539a1eff6d2d5d257d57f36.tar.bz2 matelight-fefb33736af4afddf539a1eff6d2d5d257d57f36.zip |
Host: Made rendering pipeline a bit more flexible
Diffstat (limited to 'host/crap.py')
-rw-r--r-- | host/crap.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/host/crap.py b/host/crap.py index db9f7ee..80fb647 100644 --- a/host/crap.py +++ b/host/crap.py @@ -4,17 +4,20 @@ import struct import zlib import io from time import time +import numpy import config class CRAPClient: def __init__(self, ip='127.0.0.1', port=1337): self.ip, self.port = ip, port - self.sock = socket.Socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.close = self.sock.close def sendframe(self, frame): - self.sock.sendto(frame, (self.ip, self.port)) + fb = numpy.frombuffer(frame, dtype=numpy.uint8) + fb.shape = config.frame_size, len(frame)/config.frame_size + self.sock.sendto(fb[:,:3].tobytes(), (self.ip, self.port)) def _timestamped_recv(sock): @@ -23,6 +26,8 @@ def _timestamped_recv(sock): data, addr = sock.recvfrom(config.frame_size*3+4) except io.BlockingIOError as e: raise StopIteration() + except socket.timeout: + raise StopIteration() else: yield time(), data, addr @@ -34,6 +39,7 @@ class CRAPServer: self.sock.setblocking(blocking) self.sock.bind((ip, port)) + self.blocking = blocking self.current_client = None self.last_timestamp = 0 self.begin_timestamp = 0 @@ -45,13 +51,14 @@ class CRAPServer: def __iter__(self): for timestamp, data, (addr, sport) in _timestamped_recv(self.sock): if data is None: - yield None + yield None, None - if timestamp - self.last_timestamp > config.udp_timeout\ + if timestamp - self.last_timestamp >= config.udp_timeout\ or timestamp - self.begin_timestamp > config.udp_switch_interval: self.current_client = addr self.begin_timestamp = timestamp self.log('\x1B[91mAccepting UDP data from\x1B[0m', addr) + self.sock.settimeout(config.udp_timeout) if addr == self.current_client: if len(data) == config.frame_size*3+4: @@ -63,6 +70,8 @@ class CRAPServer: elif len(data) != config.frame_size*3: self.log('Error receiving UDP frame: Invalid frame size: {}'.format(len(data))) self.last_timestamp = timestamp - yield data + yield 'udp:'+addr, data + self.current_client = None + self.sock.settimeout(None if self.blocking else 0) |