Changeset 108:7b985f3a0ee1 in xplra


Ignore:
Timestamp:
12/15/22 19:27:40 (2 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

The Python client can connect to X-Plane over TCP

File:
1 edited

Legend:

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

    r103 r108  
    121121## Hotkey modifier: Control
    122122HOTKEY_MODIFIER_CONTROL = 0x0200
     123
     124#-------------------------------------------------------------------------------
     125
     126## The default TCP port ("XR")
     127DEFAULT_TCP_PORT = 0x5852;
    123128
    124129#-------------------------------------------------------------------------------
     
    296301        self._multiBuffers = []
    297302
    298     def connect(self):
     303    def connect(self, address = None):
    299304        """Try to connect to X-Plane."""
    300305        if self._stream is not None:
    301306            return
    302307
    303         if os.name=="nt":
    304             pipe = Win32NamedPipe(r'\\.\pipe\\xplra')
    305             self._stream = io.BufferedRWPair(pipe, pipe)
    306         else:
    307             import socket
    308             s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    309             s.connect("/tmp/xplra-" + os.environ["LOGNAME"])
    310             self._stream = s.makefile("rwb")
     308        if address:
     309            if address.startswith("tcp:"):
     310                address = address[4:]
     311                colonIndex = address.find(":")
     312                if (colonIndex<=0):
     313                    port = DEFAULT_TCP_PORT
     314                else:
     315                    port = int(address[colonIndex+1:])
     316                    address = address[:colonIndex]
     317
     318                import socket
     319                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     320                print("Connecting to TCP address '%s':%d" %
     321                      (address, port))
     322                s.connect((address, port))
     323                self._stream = s.makefile("rwb")
     324            elif address!="local":
     325                raise ValueError("Invalid address: " + address)
     326
     327        if self._stream is None:
     328            if os.name=="nt":
     329                pipe = Win32NamedPipe(r'\\.\pipe\\xplra')
     330                self._stream = io.BufferedRWPair(pipe, pipe)
     331            else:
     332                import socket
     333                s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     334                s.connect("/tmp/xplra-" + os.environ["LOGNAME"])
     335                self._stream = s.makefile("rwb")
    311336
    312337        for multiBuffer in self._multiBuffers:
Note: See TracChangeset for help on using the changeset viewer.