Fix send bytes TCP py2/3

master
Raphaël Vinot 2015-03-22 01:00:32 +01:00
parent d163dfe7bd
commit 496feb0d30
4 changed files with 23 additions and 27 deletions

View File

@ -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('#')])

View File

@ -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]))

View File

@ -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)

View File

@ -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("<i", max_height)[0]
max_width = struct.unpack("<i", max_width)[0]
max_framerate = struct.unpack("<i", max_framerate)[0]
return max_height, max_width, max_framerate
def send_config(socket, height, width, framerate):
socket.write(height)
socket.write(width)
socket.write(framerate)
socket.write(struct.pack('<i', height))
socket.write(struct.pack('<i', width))
socket.write(struct.pack('<i', framerate))
def send_TCP(socket, data, long_line, gammatable):
image2data(data, long_line, gammatable)