提交 b53bd22f 编写于 作者: T tanghai

1.修复TBuffer一个bug

2.TNetTest echo 10000次,windows耗时3s,linux耗时6s,性能还行
上级 1d9f5187
......@@ -20,6 +20,7 @@
<ExpressionBlendVersion>4.0.20621.0</ExpressionBlendVersion>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\CSharp\</SolutionDir>
<RestorePackages>true</RestorePackages>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>发布\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
......@@ -32,12 +33,11 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
......@@ -45,7 +45,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
......@@ -96,6 +96,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml">
......
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.6.4.0" newVersion="6.6.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.PubSubEvents" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<appSettings>
<add key="IP" value="192.168.11.95" />
<add key="Port" value="8888" />
<add key="Account" value="egametang@163.com" />
<add key="Password" value="163bio1" />
<add key="NodePath" value="D:\Source\Demo\battledemo\Assets\Resources\Config\node.bytes" />
</appSettings>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_Calculator" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:10000/Calculator/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_Calculator" contract="Proxy.Calculator" name="NetTcpBinding_Calculator">
</endpoint>
</client>
</system.serviceModel>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.6.4.0" newVersion="6.6.4.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.PubSubEvents" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
<appSettings>
<add key="IP" value="192.168.11.95"/>
<add key="Port" value="8888"/>
<add key="Account" value="egametang@163.com"/>
<add key="Password" value="163bio1"/>
<add key="NodePath" value="D:\Source\Demo\battledemo\Assets\Resources\Config\node.bytes"/>
<add key="ClientSettingsProvider.ServiceUri" value=""/>
</appSettings>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_Calculator"/>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:10000/Calculator/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_Calculator" contract="Proxy.Calculator" name="NetTcpBinding_Calculator">
</endpoint>
</client>
</system.serviceModel>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri=""/>
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400"/>
</providers>
</roleManager>
</system.web>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
<assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.PubSubEvents" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
<assemblyIdentity name="Microsoft.Practices.Prism.PubSubEvents" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup></configuration>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
<assemblyIdentity name="Microsoft.Practices.Prism.SharedInterfaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Prism.PubSubEvents" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
<assemblyIdentity name="Microsoft.Practices.Prism.PubSubEvents" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup></configuration>

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
# Visual Studio 2012
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Game", "Game", "{BEA22CC3-9FA8-4663-82DC-6B2119A8E35E}"
......@@ -39,14 +39,18 @@ Global
{0608C0C1-F584-4092-AAF9-1E99A78E3703}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0608C0C1-F584-4092-AAF9-1E99A78E3703}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0608C0C1-F584-4092-AAF9-1E99A78E3703}.Release|Any CPU.Build.0 = Release|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Release|Any CPU.Build.0 = Release|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Release|Any CPU.Build.0 = Release|Any CPU
{7D5B90DF-43AC-42B8-9A52-2B985BC42A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D5B90DF-43AC-42B8-9A52-2B985BC42A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D5B90DF-43AC-42B8-9A52-2B985BC42A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D5B90DF-43AC-42B8-9A52-2B985BC42A5C}.Release|Any CPU.Build.0 = Release|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Release|Any CPU.Build.0 = Release|Any CPU
{B1B78524-3F75-40D7-BF12-10B25A00C590}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1B78524-3F75-40D7-BF12-10B25A00C590}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1B78524-3F75-40D7-BF12-10B25A00C590}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -59,21 +63,14 @@ Global
{DEF89A61-8AC7-4CCD-80B8-FC6AA2571E35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEF89A61-8AC7-4CCD-80B8-FC6AA2571E35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEF89A61-8AC7-4CCD-80B8-FC6AA2571E35}.Release|Any CPU.Build.0 = Release|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1888D319-0495-43F3-BA8D-163EC20D9437}.Release|Any CPU.Build.0 = Release|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47A7404D-F501-43C5-8183-4B4E9E8C24B2}.Release|Any CPU.Build.0 = Release|Any CPU
{EA0D6994-A254-4DFE-9ADC-A29B7005419F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA0D6994-A254-4DFE-9ADC-A29B7005419F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA0D6994-A254-4DFE-9ADC-A29B7005419F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA0D6994-A254-4DFE-9ADC-A29B7005419F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
{F06B995E-6508-4C5C-A396-554316C9215D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F06B995E-6508-4C5C-A396-554316C9215D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0608C0C1-F584-4092-AAF9-1E99A78E3703} = {BEA22CC3-9FA8-4663-82DC-6B2119A8E35E}
......@@ -86,4 +83,7 @@ Global
{47A7404D-F501-43C5-8183-4B4E9E8C24B2} = {C69F1D82-6684-49D1-B388-CA4AECD6AD0D}
{EA0D6994-A254-4DFE-9ADC-A29B7005419F} = {C6DA219F-E1FD-4F0D-8A61-47419D7543B7}
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
<Properties StartupItem="Platform\Common\Common.csproj">
<Properties StartupItem="Exe\Profile\Profile.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Game\Model\Component\NetworkComponent.cs">
<Files>
......
此差异已折叠。
......@@ -48,6 +48,10 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Tests\TNetTest\TNetTest.csproj">
<Project>{1888D319-0495-43F3-BA8D-163EC20D9437}</Project>
<Name>TNetTest</Name>
</ProjectReference>
<ProjectReference Include="..\..\Tests\UNetTest\UNetTest.csproj">
<Project>{47A7404D-F501-43C5-8183-4B4E9E8C24B2}</Project>
<Name>UNetTest</Name>
......
using Common.Logger;
using UNetTest;
using TNetTest;
namespace Profile
{
......@@ -7,7 +7,7 @@ namespace Profile
{
private static void Main(string[] args)
{
UServiceTest test = new UServiceTest();
TServiceTest test = new TServiceTest();
Log.Debug("Profile start");
test.ClientSendToServer();
Log.Debug("Profile stop");
......
......@@ -13,6 +13,11 @@ namespace TNet
public int FirstIndex { get; set; }
public TBuffer()
{
this.bufferList.AddLast(new byte[ChunkSize]);
}
public int Count
{
get
......@@ -63,8 +68,9 @@ namespace TNet
{
if (this.Count < buffer.Length || buffer.Length == 0)
{
throw new Exception(string.Format("bufferList size < n, bufferList: {0} buffer length: {1}",
this.Count, buffer.Length));
throw new Exception(
string.Format("bufferList size < n, bufferList: {0} buffer length: {1}",
this.Count, buffer.Length));
}
int alreadyCopyCount = 0;
while (alreadyCopyCount < buffer.Length)
......@@ -79,7 +85,7 @@ namespace TNet
else
{
Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount,
ChunkSize - this.FirstIndex);
ChunkSize - this.FirstIndex);
alreadyCopyCount += ChunkSize - this.FirstIndex;
this.FirstIndex = 0;
this.bufferList.RemoveFirst();
......@@ -92,10 +98,12 @@ namespace TNet
int alreadyCopyCount = 0;
while (alreadyCopyCount < buffer.Length)
{
if (this.LastIndex == 0)
if (this.LastIndex == ChunkSize)
{
this.bufferList.AddLast(new byte[ChunkSize]);
this.LastIndex = 0;
}
int n = buffer.Length - alreadyCopyCount;
if (ChunkSize - this.LastIndex > n)
{
......@@ -108,7 +116,7 @@ namespace TNet
Array.Copy(buffer, alreadyCopyCount, this.bufferList.Last.Value, this.LastIndex,
ChunkSize - this.LastIndex);
alreadyCopyCount += ChunkSize - this.LastIndex;
this.LastIndex = 0;
this.LastIndex = ChunkSize;
}
}
}
......
......@@ -9,7 +9,7 @@ namespace TNet
{
internal class TChannel: AChannel
{
private const int SendInterval = 50;
private const int SendInterval = 0;
private readonly TService service;
private TSocket socket;
......@@ -130,8 +130,9 @@ namespace TNet
{
sendSize = this.sendBuffer.Count;
}
int n =
await this.socket.SendAsync(this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
int n = await this.socket.SendAsync(
this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
this.sendBuffer.FirstIndex += n;
if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
{
......@@ -142,7 +143,7 @@ namespace TNet
}
catch (Exception e)
{
Log.Trace(e.ToString());
Log.Debug(e.ToString());
}
this.sendTimer = ObjectId.Empty;
......@@ -154,10 +155,9 @@ namespace TNet
{
while (true)
{
int n =
await
this.socket.RecvAsync(this.recvBuffer.Last, this.recvBuffer.LastIndex,
TBuffer.ChunkSize - this.recvBuffer.LastIndex);
int n = await this.socket.RecvAsync(
this.recvBuffer.Last, this.recvBuffer.LastIndex,
TBuffer.ChunkSize - this.recvBuffer.LastIndex);
if (n == 0)
{
break;
......
......@@ -139,7 +139,7 @@ namespace TNet
{
while (!isStop)
{
this.RunOnce(1);
this.RunOnce(0);
this.timerManager.Refresh();
}
}
......
......@@ -2,6 +2,7 @@
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Common.Helper;
using Common.Logger;
namespace TNet
......@@ -33,8 +34,8 @@ namespace TNet
{
get
{
return ((IPEndPoint) this.socket.RemoteEndPoint).Address + ":" +
((IPEndPoint) this.socket.RemoteEndPoint).Port;
IPEndPoint ipEndPoint = (IPEndPoint) this.socket.RemoteEndPoint;
return ipEndPoint.Address + ":" + ipEndPoint.Port;
}
}
......@@ -171,7 +172,6 @@ namespace TNet
private static void OnRecvComplete(SocketAsyncEventArgs e)
{
Log.Debug("OnRecvComplete: " + e.BytesTransferred);
var tcs = (TaskCompletionSource<int>) e.UserToken;
e.UserToken = null;
if (e.SocketError != SocketError.Success)
......
......@@ -30,8 +30,8 @@ namespace UNet
{
UAddress address = new UAddress(hostName, port);
ENetAddress nativeAddress = address.Struct;
this.host = NativeMethods.EnetHostCreate(ref nativeAddress,
NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
this.host = NativeMethods.EnetHostCreate(
ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
if (this.host == IntPtr.Zero)
{
......@@ -43,8 +43,8 @@ namespace UNet
public UPoller()
{
this.host = NativeMethods.EnetHostCreate(IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
0, 0, 0);
this.host = NativeMethods.EnetHostCreate(
IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
if (this.host == IntPtr.Zero)
{
......
......@@ -38,7 +38,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TcpListenerTest.cs" />
<Compile Include="TServiceTest.cs" />
</ItemGroup>
<ItemGroup>
......
......@@ -11,15 +11,18 @@ namespace TNetTest
[TestFixture]
public class TServiceTest
{
private const int echoTimes = 10000;
private readonly Barrier barrier = new Barrier(3);
private async void ClientEvent(IService service, string hostName, ushort port)
{
AChannel channel = await service.GetChannel(hostName, port);
channel.SendAsync("0123456789".ToByteArray());
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
for (int i = 0; i < echoTimes; ++i)
{
channel.SendAsync("0123456789".ToByteArray());
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
}
this.barrier.RemoveParticipant();
}
......@@ -27,10 +30,13 @@ namespace TNetTest
private async void ServerEvent(IService service)
{
AChannel channel = await service.GetChannel();
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
Array.Reverse(bytes);
channel.SendAsync(bytes);
for (int i = 0; i < echoTimes; ++i)
{
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
Array.Reverse(bytes);
channel.SendAsync(bytes);
}
this.barrier.RemoveParticipant();
}
......@@ -40,21 +46,26 @@ namespace TNetTest
{
const string hostName = "127.0.0.1";
const ushort port = 8889;
IService clientService = new TService();
IService serverService = new TService(hostName, 8889);
using(IService clientService = new TService())
using (IService serverService = new TService(hostName, 8889))
{
Task task1 = Task.Factory.StartNew(() => clientService.Start(), TaskCreationOptions.LongRunning);
Task task2 = Task.Factory.StartNew(() => serverService.Start(), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => clientService.Start(), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => serverService.Start(), TaskCreationOptions.LongRunning);
// 往server host线程增加事件,accept
serverService.Add(() => this.ServerEvent(serverService));
// 往server host线程增加事件,accept
serverService.Add(() => this.ServerEvent(serverService));
Thread.Sleep(1000);
Thread.Sleep(1000);
// 往client host线程增加事件,client线程连接server
clientService.Add(() => this.ClientEvent(clientService, hostName, port));
// 往client host线程增加事件,client线程连接server
clientService.Add(() => this.ClientEvent(clientService, hostName, port));
this.barrier.SignalAndWait();
this.barrier.SignalAndWait();
serverService.Add(serverService.Stop);
clientService.Add(clientService.Stop);
Task.WaitAll(task1, task2);
}
}
}
}
\ No newline at end of file
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Common.Helper;
using Common.Logger;
using NUnit.Framework;
namespace TNetTest
{
[TestFixture]
public class TcpListenerTest
{
private const ushort port = 11111;
private int count;
private readonly Barrier barrier = new Barrier(2);
private readonly object lockObject = new object();
[Test]
public void AcceptAsync()
{
Thread thread1 = new Thread(this.Server);
thread1.Start();
Thread.Sleep(2);
for (int i = 0; i < 1; ++i)
{
Thread thread = new Thread(this.Client);
thread.Start();
}
this.barrier.SignalAndWait();
}
private async void Client()
{
using (TcpClient 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 < 100; ++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());
}
}
}
this.barrier.RemoveParticipants(1);
}
private async void Server()
{
TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
tcpListener.Start();
while (this.count != 1)
{
Socket socket = await tcpListener.AcceptSocketAsync();
NetworkStream ns = new NetworkStream(socket);
this.Response(ns);
}
}
private async void Response(NetworkStream ns)
{
try
{
var bytes = new byte[1000];
for (int i = 0; i < 100; ++i)
{
int n = await ns.ReadAsync(bytes, 0, 100);
await ns.WriteAsync(bytes, 0, n);
}
lock (this.lockObject)
{
++this.count;
}
}
catch (Exception e)
{
Log.Debug(e.ToString());
}
}
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
......
......@@ -12,25 +12,32 @@ namespace UNetTest
[TestFixture]
public class UServiceTest
{
private const int echoTimes = 10000;
private readonly Barrier barrier = new Barrier(2);
private async void ClientEvent(IService clientService, string hostName, ushort port)
{
AChannel channel = await clientService.GetChannel(hostName, port);
channel.SendAsync("0123456789".ToByteArray());
for (int i = 0; i < echoTimes; ++i)
{
channel.SendAsync("0123456789".ToByteArray());
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes);
}
barrier.RemoveParticipant();
}
private async void ServerEvent(IService service)
{
AChannel channel = await service.GetChannel();
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
Array.Reverse(bytes);
channel.SendAsync(bytes);
for (int i = 0; i < echoTimes; ++i)
{
byte[] bytes = await channel.RecvAsync();
CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
Array.Reverse(bytes);
channel.SendAsync(bytes);
}
}
[Test]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册