Fill raw queue from exactly one recv() system call.

        Set self.eof when connection is closed.
198718
def fill_rawq(self):
        
        if self.irawq >= len(self.rawq):
            self.rawq = b''
            self.irawq = 0
        # The buffer size should be fairly small so as to avoid quadratic
        # behavior in process_rawq() above
        buf = yield from self._reader.read(50)
        self.eof = (not buf)
        self.rawq = self.rawq + buf
1332946
def flush(self):
        
        if self.buffer is None:
            return
        data = self.buffer
        self.buffer = []
        for x in self.dests:
            yield from x.enqueue_task(self, *data)
975009
def pipe_data_received(self, fd, data):
        """"""
        if fd == 2:  # stderr fd number
            self._on_stderr(data)
        elif self._on_data:
            self._on_data(data)
        else:
            self._queued_data.append(data)
162132
def _fill_buffer(self):
        
        # If we cannot fetch anymore from the server then we're done here.
        if self._all_results_fetched:
            self._eof()

        raw_results = self.result_fetcher(page=self._page, per_page=self.per_page)

        entities = []
        for raw in raw_results:
            entities.append(self.entity.deserialize(raw, bind_client=self.bind_client))

        self._buffer = collections.deque(entities)

        self.log.debug("Requested page {0} (got: {1} items)".format(self._page,
                                                                    len(self._buffer)))
        if len(self._buffer) < self.per_page:
            self._all_results_fetched = True

        self._page += 1
877799
def __recv_run(self):  # pylint: disable=too-many-branches,too-many-statements
        
        while not self.__end.is_set():
            self.__unacked = 0
            self.__last_id = None

            try:
                self.__recv_ready.clear()  # Ensure event is cleared for EG network failure/retry loop
                with Connection(userid=self.__prefix + self.__epid,
                                password=self.__passwd,
                                virtual_host=self.__vhost,
                                heartbeat=self.__heartbeat,
                                connect_timeout=self.__socket_timeout,
                                operation_timeout=self.__socket_timeout,
                                ssl=self.__get_ssl_context(self.__sslca),
                                host=self.__host) as conn,\
                        conn.channel(auto_encode_decode=False) as channel_data,\
                        conn.channel() as channel_ka:
                    logger.debug('Connected, using cipher %s', conn.transport.sock.cipher()[0])

                    channel_data.basic_qos(prefetch_size=0, prefetch_count=self.__prefetch, a_global=False)
                    # exclusive=True.  There can be only one (receiver)
                    msgtag = channel_data.basic_consume(queue=self.__epid, exclusive=True, callback=self.__recv_cb)
                    acktag = channel_ka.basic_consume(queue=('%s_ka' % self.__epid), exclusive=True, no_ack=True,
                                                      callback=self.__recv_ka_cb)
                    self.__ka_channel = channel_ka
                    self.__recv_exc_clear(log_if_exc_set='reconnected')
                    self.__recv_ready.set()
                    try:
                        #
                        # Drain loop
                        while not self.__end.is_set():
                            try:
                                while not self.__end.is_set() and self.__unacked < self.__ack_threshold:
                                    # inner loop to handle all outstanding amqp messages
                                    conn.drain_events(.1)
                            except SocketTimeout:
                                pass
                            # either have waited for .1s or threshold reached, so always ack
                            if self.__unacked:
                                logger.debug('acking (%d) up to %s', self.__unacked, self.__last_id)
                                channel_data.basic_ack(self.__last_id, multiple=True)
                                self.__unacked = 0
                            conn.heartbeat_tick()
                    finally:
                        self.__recv_ready.clear()
                        try:
                            channel_data.basic_cancel(msgtag)
                            channel_ka.basic_cancel(acktag)
                        except:
                            pass

            except exceptions.AccessRefused:
                self.__recv_log_set_exc_and_wait('Access Refused (Credentials already in use?)')
            except exceptions.ConnectionForced:
                self.__recv_log_set_exc_and_wait('Disconnected by broker (ConnectionForced)')
            except SocketTimeout:
                self.__recv_log_set_exc_and_wait('SocketTimeout exception.  wrong credentials, vhost or prefix?')
            except SSLError:
                self.__recv_log_set_exc_and_wait('ssl.SSLError Bad Certificate?')
            except (exceptions.AMQPError, SocketError):
                self.__recv_log_set_exc_and_wait('amqp/transport failure, sleeping before retry')
            except:
                self.__recv_log_set_exc_and_wait('unexpected failure, exiting', wait_seconds=0)
                break

        logger.debug('finished')
