2009-12-14 55 views
2

我有以下代码(聊天服务器的例子的几乎精确副本中列出here扭曲忽略来自MUD客户端的数据?

进口twisted.scripts.twistd 从twisted.protocols导入从twisted.internet进口协议,从扭曲反应器 基本 。申请进口服务,互联网

class MyChat(basic.LineReceiver): 
    def connectionMade(self): 
     print "Got new client!" 
     self.factory.clients.append(self) 

    def connectionLost(self, reason): 
     print "Lost a client!" 
     self.factory.clients.remove(self) 

    def lineReceived(self, line): 
     print "received", repr(line) 
     for c in self.factory.clients: 
      c.message(line) 

    def message(self, message): 
     self.transport.write(message + '\n') 

factory = protocol.ServerFactory() 
factory.protocol = MyChat 
factory.clients = [] 

if __name__ == "__main__": 
    print "Building reactor...." 
    reactor.listenTCP(50000, factory) 
    print "Running ractor...." 
    reactor.run() 
else: 
    application = service.Application("chatserver") 
    internet.TCPServer(50000, factory).setServiceParent(application) 

服务器无错误运行,如果我通过Telnet连接到它,我可以发送数据和服务器打印到控制台,继到所有客户端(如预期)。但是,如果我通过不同的工具(MUD客户端)连接到它,它永远不会获取数据。我已经确保客户端正在发送数据(用Wireshark跟踪数据包,他们正在通过线路),但服务器要么永远不会收到它,要么因为某种原因选择忽略它。

我试过这个有两个MUD客户端gmudJMC。如果它很重要,我正在运行Windows 7 x64。

有没有人知道为什么会发生这种情况?

谢谢,

麦克

编辑:

由于由Maiku Mori提供的提示,我尝试添加这是在Twisted API Docs,dataReceived指定另一种方法。一旦添加完成,MUD客户端可以很好地工作,但Telnet现在正在将每个字符都发送给自己的一组数据,而不是等待用户按Enter。

这里是新代码的文档片断:

def dataReceived(self, data): 
     print "Dreceived", repr(data) 
     for c in self.factory.clients: 
      c.message(data) 

# def lineReceived(self, line): 
#  print "received", repr(line) 
#  for c in self.factory.clients: 
#   c.message(line) 

有没有人经历了这一点,如果是的话,你怎么围绕它得到什么?理想情况下,我希望Telnet MUD客户端可以使用此应用程序。

再次感谢。

回答

3

如果任何人遇到类似问题而绊倒这个问题,我会将我的发现留作公认的答案,以便人们不必以我的方式打猎。

我通过将Twisted协议中的分隔符值从“\ r \ n”(默认值)更改为“\ n”(这是我的MUD客户端发送的内容)来解决问题。这意味着,在远程登录,当你输入的字符串:

Hello, World 

您的应用程序将获得它:

Hello, World\r 

您可能需要做在服务器端数据卫生保持对事物的顺序。我最终的代码如下:

进口twisted.scripts.twistd 从twisted.protocols汇入twisted.internet进口协议,从twisted.application进口服务反应堆 ,互联网

class MyChat(basic.LineReceiver): 
    def __init__(self): 
     self.delimiter = "\n" 

    def connectionMade(self): 
     print "Got new client!" 
     self.factory.clients.append(self) 

    def connectionLost(self, reason): 
     print "Lost a client!" 
     self.factory.clients.remove(self) 

    def lineReceived(self, line): 
     print "received", repr(line) 
     for c in self.factory.clients: 
      c.message(line) 

    def message(self, message): 
     self.transport.write(message + '\n') 

factory = protocol.ServerFactory() 
factory.protocol = MyChat 
factory.clients = [] 

if __name__ == "__main__": 
    print "Building reactor...." 
    reactor.listenTCP(50000, factory) 
    print "Running ractor...." 
    reactor.run() 
else: 
    application = service.Application("chatserver") 
    internet.TCPServer(50000, factory).setServiceParent(application) 

由于基本 为所有的帮助。

+0

啊,我以为你检查wireshark和MUD客户端使用暂停或别的东西作为分隔符。那么这是一个干净的解决方案:D – 2009-12-14 01:33:53

+0

基本上,您不能对客户端是否发送\ n,\ r,\ n \ r或\ r \ n做任何假设。就我个人而言,我不会尝试弯曲LineReceiver来处理Telnet(尤其是不是MUD流量),因为它基本上不是基于行的协议,特别是当您考虑IAC序列,ANSI颜色等时。 – Kylotan 2009-12-14 10:15:45

+0

那么您会建议使用什么Twisted组件? – 2009-12-14 14:09:14

1

你确定MUD客户端在每行之后发送行尾字符吗? lineReceived只会在行结束字符被发送后被调用。

编辑:

在这里,我找到API文档LineReceiver。你可以玩dataReceived方法来看看你是否真的获得任何类型的数据。如果我记得你可以使用它就像lineReceived

+0

我不确定他们是否是。有没有一种方法可以手动添加到下一个测试结束? – 2009-12-14 01:06:15

+0

那么你可以使用Wireshark进行测试。还有其他Receiver基类可以使用。自从我阅读Twisted API文档以来已经有一段时间了,但我认为还有另一种方法,一旦数据块实时生效就会被调用,您可以使用它来查看是否有任何数据进入。但是,自从我阅读文档以来,这已经过去将近一年了,所以我可能是错的。 – 2009-12-14 01:14:29

+0

你的回复有帮助,我得到了MUD客户端的工作,但我添加的方法打破了Telnet支持。我在我的初始文章的编辑中概述了它。 – 2009-12-14 01:17:15