4

我编写了这个 C# 代码,具有 namedPipeServer 和 NamedPipeClient,具有异步读写设置,相互连接。这两个代码都在我使用的 Visual Studio 2010 上完美运行,读写运行良好,在运行时没有任何应用程序冻结。

但我希望客户端在 unity3d 中运行。我遇到的问题是在 Unity3D 中实现的客户端代码。当我使用 Write_to_Server_Async(string message) 时,服务器端的读取不会被调用,只有在我退出 Unity3d 时才会被调用(我通常已经结束了它的进程)。我可以告诉 Unity3D 有什么问题,因为确切的代码在 Visual Studio 中完美运行,所以我知道我的代码以正确的方式实现。我听说unity3d 并没有真正使用真正的线程,除非用户手动创建一个,但即使这样也没有解决问题。我的猜测是 Unity3D 开发人员可能已经创建了他们的 .NET 库 3.5 版本(听起来很奇怪(确实解释了为什么他们仍然没有采用 4.5)),或者他们必须以某种方式构建他们的库,默认功能类似于 NamedPipeClientStream。BeginWrite 无法创建自己的真实线程。但话又说回来,我不确定它是否是线程的问题。

目前,我希望任何人都能提出很好的解释。

确保在 unity3d 中将 Debug.WriteLine 替换为 UnityEngine.Debug.Log。

下面是Client Main方法代码和类

class PipeClient 
{
    private static Asynchronus_NamedPipe_Client client;


    static void Main(string[] args)
    {
       client = new Asynchronus_NamedPipe_Client("mypipe7055");
       while (client.Is_connected_to_server()) {
           if (Console.ReadKey().Key == ConsoleKey.T)
           {
               client.Write_to_Server_Async("NEX CLIENT");

           }
       }
    }
}

Asynchronus_NamedPipe_Client 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System;
using System.IO;
using System.IO.Pipes;
using System.Text;
using System.Security.Principal;
using System.Diagnostics;
using System.Threading;

namespace NamedPipes_CLIENT
{

public class Asynchronus_NamedPipe_Client
{


    public readonly string pipe_address;
    private System.IO.Pipes.NamedPipeClientStream clientStream;

    public bool filter_message = true;



    private string Server_Message = null;




    public event ASYNC_pipe_status_callback ASYNC_external_Write_Completed;
    public event ASYNC_pipe_status_callback ASYNC_external_Read_Completed;
    public delegate void ASYNC_pipe_status_callback(string message);


    private byte[] read_buffer = new byte[1024];
    private byte[] write_buffer = new byte[1024];

    private IAsyncResult read_result;
    private IAsyncResult write_result;

    private int read_id = 1;

    public Asynchronus_NamedPipe_Client(string pipe_address)
    {
        try
        {
            this.pipe_address = pipe_address;
            //  if(clientStream.IsConnected){UnityEngine.Debug.Log("Server Already Running");}else{}
            clientStream = new NamedPipeClientStream(".", this.pipe_address, PipeDirection.InOut, PipeOptions.Asynchronous);


            clientStream.Connect(1);
            if (clientStream.IsConnected)
            {
                Console.WriteLine("Connected to Server");
                Read_from_Server_Async();
            }
            else { Console.WriteLine("Could NOT connect to Server"); }

        }
        catch (Exception oEX) { Console.WriteLine("Application Pipe Error: "+oEX.Message); }


    }



    public void Write_to_Server_Async(string message)
    {
        if (clientStream != null)
        {
            if (clientStream.CanWrite && clientStream.IsConnected)
            {

                clientStream.WaitForPipeDrain();
                ASCIIEncoding.ASCII.GetBytes(message).CopyTo(write_buffer,0);
                clientStream.BeginWrite(write_buffer, 0, write_buffer.Length, new AsyncCallback(Async_Write_Completed), 1);

            } else { close_pipe(); }
        }

    }



    public void Read_from_Server_Async()
    {
        if (clientStream.CanRead && clientStream.IsConnected)
        {
            clientStream.BeginRead(read_buffer, 0, read_buffer.Length, new AsyncCallback(Async_Read_Completed), 2);
        } else { close_pipe(); }

    }



