命名管道客户端将无法在unity3d中工作

时间:2014-12-20 17:58:29

标签: c# visual-studio-2010 unity3d namedpipeserverstream

我编写了这个C#代码,使用namedPipeServer和NamedPipeClient,通过异步读写设置,相互连接。这两个代码在visual studio 2010上运行完美,我使用的是读写功能,运行时没有任何应用程序冻结。

但我希望客户端在unity3d中运行。我遇到的问题是在Unity3D中实现的客户端代码。当我使用Write_to_Server_Async(字符串消息)时,不会调用服务器端的读取,只在我退出Unity3d时调用(我通常会结束其进程)。我可以说Unity3D有问题,因为确切的代码在visual studio中完美运行,所以我知道我的代码是以正确的方式实现的。我听说过unity3d如何不真正使用真正的线程,除非用户手动创建一个,但即使没有解决问题。我的猜测是Unity3D开发人员可能已经创建了他们的.NET库3.5版本(听起来很棒(确实解释了他们为什么还没有采用4.5))或者他们必须以某种方式构建他们的库,默认情况下像NamedPipeClientStream.BeginWrite这样的函数无法创建自己的真实线程。但话又说回来,我不确定它是否与线程有关。

目前,我希望有人能够提出好的解释。

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

以下是客户主要方法代码和类

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");
            }
        }

    }
}

}

服务器端主方法实现

    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
}

0 个答案:

没有答案