laneya

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

commit
71b6dd626e0436e4bb22243ce6c28881549f1220
parent
2799c07ad375c928777038c468060a58b4b05080
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-18 08:33
implement NetstringReceiver

this was previously implemented as part of twisted

Diffstat

M laneya/protocol.py 42 +++++++++++++++++++++++++++++++++++++++++-

1 files changed, 41 insertions, 1 deletions


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

@@ -65,7 +65,6 @@ Update
   65    65 import json
   66    66 
   67    67 from twisted.python import log
   68    -1 from twisted.protocols.basic import NetstringReceiver
   69    68 from twisted.internet.protocol import Factory
   70    69 from twisted.internet import reactor
   71    70 
@@ -119,6 +118,43 @@ class LoopingCall(object):
  119   118             self.fn(*self.args, **self.kwargs)
  120   119 
  121   120 
   -1   121 class NetstringReceiver(asyncio.Protocol):
   -1   122     """ protocol that sends and receives netstrings.
   -1   123 
   -1   124     See http://cr.yp.to/proto/netstrings.txt for the specification of
   -1   125     netstrings.
   -1   126 
   -1   127     """
   -1   128     def __init__(self):
   -1   129         self.__buffer = ''
   -1   130         self.transport = None
   -1   131 
   -1   132     def connection_made(self, transport):
   -1   133         self.transport = transport
   -1   134 
   -1   135     def string_received(self, data):
   -1   136         raise NotImplementedError
   -1   137 
   -1   138     def data_received(self, data):
   -1   139         # FIXME: invalid data should not crash the server
   -1   140         self.__buffer += data
   -1   141 
   -1   142         while ':' in self.__buffer:
   -1   143             length, remainder = self.__buffer.split(':', 1)
   -1   144             l = int(length)
   -1   145 
   -1   146             if len(remainder) > int(length):
   -1   147                 assert remainder[l] == ','
   -1   148                 s = remainder[:int(length)]
   -1   149                 self.__buffer = self.__buffer[len('%i:%s,' % (l, s)):]
   -1   150                 self.string_received(s)
   -1   151             else:
   -1   152                 break
   -1   153 
   -1   154     def send_string(self, data):
   -1   155         self.transport.write('%i:%s,' % (len(data), data))
   -1   156 
   -1   157 
  122   158 class JSONProtocol(NetstringReceiver):
  123   159     """Send and receive JSON objects."""
  124   160 
@@ -152,9 +188,11 @@ class ServerProtocol(BaseProtocol):
  152   188     """Default implementation of the server protocol."""
  153   189 
  154   190     def __init__(self, factory):
   -1   191         super(ServerProtocol, self).__init__()
  155   192         self.factory = factory
  156   193 
  157   194     def connection_made(self, transport):
   -1   195         super(ServerProtocol, self).connection_made(transport)
  158   196         self.factory.connections.append(self)
  159   197 
  160   198     def connection_lost(self, reason):
@@ -231,10 +269,12 @@ class ClientProtocol(BaseProtocol):
  231   269     """Default implementation of the client protocol."""
  232   270 
  233   271     def __init__(self, factory):
   -1   272         super(ClientProtocol, self).__init__()
  234   273         self.factory = factory
  235   274         self._response_deferreds = {}
  236   275 
  237   276     def connection_made(self, transport):
   -1   277         super(ClientProtocol, self).connection_made(transport)
  238   278         self.factory.connections.append(self)
  239   279         self.factory.connection_made()
  240   280