def on_recv(self, handler, addr, data, error): with self._lock: if addr in self.conns: conn = self.conns[addr] if error: if error == 1: msg = "" else: msg = bomb(IOError, IOError("uv error: %s" % error)) else: msg = data # emit last message conn.queue.append(msg) if conn.cr.balance < 0: # someone is waiting, return last message conn.cr.send(self.queue.popleft()) elif len(self.listeners): listener = self.listeners.popleft() if error: listener.c.send_exception(IOError, "uv error: %s" % error) else: conn = UDPConn(addr) conn.queue.append(data) self.conns[addr] = conn listener.c.send(conn, error) else: # we should probably do something there to drop connections self.task.throw(NoMoreListener) schedule()
def _on_read(self, handle, events, error): if error and error is not None: self.readable = False if error == 1: self.closing = True msg = "" else: msg = bomb(IOError, IOError("uv error: %s" % error)) else: self.readable = True msg = None self.cr.send(msg)
def test_bomb(self): try: 1/0 except: import sys b = core.bomb(*sys.exc_info()) assert b.type is ZeroDivisionError if six.PY3: assert (str(b.value).startswith('division by zero') or str(b.value).startswith('int division')) else: assert str(b.value).startswith('integer division') assert b.traceback is not None
def _read(self): buf = bytearray(DEFAULT_BUFFER_SIZE) try: recvd = recv_into(self.client, buf) msg = bytes(buf[0:recvd]) except socket.error: ex = sys.exc_info()[1] if ex.args[0] == EBADF: msg = "" if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: msg = bomb(ex, sys.exc_info()[2]) exc_clear() return msg
def _on_read(self, handle, data, error): if error: if error == 1: # EOF msg = "" else: msg = bomb(IOError, IOError("uv error: %s" % error)) else: msg = data # append the message to the queue self.queue.append(msg) if self.cr.balance < 0: # someone is waiting, return last message self.cr.send(self.queue.popleft())
def read(self): if self.closing: return "" while True: try: retval = self.queue.popleft() if self.cr.balance < 0: self.cr.send(retval) if isinstance(retval, bomb): retval.raise_() return retval except IndexError: pass msg = None buf = bytearray(DEFAULT_BUFFER_SIZE) try: recvd = recv_into(self.client, buf) msg = bytes(buf[0:recvd]) except socket.error: ex = sys.exc_info()[1] if ex.args[0] == EBADF: msg = "" self.closing = True if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: msg = bomb(ex, sys.exc_info()[2]) self.closing = True exc_clear() if msg is None: res = self._watch_read() if res is not None: self.queue.append(res) else: self.queue.append(msg)