本文整理汇总了C#中Reader.Dispatch方法的典型用法代码示例。如果您正苦于以下问题:C# Reader.Dispatch方法的具体用法?C# Reader.Dispatch怎么用?C# Reader.Dispatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Reader
的用法示例。
在下文中一共展示了Reader.Dispatch方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: handle
async void handle(TcpClient client, Action<Reader, Writer> callback)
{
try
{
using (var s = client.GetStream())
using (var b = new BufferedStream(s))
{
var w = new Writer();
var r = new Reader();
callback(r, w);
while (true)
{
r.Stream.Position = 0;
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)(w);
if (w.Stream.Position > 0)
{
var buf = w.Stream.GetBuffer();
var length = (int)w.Stream.Position;
await s.WriteAsync(buf, 0, length).ConfigureAwait(false);
w.Stream.Position = 0;
}
}
}
}
catch (Exception ex)
{
OnException(ex);
}
finally
{
client.Close();
}
}
示例2: 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;
}