提交 78090343 编写于 作者: T tanghai

C# 异步tcp库

上级 837c6190

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30110.0
VisualStudioVersion = 12.0.30501.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}"
EndProject
......@@ -68,6 +68,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logger", "Platform\Logger\Logger.csproj", "{72E16572-FC1F-4A9E-BC96-035417239298}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TNet", "Platform\TNet\TNet.csproj", "{B42D431A-3A54-4649-942A-C5356D7F9FBC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TNetTest", "Platform\TNetTest\TNetTest.csproj", "{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -360,6 +364,30 @@ Global
{72E16572-FC1F-4A9E-BC96-035417239298}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{72E16572-FC1F-4A9E-BC96-035417239298}.Release|Win32.ActiveCfg = Release|Any CPU
{72E16572-FC1F-4A9E-BC96-035417239298}.Release|x86.ActiveCfg = Release|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Debug|Win32.ActiveCfg = Debug|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Debug|x86.ActiveCfg = Debug|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Release|Any CPU.Build.0 = Release|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Release|Win32.ActiveCfg = Release|Any CPU
{B42D431A-3A54-4649-942A-C5356D7F9FBC}.Release|x86.ActiveCfg = Release|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Debug|Win32.ActiveCfg = Debug|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Debug|x86.ActiveCfg = Debug|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Release|Any CPU.Build.0 = Release|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Release|Win32.ActiveCfg = Release|Any CPU
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -367,27 +395,29 @@ Global
GlobalSection(NestedProjects) = preSolution
{C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{C46F3337-0F48-4A72-84AD-8FDD1F159BB0} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{8650195A-7904-4EBC-9D81-B392A7E9B9B3} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{999910D3-4E7D-45B1-BD2C-47289CD4D1AB} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{6C16281F-5550-4024-9504-295C63889E4F} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{5D6ECBCD-BE14-4DCB-BAEC-57089748B164} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
{3A98B35C-DEA8-489C-9203-263FFB6B065D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{48A2E149-0DAC-41B4-BB54-DFBCCD6D42B3} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{87537C92-B2C7-4E46-A6FB-02B73215C100} = {FD5F443E-CBEE-443E-821D-C47C86E09534}
{24233CD5-A5DF-484B-A482-B79CB7A0D9CB} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{C9992B7C-313E-4C9F-A954-640D01EDFB58} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{D0B4CFAC-A368-4742-9863-68776CFA9938} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{901A8E5C-C4C6-4C3C-8E18-068D75119F5D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{72E16572-FC1F-4A9E-BC96-035417239298} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{5D6ECBCD-BE14-4DCB-BAEC-57089748B164} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
{8650195A-7904-4EBC-9D81-B392A7E9B9B3} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{5AA48F9A-455D-4CD8-A605-A3AC38283E60} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
{999910D3-4E7D-45B1-BD2C-47289CD4D1AB} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{6C16281F-5550-4024-9504-295C63889E4F} = {6E9D97F0-4243-452E-B832-1A855B8118EB}
{B07D70E7-F902-4F67-A486-B7AF27D6B813} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
{6CD185D1-08E0-4729-A999-2D5B57BA8193} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
{87537C92-B2C7-4E46-A6FB-02B73215C100} = {FD5F443E-CBEE-443E-821D-C47C86E09534}
{C4E7A34A-095C-4983-AB63-FC2D20CD6824} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
{DD6F4735-E8E2-4F10-AE2B-434AB3A40236} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
{CBEDBE33-A883-4BFA-AE0A-8B3573F09BD0} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
{F2BEB8B2-0D9B-4CD9-A4BD-AE8E00903A67} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
{CB5BCF0A-4741-477A-94C6-49ECA782555F} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
{6CD185D1-08E0-4729-A999-2D5B57BA8193} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{72E16572-FC1F-4A9E-BC96-035417239298} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{B42D431A-3A54-4649-942A-C5356D7F9FBC} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
{F176D1FA-63E5-4B89-9A03-D44CCCCC069A} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
EndGlobalSection
EndGlobal
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("TNet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TNet")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("1d25432e-c932-4279-b52e-2dba415a191b")]
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B42D431A-3A54-4649-942A-C5356D7F9FBC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TNet</RootNamespace>
<AssemblyName>TNet</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TcpAcceptor.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
\ No newline at end of file
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace TNet
{
public class TcpAcceptor: IDisposable
{
private readonly Socket socket;
private readonly SocketAsyncEventArgs asyncEventArgs = new SocketAsyncEventArgs();
public TcpAcceptor(string ip, ushort port, int backLog = 100)
{
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.asyncEventArgs.Completed += OnArgsCompletion;
this.socket.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
this.socket.Listen(backLog);
}
public void Dispose()
{
this.socket.Dispose();
}
/// <summary>
/// SocketAsyncEventArgs Accept回调函数,回调可能已经到了另外的线程
/// </summary>
/// <returns></returns>
public Task<NetworkStream> AcceptAsync()
{
var tcs = new TaskCompletionSource<NetworkStream>();
try
{
asyncEventArgs.UserToken = tcs;
bool ret = this.socket.AcceptAsync(asyncEventArgs);
if (!ret)
{
if (asyncEventArgs.SocketError == SocketError.Success)
{
var acceptSocket = asyncEventArgs.AcceptSocket;
asyncEventArgs.AcceptSocket = null;
return Task.FromResult(new NetworkStream(acceptSocket, true));
}
tcs.TrySetException(new InvalidOperationException(this.asyncEventArgs.SocketError.ToString()));
}
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
return tcs.Task;
}
private static void OnArgsCompletion(object sender, SocketAsyncEventArgs e)
{
var tcs = (TaskCompletionSource<NetworkStream>)e.UserToken;
if (e.SocketError != SocketError.Success)
{
tcs.TrySetException(new InvalidOperationException(e.SocketError.ToString()));
return;
}
var acceptSocket = e.AcceptSocket;
e.AcceptSocket = null;
tcs.SetResult(new NetworkStream(acceptSocket, true));
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的常规信息通过以下特性集
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("TNetTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TNetTest")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
// 请将该类型上的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("a986af5b-92bb-4295-a58f-7fb68799fe79")]
// 程序集的版本信息由以下四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F176D1FA-63E5-4B89-9A03-D44CCCCC069A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TNetTest</RootNamespace>
<AssemblyName>TNetTest</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="TcpAcceptorTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Helper\Helper.csproj">
<Project>{24233cd5-a5df-484b-a482-b79cb7a0d9cb}</Project>
<Name>Helper</Name>
</ProjectReference>
<ProjectReference Include="..\Logger\Logger.csproj">
<Project>{72e16572-fc1f-4a9e-bc96-035417239298}</Project>
<Name>Logger</Name>
</ProjectReference>
<ProjectReference Include="..\TNet\TNet.csproj">
<Project>{b42d431a-3a54-4649-942a-c5356d7f9fbc}</Project>
<Name>TNet</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
\ No newline at end of file
using System;
using System.Net.Sockets;
using System.Threading;
using Helper;
using Logger;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TNet;
namespace TNetTest
{
[TestClass]
public class TcpAcceptorTest
{
private const ushort port = 11111;
private int count;
private readonly Barrier barrier = new Barrier(2);
private readonly object lockObject = new object();
[TestMethod]
public void AcceptAsync()
{
var thread1 = new Thread(Server);
thread1.Start();
Thread.Sleep(2);
for (int i = 0; i < 99; ++i)
{
var thread = new Thread(this.Client);
thread.Start();
}
barrier.SignalAndWait();
}
private async void Client()
{
using (var tcpClient = new TcpClient(AddressFamily.InterNetwork))
{
await tcpClient.ConnectAsync("127.0.0.1", port);
using (NetworkStream ns = tcpClient.GetStream())
{
try
{
var bytes = "tanghai".ToByteArray();
for (int i = 0; i < 100000; ++i)
{
await ns.WriteAsync(bytes, 0, bytes.Length);
int n = await ns.ReadAsync(bytes, 0, bytes.Length);
Assert.AreEqual(7, n);
}
}
catch (Exception e)
{
Log.Debug(e.ToString());
}
}
}
barrier.RemoveParticipants(1);
}
private async void Server()
{
using (var tcpAcceptor = new TcpAcceptor("127.0.0.1", port))
{
while (count != 99)
{
Log.Debug("start server response");
NetworkStream ns = await tcpAcceptor.AcceptAsync();
// 这里可能已经不在Server函数线程了
Log.Debug("server response");
Response(ns);
}
}
}
private async void Response(NetworkStream ns)
{
try
{
var bytes = new byte[1000];
for (int i = 0; i < 100000; ++i)
{
int n = await ns.ReadAsync(bytes, 0, 100);
await ns.WriteAsync(bytes, 0, n);
}
lock (lockObject)
{
++count;
}
}
catch (Exception e)
{
Log.Debug(e.ToString());
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册