- Home /
Pipe problem for interconnection between unity 5.1 and third app
Hi
I am trying to use named pipes in Unity to pass data to and from another application. I made test Pipe Server in VS2012
static void Main(string[] args) { CreatingPipeServerLoop(); }
const byte sentServerValue = 1;
const byte sentClientValue = 2;
static public void CreatingPipeServerLoop()
{
while (true)
{
Thread.Sleep(500);
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("pipetest", PipeDirection.InOut))
{
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
try
{
byte answer = (byte)pipeServer.ReadByte();
pipeServer.WriteByte(sentServerValue);
pipeServer.Flush();
}
catch (Exception e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
}
}
And I made Pipe Client in Unity3d 5.1.2f1 :
void Start () {
System.Threading.Thread pipeThread = new System.Threading.Thread(delegate() { CreatingPipeClientLoop(); });
pipeThread.Start();
}
// Update is called once per frame
void Update () {
}
const byte sentServerValue = 1;
const byte sentClientValue = 2;
public void CreatingPipeClientLoop()
{
for (int i = 0; i < 100000; i++)
{
//byte respByte = 0;// MainBaseErrorExcess;
using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream("pipetest"))
{
try
{
pipeClient.Connect();
//Console.WriteLine("Server connected.");
pipeClient.WriteByte(sentClientValue);
pipeClient.Flush();
byte respByte = (byte)pipeClient.ReadByte();
}
catch (System.Exception e)
{
//Console.WriteLine("ERROR: {0}", e.Message);
}
}
}
}
I started Server and then Client in Unity3d 5.1.2f1. Server connected 1-2 times then Unity crashed. Some times pipeClient threw exception "The operation completed successfully". I launched this test project on 5 PC with Windows 7 (64 bit) every time Unity crashed. It worked only on PC with Windows 7 (32 bit) and Windows 7 on Mac. Also it worked in Unity 4.5, befor I updated Unity I did not know about the problem. How to implement exchange between Unity and another application?
Regards, Anatoly Frolkin.
Function CreatingPipeServerLoop() is used in VS2012 console application just for test. It has endless loop and it called once in function $$anonymous$$ain(). In general program I dont use such loop. Function CreatingPipeClientLoop() is used in Unity, it does not have endless loop. It finishes when OnDisable() executes.
I change code "while(loop)" to "for (int i = 0; i < 100000; i++)" but Unity still crashed after 1-10 connections with Server App. Anyway thank you VesuvianPrime, I dont know about one thread.
Did you ever manage to solve this problem? I believe I am encountering exactly this same problem, and I'm really not sure what I can do about it.
Further information I've found: Attaching VS to the Unity Editor when it crashes tells me "A heap has been corrupted". I've managed to trim out a bunch of code, to the point where I simply create the client and connect (ie. no writing to it, or flushing, or reading), and it still happens when I loop thru this repeatedly, after between 1 and 6 times or so. Seems to be random, which makes sense with corrupted heaps.
I do not use pipes. I use sockets, I don't have any problem with sockets.