Example #1
0
    def read(self, buffer, timeout = TIMEOUT_CURRENT, assume_readable = True):
        """Reads as many bytes as possible the socket into the given buffer.
        The buffer position is updated according to the number of bytes read from the socket.
        This method could possible read 0 bytes. The method returns the total number of bytes read"""
        assert self.state == self.STATE_CONNECTED, "socket must be connected in order to read from it"

        Socket._x += 1
        if Socket._x % XMOD == 0:
            assume_readable = False

        #by default assume that we can read from the socket without blocking
        if assume_readable:
            bytes_read, _ = buffer.recv(self.fd) #read from fd to
            if bytes_read < 0 and _io.get_errno() == EAGAIN:
                #nope, need to wait before reading our data
                assume_readable = False
            #else if error != EAGAIN, assume_readable will stay True, and we fall trough and raise error below

        #if we cannot assume readability we will wait until data can be read again
        if not assume_readable:
            self.readable.wait(timeout = timeout)
            bytes_read, _ = buffer.recv(self.fd) #read from fd to

        #print 'br', bytes_read, buffer.capacity
        #
        if bytes_read < 0:
            raise _io.error_from_errno(IOError)
        else:
            return bytes_read
Example #2
0
    def read(self, buffer, timeout=TIMEOUT_CURRENT, assume_readable=True):
        """Reads as many bytes as possible the socket into the given buffer.
        The buffer position is updated according to the number of bytes read from the socket.
        This method could possible read 0 bytes. The method returns the total number of bytes read"""
        assert self.state == self.STATE_CONNECTED, "socket must be connected in order to read from it"

        Socket._x += 1
        if Socket._x % XMOD == 0:
            assume_readable = False

        #by default assume that we can read from the socket without blocking
        if assume_readable:
            bytes_read, _ = buffer.recv(self.fd)  #read from fd to
            if bytes_read < 0 and _io.get_errno() == EAGAIN:
                #nope, need to wait before reading our data
                assume_readable = False
            #else if error != EAGAIN, assume_readable will stay True, and we fall trough and raise error below

        #if we cannot assume readability we will wait until data can be read again
        if not assume_readable:
            self.readable.wait(timeout=timeout)
            bytes_read, _ = buffer.recv(self.fd)  #read from fd to

        #print 'br', bytes_read, buffer.capacity
        #
        if bytes_read < 0:
            raise _io.error_from_errno(IOError)
        else:
            return bytes_read
Example #3
0
    def write(self, buffer, timeout = TIMEOUT_CURRENT, assume_writable = True):
        """Writes as many bytes as possible from the given buffer to this socket.
        The buffer position is updated according to the number of bytes succesfully written to the socket.
        This method returns the total number of bytes written. This method could possible write 0 bytes"""
        assert self.state == self.STATE_CONNECTED, "socket must be connected in order to write to it"

        Socket._x += 1
        if Socket._x % XMOD == 0:
            assume_writable = False

        #by default assume that we can write to the socket without blocking
        if assume_writable:
            bytes_written, _ = buffer.send(self.fd) #write to fd from buffer
            if bytes_written < 0 and _io.get_errno() == EAGAIN:
                #nope, need to wait before sending our data
                assume_writable = False
            #else if error != EAGAIN, assume_writable will stay True, and we fall trough and raise error below

        #if we cannot assume write-ability we will wait until data can be written again
        if not assume_writable:
            self.writable.wait(timeout = timeout)
            bytes_written, _ = buffer.send(self.fd) #write to fd from buffer

        #print 'bw', bytes_written, buffer.capacity
        #
        if bytes_written < 0:
            raise _io.error_from_errno(IOError)
        else:
            return bytes_written
Example #4
0
    def write(self, buffer, timeout=TIMEOUT_CURRENT, assume_writable=True):
        """Writes as many bytes as possible from the given buffer to this socket.
        The buffer position is updated according to the number of bytes succesfully written to the socket.
        This method returns the total number of bytes written. This method could possible write 0 bytes"""
        assert self.state == self.STATE_CONNECTED, "socket must be connected in order to write to it"

        Socket._x += 1
        if Socket._x % XMOD == 0:
            assume_writable = False

        #by default assume that we can write to the socket without blocking
        if assume_writable:
            bytes_written, _ = buffer.send(self.fd)  #write to fd from buffer
            if bytes_written < 0 and _io.get_errno() == EAGAIN:
                #nope, need to wait before sending our data
                assume_writable = False
            #else if error != EAGAIN, assume_writable will stay True, and we fall trough and raise error below

        #if we cannot assume write-ability we will wait until data can be written again
        if not assume_writable:
            self.writable.wait(timeout=timeout)
            bytes_written, _ = buffer.send(self.fd)  #write to fd from buffer

        #print 'bw', bytes_written, buffer.capacity
        #
        if bytes_written < 0:
            raise _io.error_from_errno(IOError)
        else:
            return bytes_written