添加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

+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)
{
}
}
}