|
@@ -8,6 +8,7 @@ from twisted.internet import protocol
|
|
|
from twisted.internet import reactor
|
|
|
from twisted.python import log
|
|
|
from twisted.enterprise import adbapi
|
|
|
+from twisted.protocols.basic import LineReceiver
|
|
|
|
|
|
|
|
|
HOST = "127.0.0.1"
|
|
@@ -17,19 +18,33 @@ LISTEN_PORT = 9999
|
|
|
LISTEN_ON = "127.0.0.1"
|
|
|
|
|
|
|
|
|
+class LineRecv(LineReceiver):
|
|
|
+ def __init__(self, notify):
|
|
|
+ self.delimiter = b"\n"
|
|
|
+ self.notify = notify
|
|
|
+
|
|
|
+ def lineReceived(self, line):
|
|
|
+ log.msg("LR:", line)
|
|
|
+ self.notify.gotLine(line)
|
|
|
+
|
|
|
+
|
|
|
class PlayerProtocol(protocol.Protocol):
|
|
|
def __init__(self):
|
|
|
self.user = None
|
|
|
self.dbinit = False
|
|
|
self.db = None
|
|
|
self.buffer = ""
|
|
|
- self.lines = list()
|
|
|
+ self.linereader = LineRecv(self)
|
|
|
+ self.linereader.connectionMade()
|
|
|
|
|
|
def connectionMade(self):
|
|
|
log.msg("Client: connected to peer")
|
|
|
self.queue_twgs = self.factory.queue_twgs
|
|
|
self.queue_twgs.get().addCallback(self.serverDataReceived)
|
|
|
|
|
|
+ def gotLine(self, line):
|
|
|
+ log.msg(">>> [{0}]".format(line.decode("utf-8", "ignore")))
|
|
|
+
|
|
|
def serverDataReceived(self, chunk):
|
|
|
|
|
|
|
|
@@ -54,25 +69,14 @@ class PlayerProtocol(protocol.Protocol):
|
|
|
|
|
|
zpos = self.buffer.rindex("\x00")
|
|
|
self.buffer = self.buffer[zpos + 1 :]
|
|
|
+ self.buffer = ""
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
- pos = self.buffer.find("\x08")
|
|
|
- while pos != -1:
|
|
|
-
|
|
|
- self.buffer = self.buffer[0 : pos - 1] + self.buffer[pos + 1 :]
|
|
|
- pos = self.buffer.find("\x08")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- pos = self.buffer.find("\r")
|
|
|
- while pos != -1:
|
|
|
- line = self.buffer[0:pos]
|
|
|
- self.lines.append(line)
|
|
|
- log.msg("L> {0}".format(line))
|
|
|
- self.buffer = self.buffer[pos + 1 :]
|
|
|
- pos = self.buffer.find("\r")
|
|
|
+
|
|
|
+
|
|
|
+ self.linereader.dataReceived(chunk)
|
|
|
|
|
|
|
|
|
|
|
@@ -98,7 +102,8 @@ class PlayerProtocol(protocol.Protocol):
|
|
|
|
|
|
|
|
|
|
|
|
- log.msg("<<", chunk.decode("utf-8", "ignore"))
|
|
|
+
|
|
|
+ log.msg("<<", repr(chunk))
|
|
|
self.factory.queue_client.put(chunk)
|
|
|
|
|
|
def connectionLost(self, why):
|
|
@@ -111,7 +116,8 @@ class PlayerProtocol(protocol.Protocol):
|
|
|
self.transport.loseConnection()
|
|
|
|
|
|
|
|
|
-class GlueFactory(protocol.ClientFactory):
|
|
|
+
|
|
|
+class GlueFactory(protocol.Factory):
|
|
|
maxDelay = 10
|
|
|
protocol = PlayerProtocol
|
|
|
|
|
@@ -123,6 +129,11 @@ class GlueFactory(protocol.ClientFactory):
|
|
|
log.msg("closeIt")
|
|
|
self.queue_client.put(False)
|
|
|
|
|
|
+
|
|
|
+ def clientConnectionLost(self, connector, why):
|
|
|
+ log.msg("clientconnectionlost: %s" % why)
|
|
|
+ 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")
|
|
@@ -136,8 +147,8 @@ class GlueFactory(protocol.ClientFactory):
|
|
|
|
|
|
class TWGSServer(protocol.Protocol):
|
|
|
def connectionMade(self):
|
|
|
- self.queue_client = defer.DeferredQueue()
|
|
|
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)
|