def open(name, dscp=DSCP_DEFAULT): """Attempts to connect a stream to a remote peer. 'name' is a connection name in the form "TYPE:ARGS", where TYPE is an active stream class's name and ARGS are stream class-specific. The supported TYPEs include "unix", "tcp", and "ssl". Returns (error, stream): on success 'error' is 0 and 'stream' is the new Stream, on failure 'error' is a positive errno value and 'stream' is None. Never returns errno.EAGAIN or errno.EINPROGRESS. Instead, returns 0 and a new Stream. The connect() method can be used to check for successful connection completion.""" cls = Stream._find_method(name) if not cls: return errno.EAFNOSUPPORT, None suffix = name.split(":", 1)[1] if name.startswith("unix:"): suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix) if sys.platform == 'win32': pipename = winutils.get_pipe_name(suffix) if len(suffix) > 255: # Return invalid argument if the name is too long return errno.ENOENT, None try: # In case of "unix:" argument, the assumption is that # there is a file created in the path (suffix). open(suffix, 'r').close() except: return errno.ENOENT, None try: npipe = winutils.create_file(pipename) try: winutils.set_pipe_mode(npipe, win32pipe.PIPE_READMODE_BYTE) except pywintypes.error as e: return errno.ENOENT, None except pywintypes.error as e: if e.winerror == winutils.winerror.ERROR_PIPE_BUSY: # Pipe is busy, set the retry flag to true and retry # again during the connect function. Stream.retry_connect = True return 0, cls(None, name, errno.EAGAIN, pipe=win32file.INVALID_HANDLE_VALUE, is_server=False) return errno.ENOENT, None return 0, cls(None, name, 0, pipe=npipe, is_server=False) error, sock = cls._open(suffix, dscp) if error: return error, None else: status = ovs.socket_util.check_connection_completion(sock) return 0, cls(sock, name, status)
def create_file(self, sec_attributes=-1): """Create the file for the named pipe and store it in the '_npipe_file' property of the class. :param sec_attributes: type win32security.SECURITY_ATTRIBUTES The default value is -1 which uses the default security attributes. This means that the file handle will NOT be inherited when a new process is created. """ try: # Create the file using the name of the named pipe with the given # security attributes self._npipe_file = ovs_winutils.create_file( self.name, attributes=sec_attributes) try: ovs_winutils.set_pipe_mode( self._npipe_file, ovs_winutils.win32pipe.PIPE_READMODE_BYTE) except ovs_winutils.pywintypes.error as e: raise NamedPipeException( "Could not set pipe read mode to byte. " "Error: %s." % e.strerror, e.winerror) except ovs_winutils.pywintypes.error as e: raise NamedPipeException( "Could not create file for named pipe. " "Error: %s." % e.strerror, e.winerror)