C#/ WPF / Unity名称管道通信仅向一个方向发送消息

问题描述 投票:0回答:2

我在解决当前项目中的问题时陷入困境。我正在用WPF / C#编写一个模拟代码,并使用window-handles / hwnd在窗口中包含一个Unity-Application,非常类似于answer中的“程序员”。他还建议使用命名管道进行交流。

但是通信仅以一种方式起作用-服务器(WPF-App)能够将消息发送到客户端(嵌入式Unity-App)。如果我尝试相反的方法,则整个Unity-App会挂断并停止工作。据我所知,一旦刷新编写器或添加AutoFlush,然后编写一些东西,Unity就会卡住。

有趣的是,服务器的InBufferSize为0。这可能是问题吗?

这是我在Unity中分配给GameObject的C#脚本:

public class pipeCommunicationScript : MonoBehaviour
{
    NamedPipeClientStream client;

    public GameObject console;
    static Text consoleText;

    StreamReader reader;
    StreamWriter writer;

    int counter = 0;


    static bool _threadRunning;
    Thread _thread;

    void Start()
    {
        consoleText = console.GetComponent<Text>();
        consoleText.text = "trying to set up client";

        _thread = new Thread(StartClient);
        _thread.Start();
    }

    void StartClient()
    {
        _threadRunning = true;

        //Client
        consoleText.text = "Attempting to connect to pipe...";
        client = new NamedPipeClientStream(".","UnitySimulationPipe", PipeDirection.InOut);
        if (!client.IsConnected) { client.Connect(); }
        consoleText.text = "Connected to pipe.";

        writer = new StreamWriter(client);
        reader = new StreamReader(client);
        writer.AutoFlush = true;

        InvokeRepeating("sendThroughPipe", 5.5f, 5.5f);

        while (_threadRunning)
        {
            consoleText.text = "IN:" + reader.ReadLine();
        }

        _threadRunning = false;
    }


    void sendThroughPipe()
    {
        try
        {
            client.WaitForPipeDrain();
            writer.WriteLine("test" + counter);
        }
        catch(Exception e)
        {
            consoleText.text = "Exception while sending: " + e;
        }
        consoleText.text = "OUT: test" + counter;
        counter++;
    }


    void OnDisable()
    {
        if (_threadRunning)
        {
            _threadRunning = false;
       _thread.Join();
        }
    }
}

这是我的服务器脚本:

void StartServer()
{
    Task.Factory.StartNew(() =>
    {
        Console.WriteLine("Connection initialisation started");
        var server = new NamedPipeServerStream("UnitySimulationPipe", PipeDirection.InOut);
        server.WaitForConnection();

        while (!server.IsConnected)
        {
            Console.WriteLine("Connection not initialized");
            server.WaitForConnection();
        }

        Console.WriteLine("Connection initialized");


        reader = new StreamReader(server);
        writer = new StreamWriter(server);
        writer.AutoFlush = true;

        writer.WriteLine("startConnection");
        printOnStatusBar("OUT: startConnection");

        connected = true;


        Console.WriteLine("Current pipe readable: " + server.CanRead);

        Console.WriteLine("Pipe InBufferSize: " + server.InBufferSize);

        while (true)
        {
            Console.WriteLine("start while again");

            if (reader.Peek() > 0)
            {
                var l = reader.ReadLine();
                Console.WriteLine(reader.ReadLine());
                Console.WriteLine("read finished");
                server.WaitForPipeDrain();
            }
            Thread.Sleep(500);

        }
    });
}
c# wpf unity3d
2个回答
0
投票

实际上,最可能的答案是您提到的内容(bufferSize)。您应该检查所使用的每个对象的initialyzers参数。不用担心,我敢肯定,只要在此搜索就可以找到答案。我曾经以相同的方式创建这样的代码,它应该可以正常工作。


0
投票

好的,我将服务器对象的初始化更改为(解释不同的参数here:]

var server = new NamedPipeServerStream("UnitySimulationPipe", PipeDirection.InOut, 1, 0, 0, 1024, 1024);

但是我个人认为,这只是一个小错误。我的代码无法正常工作的主要原因是,我错过了致电

server.WaitForPipeDrain();

之后

writer.WriteLine("startConnection");
printOnStatusBar("OUT: startConnection");

以我的服务器代码。

© www.soinside.com 2019 - 2024. All rights reserved.