1055506
def _flush(self, finish=False):
    
    while ((finish and self._buffered >= 0) or
           (not finish and self._buffered >= self._blocksize)):
      tmp_buffer = []
      tmp_buffer_len = 0

      excess = 0
      while self._buffer:
        buf = self._buffer.popleft()
        size = len(buf)
        self._buffered -= size
        tmp_buffer.append(buf)
        tmp_buffer_len += size
        if tmp_buffer_len >= self._maxrequestsize:
          excess = tmp_buffer_len - self._maxrequestsize
          break
        if not finish and (
            tmp_buffer_len % self._blocksize + self._buffered <
            self._blocksize):
          excess = tmp_buffer_len % self._blocksize
          break

      if excess:
        over = tmp_buffer.pop()
        size = len(over)
        assert size >= excess
        tmp_buffer_len -= size
        head, tail = over[:-excess], over[-excess:]
        self._buffer.appendleft(tail)
        self._buffered += len(tail)
        if head:
          tmp_buffer.append(head)
          tmp_buffer_len += len(head)

      data = ''.join(tmp_buffer)
      file_len = '*'
      if finish and not self._buffered:
        file_len = self._written + len(data)
      self._send_data(data, self._written, file_len)
      self._written += len(data)
      if file_len != '*':
        break
419649
def recv(self, size):
        
        try:
            buffer = b""
            start = time.time()
            while len(buffer) < size:
                select_timeout = None
                if self.timeout is not None:
                    elapsed = time.time() - start
                    if elapsed >= self.timeout:
                        raise socket.timeout()
                    select_timeout = self.timeout - elapsed

                r, w, x = select([self.process.stdout], [], [], select_timeout)
                if r and r[0] == self.process.stdout:
                    buffer += os.read(
                        self.process.stdout.fileno(), size - len(buffer)
                    )
            return buffer
        except socket.timeout:
            if buffer:
                # Don't raise socket.timeout, return partial result instead
                return buffer
            raise  # socket.timeout is a subclass of IOError
        except IOError as e:
            raise ProxyCommandFailure(" ".join(self.cmd), e.strerror)
642887
def handle_one_request(self):
        """"""
        self.raw_requestline = self.rfile.readline()
        if not self.raw_requestline:
            self.close_connection = 1
        elif self.parse_request():
            return self.run_wsgi()
746500
def recv_callback(self, callback, **kwargs):
        """"""
        payload = None
        timeout = self.get_timeout(**kwargs)
        full_timeout = timeout
        while timeout > 0.0:
            kwargs['timeout'] = timeout
            start = time.time()
            payload = self.recv(**kwargs)
            if callback(payload):
                break
            payload = None
            stop = time.time()
            elapsed = stop - start
            timeout -= elapsed

        if not payload:
            raise IOError("recv_callback timed out in {}".format(full_timeout))

        return payload
1364149
def handle_one_request(self):
        
        try:
            self.raw_requestline = self.rfile.readline(65537)
            if len(self.raw_requestline) > 65536:
                self.requestline = ''
                self.request_version = ''
                self.command = ''
                self.send_error(414)
                return
            if not self.raw_requestline:
                self.close_connection = 1
                return
            if not self.parse_request():
                # An error code has been sent, just exit
                return
            mname = 'do_' + self.command
            if not hasattr(self, mname):
                self.send_error(501, "Unsupported method (%r)" % self.command)
                return
            method = getattr(self, mname)
            method()
            self.wfile.flush() #actually send the response if not already done.
        except socket.timeout:
            #a read or a write timed out.  Discard this connection
            self.log_error("Request timed out: %r", sys.exc_info()[1])
            self.close_connection = 1
            return
