C# 中的多线程 NamePipeServer

发布于 2024-10-09 17:20:56 字数 138 浏览 0 评论 0原文


我想使用 .NET 3.5 中新增的 NamedPipeServerStream 进行命名管道通信。 我想写多线程管道服务器。它是默认处理的还是我应该为此编写代码。我的管道服务器应该一次处理多个请求

任何解决方案或代码?

Hi
I want to use NamedPipeServerStream which is new from .NET 3.5 for namedpipe communication.
I want to write multi-threaded pipe server. is it handled by default or I should write code for that. my pipe server should handle multiple request at a time

any solution or code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在看孤独的风景 2024-10-16 17:20:56

您可以通过重复创建 NamedPipeServerStream 并等待一个连接,然后为该 NamedPipeServerStream 实例生成一个线程来编写多线程管道服务器。

根据下面链接的 .NET MSDN 文档,您只能拥有 254 个并发客户端。对于 Win32 API,您可以传递一个特殊值来根据系统资源获得无限的资源。 MSDN 文档似乎是错误的,如下所示。

以下代码未经测试,因此请不要在未经测试的情况下简单地复制和粘贴用于生产用途:

    public class PipeServer
    {
        bool running;
        Thread runningThread;
        EventWaitHandle terminateHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
        public string PipeName { get; set; }

        void ServerLoop()
        {
            while (running)
            {
                ProcessNextClient();
            }

            terminateHandle.Set();
        }

        public void Run()
        {
            running = true;
            runningThread = new Thread(ServerLoop);
            runningThread.Start();
        }

        public void Stop()
        {
            running = false;
            terminateHandle.WaitOne();
        }

        public virtual string ProcessRequest(string message)
        {
            return "";
        }

        public void ProcessClientThread(object o)
        {
            NamedPipeServerStream pipeStream = (NamedPipeServerStream)o;

            //TODO FOR YOU: Write code for handling pipe client here

            pipeStream.Close();
            pipeStream.Dispose();
        }

        public void ProcessNextClient()
        {
            try
            {
                NamedPipeServerStream pipeStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 254);
                pipeStream.WaitForConnection();

                //Spawn a new thread for each request and continue waiting
                Thread t = new Thread(ProcessClientThread);
                t.Start(pipeStream);
            }
            catch (Exception e)
            {//If there are no more avail connections (254 is in use already) then just keep looping until one is avail
            }
        }

You can write a multi threaded pipe server by repeatedly creating a NamedPipeServerStream and waiting for one connection, then spawning a thread for that instance of NamedPipeServerStream.

You can only have 254 concurrent clients though according to the .NET MSDN documentation linked below. For Win32 APIs though you can pass a special value to get unlimited based on system resources. It seems the MSDN documentation is wrong as noted below.

The below code is not tested so please do not simply copy and paste for production use without testing:

    public class PipeServer
    {
        bool running;
        Thread runningThread;
        EventWaitHandle terminateHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
        public string PipeName { get; set; }

        void ServerLoop()
        {
            while (running)
            {
                ProcessNextClient();
            }

            terminateHandle.Set();
        }

        public void Run()
        {
            running = true;
            runningThread = new Thread(ServerLoop);
            runningThread.Start();
        }

        public void Stop()
        {
            running = false;
            terminateHandle.WaitOne();
        }

        public virtual string ProcessRequest(string message)
        {
            return "";
        }

        public void ProcessClientThread(object o)
        {
            NamedPipeServerStream pipeStream = (NamedPipeServerStream)o;

            //TODO FOR YOU: Write code for handling pipe client here

            pipeStream.Close();
            pipeStream.Dispose();
        }

        public void ProcessNextClient()
        {
            try
            {
                NamedPipeServerStream pipeStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 254);
                pipeStream.WaitForConnection();

                //Spawn a new thread for each request and continue waiting
                Thread t = new Thread(ProcessClientThread);
                t.Start(pipeStream);
            }
            catch (Exception e)
            {//If there are no more avail connections (254 is in use already) then just keep looping until one is avail
            }
        }
夜还是长夜 2024-10-16 17:20:56

每个 NamedPipeServerStream 实例都是一个 Stream 实现,包装了命名管道实例的句柄。您可以(并且多线程管道服务器将)为同一个命名管道拥有多个 NamedPipeServerStream 实例:每个实例都包装一个命名管道的不同实例的句柄,为不同的客户端提供服务。命名管道实例(即使是同一个管道)由操作系统保持独立,因此不需要任何显式编码来保持每个客户端与服务器的通信独立。

您需要显式编码的是服务器的线程模型。 此 SO 中解释了服务器多线程处理的最简单方法答案,其中包含伪代码模板。如果需要支持大量并发调用者,则更具可扩展性的实现将使用线程池和异步方法,而不是为每个连接创建专用线程。

Each NamedPipeServerStream instance is a Stream implementation wrapping a handle to an instance of a named pipe. You can (and a multithreaded pipe server will) have multiple instances of NamedPipeServerStream for the same named pipe: each one wraps a handle to a different instance of the named pipe, servicing a different client. Named pipe instances (even for the same pipe) are kept separate by the operating system, so there is no need for any explicit coding to keep each client's communication with the server separate.

What you do need to code explicitly is the threading model for the server. The simplest approach to multithreading the server is explained in this SO answer, which includes a pseudo-code template. More scalable implementations, if large numbers of concurrent callers need to be supported, would use thread pooling and the asynchronous methods instead of creating a dedicated thread for each connection.

最冷一天 2024-10-16 17:20:56

NamedPipeServerStream 是点对点连接。您必须自己同步调用 - 例如写入队列的调用,您的服务器从同步队列中读取并进行调用。

NamedPipeServerStream is a point to point connection. You have to synchronise the calls yourself - for example calls written to a queue and your server reads from the synchronised queue and makes the calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文