简体   繁体   English

编写主题发布/订阅系统

[英]Writing a topic pub/sub system

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 ). 首先设置基础,我将使用c#(.NET 3.5),并且我想显式地使用原始Sockets / AIO / Threads(起初没有WCF,因为我确实想根据我的需要微调服务器和客户端)。 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. 一些客户也可能只是发布者。

  1. What do you think should be the basic building blocks of my server ( threads per client , iocp, .... ). 您认为应该是我的服务器的基本构建块(每个客户端的线程数,iocp等等)。

  2. Should client use the same NetworkStream to listen subscribed topics and send command/publish to the server? 客户端是否应该使用相同的NetworkStream侦听订阅的主题并将命令/发布发送到服务器? 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 :) ) (示例代码将不胜感激:))

  1. 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. 我将尽可能多地使用异步套接字,因为它将消除您管理自己的线程池以进行请求处理的需要。

  2. 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. Managine并发访问事物通常是所有此类应用程序中最糟糕的部分。

Have you checked out ActiveMQ? 您是否已检查ActiveMQ? I believe it already does topics and C# has the ability to talk to it through their NMS API. 我相信它已经在做主题,并且C#可以通过其NMS API与之交谈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM