From bb7d9d6813ba0864a231d6c71c87b832a47c254d Mon Sep 17 00:00:00 2001 From: SantaSpeen Date: Sat, 15 Mar 2025 02:40:28 +0300 Subject: [PATCH] just structure --- winConnect/WinConnectClient.py | 71 ------------------- winConnect/WinConnectDaemon.py | 63 ---------------- .../connectors/socket/WinConnectClient.py | 0 .../connectors/socket/WinConnectServer.py | 0 .../connectors/socket/WinConnectTCPSocket.py | 0 winConnect/connectors/socket/__init__.py | 0 6 files changed, 134 deletions(-) delete mode 100644 winConnect/WinConnectClient.py delete mode 100644 winConnect/WinConnectDaemon.py create mode 100644 winConnect/connectors/socket/WinConnectClient.py create mode 100644 winConnect/connectors/socket/WinConnectServer.py create mode 100644 winConnect/connectors/socket/WinConnectTCPSocket.py create mode 100644 winConnect/connectors/socket/__init__.py diff --git a/winConnect/WinConnectClient.py b/winConnect/WinConnectClient.py deleted file mode 100644 index 173f56e..0000000 --- a/winConnect/WinConnectClient.py +++ /dev/null @@ -1,71 +0,0 @@ -import pywintypes -import win32file - -from .WinConnectBase import WinConnectBase -from .exceptions import WinConnectConnectionNoPipeException - - -class WinConnectClient(WinConnectBase): - # see: https://mhammond.github.io/pywin32/win32pipe__CreateNamedPipe_meth.html - pipe_desiredAccess = win32file.GENERIC_READ | win32file.GENERIC_WRITE # Access mode (read/write) - pipe_shareMode = 0 # Share mode (None) - pipe_sa = None # Security attributes - pipe_CreationDisposition = win32file.OPEN_EXISTING # Open mode (open existing) - pipe_flagsAndAttributes = 0 # Flags and attributes - pipe_hTemplateFile = None # Template file - - def __init__(self, pipe_name: str): - super().__init__(pipe_name) - - def _open_pipe(self): - try: - self._pipe = win32file.CreateFile( - self._pipe_name, - self.pipe_desiredAccess, - self.pipe_shareMode, - self.pipe_sa, - self.pipe_CreationDisposition, - self.pipe_flagsAndAttributes, - self.pipe_hTemplateFile - ) - self._opened = True - self._connected = True - self._log.debug(f"[{self._pipe_name}] Pipe opened") - except pywintypes.error as e: - if e.winerror == 2: - exc = WinConnectConnectionNoPipeException(f"Error while opening pipe: Pipe not found") - exc.real_exc = e - raise exc - raise e - - def _init(self, program_name="NoName"): - self._send_message("cmd", b"get_session_settings:" + program_name.encode(self.encoding)) - self._init_session() - - def _close_session(self): - """Send close command to server""" - if not self.closed: - self._send_message("cmd", b"close:") - - def __check_pipe(self): - if not self._opened: - self._open_pipe() - if not self._inited: - self._init() - - def __enter__(self): - self.__check_pipe() - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - self.close() - - def connect(self, program_name: str="NoName"): - """Connect to server and initialize session""" - self._open_pipe() - self._init(program_name) - return self - - def read_pipe(self): - self.__check_pipe() - return self._read() diff --git a/winConnect/WinConnectDaemon.py b/winConnect/WinConnectDaemon.py deleted file mode 100644 index c974a74..0000000 --- a/winConnect/WinConnectDaemon.py +++ /dev/null @@ -1,63 +0,0 @@ -import win32pipe - -from .WinConnectBase import WinConnectBase -from .crypto import WinConnectCrypto - - -class WinConnectDaemon(WinConnectBase): - # see: https://mhammond.github.io/pywin32/win32pipe__CreateNamedPipe_meth.html - pipe_openMode = win32pipe.PIPE_ACCESS_DUPLEX # Open mode (read/write) - pipe_pipeMode = win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT # Pipe mode (message type, message read mode, blocking mode) - pipe_nMaxInstances = 1 # Max number of instances - pipe_nDefaultTimeOut = 0 # ~ ms - pipe_sa = None # Security attributes - - def __init__(self, pipe_name: str): - super().__init__(pipe_name) - self.run = True - - def _open_pipe(self): - pipe_nOutBufferSize, pipe_nInBufferSize = self._body_max_size+20, self._body_max_size+20 - self._log.debug(f"[{self._pipe_name}] Creating pipe. " - f"Settings: {self.pipe_openMode=}, {self.pipe_pipeMode=}, {self.pipe_nMaxInstances=}, " - f"{pipe_nOutBufferSize=}, {pipe_nInBufferSize=}, {self.pipe_nDefaultTimeOut=}, {self.pipe_sa=}") - self._pipe = win32pipe.CreateNamedPipe( - self._pipe_name, - self.pipe_openMode, - self.pipe_pipeMode, - self.pipe_nMaxInstances, - pipe_nOutBufferSize, - pipe_nInBufferSize, - self.pipe_nDefaultTimeOut, - self.pipe_sa - ) - self._opened = True - self._log.debug(f"[{self._pipe_name}] Pipe opened") - - def _close_session(self): - self.run = False - - def wait_client(self): - if not self._opened: - self._open_pipe() - win32pipe.ConnectNamedPipe(self._pipe, None) - self._connected = True - self._log.debug(f"[{self._pipe_name}] Client connected") - - def read_pipe(self): - if not self._connected: - self.wait_client() - if not self._inited: - self._init_session() - # if not self._read(): - # raise - return self._read() - - def listen(self): - while self.run: - yield self.read_pipe() - self.stop() - - def stop(self): - self.run = False - self.close() diff --git a/winConnect/connectors/socket/WinConnectClient.py b/winConnect/connectors/socket/WinConnectClient.py new file mode 100644 index 0000000..e69de29 diff --git a/winConnect/connectors/socket/WinConnectServer.py b/winConnect/connectors/socket/WinConnectServer.py new file mode 100644 index 0000000..e69de29 diff --git a/winConnect/connectors/socket/WinConnectTCPSocket.py b/winConnect/connectors/socket/WinConnectTCPSocket.py new file mode 100644 index 0000000..e69de29 diff --git a/winConnect/connectors/socket/__init__.py b/winConnect/connectors/socket/__init__.py new file mode 100644 index 0000000..e69de29