diff options
-rw-r--r-- | MultiplayerToolkit/Assets/MultiplayerToolkit/Message/IRecyclable.cs.meta (renamed from MultiplayerToolkit/Assets/MultiplayerToolkit/IRecyclable.cs.meta) | 2 | ||||
-rw-r--r-- | MultiplayerToolkit/Assets/MultiplayerToolkit/Message/ObjectPool.cs.meta (renamed from MultiplayerToolkit/Assets/MultiplayerToolkit/ObjectPool.cs.meta) | 2 | ||||
-rw-r--r-- | MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs | 5 | ||||
-rw-r--r-- | Projects/Message/Message/Message.csproj | 8 | ||||
-rw-r--r-- | Projects/Message/Message/MessageWriterTests.cs | 1 | ||||
-rw-r--r-- | Projects/Message/Message/MultiplayerToolkit/MessageWriter.cs | 4 | ||||
-rw-r--r-- | Projects/Packet/Packet/.gitignore | 194 | ||||
-rw-r--r-- | Projects/Packet/Packet/MultiplayerToolkit/Packet.cs | 389 | ||||
-rw-r--r-- | Projects/Packet/Packet/Packet.csproj | 69 | ||||
-rw-r--r-- | Projects/Packet/Packet/Packet.sln | 25 | ||||
-rw-r--r-- | Projects/Packet/Packet/Properties/AssemblyInfo.cs | 20 | ||||
-rw-r--r-- | Projects/Packet/Packet/UnitTest1.cs | 72 | ||||
-rw-r--r-- | Projects/Packet/Packet/packages.config | 5 |
13 files changed, 782 insertions, 14 deletions
diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/IRecyclable.cs.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Message/IRecyclable.cs.meta index 396ffcd..964d78b 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/IRecyclable.cs.meta +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Message/IRecyclable.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: b07026b6854d9e749ad6375ae957f557 +guid: c57c177c225580e468e5a7d0b97962bb MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/ObjectPool.cs.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Message/ObjectPool.cs.meta index 82ce63a..125a20c 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/ObjectPool.cs.meta +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Message/ObjectPool.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 752d8b9e476be7941a2b3d333d4e0f4d +guid: 575b08188d2f9004aaf2d7e1fe1f4ca5 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs index 7a73b98..1b64e7d 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs @@ -61,11 +61,6 @@ public class Test_TcpServer : MonoBehaviour } } - async Task<int> Foo() - { - return 1; - } - void OnAcceptTcpClient(IAsyncResult ar) { Debug.Log("OnAcceptTcpClient"); diff --git a/Projects/Message/Message/Message.csproj b/Projects/Message/Message/Message.csproj index 56698ef..6187aa2 100644 --- a/Projects/Message/Message/Message.csproj +++ b/Projects/Message/Message/Message.csproj @@ -51,12 +51,12 @@ <Reference Include="System.Core" /> </ItemGroup> <ItemGroup> - <Compile Include="IRecyclable.cs" /> - <Compile Include="MessageReader.cs" /> + <Compile Include="MultiplayerToolkit\IRecyclable.cs" /> + <Compile Include="MultiplayerToolkit\MessageReader.cs" /> <Compile Include="MessageReaderTests.cs" /> - <Compile Include="MessageWriter.cs" /> + <Compile Include="MultiplayerToolkit\MessageWriter.cs" /> <Compile Include="MessageWriterTests.cs" /> - <Compile Include="ObjectPool.cs" /> + <Compile Include="MultiplayerToolkit\ObjectPool.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\Settings.Designer.cs"> <AutoGen>True</AutoGen> diff --git a/Projects/Message/Message/MessageWriterTests.cs b/Projects/Message/Message/MessageWriterTests.cs index b6efba0..4067492 100644 --- a/Projects/Message/Message/MessageWriterTests.cs +++ b/Projects/Message/Message/MessageWriterTests.cs @@ -90,7 +90,6 @@ namespace MultiplayerToolkit Assert.IsFalse(msg.HasBytes(1)); } - [TestMethod] public void WriteProperInt() { diff --git a/Projects/Message/Message/MultiplayerToolkit/MessageWriter.cs b/Projects/Message/Message/MultiplayerToolkit/MessageWriter.cs index 0b37c5d..98d2bfc 100644 --- a/Projects/Message/Message/MultiplayerToolkit/MessageWriter.cs +++ b/Projects/Message/Message/MultiplayerToolkit/MessageWriter.cs @@ -20,8 +20,8 @@ namespace MultiplayerToolkit /// </summary> public class MessageWriter : IRecyclable { - public const int BYTE_COUNT_OF_LENGTH = 2; // 长度占2 bytes - public static int BUFFER_SIZE = 64000; // 单个消息最长支持64000字节,即62.5KB + public const int BYTE_COUNT_OF_LENGTH = 2; + public const int BUFFER_SIZE = 64000; // 单个消息最长支持64000字节,即62.5KB public static readonly ObjectPool<MessageWriter> WriterPool = new ObjectPool<MessageWriter>(() => new MessageWriter(BUFFER_SIZE)); public byte[] Buffer; diff --git a/Projects/Packet/Packet/.gitignore b/Projects/Packet/Packet/.gitignore new file mode 100644 index 0000000..9bf6cfb --- /dev/null +++ b/Projects/Packet/Packet/.gitignore @@ -0,0 +1,194 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates +.vs/ + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +x64/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +#Sandcastle generated documentation +[Hh]elp/ + +# Roslyn cache directories +*.ide/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +#NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding addin-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +## TODO: Comment the next line if you want to checkin your +## web deploy settings but do note that will include unencrypted +## passwords +#*.pubxml + +# NuGet Packages Directory +packages/* +## TODO: If the tool you use requires repositories.config +## uncomment the next line +#!packages/repositories.config + +# Enable "build/" folder in the NuGet Packages folder since +# NuGet packages use it for MSBuild targets. +# This line needs to be after the ignore of the build folder +# (and the packages folder if the line above has been uncommented) +!packages/build/ + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# LightSwitch generated files +GeneratedArtifacts/ +_Pvt_Extensions/ +ModelManifest.xml +*.ide diff --git a/Projects/Packet/Packet/MultiplayerToolkit/Packet.cs b/Projects/Packet/Packet/MultiplayerToolkit/Packet.cs new file mode 100644 index 0000000..bce39b0 --- /dev/null +++ b/Projects/Packet/Packet/MultiplayerToolkit/Packet.cs @@ -0,0 +1,389 @@ +锘縰sing Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Text; +#if UNITY_5_3_OR_NEWER +using UnityEngine; +#endif + +namespace MultiplayerToolkit +{ + public enum EPacketMode + { + Write = 0, + Read = 1, + } + + /// <summary> + /// 璇诲啓閫氱敤鐨勭畝鏄損acket + /// 缃戠粶鍖咃紝閫氱敤缁撴瀯锛屾棤璁烘槸TCP\UDP杩樻槸Steamn + /// 缁撴瀯锛 + /// -------------------------------------------------- + /// 2bytes (ushort)鍖呴暱搴︼紝涓嶅惈杩2瀛楄妭锛 + /// (-------------------------------------------------- + /// 1byte 娑堟伅绫诲瀷锛屽唴缃秷鎭佺帺瀹惰嚜瀹氫箟娑堟伅 + /// (2byte) (濡傛灉鏄帺瀹惰嚜瀹氫箟娑堟伅锛屾彁渚涗竴涓猰od搴忓彿锛孲team.UGC.Item鐨刬d鏄痷long 8bytes鏈夌偣澶э紝鎹㈡垚搴忓彿锛屾渶澶氭敮鎸65535涓猰od) + /// 2byte (ushort)娑堟伅ID 0-65535 + /// 鍐呭 + /// --------------------------------------------------) + /// </summary> + public class Packet : IDisposable + { + // 鍐欏叆缂撳啿鍖 + private List<byte> writableBuffer; + + // 璇诲彇缂撳啿鍖猴紝鏈夋晥鏁版嵁涓瀹氭槸浠庡ご寮濮 + private byte[] readableBuffer; + private int readPos; + + private EPacketMode m_Mode; + public EPacketMode mode { get { return m_Mode; } } + + private bool disposed; + + #region 鍒涘缓 + + public static Packet GetWritablePacket() + { + Packet packet = new Packet(); + packet.m_Mode = EPacketMode.Write; + packet.writableBuffer = new List<byte>(); + packet.readableBuffer = null; + packet.readPos = 0; + return packet; + } + + public static Packet GetWritablePacket(Packet readable) + { + if (readable.mode != EPacketMode.Read) + return null; + Packet packet = GetWritablePacket(); + packet.Write(readable.readableBuffer); + return packet; + } + + public static Packet GetReadablePacket(byte[] srcToCopy, int offset = 0, int length = -1) + { + Packet packet = new Packet(); + packet.m_Mode = EPacketMode.Read; + packet.writableBuffer = null; + int len = length; + if (length == -1) + { + len = srcToCopy.Length - offset; + } + packet.readableBuffer = new byte[len]; + Array.Copy(srcToCopy, offset, packet.readableBuffer, 0, len); + packet.readPos = 0; + return packet; + } + + public static Packet GetReadablePacket(Packet writable) + { + if (writable.mode != EPacketMode.Write) + return null; + Packet packet = GetReadablePacket(writable.ToArray()); + return packet; + } + + #endregion + + private Packet() + { + this.disposed = false; + } + + public void Reset(bool bReset = true) + { + if (!bReset) + { + return; + } + if (mode == EPacketMode.Write) + { + this.writableBuffer.Clear(); + } + else if (mode == EPacketMode.Read) + { + Array.Clear(this.readableBuffer, 0, this.readableBuffer.Length); + readPos = 0; + } + } + + private static T[] SubArray<T>(T[] data, int index, int length) + { + T[] result = new T[length]; + Array.Copy(data, index, result, 0, length); + return result; + } + + #region 鍐欏叆 + /// <summary> + /// 寮澶村啓鍏ヤ竴涓猧nt涓哄綋鍓峸ritableBuffer闀垮害锛屼笉鍚繖涓暱搴 + /// </summary> + public void InsertLength() + { + this.writableBuffer.InsertRange(0, BitConverter.GetBytes(this.writableBuffer.Count)); + } + + /// <summary> + /// 寮澶村啓鍏ヤ竴涓猧nt + /// </summary> + /// <param name="_value"></param> + public void InsertInt(int _value) + { + this.writableBuffer.InsertRange(0, BitConverter.GetBytes(_value)); + } + + /// <summary> + /// 杩斿洖鍒涘缓鐨勬暟缁勶紝涓嶈棰戠箒璋冪敤 + /// </summary> + /// <returns></returns> + public byte[] ToArray() + { + if (this.mode != EPacketMode.Write) + return null; + return this.writableBuffer.ToArray(); + } + + /// <summary> + /// 鍐欏叆浜嗗灏戝唴瀹 + /// </summary> + /// <returns></returns> + public int GetWritableBufferLength() + { + return this.writableBuffer.Count; + } + + public void Write(byte _value) + { + this.writableBuffer.Add(_value); + } + + public void Write(byte[] _value) + { + this.writableBuffer.AddRange(_value); + } + + public void Write(short _value) + { + this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); + } + + public void Write(int _value) + { + this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); + } + + public void Write(long _value) + { + this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); + } + + public void Write(float _value) + { + this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); + } + + public void Write(bool _value) + { + this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); + } + + public void Write(string _value) + { + this.Write((int)_value.Length); + this.writableBuffer.AddRange(Encoding.ASCII.GetBytes(_value)); + } + +#if UNITY_5_3_OR_NEWER + + public void Write(Vector3 _value) + { + this.Write(_value.x); + this.Write(_value.y); + this.Write(_value.z); + } + + public void Write(Quaternion _value) + { + this.Write(_value.x); + this.Write(_value.y); + this.Write(_value.z); + this.Write(_value.w); + } +#endif + + #endregion + + #region 璇诲彇 + + public void RewindReadPos(int step, bool bRewind = true) + { + if (bRewind) + { + this.readPos -= step; + } + } + + public int GetUnreadLength() + { + return this.readableBuffer.Length - this.readPos; + } + + public byte ReadByte(bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + byte value = this.readableBuffer[this.readPos]; + if (_moveReadPos) + { + this.readPos++; + } + return value; + } + throw new Exception("Could not read value of type 'byte'!"); + } + + public byte[] ReadBytes(int _length, bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + byte[] value = SubArray(this.readableBuffer, this.readPos, _length); + if (_moveReadPos) + { + this.readPos += _length; + } + return value; + } + throw new Exception("Could not read value of type 'byte[]'!"); + } + + public short ReadShort(bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + short value = BitConverter.ToInt16(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 2; + } + return value; + } + throw new Exception("Could not read value of type 'short'!"); + } + + public int ReadInt(bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + int value = BitConverter.ToInt32(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 4; + } + return value; + } + throw new Exception("Could not read value of type 'int'!"); + } + + public long ReadLong(bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + long value = BitConverter.ToInt64(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 8; + } + return value; + } + throw new Exception("Could not read value of type 'long'!"); + } + + public float ReadFloat(bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + float value = BitConverter.ToSingle(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 4; + } + return value; + } + throw new Exception("Could not read value of type 'float'!"); + } + + public bool ReadBool(bool _moveReadPos = true) + { + if (this.readableBuffer.Length > this.readPos) + { + bool value = BitConverter.ToBoolean(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos++; + } + return value; + } + throw new Exception("Could not read value of type 'bool'!"); + } + + public string ReadString(bool _moveReadPos = true) + { + string result; + try + { + int num = this.ReadInt(true); + string @string = Encoding.ASCII.GetString(this.readableBuffer, this.readPos, num); + if (_moveReadPos && @string.Length > 0) + { + this.readPos += num; + } + result = @string; + } + catch (Exception e) + { + throw new Exception("Could not read value of type 'string'!"); + } + return result; + } + +#if UNITY_5_3_OR_NEWER + + public Vector3 ReadVector3(bool moveReadPos = true) + { + return new Vector3(this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos)); + } + + public Quaternion ReadQuaternion(bool moveReadPos = true) + { + return new Quaternion(this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos)); + } +#endif + + #endregion + + protected virtual void Dispose(bool _disposing) + { + if (!this.disposed) + { + if (_disposing) + { + this.writableBuffer = null; + this.readableBuffer = null; + this.readPos = 0; + } + this.disposed = true; + } + } + + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + } +} diff --git a/Projects/Packet/Packet/Packet.csproj b/Projects/Packet/Packet/Packet.csproj new file mode 100644 index 0000000..fc2068c --- /dev/null +++ b/Projects/Packet/Packet/Packet.csproj @@ -0,0 +1,69 @@ +锘<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.props" Condition="Exists('packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.props')" /> + <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>{602EEAD0-42FE-4473-9B41-70C14D3AD156}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Packet</RootNamespace> + <AssemblyName>Packet</AssemblyName> + <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.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> + <NuGetPackageImportStamp> + </NuGetPackageImportStamp> + </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="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <HintPath>packages\MSTest.TestFramework.2.2.7\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath> + </Reference> + <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <HintPath>packages\MSTest.TestFramework.2.2.7\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + </ItemGroup> + <ItemGroup> + <Compile Include="MultiplayerToolkit\Packet.cs" /> + <Compile Include="UnitTest1.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>杩欏彴璁$畻鏈轰笂缂哄皯姝ら」鐩紩鐢ㄧ殑 NuGet 绋嬪簭鍖呫備娇鐢ㄢ淣uGet 绋嬪簭鍖呰繕鍘熲濆彲涓嬭浇杩欎簺绋嬪簭鍖呫傛湁鍏虫洿澶氫俊鎭紝璇峰弬瑙 http://go.microsoft.com/fwlink/?LinkID=322105銆傜己灏戠殑鏂囦欢鏄 {0}銆</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.props'))" /> + <Error Condition="!Exists('packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.targets'))" /> + </Target> + <Import Project="packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.targets" Condition="Exists('packages\MSTest.TestAdapter.2.2.7\build\net45\MSTest.TestAdapter.targets')" /> +</Project>
\ No newline at end of file diff --git a/Projects/Packet/Packet/Packet.sln b/Projects/Packet/Packet/Packet.sln new file mode 100644 index 0000000..0849e6b --- /dev/null +++ b/Projects/Packet/Packet/Packet.sln @@ -0,0 +1,25 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Packet", "Packet.csproj", "{602EEAD0-42FE-4473-9B41-70C14D3AD156}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {602EEAD0-42FE-4473-9B41-70C14D3AD156}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {602EEAD0-42FE-4473-9B41-70C14D3AD156}.Debug|Any CPU.Build.0 = Debug|Any CPU + {602EEAD0-42FE-4473-9B41-70C14D3AD156}.Release|Any CPU.ActiveCfg = Release|Any CPU + {602EEAD0-42FE-4473-9B41-70C14D3AD156}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0AA0B9B6-7243-4120-9F50-2D4B6D59E1C9} + EndGlobalSection +EndGlobal diff --git a/Projects/Packet/Packet/Properties/AssemblyInfo.cs b/Projects/Packet/Packet/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..808412c --- /dev/null +++ b/Projects/Packet/Packet/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Packet")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("P R C")] +[assembly: AssemblyProduct("Packet")] +[assembly: AssemblyCopyright("Copyright 漏 P R C 2023")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("602eead0-42fe-4473-9b41-70c14d3ad156")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Projects/Packet/Packet/UnitTest1.cs b/Projects/Packet/Packet/UnitTest1.cs new file mode 100644 index 0000000..c014389 --- /dev/null +++ b/Projects/Packet/Packet/UnitTest1.cs @@ -0,0 +1,72 @@ +锘縰sing Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace MultiplayerToolkit +{ + [TestClass] + public class MultiplayerToolkitTest + { + [TestMethod] + public void TestReadAndWrite() + { + Packet packet = Packet.GetWritablePacket(); + + Assert.AreEqual(packet.mode, EPacketMode.Write); + packet.Write(1); + packet.Write(false); + packet.Write("hello, world"); + packet.Write((byte)13); + packet.Write(12312.3333f); + packet.Write((short)1245); + packet.Write(long.MaxValue); + packet.Write(long.MinValue); + packet.InsertLength(); + + Packet read = Packet.GetReadablePacket(packet); + + Assert.AreEqual(read.mode, EPacketMode.Read); + read.ReadInt(); // length + Assert.AreEqual(1, read.ReadInt()); + Assert.AreEqual(false, read.ReadBool()); + Assert.AreEqual("hello, world", read.ReadString()); + Assert.AreEqual((byte)13, read.ReadByte()); + Assert.AreEqual(12312.3333f, read.ReadFloat()); + Assert.AreEqual((short)1245, read.ReadShort()); + Assert.AreEqual((long)long.MaxValue, read.ReadLong()); + Assert.AreEqual((long)long.MinValue, read.ReadLong()); + } + + [TestMethod] + public void TestReadAndWrite2() + { + Packet packet = Packet.GetWritablePacket(); + + Assert.AreEqual(packet.mode, EPacketMode.Write); + packet.Write(1); + packet.Write(false); + packet.Write("hello, world"); + packet.Write((byte)13); + packet.Write(12312.3333f); + packet.Write((short)1245); + packet.Write(long.MaxValue); + packet.Write(long.MinValue); + packet.InsertLength(); + + Packet read = Packet.GetReadablePacket(packet.ToArray()); + + Assert.AreEqual(read.mode, EPacketMode.Read); + read.ReadInt(); // length + Assert.AreEqual(1, read.ReadInt()); + read.RewindReadPos(4); + read.ReadInt(); + Assert.AreEqual(false, read.ReadBool()); + Assert.AreEqual("hello, world", read.ReadString()); + Assert.AreEqual((byte)13, read.ReadByte()); + Assert.AreEqual(12312.3333f, read.ReadFloat()); + Assert.AreEqual((short)1245, read.ReadShort()); + Assert.AreEqual((long)long.MaxValue, read.ReadLong()); + Assert.AreEqual((long)long.MinValue, read.ReadLong()); + } + + } +} diff --git a/Projects/Packet/Packet/packages.config b/Projects/Packet/Packet/packages.config new file mode 100644 index 0000000..54eb31a --- /dev/null +++ b/Projects/Packet/Packet/packages.config @@ -0,0 +1,5 @@ +锘<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="MSTest.TestAdapter" version="2.2.7" targetFramework="net472" /> + <package id="MSTest.TestFramework" version="2.2.7" targetFramework="net472" /> +</packages>
\ No newline at end of file |