# evennia.server.portal.portalsessionhandler

Sessionhandler for portal sessions.

class `evennia.server.portal.portalsessionhandler.``PortalSessionHandler`(*args, **kwargs)

  Bases: `evennia.server.sessionhandler.SessionHandler`

  This object holds the sessions connected to the portal at any time. It is synced with the server’s equivalent SessionHandler over the AMP connection.

  Sessions register with the handler using the connect() method. This will assign a new unique sessionid to the session and send that sessid to the server using the AMP connection.

  `__init__`(*args, **kwargs)

    Init the handler

  `at_server_connection`()

    Called when the Portal establishes connection with the Server. At this point, the AMP connection is already established.

  `generate_sessid`()

    Simply generates a sessid that’s guaranteed to be unique for this Portal run.

    Returns

      sessid

  `connect`(session)

    Called by protocol at first connect. This adds a not-yet authenticated session using an ever-increasing counter for sessid.

    Parameters

      session (PortalSession) – The Session connecting.

    Notes

    We implement a throttling mechanism here to limit the speed at which new connections are accepted - this is both a stop against DoS attacks as well as helps using the Dummyrunner tester with a large number of connector dummies.

  `sync`(session)

    Called by the protocol of an already connected session. This can be used to sync the session info in a delayed manner, such as when negotiation and handshakes are delayed.

    Parameters

      session (PortalSession) – Session to sync.

  `disconnect`(session)

    Called from portal when the connection is closed from the portal side.

    Parameters

      - session (PortalSession) – Session to disconnect.

      - delete (bool, optional) – Delete the session from the handler. Only time to not do this is when this is called from a loop, such as from self.disconnect_all().

  `disconnect_all`()

    Disconnect all sessions, informing the Server.

  `server_connect`(protocol_path='', config={})

    Called by server to force the initialization of a new protocol instance. Server wants this instance to get a unique sessid and to be connected back as normal. This is used to initiate irc/rss etc connections.

    Parameters

      - protocol_path (str) – Full python path to the class factory for the protocol used, eg ‘evennia.server.portal.irc.IRCClientFactory’

      - config (dict) – Dictionary of configuration options, fed as **kwarg to protocol class __init__ method.

    Raises

      RuntimeError – If The correct factory class is not found.

    Notes

    The called protocol class must have a method start() that calls the portalsession.connect() as a normal protocol.

  `server_disconnect`(session, reason='')

    Called by server to force a disconnect by sessid.

    Parameters

      - session (portalsession) – Session to disconnect.

      - reason (str, optional) – Motivation for disconnect.

  `server_disconnect_all`(reason='')

    Called by server when forcing a clean disconnect for everyone.

    Parameters

      reason (str, optional) – Motivation for disconnect.

  `server_logged_in`(session, data)

    The server tells us that the session has been authenticated. Update it. Called by the Server.

    Parameters

      - session (Session) – Session logging in.

      - data (dict) – The session sync data.

  `server_session_sync`(serversessions, clean=True)

    Server wants to save data to the portal, maybe because it’s about to shut down. We don’t overwrite any sessions here, just update them in-place.

    Parameters

      - serversessions (dict) –

        This is a dictionary

        {sessid:{property:value},…} describing the properties to sync on all sessions.

      - clean (bool) – If True, remove any Portal sessions that are not included in serversessions.

  `count_loggedin`(include_unloggedin=False)

    Count loggedin connections, alternatively count all connections.

    Parameters

      - include_unloggedin (bool) – Also count sessions that have

      - yet authenticated. (not) –

    Returns

      count (int) – Number of sessions.

  `sessions_from_csessid`(csessid)

    Given a session id, retrieve the session (this is primarily intended to be called by web clients)

    Parameters

      csessid (int) – Session id.

    Returns

      session (list) – The matching session, if found.

  `announce_all`(message)

    Send message to all connected sessions.

    Parameters

      message (str) – Message to relay.

    Notes

    This will create an on-the fly text-type send command.

  `data_in`(session, **kwargs)

    Called by portal sessions for relaying data coming in from the protocol to the server.

    Parameters

      session (PortalSession) – Session receiving data.

    Keyword Arguments

      kwargs (any) – Other data from protocol.

    Notes

    Data is serialized before passed on.

  `data_out`(session, **kwargs)

    Called by server for having the portal relay messages and data to the correct session protocol.

    Parameters

      session (Session) – Session sending data.

    Keyword Arguments

      kwargs (any) – Each key is a command instruction to the protocol on the form key = [[args],{kwargs}]. This will call a method send_<key> on the protocol. If no such method exixts, it sends the data to a method send_default.