262111
async def recv(self):
        
        if self.readyState != 'live':
            raise MediaStreamError

        frame = await self._queue.get()
        if frame is None:
            self.stop()
            raise MediaStreamError
        return frame
665403
def _read_body_until_close(self, response, file):
        '''
        '''
        _logger.debug('Reading body until close.')

        file_is_async = hasattr(file, 'drain')

        while True:
            data = yield from self._connection.read(self._read_size)

            if not data:
                break

            self._data_event_dispatcher.notify_read(data)

            content_data = self._decompress_data(data)

            if file:
                file.write(content_data)

                if file_is_async:
                    yield from file.drain()

        content_data = self._flush_decompressor()

        if file:
            file.write(content_data)

            if file_is_async:
                yield from file.drain()
532747
def recv(self, buffersize, flags=0):
        '''
        
        '''
        _,_,data = self._recv(buffersize)
        return data
575994
def queue(self):
        """"""
        with self.connection_pool.acquire(block=True) as conn:
            return Q(
                self.routing_key,
                exchange=self.exchange,
                routing_key=self.routing_key
            )(conn)
992023
def mock_server_receive(sock, length):
    """"""
    msg = b''
    while length:
        chunk = sock.recv(length)
        if chunk == b'':
            raise socket.error(errno.ECONNRESET, 'closed')

        length -= len(chunk)
        msg += chunk

    return msg
618410
public final void flush() {
        int totalBytes = 0;
        int amountToPoll = outgoingPackets.size();
        
        Packet packet;

        boolean shouldEncrypt = encryption != null;
        
        var queue = new ArrayDeque<byte[]>();

        while (amountToPoll-- > 0 && (packet = outgoingPackets.poll()) != null) {
            int currentBytes = totalBytes;

            boolean tooBig = (totalBytes += packet.getSize(this)) >= bufferSize;
            boolean empty = outgoingPackets.isEmpty();

            if (!tooBig || empty) {
                queue.addAll(packet.getQueue());
            }

            // If we've buffered all of the packets that we can, send them off.
            if (tooBig || empty) {
                var raw = DIRECT_BUFFER_POOL.take(empty ? totalBytes : currentBytes);
                
                byte[] input;
                
                try {
                    while ((input = queue.pollFirst()) != null) {
                        raw.put(shouldEncrypt ? encryption.doFinal(input) : input);
                    }
                } catch (Exception e) {
                    throw new IllegalStateException("An exception occurred whilst encrypting data:", e);
                }
                
                raw.flip();
                
                queue.addAll(packet.getQueue());
                
                // It is important to synchronize on the packetsToFlush here, as we don't want the callback to
                // complete before the packet is queued.
                synchronized (packetsToFlush) {
                    if (!writing.getAndSet(true)) {
                        channel.write(raw, new Tuple<>(this, raw), PACKET_HANDLER);
                    } else {
                        packetsToFlush.offerFirst(raw);
                    }
                }

                totalBytes = packet.getSize(this);
            }
        }
    }
992853
def receive(self, sock):
    """"""
    msg = None
    data = b''
    recv_done = False
    recv_len = -1
    while not recv_done:
      buf = sock.recv(BUFSIZE)
      if buf is None or len(buf) == 0:
        raise Exception("socket closed")
      if recv_len == -1:
        recv_len = struct.unpack('>I', buf[:4])[0]
        data += buf[4:]
        recv_len -= len(data)
      else:
        data += buf
        recv_len -= len(buf)
      recv_done = (recv_len == 0)

    msg = pickle.loads(data)
    return msg