如何将 TWSocket 的 OnDataAvailable() 事件推送到 Delphi 6 应用程序中的后台线程?
我有一个 Delphi 6 应用程序,它使用 ICS 组件套件进行套接字通信。我有自己的服务器套接字 VCL 组件,当新会话可用时,它会创建客户端 TWSocket 套接字。我创建的客户端套接字确实将 Multithreaded 属性设置为 TRUE,但所做的只是将客户端套接字处理套接字消息的方式更改为对后台线程(非主 VCL 线程)安全的方式。 TWSocket 不会生成线程来处理套接字数据流量,而这正是我所需要的。
我需要让接收调用发生在主 VCL 线程(主用户界面线程)之外,因为客户端套接字的传入数据是需要在 50-100 毫秒或更短时间内快速处理的音频数据。换句话说,主 VCL 线程出现故障,音频流就会中断。这就是为什么我想将每当传入数据可用时触发的 OnDataAvailable() 事件推送到高优先级后台线程。换句话说,我想将属于客户端 TWSocket 对象的消息处理循环强制到后台线程。
我相信我可以通过后台线程创建客户端套接字来做到这一点,但我希望避免这种情况,因为目前我使用我制作的充当套接字服务器的 VCL 组件。这是接受传入连接并生成客户端套接字的实体。套接字服务器在主 VCL 线程上创建。
因此我的问题是,是否有一种(相对)简单的方法来创建客户端套接字,以便它们使用现有的后台线程来进行套接字处理,特别是 FD_RECV 消息处理?如果不是现有的后台线程,那么我将为我创建的每个客户端套接字创建一个,但我需要知道如何确保新的 TWSocket 对象在运行处理套接字消息的消息循环时使用该后台线程,那么如何我这样做?
I have a Delphi 6 application that uses the ICS component suite to do socket communications. I have my own server socket VCL component that creates client TWSocket sockets when a new session becomes available. The client sockets I create do have the Multithreaded property set to TRUE, but all that does is changes the way the client socket handles socket messages to a manner that is safe from a background thread (non-main VCL thread). TWSocket does not spawn a thread to handle socket data traffic, which is what I need.
I need to have the receive calls occur off the main VCL thread, the main user interface thread, because the incoming data to the client socket is audio data that needs to be processed rapidly, in 50-100 milliseconds or less. In other words, one hiccup on the main VCL thread and the audio stream is disrupted. This is why I want to push the OnDataAvailable() event that fires whenever incoming data is available on to a high priority background thread. In other words, I want to force the message processing loop belonging to the client TWSocket object to a background thread.
I believe I can do this by creating the client socket via a background thread, but I'm hoping to avoid that since currently I use a VCL component I made that acts as a socket server. This is the entity that Accepts the incoming connection and spawns the client sockets. The socket server is created on the main VCL thread.
Therefore my question is, is there a (relatively) easy way to create the client sockets so they use an existing background thread to do their socket processing, especially the FD_RECV message handling? If not an existing background thread, then I will create one for each client socket I create, but I need to know how to make sure the new TWSocket object uses that background thread when it runs its message loop that processes socket messages, so how would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于其他 ICS/TWSocket 用户,解决方案位于软件包附带的 ICS ThrdSrv 演示项目中。仔细查看该项目,尤其是它对 ThreadAttach() 和 ThreadDetach() 方法的使用。该示例项目展示了如何创建具有在工作线程上下文中运行的消息泵的客户端套接字。
For other ICS/TWSocket users out there, the solution is in the ICS ThrdSrv demonstration project that comes with the package. Take a close look at that project, especially its use of the ThreadAttach() and ThreadDetach() methods. That sample project shows how to create client sockets that have message pumps that run in the context of a worker thread.