Skip to content

Commit

Permalink
Connecting sometimes is too quick, change some ordering logic to fix …
Browse files Browse the repository at this point in the history
…that
  • Loading branch information
LeonarddeR committed Jul 29, 2023
1 parent 5b52306 commit 547246f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
3 changes: 2 additions & 1 deletion addon/lib/driver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ def _updateCallback_supportedSettings(
attribute: protocol.AttributeT,
settings: Iterable[DriverSetting]
):
log.debug(f"Initializing settings accessor for {len(settings)} settings")
self._settingsAccessor = SettingsAccessorBase.createFromSettings(self, settings) if settings else None
self._handleRemoteDriverChange()

def _handleRemoteDriverChange(self):
return
log.debug("Handling remote driver change")

def _get_supportedSettings(self):
settings = []
Expand Down
4 changes: 3 additions & 1 deletion addon/lib/driver/settingsAccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ def createFromSettings(cls, driver: "RemoteDriver", settings: Iterable[driverSet
dct[f"_set_{s.id}"] = cls._makeSetSetting(s.id)
if not isinstance(s, (driverSetting.BooleanDriverSetting, driverSetting.NumericDriverSetting)):
dct[f"_get_{cls._getAvailableSettingsPropertyName(s.id)}"] = cls._makeGetAvailableSettings(s.id)
log.debug("Constructed dictionary to generate new dynamic SettingsAccessor")
return type("SettingsAccessor", (SettingsAccessorBase, ), dct)(driver, settingNames)

def _get_driver(self):
return self._driverRef()

def __init__(self, driver: "RemoteDriver", settingNames: List[str]):
log.debug(f"Initializing {self} for driver {driver}, settings {settingNames}")
self._driverRef = weakref.ref(driver)
self._settingNames = settingNames
for name in self._settingNames:
self.driver.requestRemoteAttribute(self._getSettingAttributeName(name))
driver.requestRemoteAttribute(self._getSettingAttributeName(name))

@classmethod
def _getSettingAttributeName(cls, setting: str) -> protocol.AttributeT:
Expand Down
24 changes: 14 additions & 10 deletions addon/lib/namedPipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ def _handleConnectCallback(self, parameter: int, timerOrWaitFired: bool):
raise WinError()
self.pipeProcessId = clientProcessId.value
self.pipeParentProcessId = getParentProcessId(self.pipeProcessId)
self._initialRead()
if self._onConnected is not None:
self._onConnected(True)
log.debug("End of handleConnectCallback for {self.pipeName}")
self._connectOl = None
self._initialRead()

def _onReadError(self, error: int):
winErr = WinError(error)
Expand Down Expand Up @@ -288,6 +288,19 @@ def __init__(
)
if fileHandle == INVALID_HANDLE_VALUE:
raise WinError()
try:
if pipeMode:
dwPipeMode = DWORD(pipeMode)
if not windll.kernel32.SetNamedPipeHandleState(fileHandle, byref(dwPipeMode), 0, 0):
raise WinError()
serverProcessId = c_ulong()
if not windll.kernel32.GetNamedPipeServerProcessId(HANDLE(fileHandle), byref(serverProcessId)):
raise WinError()
self.pipeProcessId = serverProcessId.value
self.pipeParentProcessId = getParentProcessId(self.pipeProcessId)
except Exception:
winKernel.closeHandle(fileHandle)
raise
super().__init__(
pipeName,
fileHandle,
Expand All @@ -296,15 +309,6 @@ def __init__(
ioThread=ioThread,
pipeMode=pipeMode,
)
if pipeMode:
dwPipeMode = DWORD(pipeMode)
if not windll.kernel32.SetNamedPipeHandleState(fileHandle, byref(dwPipeMode), 0, 0):
raise WinError()
serverProcessId = c_ulong()
if not windll.kernel32.GetNamedPipeServerProcessId(HANDLE(fileHandle), byref(serverProcessId)):
raise WinError()
self.pipeProcessId = serverProcessId.value
self.pipeParentProcessId = getParentProcessId(self.pipeProcessId)

def close(self):
super().close()
Expand Down
3 changes: 2 additions & 1 deletion addon/lib/protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def clearCache(self):
self._pendingAttributeRequests.clear()

def setAttributeRequestPending(self, attribute, state: bool = True):
log.debug(f"Request pending for attribute {attribute!r} set to {state!r}")
self._pendingAttributeRequests[attribute] = state

def isAttributeRequestPending(self, attribute):
Expand All @@ -273,8 +274,8 @@ def _getDefaultValue(self, attribute: AttributeT) -> AttributeValueT:
def _invokeUpdateCallback(self, attribute: AttributeT, value: AttributeValueT):
handler = self._getRawHandler(attribute)
if handler._updateCallback is not None:
log.debug(f"Calling update callback {handler._updateCallback!r} for attribute {attribute!r}")
callback = handler._updateCallback.__get__(handler.__self__)
log.debug(f"Calling update callback {callback!r} for attribute {attribute!r}")
handler.__self__._bgExecutor.submit(callback, attribute, value)

def getValue(self, attribute: AttributeT, fallBackToDefault: bool = False):
Expand Down

0 comments on commit 547246f

Please sign in to comment.