本文整理汇总了C#中Writer.RequestAdd方法的典型用法代码示例。如果您正苦于以下问题:C# Writer.RequestAdd方法的具体用法?C# Writer.RequestAdd怎么用?C# Writer.RequestAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Writer
的用法示例。
在下文中一共展示了Writer.RequestAdd方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: runClient
static async Task<int> runClient(int count)
{
int restCount = count;
using (var client = new TcpClient())
{
await client.ConnectAsync(IPAddress.Loopback, port).ConfigureAwait(false);
using (var s = client.GetStream())
{
var w = new Writer();
var r = new Reader();
var expect = 7;
r.OnAdd = x =>
{
if (expect == x)
{
Interlocked.Decrement(ref restCount);
Interlocked.Increment(ref okCounter);
}
else
{
Interlocked.Increment(ref ngCounter);
Console.WriteLine("expect: " + expect);
Console.WriteLine("fact : " + x);
}
};
var t1 = Task.Run(async () =>
{
try
{
using (var b = new BufferedStream(s))
{
for (int i = 0; i < count; ++i)
{
await recv(b, r, (int)RpcHeader.HeaderLength).ConfigureAwait(false);
var header = r.ReadHeader();
if(header.Length > r.Buffer.Length)
{
throw new InvalidDataException("packet too long size=" + header.Length);
}
await recv(b, r, (int)header.Length).ConfigureAwait(false);
r.Dispatch(header)(null);
Interlocked.Increment(ref clientCounter);
}
}
}
catch (Exception ex)
{
Console.WriteLine("client recv: " + ex.Message);
Console.WriteLine(ex);
}
});
var t2 = Task.Run(async () =>
{
try
{
for (int i = 0; i < count; ++i)
{
w.RequestAdd(3, 4);
int length = (int)w.Stream.Length;
w.Stream.Position = 0;
await s.WriteAsync(w.Stream.GetBuffer(), 0, length).ConfigureAwait(false);
Interlocked.Increment(ref sendCounter);
}
}
catch (Exception ex)
{
Console.WriteLine("client recv: " + ex.Message);
}
});
await Task.WhenAll(t1, t2).ConfigureAwait(false);
}
}
if (restCount > 0)
{
Console.WriteLine("retry: " + restCount);
}
return restCount;
}