添加client

master
sunqinzhe 4 days ago
parent 5c4c25664d
commit 6256dcb52b
  1. 72
      Assets/Scenes/SampleScene.unity
  2. 3
      Assets/Scripts/Client.meta
  3. 50
      Assets/Scripts/Client/ClientMain.cs
  4. 3
      Assets/Scripts/Client/ClientMain.cs.meta
  5. 26
      Assets/Scripts/Client/ClientUpdateJob.cs
  6. 3
      Assets/Scripts/Client/ClientUpdateJob.cs.meta
  7. 2
      Assets/Scripts/EntityData.cs
  8. 2
      Assets/Scripts/EntityType.cs
  9. 6
      Assets/Scripts/Main.cs
  10. 21
      Assets/Scripts/Server/ServerConnectionJob.cs
  11. 13
      Assets/Scripts/Server/ServerMain.cs
  12. 30
      Assets/Scripts/Server/ServerReceiveJob.cs
  13. 9
      Assets/Scripts/Server/ServerSendJob.cs
  14. 2
      Assets/Scripts/TestEntityDataBlock.cs
  15. 19
      Assets/Scripts/TestJob.cs
  16. 11
      Assets/Scripts/TestJob.cs.meta
  17. 23
      LocalPackages/com.nimin.network/Runtime/NetworkDriverAsset.cs
  18. 2
      LocalPackages/com.nimin.network/package.json
  19. 37
      Packages/packages-lock.json

@ -38,7 +38,6 @@ RenderSettings:
m_ReflectionIntensity: 1 m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0} m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994} m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.44657815, g: 0.49641192, b: 0.57481617, a: 1}
m_UseRadianceAmbientProbe: 0 m_UseRadianceAmbientProbe: 0
--- !u!157 &3 --- !u!157 &3
LightmapSettings: LightmapSettings:
@ -104,7 +103,7 @@ NavMeshSettings:
serializedVersion: 2 serializedVersion: 2
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_BuildSettings: m_BuildSettings:
serializedVersion: 2 serializedVersion: 3
agentTypeID: 0 agentTypeID: 0
agentRadius: 0.5 agentRadius: 0.5
agentHeight: 2 agentHeight: 2
@ -117,7 +116,7 @@ NavMeshSettings:
cellSize: 0.16666667 cellSize: 0.16666667
manualTileSize: 0 manualTileSize: 0
tileSize: 256 tileSize: 256
accuratePlacement: 0 buildHeightMesh: 0
maxJobWorkers: 0 maxJobWorkers: 0
preserveTilesOutsideBounds: 0 preserveTilesOutsideBounds: 0
debug: debug:
@ -209,13 +208,13 @@ Transform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 705507993} m_GameObject: {fileID: 705507993}
serializedVersion: 2
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0} m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &963194225 --- !u!1 &963194225
GameObject: GameObject:
@ -258,9 +257,17 @@ Camera:
m_projectionMatrixMode: 1 m_projectionMatrixMode: 1
m_GateFitMode: 2 m_GateFitMode: 2
m_FOVAxisMode: 0 m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24} m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0} m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect: m_NormalizedViewPortRect:
serializedVersion: 2 serializedVersion: 2
x: 0 x: 0
@ -294,13 +301,13 @@ Transform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225} m_GameObject: {fileID: 963194225}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10} m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &963194229 --- !u!114 &963194229
MonoBehaviour: MonoBehaviour:
@ -314,3 +321,56 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 694d8256118344eb2b130fceb1a11b7f, type: 3} m_Script: {fileID: 11500000, guid: 694d8256118344eb2b130fceb1a11b7f, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
--- !u!1 &1936170114
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1936170116}
- component: {fileID: 1936170115}
m_Layer: 0
m_Name: ServerMain
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1936170115
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1936170114}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1da32d64fdd4d41778db4fd91607ac27, type: 3}
m_Name:
m_EditorClassIdentifier:
address: 127.0.0.1
driverAsset: {fileID: 11400000, guid: a8a5562854ecf4cc1a49700e8bd93c19, type: 2}
--- !u!4 &1936170116
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1936170114}
serializedVersion: 2
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_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 963194228}
- {fileID: 705507995}
- {fileID: 1936170116}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 48c265b50587429a9a7555df18d64661
timeCreated: 1765786522

