--- a/src/eric7/RemoteServer/EricServer.py Sat Feb 10 11:28:58 2024 +0100 +++ b/src/eric7/RemoteServer/EricServer.py Sun Feb 11 18:35:44 2024 +0100 @@ -18,12 +18,13 @@ import zlib from eric7.UI.Info import Version -# TODO: remove dependency on 'eric7.UI.Info' from .EricRequestCategory import EricRequestCategory from .EricServerDebuggerRequestHandler import EricServerDebuggerRequestHandler from .EricServerFileSystemRequestHandler import EricServerFileSystemRequestHandler +# TODO: remove dependency on 'eric7.UI.Info' + class EricServer: """ @@ -78,6 +79,9 @@ def getSelector(self): """ Public method to get a reference to the selector object. + + @return reference to the selector object + @rtype selectors.BaseSelector """ return self.__selector @@ -118,13 +122,21 @@ @type dict or str @param sock reference to the socket to send the data to @type socket.socket + @return flag indicating a successful transmission + @rtype bool """ if isinstance(jsonCommand, dict): jsonCommand = json.dumps(jsonCommand) + print("Eric Server Send:", jsonCommand) + data = jsonCommand.encode("utf8", "backslashreplace") header = struct.pack(b"!II", len(data), zlib.adler32(data) & 0xFFFFFFFF) - sock.sendall(header) - sock.sendall(data) + try: + sock.sendall(header) + sock.sendall(data) + return True + except BrokenPipeError: + return False def __receiveBytes(self, length, sock): """ @@ -189,6 +201,7 @@ return {} jsonStr = data.decode("utf8", "backslashreplace") + print("Eric Server Receive:", jsonStr) try: return json.loads(jsonStr.strip()) except (TypeError, ValueError) as err: @@ -284,12 +297,16 @@ Private method to close the connection to an eric-ide. """ if self.__connection is not None: - print( - f"Closing 'eric-ide' connection to {self.__connection.getpeername()}." - ) self.__selector.unregister(self.__connection) - self.__connection.shutdown(socket.SHUT_RDWR) - self.__connection.close() + try: + print( + f"Closing 'eric-ide' connection to" + f" {self.__connection.getpeername()}." + ) + self.__connection.shutdown(socket.SHUT_RDWR) + self.__connection.close() + except OSError: + print("'eric-ide' connection gone.") self.__connection = None self.__debuggerRequestHandler.shutdownClients() @@ -308,10 +325,9 @@ self.__closeIdeConnection() return - if category == EricRequestCategory.Server: - if request.lower() == "shutdown": - self.__shouldStop = True - return + if category == EricRequestCategory.Server and request.lower() == "shutdown": + self.__shouldStop = True + return self.__handleRequest(category, request, params, reqestUuid) @@ -346,7 +362,7 @@ while True: try: events = self.__selector.select(timeout=None) - for key, mask in events: + for key, _mask in events: if key.data.name == "server": # it is an event for a server socket key.data.acceptHandler(key.fileobj) @@ -421,7 +437,7 @@ """ self.registerRequestHandler(EricRequestCategory.Echo, self.__handleEchoRequest) self.registerRequestHandler( - EricRequestCategory.Server, self.__handleServerRequest + EricRequestCategory.Server, self.__handleServerRequest ) self.registerRequestHandler(EricRequestCategory.Error, None) # Register a None handler to indicate we are not expecting a request of the @@ -443,8 +459,6 @@ @type dict @param reqestUuid UUID of the associated request as sent by the eric IDE @type str - @exception ValueError raised to indicate an invalid or unsupported request - handler catehory """ try: handler = self.__requestCategoryHandlerRegistry[category] @@ -456,7 +470,7 @@ params={"Category": category}, ) - def __handleEchoRequest(self, request, params, reqestUuid): + def __handleEchoRequest(self, request, params, reqestUuid): # noqa: U100 """ Private method to handle an 'Echo' request. @@ -475,7 +489,7 @@ reqestUuid=reqestUuid, ) - def __handleServerRequest(self, request, params, reqestUuid): + def __handleServerRequest(self, request, params, reqestUuid): # noqa: U100 """ Private method to handle a 'Server' request.