添加client

master
sunqinzhe 1 day 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. 22
      Assets/Scripts/Main.cs
  10. 23
      Assets/Scripts/Server/ServerConnectionJob.cs
  11. 83
      Assets/Scripts/Server/ServerMain.cs
  12. 34
      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. 24
      LocalPackages/com.nimin.network/package.json
  19. 37
      Packages/packages-lock.json

@ -38,7 +38,6 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.44657815, g: 0.49641192, b: 0.57481617, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
@ -104,7 +103,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
@ -117,7 +116,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
@ -209,13 +208,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 705507993}
serializedVersion: 2
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!1 &963194225
GameObject:
@ -258,9 +257,17 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
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_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@ -294,13 +301,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225}
serializedVersion: 2
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!114 &963194229
MonoBehaviour:
@ -314,3 +321,56 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 694d8256118344eb2b130fceb1a11b7f, type: 3}
m_Name:
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 Unity.Mathematics;
namespace GameCore.Test
namespace ECSTest
{
using Entity = Entity<EntityType>;

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

@ -1,19 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using GameCore.Test;
using UnityEngine;
public class Main : MonoBehaviour
namespace ECSTest
{
// Start is called before the first frame update
void Start()
public class Main : MonoBehaviour
{
TestEntityDataBlock.Run();
}
// Start is called before the first frame update
void Start()
{
TestEntityDataBlock.Run();
}
// Update is called once per frame
void Update()
{
// Update is called once per frame
void Update()
{
}
}
}

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

@ -7,52 +7,59 @@ using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
public class ServerMain : MonoBehaviour
namespace ECSTest
{
public string address = "127.0.0.1";
public NetworkDriverAsset driverAsset;
private NetworkDriver driver;
private JobHandle networkJobHandle;
private NativeList<NetworkConnection> connectionList;
private NativeList<int> disconnectIndexList;
private void OnEnable()
public class ServerMain : MonoBehaviour
{
driver = driverAsset.CreateServerDriver(address);
connectionList = new NativeList<NetworkConnection>(16,Allocator.Persistent);
disconnectIndexList = new NativeList<int>(16, Allocator.Persistent);
}
public string address = "127.0.0.1";
public NetworkDriverAsset driverAsset;
private void OnDisable()
{
networkJobHandle.Complete();
private NetworkDriver driver;
private NetworkPipeline unreliablePipeline;
private NetworkPipeline reliablePipeline;
private JobHandle networkJobHandle;
private NativeList<NetworkConnection> connectionList;
private NativeList<int> disconnectIndexList;
driver.Dispose();
connectionList.Dispose();
disconnectIndexList.Dispose();
}
void Update()
{
networkJobHandle.Complete();
private void OnEnable()
{
driver = driverAsset.CreateServerDriver(address, out unreliablePipeline, out reliablePipeline);
var concurrentDriver = driver.ToConcurrent();
connectionList = new NativeList<NetworkConnection>(16, Allocator.Persistent);
disconnectIndexList = new NativeList<int>(16, Allocator.Persistent);
}
networkJobHandle = driver.ScheduleUpdate();
networkJobHandle = new ServerConnectionJob()
private void OnDisable()
{
driver = driver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList,
}.Schedule(networkJobHandle);
networkJobHandle = new ServerReceiveJob()
networkJobHandle.Complete();
driver.Dispose();
connectionList.Dispose();
disconnectIndexList.Dispose();
}
void Update()
{
driver = concurrentDriver,
}.Schedule(connectionList, 16, networkJobHandle);
networkJobHandle.Complete();
var concurrentDriver = driver.ToConcurrent();
networkJobHandle = driver.ScheduleUpdate();
networkJobHandle = new ServerConnectionJob()
{
driver = driver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList,
}.Schedule(networkJobHandle);
networkJobHandle = new ServerReceiveJob()
{
driver = concurrentDriver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList.AsParallelWriter(),
}.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;
using Unity.Jobs;
using Unity.Networking.Transport;
namespace ECSTest
{
[BurstCompile]
public struct ServerReceiveJob : IJobParallelForDefer
{
public NetworkDriver.Concurrent driver;
[ReadOnly]
public NativeList<NetworkConnection> connectionList;
public NativeList<int>.ParallelWriter disconnectIndexList;
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;
using Unity.Jobs;
namespace ECSTest
{
[BurstCompile]
public struct ServerSendJob : IJobParallelFor
{
public void Execute(int index)
{
}
}
}

@ -1,7 +1,7 @@
using GameCore.TinyECS;
using UnityEngine;
namespace GameCore.Test
namespace ECSTest
{
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 packetDropPercentage = 10;
private NetworkDriver CreateDriver()
private NetworkDriver CreateDriver(out NetworkPipeline unreliablePipeline, out NetworkPipeline reliablePipeline)
{
var settings = new NetworkSettings(Allocator.Temp);
settings.WithNetworkConfigParameters(disconnectTimeoutMS: 5000);
settings.WithReliableStageParameters(32);
settings.WithSimulatorStageParameters(32, NetworkParameterConstants.MaxConnectAttempts,
settings.WithSimulatorStageParameters(32, mode: ApplyMode.SentPacketsOnly,
packetDelayMs: packetDelayMs, packetJitterMs: packetJitterMs,
packetDropPercentage: packetDropPercentage);
@ -31,24 +31,25 @@ namespace GameCore.Network
if (useSimulator)
{
driver.CreatePipeline(typeof(SimulatorPipelineStageInSend));
driver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStageInSend));
unreliablePipeline = driver.CreatePipeline(typeof(SimulatorPipelineStage));
reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStage));
}
else
{
driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
unreliablePipeline = NetworkPipeline.Null;
reliablePipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
}
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;
try
{
if (!NetworkEndPoint.TryParse(address, (ushort)port, out var endpoint))
if (!NetworkEndpoint.TryParse(address, (ushort)port, out var endpoint))
{
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();
if (!NetworkEndPoint.TryParse(address, (ushort)port, out var endpoint))
var driver = CreateDriver(out unreliablePipeline, out reliablePipeline);
if (!NetworkEndpoint.TryParse(address, (ushort)port, out var endpoint))
{
connection = default;
driver.Dispose();

@ -1,14 +1,14 @@
{
"name": "com.nimin.network",
"displayName": "GameCore Network",
"version": "0.1.0",
"unity": "2021.3",
"description": "Network based on Unity Transport",
"dependencies": {
"com.unity.transport": "1.0.0"
},
"keywords": [
"Network",
"JobSystem"
]
"name": "com.nimin.network",
"displayName": "GameCore Network",
"version": "0.1.0",
"unity": "2021.3",
"description": "Network based on Unity Transport",
"dependencies": {
"com.unity.transport": "2.6.0"
},
"keywords": [
"Network",
"JobSystem"
]
}

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

Loading…
Cancel
Save