编写主题发布/订阅系统
我正在编写一个将发布和订阅主题的客户端/服务器应用程序。 我对这个项目的架构和实现有几个问题。
首先要设置基础,我将使用 c# (.NET 3.5),并且我想明确使用原始套接字/AIO/线程(一开始没有 WCF,因为我确实想根据我的需要微调服务器和客户端)。 客户端主要订阅主题,但偶尔也可能向服务器发送命令,甚至发布数据。 有些客户也可能只是发布商。
你认为应该是什么 我的服务器的基本构建块( 每个客户端的线程数,iocp,...)。
客户端是否应该使用相同的 NetworkStream 监听订阅 主题并将命令/发布发送到 服务器? 如何等待数据和 同时写入数据到 流,这应该在 相同的线程?
(示例代码将不胜感激:))
I am writing a client/server application that will publish and subscribe to topics.
I have few questions about the architecture and the implementation for this project.
First to setup the basis i will use c# ( .NET 3.5 ) and i want to explicitly use raw Sockets/AIO/Threads(No WCF at first as i do want to fine tune the server and clients to my needs ). Clients mostly subscribe to topics but may occasionally send command to the server and even publish data . Some clients may be publishers only as well.
What do you think should be the
basic building blocks of my server (
threads per client , iocp, .... ).Should client use the same
NetworkStream to listen subscribed
topics and send command/publish to
the server? How to wait for data and
at the same time write data to the
stream , should this be done in the
same thread ?
(sample code will be appreciated :) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尽可能多地使用异步套接字,因为这将消除您管理自己的线程池以进行请求处理的需要。
客户端应该使用两个流 - 一个用于列出,另一个用于发送。 这将大大简化客户端和服务器端的事情。 再次强调,使用异步套接字来避免管理多个线程。
只需确保小心管理异步回调使用的共享资源的锁定即可。 如果可能的话,尽可能避免共享资源。 管理对事物的并发访问通常是此类应用程序中最糟糕的部分。
I would use async sockets as much as possible as it would eliminate the need for you to manage your own thread pool for request handling.
The client should use two streams - one for listing and another for sending. This would greatly simplify things on both the client and server side. Again, use async sockets to avoid having to manage multiple threads.
Just make sure to be careful managing locking with shared resources used by your async callbacks. If at all possible, avoid having shared resources as much as possible. Managine concurrent access to things is usually the worst part of any app like this.
您检查过 ActiveMQ 吗? 我相信它已经处理了主题,并且 C# 能够通过其 NMS API 与其进行对话。
Have you checked out ActiveMQ? I believe it already does topics and C# has the ability to talk to it through their NMS API.