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
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
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
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
def read(self, buffer, timeout = -1.0): """Blocks till socket becomes readable and then 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" self.readable.wait(timeout = timeout) bytes_read, _ = buffer.recv(self.fd) #read from fd to if bytes_read < 0: raise _io.error_from_errno(IOError) else: return bytes_read
def write(self, buffer, timeout = -1.0): """Blocks till socket becomes writable and then writes as many bytes as possible from given buffer to the socket. The buffer position is updated according to the number of bytes read from it. This method could possible write 0 bytes. The method returns the total number of bytes written""" assert self.state == self.STATE_CONNECTED, "socket must be connected in order to write to it" self.writable.wait(timeout = timeout) bytes_written, _ = buffer.send(self.fd) #write to fd from buffer if bytes_written < 0: raise _io.error_from_errno(IOError) else: return bytes_written
def _connect(self, addr, timeout = TIMEOUT_CURRENT): assert self.state == self.STATE_INIT, "make sure socket is not already connected or closed" try: err = self.socket.connect_ex(addr) serr = self.socket.getsockopt(_socket.SOL_SOCKET, _socket.SO_ERROR) except Exception: self.log.exception("unexpected exception thrown by connect_ex") raise if err == 0 and serr == 0: self.state = self.STATE_CONNECTED elif err == EINPROGRESS and serr != 0: raise IOError(serr, os.strerror(serr)) elif err == EINPROGRESS and serr == 0: self.state = self.STATE_CONNECTING try: self.writable.wait(timeout = timeout) self.state = self.STATE_CONNECTED except Exception: self.state = self.STATE_INIT raise else: #some other error, #unix domain socket that does not exist, Cannot assign requested address etc etc raise _io.error_from_errno(IOError)
def _connect(self, addr, timeout = -1.0): assert self.state == self.STATE_INIT, "make sure socket is not already connected or closed" try: err = self.socket.connect_ex(addr) serr = self.socket.getsockopt(_socket.SOL_SOCKET, _socket.SO_ERROR) except: self.log.exception("unexpected exception thrown by connect_ex") raise if err == 0 and serr == 0: self.state = self.STATE_CONNECTED elif err == EINPROGRESS and serr != 0: raise IOError(serr, os.strerror(serr)) elif err == EINPROGRESS and serr == 0: self.state = self.STATE_CONNECTING try: self.writable.wait(timeout = timeout) self.state = self.STATE_CONNECTED except: self.state = self.STATE_INIT raise else: #some other error, #unix domain socket that does not exist, Cannot assign requested address etc etc raise _io.error_from_errno(IOError)