async def _read_all_responses(self): """ Read all responses corresponding to IDs in ids_set using the supplied StreamReader """ def parse_tuple(bstring): return tuple(float(el) for el in bstring.split(b',')) memory = b'' responses = [] while len(self._ids) > 0: data = await self._reader.read(self._buffer_size) if not data: raise ServerClosedWhileReading all_data = memory + data memory = b'' messages, rest = split_data(all_data, rcommands.DELIMITER) if self._on_recv is not None: self._on_recv(messages, rest) if messages is not None: for msg in messages: msg_id, status, timestamps, pose, tail = split_robot_response(msg) self._ids.remove(msg_id) resp = { 'id': msg_id, 'status': status, 'timestamps': parse_tuple(timestamps), 'pose': parse_tuple(pose) } responses.append(resp) if rest is not None: memory = rest if self._on_done is not None: self._on_done() return responses
def data_received(self, data): self._log('Received: {}'.format(data)) all_data = self._merge_data_with_rest(data) messages, rest = split_data(all_data, DELIMITER) if messages is not None: for msg in messages: msg_id = msg.split(b':')[0] self._ids.remove(msg_id) if len(self._ids) == 0: self._future.set_result(True) if rest is not None: self._update_rest(rest)
def data_received(self, data): self._log('Received: {}'.format(data)) all_data = self._merge_data_with_rest(data) messages, rest = split_data(all_data, DELIMITER) if messages is not None: for msg in messages: msg_id, status, timestamp, tail = split_robot_response(msg) self._ids.remove(msg_id) if len(self._ids) == 0: self._future_session_completed.set_result(True) # close connection ? if rest is not None: self._update_rest(rest)
def read_messages(socket, delimiter=b'\r\n', buffer_size=2048): ''' Reads data from socket, where sequences of bytes separated by the delimiter constitute separate messages. Returns a tuple (messages, rest), where messages correspond to the a list of messages and rest corresponds to the remaining byte string. Possible return compinations are the following: (messages, rest) -- one or more complete messages are received + the rest of bytes (messages, None) -- a complete set of messages is received (None, rest) -- a single sequence of bytes is received (without delimiter) (None, None) -- peer has closed its socket :param socket: a TCP socket object :param delimiter: a bytes object used as a delimiter between messages :param buffer_size: size of the buffer used for reading from the socket :return: a tuple of messages and rest of bytes ''' data = socket.recv(buffer_size) return split_data(data, delimiter)
def data_received(self, data): t0 = self._current_time() self._log('Received: {}'.format(data)) all_data = self._merge_data_with_rest(data) commands, rest = split_data(all_data, DELIMITER) if commands is not None: for command in commands: command_id = command.split(b':')[0] t1 = self._current_time() msg_back = command_id + b':done:' + '{:.3f},{:.3f}'.format( t0, t1).encode() + DELIMITER self._transport.write(msg_back) self._log('Sent back: {}'.format(msg_back)) if rest is not None: self._update_rest(rest)