如何在 C++ 中实现观察者设计模式流数据?

发布于 2024-11-27 11:18:54 字数 135 浏览 0 评论 0原文

我想连续(流式传输)从服务器向客户端发送数据,而不需要客户端不断循环并检查任何数据。我认为我相信这是观察者设计模式是正确的?这怎么可能?

有人可以给我提供一个我可以用谷歌搜索的东西的列表吗?观察者模式方面是如何实现的?

谢谢

I would like to send data from a server to a client, continuously (streaming), without the need for the client to continuously loop through and check for any data. I think I am right in believing this is the observer design pattern? How is this possible?

Could someone provide me with a list of things I can google? How is the observer pattern aspect implemented?

Thanks

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

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

发布评论

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

评论(3

冬天旳寂寞 2024-12-04 11:18:54

观察者设计模式与您所描述的略有不同:

在此处输入图像描述

请注意,每个观察者都会收到“可观察”对象;因此,如果您有一台连续传输数据的服务器,那么您是否期望服务器“通知”您以及您期望服务器通知您什么?它发送给你的每个数据包?每块数据包?

简而言之:不,您无法在客户端/服务器应用程序上实现观察者模式。服务器没有(简单)方法在您的客户端应用程序上调用通知方法,并且如果您的客户端断开连接,那么它不会从可观察对象中取消注册。

所以回到你的问题......你受到架构的限制:阻塞套接字(tcp/udp)都是通过阻塞来工作的,直到你收到数据。一旦接收到数据,就必须循环调用receive才能不断获取更多数据。另一种方法是使用异步套接字:

异步套接字通信可能最接近观察者模式。此外,您希望使用 UDP 协议,因为您有流数据,而 UDP 是专门为流数据设计的。如果你不想错过任何数据包(由于 UDP 的不可靠性),那么你可以使用 reliable UDP

The observer design pattern is slightly different from what you're describing:

enter image description here

Note that each observer gets notified by the "observable" object; so if you have a server that's continuously streaming data, then would you expect the server to "notify" you and what would you expect the server to notify you of? Every packet it sends you? Every chunk of packets?

In short: no, you can't implement the observer pattern on a client/server application. There is no (easy) way for a server to invoke a notify method on your client app and if your client gets disconnected, then it won't unregister from the observable.

So back to your question... you're limited by the architecture: blocking sockets (tcp/udp) all work by blocking until you receive data. Once you receive data, you have to loop through and call receive again in order to continuously get more data. An alternative is to use asynchronous sockets:

Asynchronous socket communication is probably as close you will get to the observer pattern. Furthermore, you want to use the UDP protocol because you have streaming data and UDP is specifically designed for streaming data. If you don't want to miss any packets (due to the unreliability of UDP), then you can use reliable UDP.

与君绝 2024-12-04 11:18:54

由于通信协议完全不同,因此您无法为客户端/服务器用例实现观察者模式。 (RPC可能适合,但你使用的是TCP)

无论如何,你能做的就是有一个专用的TCP客户端来接收数据。正是这个TCP客户端和你的内部类可以一起实现观察者模式。有了这个,你的班级将不再需要等待(轮询)数据。

沙什

You can't implement observer pattern for Client/Server use case since the communication protocol is entirely different. (RPC may be suitable, but you are using TCP so)

Anyways, what you can do is have a dedicated TCP client for receiving data. It is this TCP client and your internal class can together implement Observer pattern. With this your class will no longer have to wait (poll) on data.

Shash

素食主义者 2024-12-04 11:18:54

据我了解,这里主要关心的是客户端应用程序不应被阻止等待来自服务器的数据。为此,您可以创建一个将作用于套接字事件的线程。您可以使用此线程模块注册类的上下文,并且每当收到数据时,此线程模块就可以使用注册的上下文进行回调(通知)。

As I understand, main concern here is client application should not be blocked waiting for data from server. For that you can create one thread which will act on socket events. You can register context of you class with this thread module and whenever data is received this thread module can make a callback (notify) using the context registered.

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