添加client

This commit is contained in:
sunqinzhe committed 2025-12-15 21:13:59 +08:00
1 parent 5c4c25664d
commit 6256dcb52b
19 files changed
+310 -144

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 48c265b50587429a9a7555df18d64661
timeCreated: 1765786522
+50
View File
@@ -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()
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d126d712307544dd89716677e730aa93
timeCreated: 1765786534
+26
View File
@@ -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 -1
View File
@@ -1,7 +1,7 @@
using GameCore.TinyECS;
using Unity.Mathematics;
namespace GameCore.Test
namespace ECSTest
{
using Entity = Entity<EntityType>;
+1 -1
View File
@@ -1,4 +1,4 @@
namespace GameCore.Test
namespace ECSTest
{
public enum EntityType
{
+12 -10
View File
@@ -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()
{
}
}
}
+15 -8
View File
@@ -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);
}
}
}
+48 -41
View File
@@ -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();
var concurrentDriver = driver.ToConcurrent();
networkJobHandle = driver.ScheduleUpdate();
networkJobHandle = new ServerConnectionJob()
private void OnEnable()
{
driver = driver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList,
}.Schedule(networkJobHandle);
networkJobHandle = new ServerReceiveJob()
{
driver = concurrentDriver,
}.Schedule(connectionList, 16, networkJobHandle);
driver = driverAsset.CreateServerDriver(address, out unreliablePipeline, out reliablePipeline);
connectionList = new NativeList<NetworkConnection>(16, Allocator.Persistent);
disconnectIndexList = new NativeList<int>(16, Allocator.Persistent);
}
private void OnDisable()
{
networkJobHandle.Complete();
driver.Dispose();
connectionList.Dispose();
disconnectIndexList.Dispose();
}
void Update()
{
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);
}
}
}
+28 -6
View File
@@ -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");
}
}
}
}
}
+5 -4
View File
@@ -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 -1
View File
@@ -1,7 +1,7 @@
using GameCore.TinyECS;
using UnityEngine;
namespace GameCore.Test
namespace ECSTest
{
public static class TestEntityDataBlock
{
-19
View File
@@ -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)
{
}
}
}
-11
View File
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 68aafcef1265449e6b6b2dd81d7173a8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: