def Recv(self): """Try to receive a message from the socket. In case we already have messages queued, we just return from the queue. Otherwise, we try to read data with a _rwtimeout network timeout, and making sure we don't go over 2x_rwtimeout as a global limit. """ self._CheckSocket() etime = time.time() + self._rwtimeout while not self._msgs: if time.time() > etime: raise errors.TimeoutError("Extended receive timeout") while True: try: data = self.socket.recv(4096) except socket.timeout as err: raise errors.TimeoutError("Receive timeout: %s" % str(err)) except socket.error as err: if err.args and err.args[0] == errno.EAGAIN: continue raise break if not data: raise errors.ConnectionClosedError("Connection closed while reading") new_msgs = (self._buffer + data).split(constants.LUXI_EOM) self._buffer = new_msgs.pop() self._msgs.extend(new_msgs) return self._msgs.popleft().decode("utf-8")
def Recv(self): """Try to receive a message from the read part of the socket. In case we already have messages queued, we just return from the queue. """ self._CheckSocket() while not self._msgs: data = self._rstream.read(4096) if not data: raise errors.ConnectionClosedError("Connection closed while reading") new_msgs = (self._buffer + data).split(constants.LUXI_EOM) self._buffer = new_msgs.pop() self._msgs.extend(new_msgs) return self._msgs.popleft().decode("utf-8")
etime = time.time() + self._rwtimeout while not self._msgs: if time.time() > etime: raise errors.TimeoutError("Extended receive timeout") while True: try: data = self.socket.recv(4096) except socket.timeout, err: raise errors.TimeoutError("Receive timeout: %s" % str(err)) except socket.error, err: if err.args and err.args[0] == errno.EAGAIN: continue raise break if not data: raise errors.ConnectionClosedError( "Connection closed while reading") new_msgs = (self._buffer + data).split(constants.LUXI_EOM) self._buffer = new_msgs.pop() self._msgs.extend(new_msgs) return self._msgs.popleft() def Call(self, msg): """Send a message and wait for the response. This is just a wrapper over Send and Recv. """ self.Send(msg) return self.Recv() @staticmethod