Example #1
0
 def pullup(self, length):
     ''' Pullup length bytes from brigade '''
     retval = []
     if self.total >= length:
         while length > 0:
             bucket = self.brigade.popleft()
             if len(bucket) > length:
                 self.brigade.appendleft(six.buff(bucket, length))
                 bucket = six.buff(bucket, 0, length)
             retval.append(BYTES(bucket))
             self.total -= len(bucket)
             length -= len(bucket)
     return EMPTY.join(retval)
Example #2
0
 def pullup(self, length):
     ''' Pullup length bytes from brigade '''
     retval = []
     if self.total >= length:
         while length > 0:
             bucket = self.brigade.popleft()
             if len(bucket) > length:
                 self.brigade.appendleft(six.buff(bucket, length))
                 bucket = six.buff(bucket, 0, length)
             retval.append(BYTES(bucket))
             self.total -= len(bucket)
             length -= len(bucket)
     return EMPTY.join(retval)
Example #3
0
    def handle_write(self):

        #
        # Deal with the case where send() is blocked by recv(), that happens
        # when we are using SSL and recv() returned WANT_WRITE.  In the common
        # case, this costs just one extra if in the fast path.
        #
        if self.send_blocked:
            logging.debug('stream: handle_write() => handle_read()')
            POLLER.set_readable(self)
            if not self.send_octets:
                POLLER.unset_writable(self)
            self.send_blocked = False
            self.handle_read()
            return

        status, count = self.sock.sosend(self.send_octets)

        #
        # Optimisation: reorder if branches such that the ones more relevant
        # for better performance come first.  Testing in early 2011 showed that
        # this arrangement allows to gain a little more speed.  (And the code
        # is still readable.)
        #

        if status == SUCCESS and count > 0:
            self.bytes_out += count

            if count == len(self.send_octets):
                POLLER.unset_writable(self)
                self.send_octets = EMPTY_STRING
                self.send_complete(self)
                return

            if count < len(self.send_octets):
                self.send_octets = six.buff(self.send_octets, count)
                return

            raise RuntimeError('stream: invalid count')

        if status == WANT_WRITE:
            return

        if status == WANT_READ:
            logging.debug('stream: blocking recv()')
            POLLER.unset_writable(self)
            POLLER.set_readable(self)
            self.recv_blocked = True
            return

        if status == CONNRST and count == 0:
            logging.debug('stream: RST')
            self.conn_rst = True
            POLLER.close(self)
            return

        if status == SUCCESS and count < 0:
            raise RuntimeError('stream: negative count')

        raise RuntimeError('stream: invalid status')
Example #4
0
 def skip(self, length):
     ''' Skip up to lenght bytes from brigade '''
     if self.total >= length:
         while length > 0:
             bucket = self.brigade.popleft()
             if len(bucket) > length:
                 self.brigade.appendleft(six.buff(bucket, length))
                 self.total -= length
                 return 0
             length -= len(bucket)
             self.total -= len(bucket)
     return length
Example #5
0
 def skip(self, length):
     ''' Skip up to lenght bytes from brigade '''
     if self.total >= length:
         while length > 0:
             bucket = self.brigade.popleft()
             if len(bucket) > length:
                 self.brigade.appendleft(six.buff(bucket, length))
                 self.total -= length
                 return 0
             length -= len(bucket)
             self.total -= len(bucket)
     return length