Changeset 64:29a745311929 in xplra


Ignore:
Timestamp:
04/07/13 06:25:41 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added a proper implementation of the stream for Win32

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/client/python/xplra.py

    r57 r64  
    110110#-------------------------------------------------------------------------------
    111111
     112if os.name=="nt":
     113    import win32file
     114    import io
     115
     116    class Win32NamedPipe(io.RawIOBase):
     117        """A stream object to represent a Win32 named pipe."""
     118        def __init__(self, name):
     119            """Construct the pipe with the given name."""
     120            self._handle = win32file.CreateFile(name,
     121                                                win32file.GENERIC_READ |
     122                                                win32file.GENERIC_WRITE,
     123                                                0, None,
     124                                                win32file.OPEN_EXISTING, 0, None)
     125
     126        def close(self):
     127            """Close the pipe, if not closed already."""
     128            if self._handle is not None:
     129                win32file.CloseHandle(self._handle)
     130                self._handle = None
     131
     132        @property
     133        def closed(self):
     134            """Determine if the pipe is closed or not."""
     135            return self._handle is None
     136
     137        def fileno(self):
     138            """Get the file number, which is impossible."""
     139            raise IOError("Win32NamedPipe.fileno: not supported")
     140
     141        def flush(self):
     142            """Flush the stream."""
     143            pass
     144
     145        def isatty(self):
     146            """Determine if the stream is a terminal, which it is not."""
     147            return False
     148
     149        def read(self, n = -1):
     150            """Read at most the given number of bytes from the stream."""
     151            if n==-1:
     152                return self.readall()
     153            else:
     154                (error, data) = win32file.ReadFile(self._handle, n)
     155                return data
     156
     157        def readable(self):
     158            """Determine if the stream is readable, which it is."""
     159            return self._handle is not None
     160
     161        def readall(self):
     162            """Read all bytes from the stream, which is not supported."""
     163            raise IOError("Win32NamedPipe.readall: not supported")
     164
     165        def readinto(self, buffer):
     166            """Read into the given buffer."""
     167            (error, data) = win32file.ReadFile(self._handle, len(buffer))
     168            length = len(data)
     169            buffer[:length] = data
     170            return length
     171
     172        def readline(self, limit = -1):
     173            """Read a line, which is currently not supported."""
     174            raise IOError("Win32NamedPipe.readline: not supported")
     175
     176        def readlines(self, hint = -1):
     177            """Read lines, which is currently not supported."""
     178            raise IOError("Win32NamedPipe.readlines: not supported")
     179
     180        def seek(self, offset, whence = io.SEEK_SET):
     181            """Seek in the stream, which is not supported."""
     182            raise IOError("Win32NamedPipe.seek: not supported")
     183
     184        def seekable(self):
     185            """Determine if the stream is seekable, which it is not."""
     186            return False
     187
     188        def tell(self):
     189            """Get the current position, which is not supported."""
     190            raise IOError("Win32NamedPipe.tell: not supported")
     191
     192        def truncate(self, size = None):
     193            """Truncate the stream, which is not supported."""
     194            raise IOError("Win32NamedPipe.truncate: not supported")
     195
     196        def writable(self):
     197            """Determine if the stream is writable, which it is."""
     198            return self._handle is not None
     199
     200        def write(self, buffer):
     201            """Write the given buffer into the stream."""
     202            (error, written) = win32file.WriteFile(self._handle, buffer.tobytes())
     203            return written
     204
     205        def writelines(self, lines):
     206            """Write the given lines, which is not supported."""
     207            raise IOError("Win32NamedPipe.writelines: not supported")
     208
     209#-------------------------------------------------------------------------------
     210
    112211class XPlane(object):
    113212    """The main class representing the connection to X-Plane."""
     
    141240
    142241        if os.name=="nt":
    143             self._stream = open(r'\\.\pipe\\xplra', "b")
     242            pipe = Win32NamedPipe(r'\\.\pipe\\xplra')
     243            self._stream = io.BufferedRWPair(pipe, pipe)
    144244        else:
    145245            import socket
Note: See TracChangeset for help on using the changeset viewer.