编写主题pub / sub系统

时间:2022-11-30 15:35:04

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.

首先设置基础我将使用c#(.NET 3.5),我想明确使用原始套接字/ AIO /线程(首先没有WCF,因为我想根据我的需要微调服务器和客户端)。客户端主要订阅主题,但有时可能会向服务器发送命令甚至发布数据。有些客户也可能只是发布商。

  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? How to wait for data and at the same time write data to the stream , should this be done in the same thread ?

    客户端是否应该使用相同的NetworkStream来监听订阅的主题并将命令/发布发送到服务器?如何在同一个线程中完成,如何等待数据并同时将数据写入流?

(sample code will be appreciated :) )

(示例代码将不胜感激:))

2 个解决方案

#1


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

#2


Have you checked out ActiveMQ? I believe it already does topics and C# has the ability to talk to it through their NMS API.

你检查过ActiveMQ吗?我相信它已经有了主题,C#有能力通过他们的NMS API与它交谈。

#1


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

#2


Have you checked out ActiveMQ? I believe it already does topics and C# has the ability to talk to it through their NMS API.

你检查过ActiveMQ吗?我相信它已经有了主题,C#有能力通过他们的NMS API与它交谈。