<h1>Messagepath<aclass="headerlink"href="#messagepath"title="Permalink to this headline">¶</a></h1>
<p>The main functionality of Evennia is to communicate with clients connected to it; a player enters commands or their client queries for a gui update (ingoing data). The server responds or sends data on its own as the game changes (outgoing data). It’s important to understand how this flow of information works in Evennia.</p>
<divclass="section"id="the-ingoing-message-path">
<h2>The ingoing message path<aclass="headerlink"href="#the-ingoing-message-path"title="Permalink to this headline">¶</a></h2>
<p>We’ll start by tracing data from the client to the server. Here it is in short:</p>
<h3>Client (ingoing)<aclass="headerlink"href="#client-ingoing"title="Permalink to this headline">¶</a></h3>
<p>The client sends data to Evennia in two ways.</p>
<ulclass="simple">
<li><p>When first connecting, the client can send data to the server about its
capabilities. This is things like “I support xterm256 but not unicode” and is
mainly used when a Telnet client connects. This is called a “handshake” and
will generally set some flags on the <aclass="reference internal"href="Portal-And-Server.html"><spanclass="doc">Portal Session</span></a> that
are later synced to the Server Session. Since this is not something the player
controls, we’ll not explore this further here.</p></li>
<li><p>The client can send an <em>inputcommand</em> to the server. Traditionally this only
happens when the player enters text on the command line. But with a custom
client GUI, a command could also come from the pressing of a button. Finally
the client may send commands based on a timer or some trigger.</p></li>
</ul>
<p>Exactly how the inputcommand looks when it travels from the client to Evennia
depends on the <aclass="reference internal"href="Custom-Protocols.html"><spanclass="doc">Protocol</span></a> used:</p>
<ulclass="simple">
<li><p>Telnet: A string. If GMCP or MSDP OOB protocols are used, this string will
be formatted in a special way, but it’s still a raw string. If Telnet SSL is
active, the string will be encrypted.</p></li>
<li><p>SSH: An encrypted string</p></li>
<li><p>Webclient: A JSON-serialized string.</p></li>
</ul>
</div>
<divclass="section"id="portal-session-ingoing">
<h3>Portal Session (ingoing)<aclass="headerlink"href="#portal-session-ingoing"title="Permalink to this headline">¶</a></h3>
<p>Each client is connected to the game via a <em>Portal Session</em>, one per connection. This Session is different depending on the type of connection (telnet, webclient etc) and thus know how to handle that particular data type. So regardless of how the data arrives, the Session will identify the type of the instruction and any arguments it should have. For example, the telnet protocol will figure that anything arriving normally over the wire should be passed on as a “text” type.</p>
<h3>PortalSessionHandler (ingoing)<aclass="headerlink"href="#portalsessionhandler-ingoing"title="Permalink to this headline">¶</a></h3>
<p>The <em>PortalSessionhandler</em> manages all connected Sessions in the Portal. Its <codeclass="docutils literal notranslate"><spanclass="pre">data_in</span></code> method (called by each Portal Session) will parse the command names and arguments from the protocols and convert them to a standardized form we call the <em>inputcommand</em>:</p>
<p>All inputcommands must have a name, but they may or may not have arguments and keyword arguments - in fact no default inputcommands use kwargs at all. The most common inputcommand is “text”, which has the argument the player input on the command line:</p>
<p>This inputcommand-structure is pickled together with the unique session-id of the Session to which it belongs. This is then sent over the AMP connection.</p>
<h3>ServerSessionHandler (ingoing)<aclass="headerlink"href="#serversessionhandler-ingoing"title="Permalink to this headline">¶</a></h3>
<p>On the Server side, the AMP unpickles the data and associates the session id with the server-side <aclass="reference internal"href="Sessions.html"><spanclass="doc">Session</span></a>. Data and Session are passed to the server-side <codeclass="docutils literal notranslate"><spanclass="pre">SessionHandler.data_in</span></code>. This in turn calls <codeclass="docutils literal notranslate"><spanclass="pre">ServerSession.data_in()</span></code></p>
</div>
<divclass="section"id="serversession-ingoing">
<h3>ServerSession (ingoing)<aclass="headerlink"href="#serversession-ingoing"title="Permalink to this headline">¶</a></h3>
<p>The method <codeclass="docutils literal notranslate"><spanclass="pre">ServerSession.data_in</span></code> is meant to offer a single place to override if they want to examine <em>all</em> data passing into the server from the client. It is meant to call the <codeclass="docutils literal notranslate"><spanclass="pre">ssessionhandler.call_inputfuncs</span></code> with the (potentially processed) data (so this is technically a sort of detour back to the sessionhandler).</p>
<p>In <codeclass="docutils literal notranslate"><spanclass="pre">call_inputfuncs</span></code>, the inputcommand’s name is compared against the names of all the <em>inputfuncs</em> registered with the server. The inputfuncs are named the same as the inputcommand they are supposed to handle, so the (default) inputfunc for handling our “look” command is called “text”. These are just normal functions and one can plugin new ones by simply putting them in a module where Evennia looks for such functions.</p>
<p>If a matching inputfunc is found, it will be called with the Session and the inputcommand’s arguments:</p>
<p>If no matching inputfunc is found, an inputfunc named “default” will be tried and if that is also not found, an error will be raised.</p>
</div>
<divclass="section"id="inputfunc">
<h3>Inputfunc<aclass="headerlink"href="#inputfunc"title="Permalink to this headline">¶</a></h3>
<p>The <aclass="reference internal"href="Inputfuncs.html"><spanclass="doc">Inputfunc</span></a> must be on the form <codeclass="docutils literal notranslate"><spanclass="pre">func(session,</span><spanclass="pre">*args,</span><spanclass="pre">**kwargs)</span></code>. An exception is the <codeclass="docutils literal notranslate"><spanclass="pre">default</span></code> inputfunc which has form <codeclass="docutils literal notranslate"><spanclass="pre">default(session,</span><spanclass="pre">cmdname,</span><spanclass="pre">*args,</span><spanclass="pre">**kwargs)</span></code>, where <codeclass="docutils literal notranslate"><spanclass="pre">cmdname</span></code> is the un-matched inputcommand string.</p>
<p>This is where the message’s path diverges, since just what happens next depends on the type of inputfunc was triggered. In the example of sending “look”, the inputfunc is named “text”. It will pass the argument to the <codeclass="docutils literal notranslate"><spanclass="pre">cmdhandler</span></code> which will eventually lead to the <codeclass="docutils literal notranslate"><spanclass="pre">look</span></code> command being executed.</p>
<h3>msg<aclass="headerlink"href="#msg"title="Permalink to this headline">¶</a></h3>
<p>All outgoing messages start in the <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> method. This is accessible from three places:</p>
<p>For our purposes, what is important to know is that with the exception of <codeclass="docutils literal notranslate"><spanclass="pre">from_obj</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">session</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">options</span></code>, all keywords given to the <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> method is the name of an <em>outputcommand</em> and its arguments. So <codeclass="docutils literal notranslate"><spanclass="pre">text</span></code> is actually such a command, taking a string as its argument. The reason <codeclass="docutils literal notranslate"><spanclass="pre">text</span></code> sits as the first keyword argument is that it’s so commonly used (<codeclass="docutils literal notranslate"><spanclass="pre">caller.msg("Text")</span></code> for example). Here are some examples</p>
3</pre></div></td><tdclass="code"><divclass="highlight"><pre><span></span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"Hello!"</span><spanclass="p">)</span><spanclass="c1"># using the 'text' outputfunc</span>
<p>Note the form of the <codeclass="docutils literal notranslate"><spanclass="pre">mycommand</span></code> outputfunction. This explicitly defines the arguments and keyword arguments for the function. In the case of the <codeclass="docutils literal notranslate"><spanclass="pre">text</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">prompt</span></code> calls we just specify a string - this works too: The system will convert this into a single argument for us later in the message path.</p>
<blockquote>
<div><p>Note: The <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> method sits on your Object- and Account typeclasses. It means you can easily override <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> and make custom- or per-object modifications to the flow of data as it passes through.</p>
</div></blockquote>
</div>
<divclass="section"id="serversession-outgoing">
<h3>ServerSession (outgoing)<aclass="headerlink"href="#serversession-outgoing"title="Permalink to this headline">¶</a></h3>
<p>Nothing is processed on the Session, it just serves as a gathering points for all different <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code>. It immediately passes the data on to …</p>
<h3>ServerSessionHandler (outgoing)<aclass="headerlink"href="#serversessionhandler-outgoing"title="Permalink to this headline">¶</a></h3>
<p>In the <em>ServerSessionhandler</em>, the keywords from the <codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> method are collated into one or more <em>outputcommands</em> on a standardized form (identical to inputcommands):</p>
<p>This will intelligently convert different input to the same form. So <codeclass="docutils literal notranslate"><spanclass="pre">msg("Hello")</span></code> will end up as an outputcommand <codeclass="docutils literal notranslate"><spanclass="pre">("text",</span><spanclass="pre">("Hello",),</span><spanclass="pre">{})</span></code>.</p>
<p>This is also the point where <aclass="reference external"href="/TextTags.html#inline-functions">Inlinefuncs</a> are parsed, depending on the session to receive the data. Said data is pickled together with the Session id then sent over the AMP bridge.</p>
<h3>PortalSessionHandler (outgoing)<aclass="headerlink"href="#portalsessionhandler-outgoing"title="Permalink to this headline">¶</a></h3>
<p>After the AMP connection has unpickled the data and paired the session id to the matching PortalSession, the handler next determines if this Session has a suitable method for handling the outputcommand.</p>
<p>The situation is analogous to how inputfuncs work, except that protocols are fixed things that don’t need a plugin infrastructure like the inputfuncs are handled. So instead of an “outputfunc”, the handler looks for methods on the PortalSession with names of the form <codeclass="docutils literal notranslate"><spanclass="pre">send_<commandname></span></code>.</p>
<p>For example, the common sending of text expects a PortalSession method <codeclass="docutils literal notranslate"><spanclass="pre">send_text</span></code>. This will be called as <codeclass="docutils literal notranslate"><spanclass="pre">send_text(*("Hello",),</span><spanclass="pre">**{})</span></code>. If the “prompt” outputfunction was used, send_prompt is called. In all other cases the <codeclass="docutils literal notranslate"><spanclass="pre">send_default(cmdname,</span><spanclass="pre">*args,</span><spanclass="pre">**kwargs)</span></code> will be called - this is the case for all client-custom outputcommands, like when wanting to tell the client to update a graphic or play a sound.</p>
</div>
<divclass="section"id="portalsession-outgoing">
<h3>PortalSession (outgoing)<aclass="headerlink"href="#portalsession-outgoing"title="Permalink to this headline">¶</a></h3>
<p>At this point it is up to the session to convert the command into a form understood by this particular protocol. For telnet, <codeclass="docutils literal notranslate"><spanclass="pre">send_text</span></code> will just send the argument as a string (since that is what telnet clients expect when “text” is coming). If <codeclass="docutils literal notranslate"><spanclass="pre">send_default</span></code> was called (basically everything that is not traditional text or a prompt), it will pack the data as an GMCP or MSDP command packet if the telnet client supports either (otherwise it won’t send at all). If sending to the webclient, the data will get packed into a JSON structure at all times.</p>
</div>
<divclass="section"id="client-outgoing">
<h3>Client (outgoing)<aclass="headerlink"href="#client-outgoing"title="Permalink to this headline">¶</a></h3>
<p>Once arrived at the client, the outputcommand is handled in the way supported by the client (or it may be quietly ignored if not). “text” commands will be displayed in the main window while others may trigger changes in the GUI or play a sound etc.</p>