与其在整个协议实现过程中混合决策逻辑,不如将它放在一个地方。
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/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)