-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: HeadlessAutoStart and HeadlessFrameLimiter (#318)
* feat: HeadlessAutoStart component * feat: HeadlessFrameLimiter * remove from NetMan and misc cleanup of useless virtual OnDestroy * fix refs and tests * fix poor comments * remove useless ifdef and add required using * fix potential NRE * add simple tests * fix smells
- Loading branch information
Showing
14 changed files
with
174 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using UnityEngine; | ||
using UnityEngine.Rendering; | ||
|
||
namespace Mirror | ||
{ | ||
public class HeadlessAutoStart : MonoBehaviour | ||
{ | ||
public NetworkServer server; | ||
|
||
/// <summary> | ||
/// Automatically invoke StartServer() | ||
/// <para>If the application is a Server Build or run with the -batchMode ServerRpc line arguement, StartServer is automatically invoked.</para> | ||
/// </summary> | ||
[Tooltip("Should the server auto-start when the game is started in a headless build?")] | ||
public bool startOnHeadless = true; | ||
|
||
void Start() | ||
{ | ||
// headless mode? then start the server | ||
// can't do this in Awake because Awake is for initialization. | ||
// some transports might not be ready until Start. | ||
if (server && SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null && startOnHeadless) | ||
{ | ||
_ = server.ListenAsync(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using UnityEngine; | ||
using UnityEngine.Rendering; | ||
|
||
namespace Mirror | ||
{ | ||
public class HeadlessFrameLimiter : MonoBehaviour | ||
{ | ||
static readonly ILogger logger = LogFactory.GetLogger<NetworkManager>(); | ||
|
||
/// <summary> | ||
/// Server Update frequency, per second. Use around 60Hz for fast paced games like Counter-Strike to minimize latency. Use around 30Hz for games like WoW to minimize computations. Use around 1-10Hz for slow paced games like EVE. | ||
/// </summary> | ||
[Tooltip("Server Update frequency, per second. Use around 60Hz for fast paced games like Counter-Strike to minimize latency. Use around 30Hz for games like WoW to minimize computations. Use around 1-10Hz for slow paced games like EVE.")] | ||
public int serverTickRate = 30; | ||
|
||
/// <summary> | ||
/// Set the frame rate for a headless server. | ||
/// </summary> | ||
public void Start() | ||
{ | ||
// set a fixed tick rate instead of updating as often as possible | ||
if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null) | ||
{ | ||
Application.targetFrameRate = serverTickRate; | ||
if (logger.logEnabled) logger.Log("Server Tick Rate set to: " + Application.targetFrameRate + " Hz."); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,5 @@ public interface INetworkManager | |
void StopServer(); | ||
|
||
void StopClient(); | ||
|
||
void OnDestroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using UnityEngine; | ||
using NUnit.Framework; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace Mirror.Tests | ||
{ | ||
[TestFixture] | ||
public class HeadlessAutoStartTest : MonoBehaviour | ||
{ | ||
protected GameObject testGO; | ||
protected HeadlessAutoStart comp; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
testGO = new GameObject(); | ||
comp = testGO.AddComponent<HeadlessAutoStart>(); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
Object.DestroyImmediate(testGO); | ||
} | ||
|
||
[Test] | ||
public void StartOnHeadlessValue() | ||
{ | ||
Assert.That(comp.startOnHeadless, Is.True); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using UnityEngine; | ||
using NUnit.Framework; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace Mirror.Tests | ||
{ | ||
[TestFixture] | ||
public class HeadlessFrameLimiterTest : MonoBehaviour | ||
{ | ||
protected GameObject testGO; | ||
protected HeadlessFrameLimiter comp; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
testGO = new GameObject(); | ||
comp = testGO.AddComponent<HeadlessFrameLimiter>(); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
Object.DestroyImmediate(testGO); | ||
} | ||
|
||
[Test] | ||
public void StartOnHeadlessValue() | ||
{ | ||
Assert.That(comp.serverTickRate, Is.EqualTo(30)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirror/Tests/Editor/HeadlessFrameLimiterTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters