我的 UDP 套接字服务器设计正确吗?

发布于 2024-09-26 14:17:38 字数 643 浏览 4 评论 0原文

我正在设计一个使用 MFC 进行 UDP 通信的服务器。我有以下类

  • CMyDlialog - 负责用户界面
  • CController - 充当所有类之间的中介
  • CProtocolManager - 负责编码/解码消息(这是一个静态类)
  • CConnectionManager - 负责 UDP 连接、发送、接收

I我正在创建一个 CConnectionManager 对象作为 CController 中的成员变量,并创建 CController 对象作为 CMyDialog 中的成员变量。

当用户键入内容并按发送时,我将调用 CControler 中的一个方法,该方法将调用 CProtocolManager 中的方法来构造数据包并调用 CConnectionManager 方法来发送它。

当我收到一些数据时,它是在 CConnectionManager 的线程中处理的。我在那里创建一个 CController 的本地对象并调用一个方法,该方法将数据传递给 CProtocolManager 进行解码。

现在我想向 UI 通知数据,CControler 应该如何做?我可以通过使主对话框句柄全局化来向用户界面发布消息,这是正确的方法吗?

还告诉我这个设计是否正确。

提前致谢

I am designing a server which is used in UDP communication using MFC. I have the following classes

  • CMyDlialog - Take care of User interface
  • CController - Act as an Mediator between all the classes
  • CProtocolManager - Take care of Encoding/Decoding msgs (This is a static class)
  • CConnectionManager - Take care of UDP connection, Sending, Receiving

I am creating an object of CConnectionManager as a member variable in CController and object of CController as member variable in CMyDialog.

When user type something and presses send, I am calling a method in CControler which will call a method in CProtocolManager to construct the packet and call the CConnectionManager method to send it.

When i receive some data, it is handled in a thread of CConnectionManager. There i am creating a local object of CController and call a method, which will pass the data to CProtocolManager to decode.

Now i want to inform the UI about the data, how should the CControler do it? i can post a message to the UI by making the main dialog handle global, is that a correct approach.

Also tell me whether this design is correct one.

Thanks in advance

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

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

发布评论

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

评论(2

风蛊 2024-10-03 14:17:38

我认为设计从来没有对错之分,但你可以根据许多人认为“好”的一些原则来评价它们(参见SOLID 原则)。

您的发送方法听起来很合理,但是将 Dialog 设置为全局接收绝对被认为“不太好”。请参阅好莱坞原则。我建议您最好将回调方法传递给连接管理器,该方法是 CController 的一个方法(然后让 CProtocolManager 对其进行解码并从对话框中调用另一个回调方法)。如果回调不是您想要的,您可以使用方法定义像“AbstractMessageReceiver”这样的 AbstractBaseClasses (ABC),

 virtual void receive(const char*, int length) = 0;

然后您可以在 CProtocolManager 中实现此 ABC,将其作为“AbstractMessageReceiver*”传递给 CConnectionManager,然后 CConnectionManager 调用

m_myMessageReceiver->receive(m_buffer, m_length);

或类似的方法。

这种方法减少了耦合,更易于测试并提高了可重用性。

此外,我同意 Am 的观点,即你应该考虑你的线程模型!

I think designs are never right or wrong, but you can rate them according to some principles that many people consider to be "good" (see the SOLID principles).

Your sending approach sounds reasonable, but making the Dialog global for receiving is definitely considered "not so good". See the hollywood principle. I suggest you better pass a callback method to your connection manager that is a method of CController (which then lets CProtocolManager decode it and calls another callback method from the dialog). If callbacks are not what you want you can define AbstractBaseClasses (ABC) like this "AbstractMessageReceiver" with a method

 virtual void receive(const char*, int length) = 0;

You can then implement this ABC in CProtocolManager pass this as a "AbstractMessageReceiver*" to CConnectionManager which then calls

m_myMessageReceiver->receive(m_buffer, m_length);

or something similar.

This approach reduces the coupling, is much more testable and increases the reusability.

Furthermore, I agree with Am that you should think about your threading model!

甩你一脸翔 2024-10-03 14:17:38

看来你是在一个线程中完成这一切。由于数据包来回需要时间,因此建议在不同的线程中处理耗时的工作,并将状态/结果发布到 UI 线程。 (否则 UI 将被冻结)。

It seems you are doing it all in a single thread. Since it takes time for packets to go back and forth, it's advised to make do to time-consuming job in a different thread, and post status/results the UI thread. (Or the UI will be frozen).

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