def connect(self, topic, addr, port): addr = get_ip() port = 9500 self.sock.connect((addr, port)) print("Client:") print(" remote:", self.sock.sock.getpeername()) print(" local:", self.sock.sock.getsockname()) self.topic = topic
def client(e): unpacker = Sensor("<3dL") def myfunc(data): d = unpacker.unpack(data) print(f">> Received {d}[{len(data)}]") sub = Subscriber() sub.connect("bob", get_ip(), 9500) sub.event = e sub.subscribe("bob", myfunc) sub.loop() print("end client ------------------")
def bind(self, topic, port=None): addr = get_ip() self.sock.bind(addr, port) addr, port = self.sock.bindaddress print(f">> Binding for {topic} on {addr}:{port}") self.topic = topic
def main(): args = handle_args() # if args["version"]: # print(f">> udp_server version {version}") # exit(0) port = args["port"] host = args["host"] if host is None: host = get_ip() jpeg_quality = args["quality"] size = args["size"] camera = args["camera"] gray = args["grayscale"] # gray = True grabber = VideoGrabber(jpeg_quality, size, camera, gray) grabber.daemon = True grabber.start() running = True sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Bind the socket to the port server_address = (host, port) # address = server_address print(f'{host_name}[{host}]:{port} at {size}\n') sock.bind(server_address) try: while running: try: data_packed, address = sock.recvfrom(struct.calcsize('<L')) data = struct.unpack('<L', data_packed)[0] if data == 1: buffer = grabber.get_buffer() if buffer is None: sock.sendto(struct.pack('<L', struct.calcsize('<L')), address) sock.sendto(struct.pack('<L', 404), address) #capture error continue # print(len(buffer)) if len(buffer) > MAX_PACKET_SIZE: split = MAX_PACKET_SIZE #65000 num = len(buffer) // split rem = len(buffer) % split print(f"{num} {rem}") sock.sendto(struct.pack('<LB', len(buffer), num + 1), address) for i in range(num): sock.sendto(buffer[i * split:i * split + split], address) sock.sendto(buffer[-rem:], address) continue sock.sendto(struct.pack('<LB', len(buffer), 1), address) sock.sendto(buffer, address) elif data == 0: grabber.stop() running = False except Exception as e: bb = buffer # print(bb) print( f"{Fore.RED}*** {e} buffer: {len(bb)} {type(bb)} ***{Fore.RESET}" ) time.sleep(1) except KeyboardInterrupt: print("ctrl-C ...") grabber.stop() running = False print("Quitting..") grabber.join() sock.close()