Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://github.com/evennia/evennia/blob/master/evennia/server/portal/portal.py#L90
- def _portal_maintenance():
- """
- The maintenance function handles repeated checks and updates that
- the server needs to do. It is called every minute.
- """
- # check for idle sessions
- now = time.time()
- reason = "Idle timeout exceeded, disconnecting."
- for session in [sess for sess in PORTAL_SESSIONS.values()
- if (now - sess.cmd_last) > _IDLE_TIMEOUT]:
- # Our Code here
- # session.data_in(disconnect=[text, kwargs])
- session.disconnect(reason=reason)
- PORTAL_SESSIONS.disconnect(session)
- GOES TO:
- https://github.com/evennia/evennia/blob/master/evennia/server/portal/webclient.py#L145
- def data_in(self, **kwargs):
- """
- SNIP
- """
- if "websocket_close" in kwargs:
- self.disconnect()
- return
- self.sessionhandler.data_in(self, **kwargs)
- GOES TO:
- https://github.com/evennia/evennia/blob/master/evennia/server/portal/portalsessionhandler.py#L340
- def data_in(self, session, **kwargs):
- """
- SNIP
- """
- #SNIP
- kwargs = self.clean_senddata(session, kwargs) ***THIS IS IMPORTANT SEE BELOW
- # relay data to Server
- self.command_counter += 1
- session.cmd_last = now
- self.portal.amp_protocol.send_MsgPortal2Server(session,
- **kwargs)
- GO FOR A DETOUR THROUGH CLEAN_SENDDATA:
- https://github.com/evennia/evennia/blob/master/evennia/server/sessionhandler.py#L157
- """
- Clean up data for sending across the AMP wire. Also apply INLINEFUNCS.
- Args:
- session (Session): The relevant session instance.
- kwargs (dict) Each keyword represents a
- send-instruction, with the keyword itself being the name
- of the instruction (like "text"). Suitable values for each
- keyword are:
- - arg -> [[arg], {}]
- - [args] -> [[args], {}]
- - {kwargs} -> [[], {kwargs}]
- - [args, {kwargs}] -> [[arg], {kwargs}]
- - [[args], {kwargs}] -> [[args], {kwargs}]
- Returns:
- kwargs (dict): A cleaned dictionary of cmdname:[[args],{kwargs}] pairs,
- where the keys, args and kwargs have all been converted to
- send-safe entities (strings or numbers), and inlinefuncs have been
- applied.
- """
- options = kwargs.pop("options", None) or {}
- raw = options.get("raw", False)
- strip_inlinefunc = options.get("strip_inlinefunc", False)
- def _validate(data):
- "Helper function to convert data to AMP-safe (picketable) values"
- if isinstance(data, dict):
- newdict = {}
- for key, part in data.items():
- newdict[key] = _validate(part)
- return newdict
- elif hasattr(data, "__iter__"):
- return [_validate(part) for part in data]
- elif isinstance(data, basestring):
- # make sure strings are in a valid encoding
- try:
- data = data and to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"])
- except LookupError:
- # wrong encoding set on the session. Set it to a safe one
- session.protocol_flags["ENCODING"] = "utf-8"
- data = to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"])
- if _INLINEFUNC_ENABLED and not raw and isinstance(self, ServerSessionHandler):
- # only parse inlinefuncs on the outgoing path (sessionhandler->)
- data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session)
- return data
- elif hasattr(data, "id") and hasattr(data, "db_date_created") \
- and hasattr(data, '__dbclass__'):
- # convert database-object to their string representation.
- return _validate(unicode(data))
- else:
- return data
- rkwargs = {}
- for key, data in kwargs.iteritems():
- key = _validate(key)
- if not data:
- if key == "text":
- # we don't allow sending text = None, this must mean
- # that the text command is not to be used.
- continue
- rkwargs[key] = [ [], {} ]
- elif isinstance(data, dict):
- rkwargs[key] = [ [], _validate(data) ]
- elif hasattr(data, "__iter__"):
- if isinstance(data[-1], dict):
- if len(data) == 2:
- if hasattr(data[0], "__iter__"):
- rkwargs[key] = [_validate(data[0]), _validate(data[1])]
- else:
- rkwargs[key] = [[_validate(data[0])], _validate(data[1])]
- else:
- rkwargs[key] = [ _validate(data[:-1]), _validate(data[-1]) ]
- else:
- rkwargs[key] = [ _validate(data), {} ]
- else:
- rkwargs[key] = [ [_validate(data)], {} ]
- rkwargs[key][1]["options"] = options
- return rkwargs
- WHICH PUTS WHAT WE SEND IN A USABLE STATE TO SEND THROUGH THE AMP AND IS PICKED UP BY THE SERVERSESSIONHANDLER
- https://github.com/evennia/evennia/blob/master/evennia/server/sessionhandler.py#L715
- def data_in(self, session, **kwargs):
- """
- We let the data take a "detour" to session.data_in
- so the user can override and see it all in one place.
- That method is responsible to in turn always call
- this class' `sessionhandler.call_inputfunc` with the
- (possibly processed) data.
- """
- if session:
- session.data_in(**kwargs)
- TO
- https://github.com/evennia/evennia/blob/master/evennia/server/serversession.py#L381
- def data_in(self, **kwargs):
- """
- Receiving data from the client, sending it off to
- the respective inputfuncs.
- Kwargs:
- kwargs (any): Incoming data from protocol on
- the form `{"commandname": ((args), {kwargs}),...}`
- Notes:
- This method is here in order to give the user
- a single place to catch and possibly process all incoming data from
- the client. It should usually always end by sending
- this data off to `self.sessionhandler.call_inputfuncs(self, **kwargs)`.
- """
- self.sessionhandler.call_inputfuncs(self, **kwargs)
- TO
- https://github.com/evennia/evennia/blob/master/evennia/server/sessionhandler.py#L727
- def call_inputfuncs(self, session, **kwargs):
- """
- Split incoming data into its inputfunc counterparts.
- This should be called by the serversession.data_in
- as sessionhandler.call_inputfunc(self, **kwargs).
- We also intercept OOB communication here.
- Args:
- sessions (Session): Session.
- Kwargs:
- kwargs (any): Incoming data from protocol on
- the form `{"commandname": ((args), {kwargs}),...}`
- """
- # distribute incoming data to the correct receiving methods.
- if session:
- input_debug = session.protocol_flags.get("INPUTDEBUG", False)
- for cmdname, (cmdargs, cmdkwargs) in kwargs.iteritems():
- cname = cmdname.strip().lower()
- try:
- cmdkwargs.pop("options", None)
- if cname in _INPUT_FUNCS:
- _INPUT_FUNCS[cname](session, *cmdargs, **cmdkwargs)
- else:
- _INPUT_FUNCS["default"](session, cname, *cmdargs, **cmdkwargs)
- except Exception, err:
- if input_debug:
- session.msg(err)
- log_trace()
- WHICH THEN ACTS LIKE A COMMANDHANDLER FOR FUNCTIONS IN THE INPUTFUNC FILES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement