diff options
Diffstat (limited to 'MultiplayerToolkit/Assets')
11 files changed, 887 insertions, 216 deletions
diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet.cs index 5048a6a..5638189 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet.cs @@ -3,295 +3,323 @@ using System.Collections.Generic; using System.Text; using UnityEngine; -public class Packet : IDisposable +namespace MultiplayerToolkit { - private List<byte> buffer; - private byte[] readableBuffer; - private int readPos; - private bool disposed; - public Packet() + public class Packet : IDisposable { - this.buffer = new List<byte>(); - this.readPos = 0; - } + private List<byte> buffer; + private byte[] readableBuffer; + private int readPos; + private bool disposed; - /// <summary> - /// 开头写入一个int作为ID - /// </summary> - /// <param name="_id"></param> - public Packet(int _id) - { - this.buffer = new List<byte>(); - this.readPos = 0; - this.Write(_id); - } + public Packet() + { + this.buffer = new List<byte>(); + this.readPos = 0; + } - public Packet(byte[] _data) - { - this.buffer = new List<byte>(); - this.readPos = 0; - this.SetBytes(_data); - } + /// <summary> + /// 开头写入一个int作为ID + /// </summary> + /// <param name="_id"></param> + public Packet(int _id) + { + this.buffer = new List<byte>(); + this.readPos = 0; + this.Write(_id); + } - public void SetBytes(byte[] _data) - { - this.Write(_data); - this.readableBuffer = this.buffer.ToArray(); - } + public Packet(byte[] _data) + { + this.buffer = new List<byte>(); + this.readPos = 0; + this.SetBytes(_data); + } - /// <summary> - /// 开头写入一个int为当前buffer长度 - /// </summary> - public void InsertLength() - { - this.buffer.InsertRange(0, BitConverter.GetBytes(this.buffer.Count)); - } + /// <summary> + /// 清空内容并重置 + /// </summary> + /// <param name="_data"></param> + public void SetBytes(byte[] _data) + { + if (buffer != null) + buffer.Clear(); + readPos = 0; + disposed = false; + this.Write(_data); + this.readableBuffer = this.buffer.ToArray(); + } - /// <summary> - /// 开头写入一个int - /// </summary> - /// <param name="_value"></param> - public void InsertInt(int _value) - { - this.buffer.InsertRange(0, BitConverter.GetBytes(_value)); - } + /// <summary> + /// 从offset开始的length个字节 + /// </summary> + /// <param name="_data"></param> + /// <param name="offset"></param> + /// <param name="length"></param> + public void SetBytes(byte[] _data, int offset, int length) + { + if (buffer != null) + buffer.Clear(); + readPos = 0; + disposed = false; + this.Write(Utils.SubArray(_data, offset, length)); + this.readableBuffer = this.buffer.ToArray(); + } - public byte[] ToArray() - { - this.readableBuffer = this.buffer.ToArray(); - return this.readableBuffer; - } + /// <summary> + /// 开头写入一个int为当前buffer长度 + /// </summary> + public void InsertLength() + { + this.buffer.InsertRange(0, BitConverter.GetBytes(this.buffer.Count)); + } - public int Length() - { - return this.buffer.Count; - } + /// <summary> + /// 开头写入一个int + /// </summary> + /// <param name="_value"></param> + public void InsertInt(int _value) + { + this.buffer.InsertRange(0, BitConverter.GetBytes(_value)); + } - public int UnreadLength() - { - return this.Length() - this.readPos; - } + public byte[] ToArray() + { + this.readableBuffer = this.buffer.ToArray(); + return this.readableBuffer; + } - public void Reset(bool _shouldReset = true) - { - if (_shouldReset) + public int Length() { - this.buffer.Clear(); - this.readableBuffer = null; - this.readPos = 0; - return; + return this.buffer.Count; } - this.readPos -= 4; - } - public void Write(byte _value) - { - this.buffer.Add(_value); - } + public int UnreadLength() + { + return this.Length() - this.readPos; + } - public void Write(byte[] _value) - { - this.buffer.AddRange(_value); - } + public void Reset(bool _shouldReset = true) + { + if (_shouldReset) + { + this.buffer.Clear(); + this.readableBuffer = null; + this.readPos = 0; + return; + } + this.readPos -= 4; + } - public void Write(short _value) - { - this.buffer.AddRange(BitConverter.GetBytes(_value)); - } + public void Write(byte _value) + { + this.buffer.Add(_value); + } - public void Write(int _value) - { - this.buffer.AddRange(BitConverter.GetBytes(_value)); - } + public void Write(byte[] _value) + { + this.buffer.AddRange(_value); + } - public void Write(long _value) - { - this.buffer.AddRange(BitConverter.GetBytes(_value)); - } + public void Write(short _value) + { + this.buffer.AddRange(BitConverter.GetBytes(_value)); + } - public void Write(float _value) - { - this.buffer.AddRange(BitConverter.GetBytes(_value)); - } + public void Write(int _value) + { + this.buffer.AddRange(BitConverter.GetBytes(_value)); + } - public void Write(bool _value) - { - this.buffer.AddRange(BitConverter.GetBytes(_value)); - } + public void Write(long _value) + { + this.buffer.AddRange(BitConverter.GetBytes(_value)); + } - public void Write(string _value) - { - this.Write(_value.Length); - this.buffer.AddRange(Encoding.ASCII.GetBytes(_value)); - } + public void Write(float _value) + { + this.buffer.AddRange(BitConverter.GetBytes(_value)); + } - public void Write(Vector3 _value) - { - this.Write(_value.x); - this.Write(_value.y); - this.Write(_value.z); - } + public void Write(bool _value) + { + this.buffer.AddRange(BitConverter.GetBytes(_value)); + } - public void Write(Quaternion _value) - { - this.Write(_value.x); - this.Write(_value.y); - this.Write(_value.z); - this.Write(_value.w); - } + public void Write(string _value) + { + this.Write(_value.Length); + this.buffer.AddRange(Encoding.ASCII.GetBytes(_value)); + } - public byte ReadByte(bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + 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); + } + + public byte ReadByte(bool _moveReadPos = true) { - byte arg_31_0 = this.readableBuffer[this.readPos]; - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos++; + byte arg_31_0 = this.readableBuffer[this.readPos]; + if (_moveReadPos) + { + this.readPos++; + } + return arg_31_0; } - return arg_31_0; + throw new Exception("Could not read value of type 'byte'!"); } - throw new Exception("Could not read value of type 'byte'!"); - } - public byte[] ReadBytes(int _length, bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + public byte[] ReadBytes(int _length, bool _moveReadPos = true) { - byte[] arg_3B_0 = this.buffer.GetRange(this.readPos, _length).ToArray(); - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos += _length; + byte[] arg_3B_0 = this.buffer.GetRange(this.readPos, _length).ToArray(); + if (_moveReadPos) + { + this.readPos += _length; + } + return arg_3B_0; } - return arg_3B_0; + throw new Exception("Could not read value of type 'byte[]'!"); } - throw new Exception("Could not read value of type 'byte[]'!"); - } - public byte[] CloneBytes() - { - return this.buffer.ToArray(); - } + public byte[] CloneBytes() + { + return this.buffer.ToArray(); + } - public short ReadShort(bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + public short ReadShort(bool _moveReadPos = true) { - short arg_35_0 = BitConverter.ToInt16(this.readableBuffer, this.readPos); - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos += 2; + short arg_35_0 = BitConverter.ToInt16(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 2; + } + return arg_35_0; } - return arg_35_0; + throw new Exception("Could not read value of type 'short'!"); } - throw new Exception("Could not read value of type 'short'!"); - } - public int ReadInt(bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + public int ReadInt(bool _moveReadPos = true) { - int arg_35_0 = BitConverter.ToInt32(this.readableBuffer, this.readPos); - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos += 4; + int arg_35_0 = BitConverter.ToInt32(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 4; + } + return arg_35_0; } - return arg_35_0; + throw new Exception("Could not read value of type 'int'!"); } - throw new Exception("Could not read value of type 'int'!"); - } - public long ReadLong(bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + public long ReadLong(bool _moveReadPos = true) { - long arg_35_0 = BitConverter.ToInt64(this.readableBuffer, this.readPos); - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos += 8; + long arg_35_0 = BitConverter.ToInt64(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 8; + } + return arg_35_0; } - return arg_35_0; + throw new Exception("Could not read value of type 'long'!"); } - throw new Exception("Could not read value of type 'long'!"); - } - public float ReadFloat(bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + public float ReadFloat(bool _moveReadPos = true) { - float arg_35_0 = BitConverter.ToSingle(this.readableBuffer, this.readPos); - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos += 4; + float arg_35_0 = BitConverter.ToSingle(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos += 4; + } + return arg_35_0; } - return arg_35_0; + throw new Exception("Could not read value of type 'float'!"); } - throw new Exception("Could not read value of type 'float'!"); - } - public bool ReadBool(bool _moveReadPos = true) - { - if (this.buffer.Count > this.readPos) + public bool ReadBool(bool _moveReadPos = true) { - bool arg_35_0 = BitConverter.ToBoolean(this.readableBuffer, this.readPos); - if (_moveReadPos) + if (this.buffer.Count > this.readPos) { - this.readPos++; + bool arg_35_0 = BitConverter.ToBoolean(this.readableBuffer, this.readPos); + if (_moveReadPos) + { + this.readPos++; + } + return arg_35_0; } - return arg_35_0; + throw new Exception("Could not read value of type 'bool'!"); } - throw new Exception("Could not read value of type 'bool'!"); - } - public string ReadString(bool _moveReadPos = true) - { - string result; - try + public string ReadString(bool _moveReadPos = true) { - int num = this.ReadInt(true); - string @string = Encoding.ASCII.GetString(this.readableBuffer, this.readPos, num); - if (_moveReadPos && @string.Length > 0) + string result; + try { - this.readPos += num; + 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; } - result = @string; + catch (Exception e) + { + throw new Exception("Could not read value of type 'string'!"); + } + return result; } - catch (Exception e) + + public Vector3 ReadVector3(bool moveReadPos = true) { - throw new Exception("Could not read value of type 'string'!"); + return new Vector3(this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos)); } - return result; - } - 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)); - } + public Quaternion ReadQuaternion(bool moveReadPos = true) + { + return new Quaternion(this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos), this.ReadFloat(moveReadPos)); + } - protected virtual void Dispose(bool _disposing) - { - if (!this.disposed) + protected virtual void Dispose(bool _disposing) { - if (_disposing) + if (!this.disposed) { - this.buffer = null; - this.readableBuffer = null; - this.readPos = 0; + if (_disposing) + { + this.buffer = null; + this.readableBuffer = null; + this.readPos = 0; + } + this.disposed = true; } - this.disposed = true; } - } - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - } + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + } } diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test.meta new file mode 100644 index 0000000..14184a4 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 97fbdce10e011b84e926258a780d7b30 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp.meta new file mode 100644 index 0000000..1dfc270 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 00d4baaf9e4843b479e42072c0ce63e6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/TestTcp.unity b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/TestTcp.unity new file mode 100644 index 0000000..756fc8f --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/TestTcp.unity @@ -0,0 +1,412 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &135734419 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 135734420} + - component: {fileID: 135734421} + m_Layer: 0 + m_Name: Client + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 0 +--- !u!4 &135734420 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 135734419} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 3 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &135734421 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 135734419} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a595fca5a029c0b44948a46e2348708d, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!1 &512916081 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 512916082} + - component: {fileID: 512916083} + m_Layer: 0 + m_Name: Server + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &512916082 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 512916081} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &512916083 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 512916081} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: af88a1b42fd8b7b4e97ec2f54f07ad84, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!1 &1259444343 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1259444346} + - component: {fileID: 1259444345} + - component: {fileID: 1259444344} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &1259444344 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1259444343} + m_Enabled: 1 +--- !u!20 &1259444345 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1259444343} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &1259444346 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1259444343} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1345359045 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1345359047} + - component: {fileID: 1345359046} + - component: {fileID: 1345359048} + m_Layer: 0 + m_Name: Directional Light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &1345359046 +Light: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1345359045} + m_Enabled: 1 + serializedVersion: 10 + m_Type: 1 + m_Shape: 0 + m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_InnerSpotAngle: 21.80208 + m_CookieSize: 10 + m_Shadows: + m_Type: 2 + m_Resolution: -1 + m_CustomResolution: -1 + m_Strength: 1 + m_Bias: 0.05 + m_NormalBias: 0.4 + m_NearPlane: 0.2 + m_CullingMatrixOverride: + e00: 1 + e01: 0 + e02: 0 + e03: 0 + e10: 0 + e11: 1 + e12: 0 + e13: 0 + e20: 0 + e21: 0 + e22: 1 + e23: 0 + e30: 0 + e31: 0 + e32: 0 + e33: 1 + m_UseCullingMatrixOverride: 0 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingLayerMask: 1 + m_Lightmapping: 4 + m_LightShadowCasterMode: 0 + m_AreaSize: {x: 1, y: 1} + m_BounceIntensity: 1 + m_ColorTemperature: 6570 + m_UseColorTemperature: 0 + m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} + m_UseBoundingSphereOverride: 0 + m_UseViewFrustumForShadowCasterCull: 1 + m_ShadowRadius: 0 + m_ShadowAngle: 0 +--- !u!4 &1345359047 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1345359045} + m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} + m_LocalPosition: {x: 0, y: 3, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!114 &1345359048 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1345359045} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 474bcb49853aa07438625e644c072ee6, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Version: 1 + m_UsePipelineSettings: 1 + m_AdditionalLightsShadowResolutionTier: 2 + m_LightLayerMask: 1 + m_CustomShadowLayers: 0 + m_ShadowLayerMask: 1 + m_LightCookieSize: {x: 1, y: 1} + m_LightCookieOffset: {x: 0, y: 0} diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/TestTcp.unity.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/TestTcp.unity.meta new file mode 100644 index 0000000..3b9302d --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/TestTcp.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 498231a393462264e8fdfa6ece38db73 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs new file mode 100644 index 0000000..5dfe6d5 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs @@ -0,0 +1,42 @@ +using MultiplayerToolkit; +using Steamworks; +using System.Collections; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using UnityEditor.PackageManager; +using UnityEngine; + +public class Test_TcpClient : MonoBehaviour +{ + TcpClient client; + + + void Start() + { + Invoke("ConnectServer", 2); + } + + // Update is called once per frame + void Update() + { + + } + + private void ConnectServer() + { + Debug.Log("ConnectServer"); + + IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.13.1"), 42046); + client = new TcpClient(endpoint); + client.Connect("127.0.1.1", 42046); + NetworkStream stream = client.GetStream(); + Packet packet = new Packet(); + packet.Write((int)123); + packet.Write("hello"); + stream.Write(packet.ToArray()); + Debug.Log("connected, "+ client.Connected); + + } + +} diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs.meta new file mode 100644 index 0000000..fba0847 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a595fca5a029c0b44948a46e2348708d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs new file mode 100644 index 0000000..7e3e9f0 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using UnityEngine; +using System.IO; +using MultiplayerToolkit; +using System.Threading; +using System.Threading.Tasks; +using Unity.Mathematics; + +public class Test_TcpServer : MonoBehaviour +{ + private const int PORT = 42046; + + private TcpClient client; + + private const int BUFFER_SIZE = 1024; + + private byte[] buffer = new byte[BUFFER_SIZE]; + + private TcpListener listener; + + private List<Action> executeOnMainThread = new List<Action>(); + + void Start() + { + listener = new TcpListener(IPAddress.Parse("127.0.1.1"), PORT); + listener.Start(); + //listener.BeginAcceptTcpClient(OnAcceptTcpClient, null); + + Listen(); + + Debug.Log("listen"); + } + + async void Listen() + { + while (true) + { + client = await listener.AcceptTcpClientAsync(); + ExecuteOnMainThread(() => + { + Debug.Log("client.Client.LocalEndPoint=" + client.Client.LocalEndPoint); + Debug.Log("client.Client.RemoteEndPoint=" + client.Client.RemoteEndPoint); + }); + NetworkStream stream = client.GetStream(); + int readLen = await stream.ReadAsync(buffer, 0, BUFFER_SIZE); + Packet packet = new Packet(); + packet.SetBytes(buffer, 0, readLen); + int value = packet.ReadInt(); + string str = packet.ReadString(); + ExecuteOnMainThread(() => + { + Debug.Log(value); + Debug.Log(str); + }); + } + } + + async Task<int> Foo2() + { + await Foo(); + + return 1; + } + + async Task<int> Foo() + { + return 1; + } + + void OnAcceptTcpClient(IAsyncResult ar) + { + Debug.Log("OnAcceptTcpClient"); + client = listener.EndAcceptTcpClient(ar); + Debug.Log("client.Client.LocalEndPoint=" + client.Client.LocalEndPoint); + Debug.Log("client.Client.RemoteEndPoint=" + client.Client.RemoteEndPoint); + NetworkStream stream = client.GetStream(); + int readLen = stream.Read(buffer, 0, BUFFER_SIZE); + Packet packet = new Packet(); + packet.SetBytes(buffer, 0, readLen); + int value = packet.ReadInt(); + Debug.Log(value); + + listener.BeginAcceptTcpClient(OnAcceptTcpClient, null); + } + + private void ExecuteOnMainThread(Action action) + { + lock(executeOnMainThread) + { + executeOnMainThread.Add(action); + } + } + + private void Update() + { + lock (executeOnMainThread) + { + for(int i = 0; i < executeOnMainThread.Count; ++i) + { + executeOnMainThread[i].Invoke(); + } + executeOnMainThread.Clear(); + } + } + +} diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs.meta new file mode 100644 index 0000000..47bb854 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpServer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: af88a1b42fd8b7b4e97ec2f54f07ad84 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Utils/Utils.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Utils/Utils.cs new file mode 100644 index 0000000..7f11a1d --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Utils/Utils.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MultiplayerToolkit +{ + + public static class Utils + { + + + public static T[] SubArray<T>(T[] data, int index, int length) + { + T[] result = new T[length]; + Array.Copy(data, index, result, 0, length); + return result; + } + + + } + +} diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Utils/Utils.cs.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/Utils/Utils.cs.meta new file mode 100644 index 0000000..fef2c5f --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Utils/Utils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2eb5a259c770a134fbbadd650bf84b9e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |