网络流池

发布于 2024-10-09 00:29:33 字数 918 浏览 0 评论 0原文

我有一个多线程应用程序,它通过 TCP 连接与服务器进行通信。该应用程序将部署为 Windows 服务。

它的实现方式是,有一个Controller,它创建Communicator对象,将端口号、消息计数等属性分配给Communicator并调用其 StartClient 方法来开始与服务器的对话。

StartClient 方法中,每个 Communicator 对象使用 Controller 指定的端口号和 URL 创建到服务器的连接。建立连接后,它在内部创建一个线程并调用 ReadMessages 方法,该方法不断从服务器读取消息,直到达到消息计数,然后关闭。

根据运行时条件,可能需要重用 Communicator 对象来再次与服务器通信,因此将再次调用 ReadMessages 方法。

最初,当 ReadMessages 方法完成时,我们一直在调用 NetworkStream、StreamReader 和 StreamWriter 对象的 Dispose() 方法,但在重新连接的情况下,它会抛出 “无法访问已处置的对象”错误。因此,我们注释掉了 Dispose 方法调用以进行测试。

截至目前,它工作正常,但我担心,这不是实现此功能的最佳方式,因为我从来没有处理过这些对象。

我在考虑对象池,是否可以有一个可由不同线程重用的 Stream 对象池?

解决此问题的一种方法是每次 Communicator 连接到服务器时创建 Stream 对象的新实例,但我认为这将是一项昂贵的操作。

您能否帮助我找到一种更好的方法来处理这里的情况,以便我可以重用 Communicator 对象而不影响性能?

I have a multi-threaded application which communicates with a server over a TCP connection. The application would be deployed as a windows service.

The way it has been implemeted is, there is Controller which creates Communicator objects, assigns the port number, message count etc. properties to the Communicator and invokes its StartClient method to commence the dialog with the server.

Within the StartClient method, each Communicator object creates a connection to the server, using the port number and url specified by the Controller. After establishing the connection, it internally creates a thread and calls the ReadMessages method which keeps reading from the server till the message count is met and then gets closed down.

Based on the runtime conditions, there might be a need to reuse the Communicator object to talk with the server again and hence, the ReadMessages method woudl be called again.

Initially, we had been calling Dispose() method for the NetworkStream, StreamReader and StreamWriter objects when the ReadMessages method completed, but with the reconnecting scenario, it used to throw "Cannot access a disposed object" error. So, we commented out the Dispose method call for testing.

As of now, it works fine, but I am concerned that, this isnt the best way to achieve this functionlity as I am not disposing the objects ever.

I was thinking in terms of object pooling, If it is possible to have a pool of Stream objects which could be reused by different threads?

One way to tackle this can be to create a new instance of Stream objects each time the Communicator connects with the server, but I think that would be an expensive operation.

Can you please help me identify a better approach to handle the situation here so that I can reuse the Communicator object without a performance hit?

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

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

发布评论

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

评论(1

や三分注定 2024-10-16 00:29:33

该方法将基于您需要读取消息的频率 - 如果偶尔是 n,我建议您重构您的通信器对象以使“ReadMessages”操作原子化 - 即它将连接到服务器,创建网络流,阅读消息,然后处理掉所有东西。

The approach will be based on how frequently you need to read messages - if its occasional the n, I would recommend that you re-factor your communicator object to make "ReadMessages" operation atomic - i.e. it would connect to the server, create network stream, read messages and then dispose every thing.

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