扭曲:如何在初始连接时识别协议,然后委派给适当的协议实现?

扭曲:如何在初始连接时识别协议,然后委派给适当的协议实现?,第1张

扭曲:如何在初始连接时识别协议,然后委派给适当的协议实现?

与其在整个协议实现过程中混合决策逻辑,不如将它放在一个地方。

class DecisionProtocol(Protocol):    def connectionMade(self):        self.state = "undecided"    def makeProgressTowardsDecision(self, bytes):        # Do some stuff, eventually return ProtoA() or ProtoB()    def dataReceived(self, bytes):        if self.state == "undecided": proto, extra = self.makeProgressTowardsDecision(bytes) if proto is not None:     self.state = "decided"     self.decidedonProtocol = proto     self.decidedOnProtocol.makeConnection(self.transport)     if extra:         self.decidedOnProtocol.dataReceived(extra)        else: self.decidedOnProtocol.dataReceived(bytes)    def connectionLost(self, reason):        if self.state == "decided": self.decidedOnProtocol.connectionLost(reason)

最终,您可以用更少的样板实现此目的:http :
//tm.tl/3204/



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674772.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存