当前位置: 首页>>代码示例>>C#>>正文


C# Reader.ReadHeader方法代码示例

本文整理汇总了C#中Reader.ReadHeader方法的典型用法代码示例。如果您正苦于以下问题:C# Reader.ReadHeader方法的具体用法?C# Reader.ReadHeader怎么用?C# Reader.ReadHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Reader的用法示例。


在下文中一共展示了Reader.ReadHeader方法的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();
            }
        }
开发者ID:hythof,项目名称:csharp,代码行数:44,代码来源:RpcServer.cs

示例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;
        }
开发者ID:hythof,项目名称:csharp,代码行数:81,代码来源:Program.cs


注:本文中的Reader.ReadHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。