コード例 #1
0
        public async Task <byte[]> SendMessageAsync(byte[] message)
        {
            using (var pipe = new NamedPipeClientStream(_serverName, _pipeName, PipeDirection.InOut, PipeOptions.Asynchronous))
            {
                await pipe.ConnectAsync();

                var requestLengthBytes = BitConverter.GetBytes(message.Length);
                await pipe.WriteAsync(requestLengthBytes, 0, 4);

                await pipe.WriteAsync(message, 0, message.Length);

                await pipe.FlushAsync();

                var responseLengthBytes = await pipe.ReadExactlyAsync(4);

                var responseLength = BitConverter.ToInt32(responseLengthBytes, 0);
                var response       = await pipe.ReadExactlyAsync(responseLength);

                return(response);
            }
        }
コード例 #2
0
        public async Task <Stream> SendMessageAsync(Stream message)
        {
            using (var pipe = new NamedPipeClientStream(_serverName, _pipeName, PipeDirection.InOut, PipeOptions.Asynchronous))
            {
                await pipe.ConnectAsync();

                var requestLengthBytes = BitConverter.GetBytes(message.Length);
                await pipe.WriteAsync(requestLengthBytes, 0, 4);

                await message.CopyToAsync(pipe);

                await pipe.FlushAsync();

                var responseLengthBytes = await pipe.ReadExactlyAsync(4);

                var responseLength = BitConverter.ToInt32(responseLengthBytes, 0);

                bool success = false;
                var  ms      = new RecyclableMemoryStream(StreamPool.Shared);
                try
                {
                    await pipe.ReadExactlyAsync(ms, responseLength);

                    ms.Position = 0;
                    success     = true;
                }
                finally
                {
                    if (!success)
                    {
                        ms.Dispose();
                    }
                }

                return(ms);
            }
        }