def _clearCallbacks(self): """Clear any callbacks added by this class. Called just after the socket is closed. """ BaseSocket._clearCallbacks(self) self._connCallback = None if self._endpointDeferred is not None: self._endpointDeferred.cancel() self._endpointDeferred = None if self._protocol is not None: self._protocol.roAbort() self._protocol = None
def __init__( self, host, port, readCallback=nullCallback, stateCallback=nullCallback, tkSock=None, timeLim=None, name="", ): """Construct a TCPSocket Inputs: - host IP address as dotted name or dotted numbers - port IP port - readCallback function to call when data read; receives: self - stateCallback function to call when state or reason changes; receives: self - tkSock existing tk socket (if missing, one is created and connected) - timeLim time limit to make connection (sec); no limit if None or 0 - name a string to identify this socket; strictly optional """ self._host = host self._port = port self._connectTimer = RO.TkUtil.Timer() self.__buffer = "" BaseSocket.__init__( self, readCallback=readCallback, stateCallback=stateCallback, name=name, ) if tkSock is None: sockArgs = ('-async', host, port) else: sockArgs = None self._tkSocketWrapper = _TkSocketWrapper(tkSock=tkSock, sockArgs=sockArgs, name=name) try: # add callbacks; the write callback indicates the socket is connected # and is just used to detect state self._tkSocketWrapper.setCallback(self._doRead, doWrite=False) self._tkSocketWrapper.setCallback(self._doConnect, doWrite=True) except Tkinter.TclError as e: raise RuntimeError(e) self._setState(self.Connecting) if timeLim: self._connectTimer.start(timeLim, self._connectTimeout) self._checkSocket()
def __init__(self, endpoint = None, protocol = None, state = BaseSocket.Connected, readCallback = nullCallback, stateCallback = nullCallback, timeLim = None, name = "", lineTerminator = "\r\n", ): """Construct a Socket Inputs: - endpoint a Twisted endpoint, e.g. twisted.internet.endpoints.TCP4ClientEndpoint; - protocol a Twisted protocol; you must either specify endpoint or protocol, but not both - state the initial state - readCallback function to call when data read; receives: self - stateCallback a state callback function; see addStateCallback for details - timeLim time limit to make connection (sec); no limit if None or 0 - name a string to identify this socket; strictly optional - lineTerminator specifies the terminator used in writeLine """ #print "Socket(name=%r, endpoint=%r, protocol=%r, state=%r, readCallback=%r, stateCallback=%r)" % \ # (name, endpoint, protocol, state, readCallback, stateCallback) if bool(endpoint is None) == bool(protocol is None): raise RuntimeError("Must provide one of endpoint or protocol") self._endpoint = endpoint self._endpointDeferred = None self._protocol = None self._data = None self._connectTimer = Timer() BaseSocket.__init__(self, state = state, readCallback = readCallback, stateCallback = stateCallback, name = name, lineTerminator = lineTerminator ) if protocol is not None: self._connectionMade(protocol) else: if timeLim: self._connectTimer.start(timeLim, self._connectTimeout) self._setState(BaseSocket.Connecting) self._endpointDeferred = self._endpoint.connect(_SocketProtocolFactory()) setCallbacks( deferred = self._endpointDeferred, callback = self._connectionMade, errback = self._connectionLost, )
def __init__(self, host, port, readCallback = nullCallback, stateCallback = nullCallback, tkSock = None, timeLim = None, name = "", lineTerminator = "\r\n", ): """Construct a TCPSocket Inputs: - host IP address as dotted name or dotted numbers - port IP port - readCallback function to call when data read; receives: self - stateCallback function to call when state or reason changes; receives: self - tkSock existing tk socket (if missing, one is created and connected) - timeLim time limit to make connection (sec); no limit if None or 0 - name a string to identify this socket; strictly optional """ self._host = host self._port = port self._connectTimer = RO.TkUtil.Timer() self.__buffer = "" BaseSocket.__init__(self, readCallback = readCallback, stateCallback = stateCallback, name = name, lineTerminator=lineTerminator, ) if tkSock is None: sockArgs = ('-async', host, port) else: sockArgs = None self._tkSocketWrapper = _TkSocketWrapper(tkSock=tkSock, sockArgs=sockArgs, name=name) try: # add callbacks; the write callback indicates the socket is connected # and is just used to detect state self._tkSocketWrapper.setCallback(self._doRead, doWrite=False) self._tkSocketWrapper.setCallback(self._doConnect, doWrite=True) except Tkinter.TclError as e: raise RuntimeError(e) self._setState(self.Connecting) if timeLim: self._connectTimer.start(timeLim, self._connectTimeout) self._checkSocket()
def __init__( self, endpoint=None, protocol=None, state=BaseSocket.Connected, readCallback=nullCallback, stateCallback=nullCallback, timeLim=None, name="", ): """Construct a Socket Inputs: - endpoint a Twisted endpoint, e.g. twisted.internet.endpoints.TCP4ClientEndpoint; - protocol a Twisted protocol; you must either specify endpoint or protocol, but not both - state the initial state - readCallback function to call when data read; receives: self - stateCallback a state callback function; see addStateCallback for details - timeLim time limit to make connection (sec); no limit if None or 0 - name a string to identify this socket; strictly optional """ #print "Socket(name=%r, endpoint=%r, protocol=%r, state=%r, readCallback=%r, stateCallback=%r)" % \ # (name, endpoint, protocol, state, readCallback, stateCallback) if bool(endpoint is None) == bool(protocol is None): raise RuntimeError("Must provide one of endpoint or protocol") self._endpoint = endpoint self._endpointDeferred = None self._protocol = None self._data = None self._connectTimer = Timer() BaseSocket.__init__(self, state=state, readCallback=readCallback, stateCallback=stateCallback, name=name) if protocol is not None: self._connectionMade(protocol) else: if timeLim: self._connectTimer.start(timeLim, self._connectTimeout) self._setState(BaseSocket.Connecting) self._endpointDeferred = self._endpoint.connect( _SocketProtocolFactory()) setCallbacks( deferred=self._endpointDeferred, callback=self._connectionMade, errback=self._connectionLost, )
def _clearCallbacks(self): """Clear any callbacks added by this class. Called just after the socket is closed. """ BaseSocket._clearCallbacks(self) self._tkSocketWrapper.clearCallbacks()
def _setState(self, *args, **kwargs): BaseSocket._setState(self, *args, **kwargs) if self.isReady or self.isDone: self._connectTimer.cancel()