laneya

multiplayer roguelike game
git clone https://git.ce9e.org/laneya.git

commit
a19687ee706884c59c4ce7eb16d35c0e50d0d827
parent
566d59cb94e1e05feb9aeffbfc4cc5d77ffbacb6
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-04 09:57
validate actions before processing

Diffstat

A laneya/actions.py 16 ++++++++++++++++
M laneya/protocol.py 12 ++++++++++++

2 files changed, 28 insertions, 0 deletions


diff --git a/laneya/actions.py b/laneya/actions.py

@@ -0,0 +1,16 @@
   -1     1 """Collection of available actions.
   -1     2 
   -1     3 This module describes the available actions.  The actions are modelled as
   -1     4 functions that may perform arbitrary validation on the incoming data.
   -1     5 
   -1     6 The actual processing of actions happens in the server and client.
   -1     7 
   -1     8 """
   -1     9 
   -1    10 
   -1    11 def echo(foo=''):
   -1    12     """Example server action."""
   -1    13 
   -1    14 
   -1    15 def other(foo=''):
   -1    16     """Example client action."""

diff --git a/laneya/protocol.py b/laneya/protocol.py

@@ -68,6 +68,7 @@ from twisted.python import log
   68    68 from twisted.protocols.basic import NetstringReceiver
   69    69 
   70    70 import deferred as q
   -1    71 import actions
   71    72 
   72    73 
   73    74 key = 0
@@ -134,9 +135,18 @@ class Protocol(JSONProtocol):
  134   135             log.err('Invalid message: %s' % message)
  135   136             raise InvalidError
  136   137 
   -1   138     def validate_action(self, action, data):
   -1   139         try:
   -1   140             fn = getattr(actions, action)
   -1   141             fn(**data)
   -1   142         except:
   -1   143             log.err('Invalid action: %s %s' % (action, data))
   -1   144             raise InvalidError
   -1   145 
  137   146     def jsonReceived(self, message):
  138   147         if message['type'] == 'request':
  139   148             self.validate_message(message, ['action', 'data', 'key', 'type'])
   -1   149             self.validate_action(message['action'], message['data'])
  140   150             self._requestReceived(
  141   151                 message['key'],
  142   152                 message['action'],
@@ -158,7 +168,9 @@ class Protocol(JSONProtocol):
  158   168 
  159   169         elif message['type'] == 'update':
  160   170             self.validate_message(message, ['action', 'data', 'type'])
   -1   171             self.validate_action(message['action'], message['data'])
  161   172             self.updateReceived(message['action'], **message['data'])
   -1   173 
  162   174         else:
  163   175             log.err('Message type not known: %s' % message['type'])
  164   176