    private void Async_Write_Completed(IAsyncResult result)
    {
        clientStream.EndWrite(result);
        Debug.WriteLine("Written To Server => " + ASCIIEncoding.ASCII.GetString(write_buffer));
     // close_pipe();
    }



    private void Async_Read_Completed(IAsyncResult result)
    {

        clientStream.EndRead(result);
        Server_Message = ASCIIEncoding.ASCII.GetString(read_buffer);
        this.Server_Message.Trim();
        Console.WriteLine("Received from Server => " + Server_Message);
        Debug.WriteLine("Received from Server => " + Server_Message);
          if (clientStream.CanRead && clientStream.IsConnected)
        {
        Read_from_Server_Async();
        }
          else { close_pipe(); }

    }

    public Boolean Is_connected_to_server() {
        return clientStream.IsConnected;
    }



    public void close_pipe()
    {
        if (clientStream != null)
        {
            if (clientStream.IsConnected)
            {
                clientStream.Close();
                clientStream.Dispose();

                Debug.WriteLine(" Pipe Closed");
            }
        }

    }
}

}

服务器端Main方法实现

    static void Main(string[] args)
    {


       Asynchronus_NamedPipe_Server Async_server = new Asynchronus_NamedPipe_Server("mypipe7055");

        while (true)
            {

              do
              {
                  Async_server.Write_to_Client_Async("yeye");
                  Console.WriteLine("escape key");

              } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

            }

    }

服务器端类 -----

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Pipes;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;


namespace Application_Pipe
{
public class Asynchronus_NamedPipe_Server
{
    public readonly string pipe_address;
    private System.IO.Pipes.NamedPipeServerStream namedPipeServerStream;
    private string Server_Message;



    public delegate void ASYNC_pipe_status_callback(string message);


    private byte[] read_buffer = new byte[1024];
    private byte[] write_buffer = new byte[1024];


    public Asynchronus_NamedPipe_Server(string pipe_address)
    {
        try
        {

            this.pipe_address = pipe_address;
            namedPipeServerStream = new NamedPipeServerStream(this.pipe_address,
                 PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); //new NamedPipeServerStream(pipe_address);
            Console.WriteLine("Connecting to Client...");
            namedPipeServerStream.WaitForConnection();
            Console.WriteLine("Connected to Client");
           Read_from_Client_Async();

        }
        catch (Exception oEX) { Console.WriteLine(oEX.Message); }
    }





    public void Write_to_Client_Async(string message)
    {
        if (namedPipeServerStream != null)
        {
            if (namedPipeServerStream.CanWrite && namedPipeServerStream.IsConnected)
            {
                namedPipeServerStream.WaitForPipeDrain();
                ASCIIEncoding.ASCII.GetBytes(message).CopyTo(write_buffer,0);

                namedPipeServerStream.BeginWrite(write_buffer, 0, write_buffer.Length, new AsyncCallback(Async_Write_Completed), 2);

            }
            else { close_pipe(); }
        }
    }



    public void Read_from_Client_Async()
    {

        if (namedPipeServerStream != null)
        {
            if (namedPipeServerStream.CanRead && namedPipeServerStream.IsConnected)
            {

                namedPipeServerStream.BeginRead(read_buffer, 0, read_buffer.Length, new AsyncCallback(Async_Read_Completed), 1);
            } else { close_pipe(); }

        }
    }



    private void Async_Read_Completed(IAsyncResult result)
    {



       namedPipeServerStream.EndRead(result);

        this.Server_Message = ASCIIEncoding.ASCII.GetString(read_buffer);
        this.Server_Message.Trim();
        Debug.WriteLine("Received from Client => " + this.Server_Message+" <=REnd");

       Read_from_Client_Async();

    }


    private void Async_Write_Completed(IAsyncResult result)
    {
        namedPipeServerStream.EndWrite(result);
        Debug.WriteLine("Written To Client => " + ASCIIEncoding.ASCII.GetString(write_buffer));


    }

    public Boolean Is_connected_to_server()
    {
        return this.namedPipeServerStream.IsConnected;
    }


    public void close_pipe()
    {
        if(namedPipeServerStream.IsConnected){
        namedPipeServerStream.Disconnect();
        }
        namedPipeServerStream.Close();
        namedPipeServerStream.Dispose();

        Debug.WriteLine(" Pipe Closed");
    }

} //------class End
}
4

0 回答 0