Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | None | 0 0
  1. https://github.com/evennia/evennia/blob/master/evennia/server/portal/portal.py#L90
  2. def _portal_maintenance():
  3.     """
  4.    The maintenance function handles repeated checks and updates that
  5.    the server needs to do. It is called every minute.
  6.    """
  7.     # check for idle sessions
  8.     now = time.time()
  9.  
  10.     reason = "Idle timeout exceeded, disconnecting."
  11.     for session in [sess for sess in PORTAL_SESSIONS.values()
  12.                     if (now - sess.cmd_last) > _IDLE_TIMEOUT]:
  13.         # Our Code here
  14.         # session.data_in(disconnect=[text, kwargs])
  15.         session.disconnect(reason=reason)
  16.         PORTAL_SESSIONS.disconnect(session)
  17.  
  18. GOES TO:
  19.  
  20. https://github.com/evennia/evennia/blob/master/evennia/server/portal/webclient.py#L145
  21.     def data_in(self, **kwargs):
  22.         """
  23.         SNIP
  24.         """
  25.  
  26.         if "websocket_close" in kwargs:
  27.             self.disconnect()
  28.             return
  29.  
  30.         self.sessionhandler.data_in(self, **kwargs)
  31.  
  32. GOES TO:
  33.  
  34. https://github.com/evennia/evennia/blob/master/evennia/server/portal/portalsessionhandler.py#L340
  35.  
  36.     def data_in(self, session, **kwargs):
  37.         """
  38.         SNIP
  39.        """
  40.             #SNIP
  41.             kwargs = self.clean_senddata(session, kwargs) ***THIS IS IMPORTANT SEE BELOW
  42.  
  43.             # relay data to Server
  44.             self.command_counter += 1
  45.             session.cmd_last = now
  46.             self.portal.amp_protocol.send_MsgPortal2Server(session,
  47.                                                            **kwargs)   
  48.  
  49. GO FOR A DETOUR THROUGH CLEAN_SENDDATA:
  50. https://github.com/evennia/evennia/blob/master/evennia/server/sessionhandler.py#L157
  51.  
  52.         """
  53.        Clean up data for sending across the AMP wire. Also apply INLINEFUNCS.
  54.        Args:
  55.            session (Session): The relevant session instance.
  56.            kwargs (dict) Each keyword represents a
  57.                send-instruction, with the keyword itself being the name
  58.                of the instruction (like "text"). Suitable values for each
  59.                keyword are:
  60.                    - arg                ->  [[arg], {}]
  61.                    - [args]             ->  [[args], {}]
  62.                    - {kwargs}           ->  [[], {kwargs}]
  63.                    - [args, {kwargs}]   ->  [[arg], {kwargs}]
  64.                    - [[args], {kwargs}] ->  [[args], {kwargs}]
  65.        Returns:
  66.            kwargs (dict): A cleaned dictionary of cmdname:[[args],{kwargs}] pairs,
  67.                where the keys, args and kwargs have all been converted to
  68.                send-safe entities (strings or numbers), and inlinefuncs have been
  69.                applied.
  70.        """
  71.         options = kwargs.pop("options", None) or {}
  72.         raw = options.get("raw", False)
  73.         strip_inlinefunc = options.get("strip_inlinefunc", False)
  74.  
  75.         def _validate(data):
  76.             "Helper function to convert data to AMP-safe (picketable) values"
  77.             if isinstance(data, dict):
  78.                 newdict = {}
  79.                 for key, part in data.items():
  80.                     newdict[key] = _validate(part)
  81.                 return newdict
  82.             elif hasattr(data, "__iter__"):
  83.                 return [_validate(part) for part in data]
  84.             elif isinstance(data, basestring):
  85.                 # make sure strings are in a valid encoding
  86.                 try:
  87.                     data = data and to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"])
  88.                 except LookupError:
  89.                     # wrong encoding set on the session. Set it to a safe one
  90.                     session.protocol_flags["ENCODING"] = "utf-8"
  91.                     data = to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"])
  92.                 if _INLINEFUNC_ENABLED and not raw and isinstance(self, ServerSessionHandler):
  93.                     # only parse inlinefuncs on the outgoing path (sessionhandler->)
  94.                     data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session)
  95.                 return data
  96.             elif hasattr(data, "id") and hasattr(data, "db_date_created") \
  97.                     and hasattr(data, '__dbclass__'):
  98.                 # convert database-object to their string representation.
  99.                 return _validate(unicode(data))
  100.             else:
  101.                 return data
  102.  
  103.         rkwargs = {}
  104.         for key, data in kwargs.iteritems():
  105.             key = _validate(key)
  106.             if not data:
  107.                 if key == "text":
  108.                     # we don't allow sending text = None, this must mean
  109.                     # that the text command is not to be used.
  110.                     continue
  111.                 rkwargs[key] = [ [], {} ]
  112.             elif isinstance(data, dict):
  113.                 rkwargs[key] = [ [], _validate(data) ]
  114.             elif hasattr(data, "__iter__"):
  115.                 if isinstance(data[-1], dict):
  116.                     if len(data) == 2:
  117.                         if hasattr(data[0], "__iter__"):
  118.                             rkwargs[key] = [_validate(data[0]), _validate(data[1])]
  119.                         else:
  120.                             rkwargs[key] = [[_validate(data[0])], _validate(data[1])]
  121.                     else:
  122.                         rkwargs[key] = [ _validate(data[:-1]), _validate(data[-1]) ]
  123.                 else:
  124.                     rkwargs[key] = [ _validate(data), {} ]
  125.             else:
  126.                 rkwargs[key] = [ [_validate(data)], {} ]
  127.             rkwargs[key][1]["options"] = options
  128.         return rkwargs
  129.  
  130. WHICH PUTS WHAT WE SEND IN A USABLE STATE TO SEND THROUGH THE AMP AND IS PICKED UP BY THE SERVERSESSIONHANDLER
  131.  
  132. https://github.com/evennia/evennia/blob/master/evennia/server/sessionhandler.py#L715
  133.     def data_in(self, session, **kwargs):
  134.         """
  135.        We let the data take a "detour" to session.data_in
  136.        so the user can override and see it all in one place.
  137.        That method is responsible to in turn always call
  138.        this class' `sessionhandler.call_inputfunc` with the
  139.        (possibly processed) data.
  140.        """
  141.         if session:
  142.             session.data_in(**kwargs)
  143.  
  144. TO
  145.  
  146. https://github.com/evennia/evennia/blob/master/evennia/server/serversession.py#L381
  147.     def data_in(self, **kwargs):
  148.         """
  149.        Receiving data from the client, sending it off to
  150.        the respective inputfuncs.
  151.        Kwargs:
  152.            kwargs (any): Incoming data from protocol on
  153.                the form `{"commandname": ((args), {kwargs}),...}`
  154.        Notes:
  155.            This method is here in order to give the user
  156.            a single place to catch and possibly process all incoming data from
  157.            the client. It should usually always end by sending
  158.            this data off to `self.sessionhandler.call_inputfuncs(self, **kwargs)`.
  159.        """
  160.         self.sessionhandler.call_inputfuncs(self, **kwargs)
  161.  
  162. TO
  163.  
  164. https://github.com/evennia/evennia/blob/master/evennia/server/sessionhandler.py#L727
  165.  
  166.     def call_inputfuncs(self, session, **kwargs):
  167.         """
  168.        Split incoming data into its inputfunc counterparts.
  169.        This should be called by the serversession.data_in
  170.        as sessionhandler.call_inputfunc(self, **kwargs).
  171.        We also intercept OOB communication here.
  172.        Args:
  173.            sessions (Session): Session.
  174.        Kwargs:
  175.            kwargs (any): Incoming data from protocol on
  176.                the form `{"commandname": ((args), {kwargs}),...}`
  177.        """
  178.  
  179.         # distribute incoming data to the correct receiving methods.
  180.         if session:
  181.             input_debug = session.protocol_flags.get("INPUTDEBUG", False)
  182.             for cmdname, (cmdargs, cmdkwargs) in kwargs.iteritems():
  183.                 cname = cmdname.strip().lower()
  184.                 try:
  185.                     cmdkwargs.pop("options", None)
  186.                     if cname in _INPUT_FUNCS:
  187.                         _INPUT_FUNCS[cname](session, *cmdargs, **cmdkwargs)
  188.                     else:
  189.                         _INPUT_FUNCS["default"](session, cname, *cmdargs, **cmdkwargs)
  190.                 except Exception, err:
  191.                     if input_debug:
  192.                         session.msg(err)
  193.                     log_trace()
  194.  
  195. WHICH THEN ACTS LIKE A COMMANDHANDLER FOR FUNCTIONS IN THE INPUTFUNC FILES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement