借助 Twisted,“connectionMade”如何实现? 触发特定的延迟?

发布于 2024-07-14 03:19:09 字数 843 浏览 7 评论 0原文

这是一个更大计划的一部分; 我将仅解释相关部分。 基本上,我的代码想要创建到远程主机的新连接。 这应该返回一个 Deferred,一旦建立连接就会触发,这样我就可以在上面发送一些东西。

我正在使用 twisted.internet.interfaces.IReactorSSL.connectSSL 创建连接。 它会在我的 ClientFactory 实例上调用 buildProtocol 来获取新连接 (twisted.internet.protocol.Protocol) 对象,并返回一个 twisted.internet.interfaces.IConnector。 当连接启动时,Twisted 在工厂上调用 startedConnecting,并为其提供 IConnector。 当实际建立连接时,将调用协议的 connectionMade 回调,不带任何参数。

现在,如果我只需要每个主机/端口一个连接,那么其余的就很容易了。 在调用 connectSSL 之前,我会创建一个 Deferred 并将其放入以(主机、端口)为键的字典中。 然后,在协议的 connectionMade 中,我可以使用 self.transport.getPeer() 来检索主机/端口,使用它来查找 Deferred,并触发其回调。 但如果我想创建多个连接,这显然会失败。

问题是,我看不到任何其他方法可以将调用 connectSSL 之前创建的 Deferred 与稍后的 connectionMade 关联起来。

This is part of a larger program; I'll explain only the relevant parts. Basically, my code wants to create a new connection to a remote host. This should return a Deferred, which fires once the connection is established, so I can send something on it.

I'm creating the connection with twisted.internet.interfaces.IReactorSSL.connectSSL. That calls buildProtocol on my ClientFactory instance to get a new connection (twisted.internet.protocol.Protocol) object, and returns a twisted.internet.interfaces.IConnector. When the connection is started, Twisted calls startedConnecting on the factory, giving it the IConnector. When the connection is actually made, the protocol's connectionMade callback is called, with no arguments.

Now, if I only needed one connection per host/port, the rest would be easy. Before calling connectSSL, I would create a Deferred and put it in a dictionary keyed on (host, port). Then, in the protocol's connectionMade, I could use self.transport.getPeer() to retrieve the host/port, use it to look up the Deferred, and fire its callbacks. But this obviously breaks down if I want to create more than one connection.

The problem is that I can't see any other way to associate a Deferred I created before calling connectSSL with the connectionMade later on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2024-07-21 03:19:09

再看看这个,我想我已经找到了一个解决方案,尽管希望有更好的方法; 这看起来有点奇怪。

Twisted 有一个类 ClientCreator,用于生成简单的一次性连接。 理论上它可以满足我的要求; 连接并返回一个 Deferred,该延迟在连接建立时触发。 不过,我认为我不能使用它,因为我将失去将参数传递给协议构造函数的能力,因此无法在连接之间共享状态。

但是,我刚刚意识到 ClientFactory 构造函数确实接受 *args 传递给协议构造函数。 或者至少看起来是这样; 几乎没有这方面的文档。 在这种情况下,我可以给它一个对我的工厂的引用(或者其他任何东西,如果不再需要工厂的话)。 我得到了建立连接时触发的Deferred

Looking at this some more, I think I've come up with a solution, although hopefully there is a better way; this seems kind of weird.

Twisted has a class, ClientCreator that is used for producing simple single-use connections. It in theory does what I want; connects and returns a Deferred that fires when the connection is established. I didn't think I could use this, though, since I'd lose the ability to pass arguments to the protocol constructor, and therefore have no way to share state between connections.

However, I just realized that the ClientFactory constructor does accept *args to pass to the protocol constructor. Or at least it looks like it; there is virtually no documentation for this. In that case, I can give it a reference to my factory (or whatever else, if the factory is no longer necessary). And I get back the Deferred that fires when the connection is established.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文