[英]Is my design for UDP socket server correct?
I am designing a server which is used in UDP communication using MFC. 我正在设计一个使用MFC进行UDP通信的服务器。 I have the following classes
我有以下课程
I am creating an object of CConnectionManager as a member variable in CController and object of CController as member variable in CMyDialog. 我正在将CConnectionManager的对象创建为CController中的成员变量,并将CController的对象创建为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. 当用户键入内容并按send时,我正在CControler中调用一个方法,该方法将在CProtocolManager中调用一个方法来构造数据包,并调用CConnectionManager方法来发送它。
When i receive some data, it is handled in a thread of CConnectionManager. 当我收到一些数据时,它将在CConnectionManager的线程中处理。 There i am creating a local object of CController and call a method, which will pass the data to CProtocolManager to decode.
我在这里创建一个CController的本地对象并调用一个方法,该方法会将数据传递给CProtocolManager进行解码。
Now i want to inform the UI about the data, how should the CControler do it? 现在,我想通知UI有关数据,CControler应该如何做? i can post a message to the UI by making the main dialog handle global, is that a correct approach.
我可以通过使主对话框句柄变为全局来向UI发布消息,这是一种正确的方法。
Also tell me whether this design is correct one. 还告诉我这种设计是否正确。
Thanks in advance 提前致谢
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.
由于数据包来回往返需要时间,因此建议在其他线程中完成耗时的工作,并发布状态/结果UI线程。 (Or the UI will be frozen).
(否则,UI将被冻结)。
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 ). 我认为设计永远不会是非对非,但是您可以根据许多人认为是“好”的一些原则(请参见SOLID原则 ) 对它们进行评级 。
Your sending approach sounds reasonable, but making the Dialog global for receiving is definitely considered "not so good". 您的发送方法听起来很合理,但是将Dialog全局设置为接收状态绝对被认为“不太好”。 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).
我建议您最好将回调方法传递给连接管理器,该方法是CController的方法(然后让CProtocolManager对其进行解码并从对话框中调用另一个回调方法)。 If callbacks are not what you want you can define AbstractBaseClasses (ABC) like this "AbstractMessageReceiver" with a method
如果回调不是您想要的,则可以使用方法定义类似于“ AbstractMessageReceiver”的AbstractBaseClasses(ABC)
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 然后,您可以在CProtocolManager中实现此ABC,将其作为“ AbstractMessageReceiver *”传递给CConnectionManager,然后调用
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! 此外,我同意Am,您应该考虑自己的线程模型!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.