我正在开发基于 Twisted 框架的 P2P 应用程序。因此,我可以同时拥有传入和传出连接。有没有简单的方法来区分它们?目前我只是创建另一个工厂,将连接标记为传出并将所有工厂调用委托给原始工厂,但必须有更简单的方法。
class OutgoingProtocolFactory(MyProtocolFactory):
"""
A rather simple factory that is used to earmark connections as outgoing.
"""
def __init__(self, parentFactory):
self.factory = parentFactory
def buildProtocol(self, addr):
connection = MyProtocolFactory.buildProtocol(self.factory, addr)
connection.factory = self.factory
connection.incoming = False
return connection
def clientConnectionFailed(self, connector, reason):
self.factory.clientConnectionFailed(connector, reason)
def clientConnectionLost(self, connector, reason):
self.factory.clientConnectionLost(connector, reason)
有什么想法吗?