mirror of
https://github.com/SantaSpeen/winConnect.git
synced 2026-04-18 22:40:05 +00:00
just structure
This commit is contained in:
@@ -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()
|
||||
@@ -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()
|
||||
0
winConnect/connectors/socket/WinConnectClient.py
Normal file
0
winConnect/connectors/socket/WinConnectClient.py
Normal file
0
winConnect/connectors/socket/WinConnectServer.py
Normal file
0
winConnect/connectors/socket/WinConnectServer.py
Normal file
0
winConnect/connectors/socket/WinConnectTCPSocket.py
Normal file
0
winConnect/connectors/socket/WinConnectTCPSocket.py
Normal file
0
winConnect/connectors/socket/__init__.py
Normal file
0
winConnect/connectors/socket/__init__.py
Normal file
Reference in New Issue
Block a user