用transport库编写server jobs

master
孙钦者 10 hours ago
parent 3fe3366e75
commit 5c4c25664d
  1. 2
      .gitignore
  2. 18
      Assets/Scenes/NetworkDriverAsset.asset
  3. 8
      Assets/Scenes/NetworkDriverAsset.asset.meta
  4. 18
      Assets/Scripts/Game.Test.asmdef
  5. 5
      Assets/Scripts/Server.meta
  6. 34
      Assets/Scripts/Server/ServerConnectionJob.cs
  7. 3
      Assets/Scripts/Server/ServerConnectionJob.cs.meta
  8. 58
      Assets/Scripts/Server/ServerMain.cs
  9. 11
      Assets/Scripts/Server/ServerMain.cs.meta
  10. 15
      Assets/Scripts/Server/ServerReceiveJob.cs
  11. 3
      Assets/Scripts/Server/ServerReceiveJob.cs.meta
  12. 12
      Assets/Scripts/Server/ServerSendJob.cs
  13. 3
      Assets/Scripts/Server/ServerSendJob.cs.meta
  14. 84
      LocalPackages/com.nimin.network/Runtime/NetworkDriverAsset.cs
  15. 17
      LocalPackages/com.nimin.network/Runtime/nimin.network.asmdef
  16. 10
      Packages/manifest.json
  17. 38
      Packages/packages-lock.json

2
.gitignore vendored

@ -5,6 +5,8 @@
/[Oo]bj/
/UserSettings/
.DS_Store
.vs/
.idea/
*.csproj

@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b79190dcfae6a2f4fbdfc9deccf00ef7, type: 3}
m_Name: NetworkDriverAsset
m_EditorClassIdentifier:
useSimulator: 0
packetDelayMs: 500
packetJitterMs: 300
packetDropPercentage: 10

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a8a5562854ecf4cc1a49700e8bd93c19
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

@ -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

@ -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);
}
}

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

@ -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

@ -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

@ -7,15 +7,81 @@ using UnityEngine;
namespace GameCore.Network
{
public class NewBehaviourScript : ScriptableObject
[CreateAssetMenu(menuName = "GameCore/Network/" + nameof(NetworkDriverAsset),
fileName = nameof(NetworkDriverAsset))]
public class NetworkDriverAsset : ScriptableObject
{
// private NetworkDriver CreateNetworkDriver()
// {
// var settings = new NetworkSettings(Allocator.Temp);
// settings.WithNetworkConfigParameters(disconnectTimeoutMS: 5000);
// settings.WithReliableStageParameters(32);
//
// }
}
public uint port = 7777;
[Header("Simulator Settings")] public bool useSimulator;
public int packetDelayMs = 500;
public int packetJitterMs = 300;
public int packetDropPercentage = 10;
private NetworkDriver CreateDriver()
{
var settings = new NetworkSettings(Allocator.Temp);
settings.WithNetworkConfigParameters(disconnectTimeoutMS: 5000);
settings.WithReliableStageParameters(32);
settings.WithSimulatorStageParameters(32, NetworkParameterConstants.MaxConnectAttempts,
packetDelayMs: packetDelayMs, packetJitterMs: packetJitterMs,
packetDropPercentage: packetDropPercentage);
var driver = NetworkDriver.Create(settings);
if (useSimulator)
{
driver.CreatePipeline(typeof(SimulatorPipelineStageInSend));
driver.CreatePipeline(typeof(ReliableSequencedPipelineStage), typeof(SimulatorPipelineStageInSend));
}
else
{
driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
}
return driver;
}
public NetworkDriver CreateServerDriver(string address)
{
var driver = CreateDriver();
var ok = false;
try
{
if (!NetworkEndPoint.TryParse(address, (ushort)port, out var endpoint))
{
return default;
}
if (driver.Bind(endpoint) != 0)
{
Debug.Log("Failed to bind to " + endpoint);
return default;
}
ok = true;
driver.Listen();
return driver;
}
finally
{
if (!ok)
driver.Dispose();
}
}
public NetworkDriver CreateClientDriver(string address, out NetworkConnection connection)
{
var driver = CreateDriver();
if (!NetworkEndPoint.TryParse(address, (ushort)port, out var endpoint))
{
connection = default;
driver.Dispose();
return default;
}
connection = driver.Connect(endpoint);
return driver;
}
}
}

@ -4,23 +4,6 @@
"references": [
"Unity.Networking.Transport"
],
"includePlatforms": [
"Android",
"Editor",
"iOS",
"LinuxStandalone64",
"Lumin",
"macOSStandalone",
"PS4",
"Stadia",
"Switch",
"tvOS",
"WSA",
"WebGL",
"WindowsStandalone32",
"WindowsStandalone64",
"XboxOne"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,

@ -1,10 +1,13 @@
{
"dependencies": {
"com.unity.collab-proxy": "1.17.1",
"com.nimin.lowlevel": "file:../LocalPackages/com.nimin.lowlevel",
"com.nimin.network": "file:../LocalPackages/com.nimin.network",
"com.nimin.tinyecs": "file:../LocalPackages/com.nimin.tinyecs",
"com.unity.feature.development": "1.0.1",
"com.unity.ide.rider": "3.0.15",
"com.unity.ide.visualstudio": "2.0.16",
"com.unity.ide.vscode": "1.2.5",
"com.unity.jobs": "0.70.0-preview.7",
"com.unity.test-framework": "1.1.31",
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.6.4",
@ -40,9 +43,6 @@
"com.unity.modules.video": "1.0.0",
"com.unity.modules.vr": "1.0.0",
"com.unity.modules.wind": "1.0.0",
"com.unity.modules.xr": "1.0.0",
"com.nimin.lowlevel": "file:../LocalPackages/com.nimin.lowlevel",
"com.nimin.tinyecs": "file:../LocalPackages/com.nimin.tinyecs",
"com.nimin.network": "file:../LocalPackages/com.nimin.network"
"com.unity.modules.xr": "1.0.0"
}
}

@ -35,22 +35,14 @@
},
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": {
"version": "1.17.1",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.services.core": "1.0.1"
},
"url": "https://packages.unity.com"
},
"com.unity.collections": {
"version": "1.2.4",
"version": "1.4.0",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.6.6",
"com.unity.test-framework": "1.1.31"
"com.unity.test-framework": "1.1.31",
"com.unity.nuget.mono-cecil": "1.11.4"
},
"url": "https://packages.unity.com"
},
@ -107,6 +99,15 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.jobs": {
"version": "0.70.0-preview.7",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.collections": "1.4.0"
},
"url": "https://packages.unity.com"
},
"com.unity.mathematics": {
"version": "1.2.6",
"depth": 2,
@ -114,8 +115,8 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.newtonsoft-json": {
"version": "3.2.1",
"com.unity.nuget.mono-cecil": {
"version": "1.11.4",
"depth": 2,
"source": "registry",
"dependencies": {},
@ -128,17 +129,6 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.services.core": {
"version": "1.14.0",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.modules.androidjni": "1.0.0",
"com.unity.nuget.newtonsoft-json": "3.2.1",
"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.settings-manager": {
"version": "2.1.0",
"depth": 2,

Loading…
Cancel
Save