laneya

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

commit
4a12cede8bbaa688a2d7e94f0db20a7e5d97cfe8
parent
1af1480fb91994230d01aa603d916be6489a05d4
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-04 08:51
implement broadcast in server protocol

havily based on
https://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother

Diffstat

M laneya/server.py 27 +++++++++++++++++++++++++--

1 files changed, 25 insertions, 2 deletions


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

@@ -9,18 +9,41 @@ import protocol
    9     9 
   10    10 
   11    11 class ServerProtocol(protocol.Protocol):
   -1    12     def __init__(self, factory):
   -1    13         self.factory = factory
   -1    14 
   -1    15     def connectionMade(self):
   -1    16         self.factory.connections.append(self)
   -1    17 
   -1    18     def connectionLost(self, reason):
   -1    19         self.factory.connections.remove(self)
   -1    20 
   -1    21     def broadcastUpdate(self, command, **kwargs):
   -1    22         """Broadcast an update to all connected clients."""
   -1    23 
   -1    24         for connection in self.factory.connections:
   -1    25             connection.sendUpdate(command, **kwargs)
   -1    26 
   12    27     def requestReceived(self, command, **kwargs):  # TODO
   13    28         if command == 'echo':
   14    29             return kwargs
   15    30         else:
   16    -1             self.sendUpdate(command, **kwargs)
   -1    31             self.broadcastUpdate(command, **kwargs)
   17    32             return {}
   18    33 
   19    34 
   -1    35 class ServerProtocolFactory(Factory):
   -1    36     def __init__(self):
   -1    37         self.connections = []
   -1    38 
   -1    39     def buildProtocol(self, addr):
   -1    40         return ServerProtocol(self)
   -1    41 
   -1    42 
   20    43 def main():
   21    44     log.startLogging(sys.stdout)
   22    45     endpoint = TCP4ServerEndpoint(reactor, 5001)
   23    -1     endpoint.listen(Factory.forProtocol(ServerProtocol))
   -1    46     endpoint.listen(ServerProtocolFactory())
   24    47     reactor.run()
   25    48 
   26    49