123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import sys
- import re
- from twisted.internet import defer
- from twisted.internet import protocol
- from twisted.internet import reactor
- from twisted.python import log
- from twisted.enterprise import adbapi
- HOST = "127.0.0.1"
- PORT = 2002
- LISTEN_PORT = 9999
- LISTEN_ON = "127.0.0.1"
- class PlayerProtocol(protocol.Protocol):
- def __init__(self):
- self.user = None
- self.dbinit = False
- self.db = None
- self.buffer = ""
- def connectionMade(self):
- log.msg("Client: connected to peer")
- self.queue_twgs = self.factory.queue_twgs
- self.queue_twgs.get().addCallback(self.serverDataReceived)
- def serverDataReceived(self, chunk):
-
-
-
-
- if chunk is False:
- self.queue_twgs = None
- log.msg("Client: disconnecting from peer")
- self.factory.continueTrying = False
- self.transport.loseConnection()
- else:
-
- if self.user is None:
- self.buffer += chunk.decode("utf-8", "ignore")
-
-
-
- parts = self.buffer.split("\x00")
- if len(parts) >= 5:
-
- self.user = parts[1]
- log.msg("User: {0}".format(self.user))
-
- zpos = self.buffer.rindex("\x00")
- self.buffer = self.buffer[zpos + 1 :]
- self.buffer = ""
-
-
-
-
-
-
-
-
-
-
- self.transport.write(chunk)
- self.queue_twgs.get().addCallback(self.serverDataReceived)
-
-
-
-
-
-
-
-
-
-
- def dataReceived(self, chunk):
-
-
-
-
- self.factory.queue_client.put(chunk)
- def connectionLost(self, why):
- log.msg("connectionLost because: %s" % why)
-
-
-
- self.factory.queue_client.put(False)
- self.queue_twgs = None
- self.transport.loseConnection()
- class GlueFactory(protocol.ClientFactory):
-
- maxDelay = 10
- protocol = PlayerProtocol
- def __init__(self, queue_client, queue_twgs):
- self.queue_client = queue_client
- self.queue_twgs = queue_twgs
- def closeIt(self):
- log.msg("closeIt")
- self.queue_client.put(False)
-
-
-
-
- def clientConnectionFailed(self, connector, why):
- log.msg("connectionFailed: %s" % why)
- self.queue_client.put(b"Sorry! I'm Unable to connect to the game server.\r\n")
-
-
- reactor.callLater(2, self.closeIt)
- class TWGSServer(protocol.Protocol):
- def __init__(self):
- self.buffer = ""
- def connectionMade(self):
- self.queue_twgs = defer.DeferredQueue()
- self.queue_client = defer.DeferredQueue()
- self.queue_client.get().addCallback(self.clientDataReceived)
- factory = GlueFactory(self.queue_client, self.queue_twgs)
- reactor.connectTCP(HOST, PORT, factory, 5)
- def gotLine(self, line):
-
- log.msg(">>> [{0}]".format(line))
- def clientDataReceived(self, chunk):
- if chunk is False:
- self.transport.loseConnection()
- else:
- self.buffer += chunk.decode("utf-8", "ignore")
-
- while "\x08" in self.buffer:
- part = self.buffer.partition("\x08")
- self.buffer = part[0][:-1] + part[2]
-
-
- while "\n" in self.buffer:
- part = self.buffer.partition("\n")
- line = part[0].replace("\r", "")
- self.gotLine(line)
- self.buffer = part[2]
-
- self.transport.write(chunk)
- self.queue_client.get().addCallback(self.clientDataReceived)
- def dataReceived(self, chunk):
-
- self.queue_twgs.put(chunk)
- def connectionLost(self, why):
- log.msg("lost connection %s" % why)
- self.queue_twgs.put(False)
- def connectionFailed(self, why):
- log.msg("connectionFailed: %s" % why)
- if __name__ == "__main__":
- log.startLogging(sys.stdout)
- factory = protocol.Factory()
- factory.protocol = TWGSServer
- reactor.listenTCP(LISTEN_PORT, factory, interface=LISTEN_ON)
- reactor.run()
|