用transport库编写server jobs

This commit is contained in:
孙钦者 committed 2025-12-15 04:09:24 +08:00
1 parent 3fe3366e75
commit 5c4c25664d
17 files changed
+264 -75

No files matched your search

-18
View File
@@ -1,18 +0,0 @@
{
"name": "Game.Test",
"rootNamespace": "",
"references": [
"nimin.tinyecs",
"Unity.Mathematics",
"Unity.Burst"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -1,6 +1,7 @@
fileFormatVersion: 2
guid: 21d33bf0450d14e5289fe083b8566dbe
AssemblyDefinitionImporter:
guid: d142cdba4cc3f491d81c6e563975b2cf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
@@ -0,0 +1,34 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
[BurstCompile]
public struct ServerConnectionJob : IJob
{
public NetworkDriver driver;
public NativeList<NetworkConnection> connectionList;
public NativeList<int> disconnectIndexList;
public void Execute()
{
// disconnect
disconnectIndexList.Sort();
for (int i = disconnectIndexList.Length - 1; i >= 0; --i)
{
var index = disconnectIndexList[i];
connectionList.RemoveAtSwapBack(index);
}
disconnectIndexList.Clear();
// connect
NetworkConnection c;
while ((c = driver.Accept()) != default)
{
connectionList.Add(c);
Debug.Log("Accepted a connection");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 11f164db7c4a4cdb9ea508c2fd4d73c8
timeCreated: 1765740990
+58
View File
@@ -0,0 +1,58 @@
using System;
using System.Collections;
using System.Collections.Generic;
using GameCore.Network;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
using UnityEngine;
public class ServerMain : MonoBehaviour
{
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()
{
driver = driverAsset.CreateServerDriver(address);
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,
}.Schedule(connectionList, 16, networkJobHandle);
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1da32d64fdd4d41778db4fd91607ac27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+15
View File
@@ -0,0 +1,15 @@
using Unity.Burst;
using Unity.Jobs;
using Unity.Networking.Transport;
[BurstCompile]
public struct ServerReceiveJob : IJobParallelForDefer
{
public NetworkDriver.Concurrent driver;
public void Execute(int index)
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8ef77fd6553341dca22f002180a6110c
timeCreated: 1765741627
+12
View File
@@ -0,0 +1,12 @@
using Unity.Burst;
using Unity.Jobs;
[BurstCompile]
public struct ServerSendJob : IJobParallelFor
{
public void Execute(int index)
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ea3a154338e247378d2088203f16575c
timeCreated: 1765739309