def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): try: self.log.debug( "WampLongPoll: TX {octets}", octets=_LazyHexFormatter(msg), ) payload, isBinary = self._serializer.serialize(msg) except Exception as e: # all exceptions raised from above should be serialization errors .. f = create_failure() self.log.error( "Unable to serialize WAMP application payload: {msg}", msg=failure_message(f), ) self.log.debug("{tb}", tb=failure_format_traceback(f)) raise SerializationError( "unable to serialize WAMP application payload ({0})". format(e)) else: self._receive.queue(payload) else: raise TransportLost()
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): self.log.trace('{klass}.send() (serializer={serializer}): TX WAMP message: "{msg}"', klass=self.__class__.__name__, msg=msg, serializer=self._serializer) try: payload, _ = self._serializer.serialize(msg) except SerializationError as e: # all exceptions raised from above should be serialization errors .. raise SerializationError("WampRawSocketProtocol: unable to serialize WAMP application payload ({0})".format(e)) else: payload_len = len(payload) if 0 < self._max_len_send < payload_len: emsg = 'tried to send RawSocket message with size {} exceeding payload limit of {} octets'.format( payload_len, self._max_len_send) self.log.warn(emsg) raise PayloadExceededError(emsg) else: self.sendString(payload) self.log.trace('{klass}.send(): TX {octets} octets', klass=self.__class__.__name__, octets=_LazyHexFormatter(payload)) else: raise TransportLost()
def abort(self): """ Implements :func:`autobahn.wamp.interfaces.ITransport.abort` """ if self.isOpen(): self._bailout(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_GOING_AWAY) else: raise TransportLost()
def close(self): """ Implements :func:`autobahn.wamp.interfaces.ITransport.close` """ if self.isOpen(): self.sendClose(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_NORMAL) else: raise TransportLost()
def close(self): """ Implements :func:`autobahn.wamp.interfaces.ITransport.close` """ if self.isOpen(): self.transport.loseConnection() else: raise TransportLost()
def abort(self): """ Implements :func:`autobahn.wamp.interfaces.ITransport.abort` """ if self.isOpen(): self.onClose(True, 1000, u"session aborted") self._receive._kill() del self._parent._transports[self._transport_id] else: raise TransportLost()
def lost(fail): rtn = orig(fail) if not txaio.is_called(done): # asyncio will call connection_lost(None) in case of # a transport failure, in which case we create an # appropriate exception if fail is None: fail = TransportLost("failed to complete connection") txaio.reject(done, fail) return rtn
def onMessage(self, msg): """ Callback fired when a WAMP message was received. May run asynchronously. The callback should return or fire the returned deferred/future when it's done processing the message. In particular, an implementation of this callback must not access the message afterwards. :param msg: The WAMP message received. :type msg: object implementing :class:`autobahn.wamp.interfaces.IMessage` """ if self._session_id is None: # no frontend session established yet, so we expect one of HELLO, ABORT, AUTHENTICATE # https://wamp-proto.org/_static/gen/wamp_latest.html#session-establishment if isinstance(msg, message.Hello): yield self._process_Hello(msg) # https://wamp-proto.org/_static/gen/wamp_latest.html#session-closing elif isinstance(msg, message.Abort): self.transport.send(message.Abort(ApplicationError.AUTHENTICATION_FAILED, message='Client aborted the session opening handshake')) # https://wamp-proto.org/_static/gen/wamp_latest.html#wamp-level-authentication elif isinstance(msg, message.Authenticate): yield self._process_Authenticate(msg) else: raise ProtocolError("Received {} message while proxy frontend session is not joined".format(msg.__class__.__name__)) else: # frontend session is established: process WAMP message if isinstance(msg, message.Hello) or isinstance(msg, message.Abort) or isinstance(msg, message.Authenticate): raise ProtocolError("Received {} message while proxy frontend session is already joined".format(msg.__class__.__name__)) # https://wamp-proto.org/_static/gen/wamp_latest.html#session-closing elif isinstance(msg, message.Goodbye): if self._backend_session: yield self._controller.unmap_backend(self, self._backend_session) self._backend_session = None else: self.log.warn('Frontend session left, but no active backend session to close') # complete the closing handshake (initiated by the client in this case) by replying with GOODBYE self.transport.send(message.Goodbye(message='Client aborted the session opening handshake')) else: if self._backend_session is None or self._backend_session._transport is None: raise TransportLost( "Expected to relay {} message, but proxy backend session or transport is gone".format( msg.__class__.__name__, ) ) else: # if we have an active backend connection, forward the WAMP message .. self._backend_session._transport.send(msg)
def abort(self): """ Implements :func:`autobahn.wamp.interfaces.ITransport.abort` """ if self.isOpen(): if hasattr(self.transport, 'abortConnection'): # ProcessProtocol lacks abortConnection() self.transport.abortConnection() else: self.transport.loseConnection() else: raise TransportLost()
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): try: self.log.debug("WampLongPoll: TX {0}".format(msg)) payload, isBinary = self._serializer.serialize(msg) except Exception as e: # all exceptions raised from above should be serialization errors .. raise SerializationError("unable to serialize WAMP application payload ({0})".format(e)) else: self._receive.queue(payload) else: raise TransportLost()
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): try: if self.factory.debug_wamp: print("TX {0}".format(msg)) payload, isBinary = self._serializer.serialize(msg) except Exception as e: ## all exceptions raised from above should be serialization errors .. raise SerializationError("Unable to serialize WAMP application payload ({0})".format(e)) else: self.sendMessage(payload, isBinary) else: raise TransportLost()
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): self.log.trace("WampRawSocketProtocol (serializer={serializer}): TX WAMP message: {msg}", msg=msg, serializer=self._serializer) try: payload, _ = self._serializer.serialize(msg) except Exception as e: # all exceptions raised from above should be serialization errors .. raise SerializationError("WampRawSocketProtocol: unable to serialize WAMP application payload ({0})".format(e)) else: self.sendString(payload) self.log.trace("WampRawSocketProtocol: TX octets: {octets}", octets=_LazyHexFormatter(payload)) else: raise TransportLost()
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): if self.factory.debug: log.msg("TX WAMP message: {}".format(msg)) try: bytes, _ = self.factory._serializer.serialize(msg) except Exception as e: ## all exceptions raised from above should be serialization errors .. raise SerializationError("Unable to serialize WAMP application payload ({})".format(e)) else: self.sendString(bytes) if self.factory.debug: log.msg("TX octets: {}".format(binascii.hexlify(bytes))) else: raise TransportLost()
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): self.log.debug( "WampRawSocketProtocol: TX WAMP message: {0}".format(msg)) try: payload, _ = self._serializer.serialize(msg) except Exception as e: # all exceptions raised from above should be serialization errors .. raise SerializationError( "WampRawSocketProtocol: unable to serialize WAMP application payload ({0})" .format(e)) else: self.sendString(payload) self.log.debug("WampRawSocketProtocol: TX octets: {0}".format( binascii.hexlify(payload))) else: raise TransportLost()
def on_connect_success(result): # async connect call returns a 2-tuple transport, proto = result # in the case where we .abort() the transport / connection # during setup, we still get on_connect_success but our # transport is already closed (this will happen if # e.g. there's an "open handshake timeout") -- I don't # know if there's a "better" way to detect this? #python # doesn't know of one, anyway if transport.is_closing(): if not txaio.is_called(done): reason = getattr(proto, "_onclose_reason", "Connection already closed") txaio.reject(done, TransportLost(reason)) return # if e.g. an SSL handshake fails, we will have # successfully connected (i.e. get here) but need to # 'listen' for the "connection_lost" from the underlying # protocol in case of handshake failure .. so we wrap # it. Also, we don't increment transport.success_count # here on purpose (because we might not succeed). # XXX double-check that asyncio behavior on TLS handshake # failures is in fact as described above orig = proto.connection_lost @wraps(orig) def lost(fail): rtn = orig(fail) if not txaio.is_called(done): # asyncio will call connection_lost(None) in case of # a transport failure, in which case we create an # appropriate exception if fail is None: fail = TransportLost("failed to complete connection") txaio.reject(done, fail) return rtn proto.connection_lost = lost
def send(self, msg): """ Implements :func:`autobahn.wamp.interfaces.ITransport.send` """ if self.isOpen(): try: self.log.trace( "WAMP SEND: message={message}, session={session}, authid={authid}", authid=self._session._authid, session=self._session._session_id, message=msg, ) payload, isBinary = self._serializer.serialize(msg) except Exception as e: self.log.error("WAMP message serialization error") # all exceptions raised from above should be serialization errors .. raise SerializationError(u"WAMP message serialization error: {0}".format(e)) else: self.sendMessage(payload, isBinary) else: raise TransportLost()