async private Task <int> TestAsyncOutputStream_BeginCancelBeinOutputRead_RemotelyInvokable(string pipesHandle) { string[] pipeHandlers = pipesHandle.Split(' '); using (AnonymousPipeClientStream pipeRead = new AnonymousPipeClientStream(PipeDirection.In, pipeHandlers[0])) using (AnonymousPipeClientStream pipeWrite = new AnonymousPipeClientStream(PipeDirection.Out, pipeHandlers[1])) { // Signal child process start await pipeWrite.WriteAsync(new byte[1], 0, 1); // Wait parent signal to produce number 1,2,3 Assert.True(await WaitPipeSignal(pipeRead, WaitInMS), "Missing parent signal to produce number 1,2,3"); Console.WriteLine(1); Console.WriteLine(2); Console.WriteLine(3); await pipeWrite.WriteAsync(new byte[1], 0, 1); // Wait parent cancellation signal and produce new values 4,5,6 Assert.True(await WaitPipeSignal(pipeRead, WaitInMS), "Missing parent signal to produce number 4,5,6"); Console.WriteLine(4); Console.WriteLine(5); Console.WriteLine(6); // Wait parent re-start listening signal and produce 7,8,9 Assert.True(await WaitPipeSignal(pipeRead, WaitInMS), "Missing parent re-start listening signal"); Console.WriteLine(7); Console.WriteLine(8); Console.WriteLine(9); return(RemoteExecutor.SuccessExitCode); } }
private async Task <bool> DoListen() { byte[] buffer; int streamSize = 0; bool isCancel = false; try { buffer = await GetByteArrayFromStreamAsync(_pipeRead, sizeof(int)); streamSize = BitConverter.ToInt32(buffer, 0); if (streamSize == 0) { return(false); } } catch (Exception e) { Logger.LogError(e); return(false); } buffer = await GetByteArrayFromStreamAsync(_pipeRead, streamSize); string request = buffer.GetString(); //Encoding.UTF8.GetString(buffer); Logger.LogDebug(request); isCancel = request == close_channel_method_name; var response = isCancel ? "OK" : await _messageProcessor(request, _cancellationToken); Logger.LogDebug(response); buffer = response.GetBytes(); //Encoding.UTF8.GetBytes(response); #if NETCOREAPP await _pipeWrite.WriteAsync(BitConverter.GetBytes(buffer.Length).AsMemory(0, sizeof(int)), _cancellationToken); await _pipeWrite.WriteAsync(buffer.AsMemory(0, buffer.Length), _cancellationToken); #else await _pipeWrite.WriteAsync(BitConverter.GetBytes(buffer.Length), 0, sizeof(int), _cancellationToken); await _pipeWrite.WriteAsync(buffer, 0, buffer.Length, _cancellationToken); #endif return(!isCancel); }
async private Task <int> TestAsyncOutputStream_CancelOutputRead_RemotelyInvokable(string pipesHandle) { string[] pipeHandlers = pipesHandle.Split(' '); using (AnonymousPipeClientStream pipeRead = new AnonymousPipeClientStream(PipeDirection.In, pipeHandlers[0])) using (AnonymousPipeClientStream pipeWrite = new AnonymousPipeClientStream(PipeDirection.Out, pipeHandlers[1])) { // Signal child process start await pipeWrite.WriteAsync(new byte[1], 0, 1); // Wait parent signal to produce number 1 // Generate output 1 and signal parent Assert.True(await WaitPipeSignal(pipeRead, WaitInMS), "Missing parent signal to produce number 1"); Console.WriteLine(1); await pipeWrite.WriteAsync(new byte[1], 0, 1); // Wait parent signal to produce number 2 // Generate output 2 and signal parent Assert.True(await WaitPipeSignal(pipeRead, WaitInMS), "Missing parent signal to produce number 2"); Console.WriteLine(2); await pipeWrite.WriteAsync(new byte[1], 0, 1); return(RemoteExecutor.SuccessExitCode); } }
private async Task WriteDateTime(DateTime dt, CancellationToken token) { var dtLong = dt.ToBinary(); var bArray = BitConverter.GetBytes(dtLong); // await Task.Delay(30, token); await _semaphoreSlim.WaitAsync(token).ConfigureAwait(false); try { // Console.WriteLine($"PgClient [{_process.Id}]: Sent"); await _pipeClient.WriteAsync(bArray, 0, bArray.Length, token).ConfigureAwait(false); } finally { _semaphoreSlim.Release(); } }
public static void ClientPInvokeChecks() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) { Task serverTask = Task.Run(() => DoStreamOperations(server)); Assert.False(client.CanRead); Assert.False(client.CanSeek); Assert.False(client.CanTimeout); Assert.True(client.CanWrite); Assert.False(client.IsAsync); Assert.True(client.IsConnected); Assert.Equal(0, client.OutBufferSize); Assert.Equal(PipeTransmissionMode.Byte, client.ReadMode); Assert.NotNull(client.SafePipeHandle); Assert.Equal(PipeTransmissionMode.Byte, client.TransmissionMode); client.Write(new byte[] { 123 }, 0, 1); client.WriteAsync(new byte[] { 124 }, 0, 1).Wait(); client.WaitForPipeDrain(); client.Flush(); serverTask.Wait(); } } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) { Task serverTask = Task.Run(() => DoStreamOperations(server)); Assert.Equal(4096, client.InBufferSize); byte[] readData = new byte[] { 0, 1 }; Assert.Equal(1, client.Read(readData, 0, 1)); Assert.Equal(1, client.ReadAsync(readData, 1, 1).Result); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); serverTask.Wait(); } } }
public static void ClientPInvokeChecks() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) { Task serverTask = DoServerOperationsAsync(server); Console.WriteLine("client.CanRead = {0}", client.CanRead); Console.WriteLine("client.CanSeek = {0}", client.CanSeek); Console.WriteLine("client.CanTimeout = {0}", client.CanTimeout); Console.WriteLine("client.CanWrite = {0}", client.CanWrite); Console.WriteLine("client.IsAsync = {0}", client.IsAsync); Console.WriteLine("client.IsConnected = {0}", client.IsConnected); Console.WriteLine("client.OutBufferSize = {0}", client.OutBufferSize); Console.WriteLine("client.ReadMode = {0}", client.ReadMode); Console.WriteLine("client.SafePipeHandle = {0}", client.SafePipeHandle); Console.WriteLine("client.TransmissionMode = {0}", client.TransmissionMode); client.Write(new byte[] { 123 }, 0, 1); client.WriteAsync(new byte[] { 124 }, 0, 1).Wait(); client.WaitForPipeDrain(); client.Flush(); serverTask.Wait(); } } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) { Task serverTask = DoServerOperationsAsync(server); Console.WriteLine("client.InBufferSize = {0}", client.InBufferSize); byte[] readData = new byte[] { 0, 1 }; client.Read(readData, 0, 1); client.ReadAsync(readData, 1, 1).Wait(); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); serverTask.Wait(); } } }
public static async Task ClientPInvokeChecks() { using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.In, System.IO.HandleInheritability.None, 4096)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.Out, server.ClientSafePipeHandle)) { Task serverTask = Task.Run(() => DoStreamOperations(server)); Assert.False(client.CanRead); Assert.False(client.CanSeek); Assert.False(client.CanTimeout); Assert.True(client.CanWrite); Assert.False(client.IsAsync); Assert.True(client.IsConnected); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Assert.Equal(0, client.OutBufferSize); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Assert.True(client.OutBufferSize > 0); } else { Assert.Throws <PlatformNotSupportedException>(() => client.OutBufferSize); } Assert.Equal(PipeTransmissionMode.Byte, client.ReadMode); Assert.NotNull(client.SafePipeHandle); Assert.Equal(PipeTransmissionMode.Byte, client.TransmissionMode); client.Write(new byte[] { 123 }, 0, 1); await client.WriteAsync(new byte[] { 124 }, 0, 1); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { client.WaitForPipeDrain(); } else { Assert.Throws <PlatformNotSupportedException>(() => client.WaitForPipeDrain()); } client.Flush(); await serverTask; } } using (AnonymousPipeServerStream server = new AnonymousPipeServerStream(PipeDirection.Out)) { using (AnonymousPipeClientStream client = new AnonymousPipeClientStream(PipeDirection.In, server.ClientSafePipeHandle)) { Task serverTask = Task.Run(() => DoStreamOperations(server)); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Assert.Equal(4096, client.InBufferSize); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Assert.True(client.InBufferSize > 0); } else { Assert.Throws <PlatformNotSupportedException>(() => client.InBufferSize); } byte[] readData = new byte[] { 0, 1 }; Assert.Equal(1, client.Read(readData, 0, 1)); Assert.Equal(1, client.ReadAsync(readData, 1, 1).Result); Assert.Equal(123, readData[0]); Assert.Equal(124, readData[1]); await serverTask; } } }