From 496feb0d30c9c2576c2308d3830d9cf63fe71e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 22 Mar 2015 01:00:32 +0100 Subject: [PATCH] Fix send bytes TCP py2/3 --- v2/backend/forwarding/forward.py | 1 - v2/backend/forwarding/receiver.py | 22 +++++++------------ .../PixelControl_TCP/PixelControl_TCP.pyde | 9 ++++---- .../processing/PixelControl_TCP/network.py | 18 +++++++++------ 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/v2/backend/forwarding/forward.py b/v2/backend/forwarding/forward.py index d118c49..9c775d7 100644 --- a/v2/backend/forwarding/forward.py +++ b/v2/backend/forwarding/forward.py @@ -25,7 +25,6 @@ def send(r, s): print(r.llen('new')) data = r.rpop('new') if data is not None and len(data) > 0: - print(len(data)) now = time.time() end = now + wait_time a = bytes([ord('*')]) + bytearray(data) + bytes([ord('#')]) diff --git a/v2/backend/forwarding/receiver.py b/v2/backend/forwarding/receiver.py index 3201ca4..6eba3c9 100644 --- a/v2/backend/forwarding/receiver.py +++ b/v2/backend/forwarding/receiver.py @@ -2,7 +2,6 @@ import socketserver import redis -import time class MyTCPHandler(socketserver.BaseRequestHandler): @@ -26,14 +25,15 @@ class MyTCPHandler(socketserver.BaseRequestHandler): self.imgsize = height * width * 24 def _send_config_to_client(self): - self.request.sendall(bytearray([self.max_height])) - self.request.sendall(bytearray([self.max_width])) - self.request.sendall(bytearray([self.max_framerate])) + self.request.sendall(self.max_height.to_bytes(4, byteorder='little')) + self.request.sendall(self.max_width.to_bytes(4, byteorder='little')) + self.request.sendall(self.max_framerate.to_bytes(4, byteorder='little')) def _receive_client_config(self): - height = int.from_bytes(self.request.recv(1), byteorder='little') - width = int.from_bytes(self.request.recv(1), byteorder='little') - framerate = int.from_bytes(self.request.recv(1), byteorder='little') + height = int.from_bytes(self.request.recv(4), byteorder='little') + width = int.from_bytes(self.request.recv(4), byteorder='little') + framerate = int.from_bytes(self.request.recv(4), byteorder='little') + print(height, width, framerate) good, reason = self._check_config(height, width, framerate) if good: self._set_config(framerate, height, width) @@ -57,17 +57,11 @@ class MyTCPHandler(socketserver.BaseRequestHandler): print(reason) return None print('Start receiving from {}...'.format(self.client_address[0])) - got_one_frame = False while True: data = self.request.recv(self.imgsize) - self.r.lpush('new', data) if len(data) == 0: - if not got_one_frame: - time.sleep(1) - continue break - else: - got_one_frame = True + self.r.lpush('new', data) print('... Done with {}.'.format(self.client_address[0])) diff --git a/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde b/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde index 2da0b55..873a979 100644 --- a/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde +++ b/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde @@ -30,11 +30,11 @@ def TCPConfigure(server, port): return Client(this, server, port) def check_config(max_height, max_width, max_framerate): - if height > max_height: + if height <= 0 or height > max_height: return False, "height cannot be higher than {}. Current: {}.".format(max_height, height) - if width > max_width: + if width <= 0 or width > max_width: return False, "width cannot be higher than {}. Current: {}.".format(max_width, width) - if framerate > max_framerate: + if framerate <= 0 or framerate > max_framerate: return False, "framerate cannot be higher than {}. Current: {}.".format(max_framerate, framerate) return True, None @@ -46,11 +46,10 @@ def setup(): ledTCP = TCPConfigure("127.0.0.1", 9999) max_height, max_width, max_framerate = receive_config(ledTCP) - print(max_height, max_width, max_framerate) good, reason = check_config(max_height, max_width, max_framerate) if not good: raise Exception(reason) - send_config(ledTCP) + send_config(ledTCP, height, width, framerate) size(width, height) dimension = width * height frameRate(framerate) diff --git a/v2/backend/processing/PixelControl_TCP/network.py b/v2/backend/processing/PixelControl_TCP/network.py index 62bd262..6ac285f 100644 --- a/v2/backend/processing/PixelControl_TCP/network.py +++ b/v2/backend/processing/PixelControl_TCP/network.py @@ -1,11 +1,12 @@ import jarray import time +import struct from data_generator import image2data def receive_config(socket): - max_height = jarray.zeros(1, "b") - max_width = jarray.zeros(1, "b") - max_framerate = jarray.zeros(1, "b") + max_height = jarray.zeros(4, "b") + max_width = jarray.zeros(4, "b") + max_framerate = jarray.zeros(4, "b") while True: available_bytes = socket.available() if available_bytes > 0: @@ -14,12 +15,15 @@ def receive_config(socket): socket.readBytes(max_height) socket.readBytes(max_width) socket.readBytes(max_framerate) - return max_height[0], max_width[0], max_framerate[0] + max_height = struct.unpack("