Skip to content

Commit

Permalink
Improve PausedSocketFix. Also stop running NetManagers in Unity in On…
Browse files Browse the repository at this point in the history
…ApplicationQuit (when using library source code)
  • Loading branch information
RevenantX committed Nov 22, 2024
1 parent 60c453d commit c04b946
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 12 deletions.
15 changes: 8 additions & 7 deletions LiteNetLib/NetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,20 +1603,21 @@ public void Stop(bool sendDisconnectMessages)
return;
NetDebug.Write("[NM] Stop");

//Send last disconnect
for (var netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer)
netPeer.Shutdown(null, 0, 0, !sendDisconnectMessages);

//Stop
CloseSocket();

#if UNITY_SOCKET_FIX
if (_useSocketFix)
{
_pausedSocketFix.Deinitialize();
_pausedSocketFix = null;
}
#endif

//Send last disconnect
for (var netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer)
netPeer.Shutdown(null, 0, 0, !sendDisconnectMessages);

//Stop
CloseSocket();

_updateTriggerEvent.Set();
if (!_manualMode)
{
Expand Down
14 changes: 12 additions & 2 deletions LiteNetLib/PausedSocketFix.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if UNITY_2018_3_OR_NEWER
using System.Net;
using UnityEngine;

namespace LiteNetLib
{
Expand All @@ -19,14 +20,23 @@ public PausedSocketFix(NetManager netManager, IPAddress ipv4, IPAddress ipv6, in
_ipv6 = ipv6;
_port = port;
_manualMode = manualMode;
UnityEngine.Application.focusChanged += Application_focusChanged;
Application.focusChanged += Application_focusChanged;
Application.quitting += Deinitialize;
_initialized = true;
}

public void Deinitialize()
{
if (_initialized)
UnityEngine.Application.focusChanged -= Application_focusChanged;
{
Application.focusChanged -= Application_focusChanged;
Application.quitting -= Deinitialize;
}

if (_netManager.IsRunning)
{
_netManager.Stop();
}
_initialized = false;
}

Expand Down
Binary file modified LiteNetLibSampleUnity/Assets/LiteNetLib.dll
Binary file not shown.
104 changes: 103 additions & 1 deletion LiteNetLibSampleUnity/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ PlayerSettings:
androidMinimumWindowWidth: 400
androidMinimumWindowHeight: 300
androidFullscreenMode: 1
androidAutoRotationBehavior: 1
defaultIsNativeResolution: 1
macRetinaSupport: 1
runInBackground: 0
captureSingleScreen: 0
muteOtherAudioSources: 0
Prepare IOS For Recording: 0
Force IOS Speakers When Recording: 0
audioSpatialExperience: 0
deferSystemGesturesMode: 0
hideHomeButton: 0
submitAnalytics: 1
Expand Down Expand Up @@ -136,6 +138,8 @@ PlayerSettings:
vulkanEnableLateAcquireNextImage: 0
vulkanEnableCommandBufferRecycling: 1
loadStoreDebugModeEnabled: 0
visionOSBundleVersion: 1.0
tvOSBundleVersion: 1.0
bundleVersion: 1.0
preloadedAssets: []
metroInputSource: 0
Expand Down Expand Up @@ -234,6 +238,7 @@ PlayerSettings:
iOSMetalForceHardShadows: 0
metalEditorSupport: 1
metalAPIValidation: 1
metalCompileShaderBinary: 0
iOSRenderExtraFrameOnPause: 1
iosCopyPluginsCodeInsteadOfSymlink: 0
appleDeveloperTeamID:
Expand Down Expand Up @@ -384,6 +389,103 @@ PlayerSettings:
m_Height: 36
m_Kind: 0
m_SubKind:
- m_BuildTarget: iPhone
m_Icons:
- m_Textures: []
m_Width: 180
m_Height: 180
m_Kind: 0
m_SubKind: iPhone
- m_Textures: []
m_Width: 120
m_Height: 120
m_Kind: 0
m_SubKind: iPhone
- m_Textures: []
m_Width: 167
m_Height: 167
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 152
m_Height: 152
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 76
m_Height: 76
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 120
m_Height: 120
m_Kind: 3
m_SubKind: iPhone
- m_Textures: []
m_Width: 80
m_Height: 80
m_Kind: 3
m_SubKind: iPhone
- m_Textures: []
m_Width: 80
m_Height: 80
m_Kind: 3
m_SubKind: iPad
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 3
m_SubKind: iPad
- m_Textures: []
m_Width: 87
m_Height: 87
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 58
m_Height: 58
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 29
m_Height: 29
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 58
m_Height: 58
m_Kind: 1
m_SubKind: iPad
- m_Textures: []
m_Width: 29
m_Height: 29
m_Kind: 1
m_SubKind: iPad
- m_Textures: []
m_Width: 60
m_Height: 60
m_Kind: 2
m_SubKind: iPhone
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 2
m_SubKind: iPhone
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 2
m_SubKind: iPad
- m_Textures: []
m_Width: 20
m_Height: 20
m_Kind: 2
m_SubKind: iPad
- m_Textures: []
m_Width: 1024
m_Height: 1024
m_Kind: 4
m_SubKind: App Store
m_BuildTargetBatching: []
m_BuildTargetShaderSettings: []
m_BuildTargetGraphicsJobs:
Expand Down Expand Up @@ -547,7 +649,6 @@ PlayerSettings:
switchScreenResolutionBehavior: 2
switchUseCPUProfiler: 0
switchEnableFileSystemTrace: 0
switchUseGOLDLinker: 0
switchLTOSetting: 0
switchApplicationID: 0x0005000C10000001
switchNSODependencies:
Expand Down Expand Up @@ -846,6 +947,7 @@ PlayerSettings:
metroSplashScreenBackgroundColor: {r: 0.13333334, g: 0.17254902, b: 0.21568628,
a: 1}
metroSplashScreenUseBackgroundColor: 1
syncCapabilities: 0
platformCapabilities: {}
metroTargetDeviceFamilies: {}
metroFTAName:
Expand Down
4 changes: 2 additions & 2 deletions LiteNetLibSampleUnity/ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.3.12f1
m_EditorVersionWithRevision: 2022.3.12f1 (4fe6e059c7ef)
m_EditorVersion: 2022.3.50f1
m_EditorVersionWithRevision: 2022.3.50f1 (c3db7f8bf9b1)

0 comments on commit c04b946

Please sign in to comment.