- Home /
Windows build for game using named pipes hangs on Stream.BeginRead
I have a standalone simple game that uses named pipes to communicate with a Windows forms application. The Unity game is built with .NET 2.0 (not 2.0 subset) and the Windows application is built on the .NET 4.0 Client Profile. My problem is that, whenever I launch the Windows forms app ("the server") and the Unity game ("the client"), if I close the client, it becomes non-responsive. It closes only after I close the server window. Below I post the simplified code for both the server and client. Any help on this issue would be much appreciated; also I would like to know if anyone can replicate this behaviour.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
namespace TestServer
{
public class Server
{
private const int BUFFER_SIZE = 1024;
private NamedPipeServerStream PipelineStream = null;
public Server()
{
OpenPipe();
while (true)
{
if (PipelineStream != null && PipelineStream.IsConnected)
{
Thread.Sleep(1000);
}
}
ClosePipe();
}
private void OpenPipe()
{
PipelineStream = new NamedPipeServerStream("Pipeline", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFER_SIZE, BUFFER_SIZE);
PipelineStream.WaitForConnection();
}
private void ClosePipe()
{
if (PipelineStream != null)
{
PipelineStream.Flush();
PipelineStream.Close();
PipelineStream.Dispose();
PipelineStream = null;
}
}
}
}
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Text;
public class Client : MonoBehaviour
{
private NamedPipeClientStream PipelineStream = null;
private void Start()
{
OpenPipe();
ReadAsync();
}
private void OnDestroy()
{
ClosePipe();
}
private void OpenPipe()
{
PipelineStream = new NamedPipeClientStream(".", "Pipeline", PipeDirection.In, PipeOptions.Asynchronous);
PipelineStream.Connect();
}
private void ClosePipe()
{
if (PipelineStream != null)
{
PipelineStream.Close();
PipelineStream.Dispose();
PipelineStream = null;
}
}
private void ReadAsync()
{
if (PipelineStream != null && PipelineStream.IsConnected)
{
byte[] buffer = new byte[1];
PipelineStream.BeginRead(buffer, 0, 1, ReadAsyncCallback, null);
}
}
private void ReadAsyncCallback(IAsyncResult result)
{
int amountRead = PipelineStream.EndRead(result);
if (amountRead > 0)
ReadAsync();
}
}
Interestingly enough the above code works without a glitch and properly on a $$anonymous$$ac and in Windows 7 running on a $$anonymous$$ac under Parallels.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Send data from unity to windows form application using named pipe? 0 Answers
Type or namespace name Pipes does not exist in the namespace System.IO. 1 Answer
finish on UnityPlayerActivity kills the entire application 2 Answers
Need help for simple moveforward function in Unity Web Player and browser communication 0 Answers