The identified channel client will not work in unit3d

I wrote this code in C # to have namedPipeServer and NamedPipeClient, with asynchronous read and write settings, connect to each other. Both codes work fine on Visual Studio 2010, which I use when reading and writing works well, without hanging the application at runtime.

But I want the client side to work in unit3d. The problem I am facing is the client code implemented in Unity3D. When I use Write_to_Server_Async (string message) the server side read is not called and is only called when I exit Unity3d (I usually end its process). I might say there is something wrong with Unity3D because the exact code works great in visual studio, so I know my code is implemented correctly. I've heard about how unity3d doesn't really use real streams unless the user manually creates it, but didn't even solve the problem. My guess is that the Unity3D devs could have built their own version of the .NET library 3.5 (sounds bizzare (explains why they still haven't adopted 4.5)), or somehow they had to structure their library that way,so that functions like NamedPipeClientStream.BeginWrite cannot create its own real thread by default. But then again I'm not sure what the issue is with threads.

At the moment, I would like someone to come up with a good explanation.

Make sure to replace Debug.WriteLine with UnityEngine.Debug.Log in unity3d.

Following is the code and class of the main client method

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 Class

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

    }
}

}

      

Executing the main method on the server side

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

            }

    }

      

Server class -----

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
}

      

+3


source to share





All Articles