用transport库编写server jobs
This commit is contained in:
1 parent
3fe3366e75
commit
5c4c25664d
17 files changed
+264
-75
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user