@ -0,0 +1,50 @@
using System;
using GameCore.Network;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
namespace ECSTest
{
public class ClientMain : MonoBehaviour
{
public string address = "127.0.0.1";
public NetworkDriverAsset driverAsset;
private NetworkDriver driver;
private NetworkPipeline unreliablePipeline;
private NetworkPipeline reliablePipeline;
private NativeReference<NetworkConnection> connection;
private JobHandle jobHandle;
private void OnEnable()
{
driver = driverAsset.CreateClientDriver(address, out var c, out unreliablePipeline, out reliablePipeline);
connection = new NativeReference<NetworkConnection>(c, Allocator.Persistent);
}
private void OnDisable()
{
jobHandle.Complete();
if (connection.IsCreated)
{
driver.Disconnect(connection.Value);
driver.ScheduleUpdate().Complete();
}
connection.Dispose();
driver.Dispose();
}
void Update()
{
jobHandle.Complete();
//TODO Render()
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d126d712307544dd89716677e730aa93
timeCreated: 1765786534

@ -0,0 +1,26 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
namespace ECSTest
{
[BurstCompile]
public struct ClientUpdateJob : IJob
{
public NetworkDriver driver;
public NetworkPipeline unreliablePipeline;
public NetworkPipeline reliablePipeline;
public NativeReference<NetworkConnection> connection;
public void Execute()
{
NetworkEvent.Type cmd;
while ((cmd = connection.Value.PopEvent(driver, out var stream, out var networkPipeline)) !=
NetworkEvent.Type.Empty)
{
}
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ffb4af76205e4a638c5343a82b13ce08
timeCreated: 1765789910

@ -1,7 +1,7 @@
using GameCore.TinyECS; using GameCore.TinyECS;
using Unity.Mathematics; using Unity.Mathematics;
namespace GameCore.Test namespace ECSTest
{ {
using Entity = Entity<EntityType>; using Entity = Entity<EntityType>;

@ -1,4 +1,4 @@
namespace GameCore.Test namespace ECSTest
{ {
public enum EntityType public enum EntityType
{ {

@ -1,10 +1,11 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using GameCore.Test;
using UnityEngine; using UnityEngine;
public class Main : MonoBehaviour namespace ECSTest
{ {
public class Main : MonoBehaviour
{
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -16,4 +17,5 @@ public class Main : MonoBehaviour
{ {
} }
}
} }

@ -1,10 +1,11 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
using Unity.Burst; namespace ECSTest
using Unity.Collections; {
using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
[BurstCompile] [BurstCompile]
public struct ServerConnectionJob : IJob public struct ServerConnectionJob : IJob
{ {
@ -21,6 +22,7 @@
var index = disconnectIndexList[i]; var index = disconnectIndexList[i];
connectionList.RemoveAtSwapBack(index); connectionList.RemoveAtSwapBack(index);
} }
disconnectIndexList.Clear(); disconnectIndexList.Clear();
// connect // connect
@ -28,7 +30,12 @@
while ((c = driver.Accept()) != default) while ((c = driver.Accept()) != default)
{ {
connectionList.Add(c); connectionList.Add(c);
Debug.Log("Accepted a connection"); Debug.Log("[Server] Accepted a connection");
} }
if (connectionList.Length > disconnectIndexList.Capacity)
disconnectIndexList.SetCapacity(connectionList.Length);
} }
} }
}

@ -7,21 +7,25 @@ using Unity.Jobs;
using Unity.Networking.Transport; using Unity.Networking.Transport;
using UnityEngine; using UnityEngine;
public class ServerMain : MonoBehaviour namespace ECSTest
{ {
public class ServerMain : MonoBehaviour
{
public string address = "127.0.0.1"; public string address = "127.0.0.1";
public NetworkDriverAsset driverAsset; public NetworkDriverAsset driverAsset;
private NetworkDriver driver; private NetworkDriver driver;
private NetworkPipeline unreliablePipeline;
private NetworkPipeline reliablePipeline;
private JobHandle networkJobHandle; private JobHandle networkJobHandle;
private NativeList<NetworkConnection> connectionList; private NativeList<NetworkConnection> connectionList;
private NativeList<int> disconnectIndexList; private NativeList<int> disconnectIndexList;
private void OnEnable() private void OnEnable()
{ {
driver = driverAsset.CreateServerDriver(address); driver = driverAsset.CreateServerDriver(address, out unreliablePipeline, out reliablePipeline);
connectionList = new NativeList<NetworkConnection>(16,Allocator.Persistent); connectionList = new NativeList<NetworkConnection>(16, Allocator.Persistent);
disconnectIndexList = new NativeList<int>(16, Allocator.Persistent); disconnectIndexList = new NativeList<int>(16, Allocator.Persistent);
} }
@ -52,7 +56,10 @@ public class ServerMain : MonoBehaviour
networkJobHandle = new ServerReceiveJob() networkJobHandle = new ServerReceiveJob()
{ {
driver = concurrentDriver, driver = concurrentDriver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList.AsParallelWriter(),
}.Schedule(connectionList, 16, networkJobHandle); }.Schedule(connectionList, 16, networkJobHandle);
} }
}
} }

@ -1,15 +1,37 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
using Unity.Burst; namespace ECSTest
using Unity.Jobs; {
using Unity.Networking.Transport;
[BurstCompile] [BurstCompile]
public struct ServerReceiveJob : IJobParallelForDefer public struct ServerReceiveJob : IJobParallelForDefer
{ {
public NetworkDriver.Concurrent driver; public NetworkDriver.Concurrent driver;
[ReadOnly]
public NativeList<NetworkConnection> connectionList;
public NativeList<int>.ParallelWriter disconnectIndexList;
public void Execute(int index) public void Execute(int index)
{ {
var connection = connectionList[index];
NetworkEvent.Type cmd;
while ((cmd = driver.PopEventForConnection(connection, out var stream)) !=
NetworkEvent.Type.Empty)
{
if (cmd == NetworkEvent.Type.Data)
{
//TODO
}
else if (cmd == NetworkEvent.Type.Disconnect)
{
disconnectIndexList.AddNoResize(index);
Debug.Log("[Server] Received a disconnection");
}
}
} }
} }
}

@ -1,12 +1,13 @@
using Unity.Burst;
using Unity.Jobs;
using Unity.Burst; namespace ECSTest
using Unity.Jobs; {
[BurstCompile] [BurstCompile]
public struct ServerSendJob : IJobParallelFor public struct ServerSendJob : IJobParallelFor
{ {
public void Execute(int index) public void Execute(int index)
{ {
} }
} }
}

@ -1,7 +1,7 @@
using GameCore.TinyECS; using GameCore.TinyECS;
using UnityEngine; using UnityEngine;
namespace GameCore.Test namespace ECSTest
{ {
public static class TestEntityDataBlock public static class TestEntityDataBlock
{ {

@ -1,19 +0,0 @@
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
namespace GameCore.Test
{
[BurstCompile]
public struct TestJob : IJobParallelFor
{
public void Execute(int index)
{
}
}
}

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 68aafcef1265449e6b6b2dd81d7173a8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -18,12 +18,12 @@ namespace GameCore.Network
public int packetJitterMs = 300; public int packetJitterMs = 300;
public int packetDropPercentage = 10; public int packetDropPercentage = 10;
private NetworkDriver CreateDriver() private NetworkDriver CreateDriver(out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline)
{ {
var settings = new NetworkSettings(Allocator.Temp); var settings = new NetworkSettings(Allocator.Temp);
settings.WithNetworkConfigParameters(disconnectTimeoutMS: 5000); settings.WithNetworkConfigParameters(disconnectTimeoutMS: 5000);
settings.WithReliableStageParameters(32); settings.WithReliableStageParameters(32);
settings.WithSimulatorStageParameters(32, NetworkParameterConstants.MaxConnectAttempts, settings.WithSimulatorStageParameters(32, mode: ApplyMode.SentPacketsOnly,
packetDelayMs: packetDelayMs, packetJitterMs: packetJitterMs, packetDelayMs: packetDelayMs, packetJitterMs: packetJitterMs,
packetDropPercentage: packetDropPercentage); packetDropPercentage: packetDropPercentage);
@ -31,24 +31,25 @@ namespace GameCore.Network
if (useSimulator) if (useSimulator)
{ {
driver.CreatePipeline(typeof(SimulatorPipelineStageInSend)); unreliablePipeline = driver.CreatePipeline(typeof(SimulatorPipelineStage));
driver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStageInSend)); reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage));
} }
else else
{ {
driver.CreatePipeline(typeof(ReliableSequencedPipelineStage)); unreliablePipeline = NetworkPipeline.Null;
reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
} }
return driver; return driver;
} }
public NetworkDriver CreateServerDriver(string address) public NetworkDriver CreateServerDriver(string address, out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline)
{ {
var driver = CreateDriver(); var driver = CreateDriver(out unreliablePipeline, out reliablePipeline);
var ok = false; var ok = false;
try try
{ {
if (!NetworkEndPoint.TryParse(address, (ushort)port, out var endpoint)) if (!NetworkEndpoint.TryParse(address, (ushort)port, out var endpoint))
{ {
return default; return default;
} }
@ -70,10 +71,10 @@ namespace GameCore.Network
} }
} }
public NetworkDriver CreateClientDriver(string address, out NetworkConnection connection) public NetworkDriver CreateClientDriver(string address, out NetworkConnection connection, out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline)
{ {
var driver = CreateDriver(); var driver = CreateDriver(out unreliablePipeline, out reliablePipeline);
if (!NetworkEndPoint.TryParse(address, (ushort)port, out var endpoint)) if (!NetworkEndpoint.TryParse(address, (ushort)port, out var endpoint))
{ {
connection = default; connection = default;
driver.Dispose(); driver.Dispose();

@ -5,7 +5,7 @@
"unity": "2021.3", "unity": "2021.3",
"description": "Network based on Unity Transport", "description": "Network based on Unity Transport",
"dependencies": { "dependencies": {
"com.unity.transport": "1.0.0" "com.unity.transport": "2.6.0"
}, },
"keywords": [ "keywords": [
"Network", "Network",

@ -14,7 +14,7 @@
"depth": 0, "depth": 0,
"source": "local", "source": "local",
"dependencies": { "dependencies": {
"com.unity.transport": "1.0.0" "com.unity.transport": "2.6.0"
} }
}, },
"com.nimin.tinyecs": { "com.nimin.tinyecs": {
@ -26,8 +26,8 @@
} }
}, },
"com.unity.burst": { "com.unity.burst": {
"version": "1.8.21", "version": "1.8.24",
"depth": 1, "depth": 2,
"source": "registry", "source": "registry",
"dependencies": { "dependencies": {
"com.unity.mathematics": "1.2.1", "com.unity.mathematics": "1.2.1",
@ -36,13 +36,14 @@
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.collections": { "com.unity.collections": {
"version": "1.4.0", "version": "2.2.1",
"depth": 1, "depth": 2,
"source": "registry", "source": "registry",
"dependencies": { "dependencies": {
"com.unity.burst": "1.6.6", "com.unity.burst": "1.8.8",
"com.unity.test-framework": "1.1.31", "com.unity.nuget.mono-cecil": "1.11.4",
"com.unity.nuget.mono-cecil": "1.11.4" "com.unity.modules.unityanalytics": "1.0.0",
"com.unity.test-framework.performance": "3.0.2"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
@ -109,7 +110,7 @@
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.mathematics": { "com.unity.mathematics": {
"version": "1.2.6", "version": "1.3.2",
"depth": 2, "depth": 2,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
@ -147,6 +148,16 @@
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.test-framework.performance": {
"version": "3.0.2",
"depth": 3,
"source": "registry",
"dependencies": {
"com.unity.test-framework": "1.1.31",
"com.unity.modules.jsonserialize": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.testtools.codecoverage": { "com.unity.testtools.codecoverage": {
"version": "1.2.6", "version": "1.2.6",
"depth": 1, "depth": 1,
@ -179,13 +190,13 @@
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.transport": { "com.unity.transport": {
"version": "1.5.0", "version": "2.6.0",
"depth": 1, "depth": 1,
"source": "registry", "source": "registry",
"dependencies": { "dependencies": {
"com.unity.burst": "1.6.6", "com.unity.burst": "1.8.24",
"com.unity.collections": "1.2.4", "com.unity.collections": "2.2.1",
"com.unity.mathematics": "1.2.6" "com.unity.mathematics": "1.3.2"
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },

Loading…
Cancel
Save