Quantcast
Channel: Questions in topic: "pipe"
Viewing all articles
Browse latest Browse all 89

Windows build for game using named pipes hangs on Stream.BeginRead

$
0
0
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(); } }

Viewing all articles
Browse latest Browse all 89

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>