本文整理汇总了C#中System.IO.Pipes.NamedPipeServerStream.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# NamedPipeServerStream.WriteAsync方法的具体用法?C# NamedPipeServerStream.WriteAsync怎么用?C# NamedPipeServerStream.WriteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Pipes.NamedPipeServerStream
的用法示例。
在下文中一共展示了NamedPipeServerStream.WriteAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NamedPipeReadViaAsyncFileStream
public async Task NamedPipeReadViaAsyncFileStream(bool asyncReads)
{
string name = Guid.NewGuid().ToString("N");
using (var server = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
Task serverTask = Task.Run(async () =>
{
await server.WaitForConnectionAsync();
await server.WriteAsync(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 6);
});
WaitNamedPipeW(@"\\.\pipe\" + name, -1);
using (SafeFileHandle clientHandle = CreateFileW(@"\\.\pipe\" + name, GENERIC_READ, FileShare.None, IntPtr.Zero, FileMode.Open, (int)PipeOptions.Asynchronous, IntPtr.Zero))
using (var client = new FileStream(clientHandle, FileAccess.Read, bufferSize: 3, isAsync: true))
{
var arr = new byte[1];
for (int i = 0; i < 6; i++)
{
Assert.Equal(1, asyncReads ?
await client.ReadAsync(arr, 0, 1) :
client.Read(arr, 0, 1));
Assert.Equal(i, arr[0]);
}
}
await serverTask;
}
}
示例2: NamedPipeViaFileStream_AllDataCopied
public async Task NamedPipeViaFileStream_AllDataCopied(bool useAsync, int writeSize, int numWrites)
{
long totalLength = writeSize * numWrites;
var expectedData = new byte[totalLength];
new Random(42).NextBytes(expectedData);
var results = new MemoryStream();
var pipeOptions = useAsync ? PipeOptions.Asynchronous : PipeOptions.None;
string name = Guid.NewGuid().ToString("N");
using (var server = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, pipeOptions))
{
Task serverTask = Task.Run(async () =>
{
await server.WaitForConnectionAsync();
for (int i = 0; i < numWrites; i++)
{
await server.WriteAsync(expectedData, i * writeSize, writeSize);
}
server.Dispose();
});
Assert.True(WaitNamedPipeW(@"\\.\pipe\" + name, -1));
using (SafeFileHandle clientHandle = CreateFileW(@"\\.\pipe\" + name, GENERIC_READ, FileShare.None, IntPtr.Zero, FileMode.Open, (int)pipeOptions, IntPtr.Zero))
using (var client = new FileStream(clientHandle, FileAccess.Read, bufferSize: 3, isAsync: useAsync))
{
Task copyTask = client.CopyToAsync(results, (int)totalLength);
await await Task.WhenAny(serverTask, copyTask);
await copyTask;
}
}
byte[] actualData = results.ToArray();
Assert.Equal(expectedData.Length, actualData.Length);
Assert.Equal<byte>(expectedData, actualData);
}
示例3: ServerPInvokeChecks
public static async Task ServerPInvokeChecks()
{
// calling every API related to server and client to detect any bad PInvokes
using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.Out))
{
Task clientTask = Task.Run(() => StartClient(PipeDirection.In));
server.WaitForConnection();
Assert.False(server.CanRead);
Assert.False(server.CanSeek);
Assert.False(server.CanTimeout);
Assert.True(server.CanWrite);
Assert.False(server.IsAsync);
Assert.True(server.IsConnected);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Equal(0, server.OutBufferSize);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Assert.True(server.OutBufferSize > 0);
}
else
{
Assert.Throws<PlatformNotSupportedException>(() => server.OutBufferSize);
}
Assert.Equal(PipeTransmissionMode.Byte, server.ReadMode);
Assert.NotNull(server.SafePipeHandle);
Assert.Equal(PipeTransmissionMode.Byte, server.TransmissionMode);
server.Write(new byte[] { 123 }, 0, 1);
await server.WriteAsync(new byte[] { 124 }, 0, 1);
server.Flush();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
server.WaitForPipeDrain();
}
else
{
Assert.Throws<PlatformNotSupportedException>(() => server.WaitForPipeDrain());
}
await clientTask;
}
using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.In))
{
Task clientTask = Task.Run(() => StartClient(PipeDirection.Out));
server.WaitForConnection();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Equal(0, server.InBufferSize);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Assert.True(server.InBufferSize > 0);
}
else
{
Assert.Throws<PlatformNotSupportedException>(() => server.InBufferSize);
}
byte[] readData = new byte[] { 0, 1 };
Assert.Equal(1, server.Read(readData, 0, 1));
Assert.Equal(1, server.ReadAsync(readData, 1, 1).Result);
Assert.Equal(123, readData[0]);
Assert.Equal(124, readData[1]);
}
}
示例4: writeMessageAsync
public async Task writeMessageAsync(NamedPipeServerStream stream, string message, CancellationToken cancelToken,bool addDefaultMessageType = false)
{
byte[] msg;
if (addDefaultMessageType)
msg = Encoding.ASCII.GetBytes("<" + m_defaultMessageType + ">" + message + "</" + m_defaultMessageType + ">");
else
msg = Encoding.ASCII.GetBytes(message);
try
{
await stream.WriteAsync(msg, 0, msg.Length,cancelToken);
}
catch
{
logMessage("write operation cancelled");
}
}
示例5: ClientCloneTests
public static void ClientCloneTests()
{
const string pipeName = "fooClientclone";
byte[] msg1 = new byte[] { 5, 7, 9, 10 };
byte[] received0 = new byte[] { };
byte[] received1 = new byte[] { 0, 0, 0, 0 };
using (NamedPipeServerStream server = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte))
{
using (NamedPipeClientStream clientBase = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None))
{
Task clientTask = Task.Run(() =>
{
clientBase.Connect();
using (NamedPipeClientStream client = new NamedPipeClientStream(PipeDirection.InOut, false, true, clientBase.SafePipeHandle))
{
client.Write(msg1, 0, msg1.Length);
int serverCount = client.NumberOfServerInstances;
Assert.Equal(1, serverCount);
Assert.Equal(PipeTransmissionMode.Byte, client.TransmissionMode);
}
});
server.WaitForConnection();
int len1 = server.Read(received1, 0, msg1.Length);
Assert.Equal(msg1.Length, len1);
Assert.Equal(msg1, received1);
// test special cases of buffer lengths = 0
int len0 = server.Read(received0, 0, 0);
Assert.Equal(0, len0);
server.Write(received0, 0, 0);
Task<int> serverTaskRead = server.ReadAsync(received0, 0, 0);
serverTaskRead.Wait();
Assert.Equal(0, serverTaskRead.Result);
server.WriteAsync(received0, 0, 0).Wait();
}
}
}
示例6: ServerPInvokeChecks
public static void ServerPInvokeChecks()
{
// calling every API related to server and client to detect any bad PInvokes
using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.Out))
{
Task clientTask = StartClientAsync(PipeDirection.In);
server.WaitForConnection();
Console.WriteLine("server.CanRead = {0}", server.CanRead);
Console.WriteLine("server.CanSeek = {0}", server.CanSeek);
Console.WriteLine("server.CanTimeout = {0}", server.CanTimeout);
Console.WriteLine("server.CanWrite = {0}", server.CanWrite);
Console.WriteLine("server.IsAsync = {0}", server.IsAsync);
Console.WriteLine("server.IsConnected = {0}", server.IsConnected);
Console.WriteLine("server.OutBufferSize = {0}", server.OutBufferSize);
Console.WriteLine("server.ReadMode = {0}", server.ReadMode);
Console.WriteLine("server.SafePipeHandle = {0}", server.SafePipeHandle);
Console.WriteLine("server.TransmissionMode = {0}", server.TransmissionMode);
server.Write(new byte[] { 123 }, 0, 1);
server.WriteAsync(new byte[] { 124 }, 0, 1).Wait();
server.Flush();
server.WaitForPipeDrain();
clientTask.Wait();
}
using (NamedPipeServerStream server = new NamedPipeServerStream("foo", PipeDirection.In))
{
Task clientTask = StartClientAsync(PipeDirection.Out);
server.WaitForConnection();
Console.WriteLine("server.InBufferSize = {0}", server.InBufferSize);
byte[] readData = new byte[] { 0, 1 };
server.Read(readData, 0, 1);
server.ReadAsync(readData, 1, 1).Wait();
Assert.Equal(123, readData[0]);
Assert.Equal(124, readData[1]);
}
}