ソースを参照

Trying linereceiver, but doesn't seem to be working.

Steve Thielemann 5 年 前
コミット
d9c90debd4
1 ファイル変更30 行追加19 行削除
  1. 30 19
      tcp-proxy.py

+ 30 - 19
tcp-proxy.py

@@ -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
 
 # Connect to:
 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"  # \r"
+        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):
         # rlogin looks like this:  \x00 password \x00 username \x00 terminal/speed \x00
         # b'\x00up2lat3\x00bugz\x00ansi-bbs/115200\x00'
@@ -54,25 +69,14 @@ class PlayerProtocol(protocol.Protocol):
                     # Reset buffer -- remove everything before last \x00
                     zpos = self.buffer.rindex("\x00")
                     self.buffer = self.buffer[zpos + 1 :]
+                    self.buffer = ""
                     # init sqlite db using the username
             else:
                 # process the buffer
                 # Handle backspaces by deleting previous character.
-                pos = self.buffer.find("\x08")
-                while pos != -1:
-                    # Ok, there's a backspace, so:
-                    self.buffer = self.buffer[0 : pos - 1] + self.buffer[pos + 1 :]
-                    pos = self.buffer.find("\x08")
-
-                # There won't be ansi color codes from the user!
-                # There will be \r\n  (If I remember correctly)
-                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")
+
+                # Send the received data into the linereader for "automatic" line processing.
+                self.linereader.dataReceived(chunk)
 
                 #
                 #
@@ -98,7 +102,8 @@ class PlayerProtocol(protocol.Protocol):
         # log.msg("Client: %d bytes received from peer" % len(chunk))
         # clean, strip ANSI, etc.
 
-        log.msg("<<", chunk.decode("utf-8", "ignore"))
+        # 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.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)
 
+    # This was needed when I replaced ClientFactory with Factory.
+    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)