From 15a9f334e491ae0e15d3401f8c29e10e47e699a8 Mon Sep 17 00:00:00 2001 From: SeaFood Date: Sat, 22 Jul 2023 08:00:39 +0200 Subject: [PATCH] Addition of SteamNetworkingMessages (WIP) Update of SteamClient Update of SteamServer --- Backup/Facepunch.Steamworks.sln | 107 +++++++ Facepunch.Steamworks.sln | 4 +- Facepunch.Steamworks/SteamClient.cs | 1 + .../SteamNetworkingFakeUDPPort.cs | 10 + .../SteamNetworkingMessages.cs | 122 ++++++++ .../SteamNetworkingSockets.cs | 4 + Facepunch.Steamworks/SteamServer.cs | 7 + UpgradeLog.htm | 280 ++++++++++++++++++ 8 files changed, 533 insertions(+), 2 deletions(-) create mode 100644 Backup/Facepunch.Steamworks.sln create mode 100644 Facepunch.Steamworks/SteamNetworkingFakeUDPPort.cs create mode 100644 Facepunch.Steamworks/SteamNetworkingMessages.cs create mode 100644 UpgradeLog.htm diff --git a/Backup/Facepunch.Steamworks.sln b/Backup/Facepunch.Steamworks.sln new file mode 100644 index 0000000..26ca56b --- /dev/null +++ b/Backup/Facepunch.Steamworks.sln @@ -0,0 +1,107 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29009.5 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generator", "Generator\Generator.csproj", "{B7225D11-2AAA-49D6-AE93-A73696EA35FE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facepunch.Steamworks.Win64", "Facepunch.Steamworks\Facepunch.Steamworks.Win64.csproj", "{8C73DA93-73AD-4445-9A2C-15D4A44337D3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facepunch.Steamworks.Win32", "Facepunch.Steamworks\Facepunch.Steamworks.Win32.csproj", "{2D6247F6-8AB2-405F-A00E-3A364B808A55}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facepunch.Steamworks.Posix", "Facepunch.Steamworks\Facepunch.Steamworks.Posix.csproj", "{C62FF421-BE44-4DB0-B99A-E13E007A30B9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Facepunch.Steamworks.TestWin32", "Facepunch.Steamworks.Test\Facepunch.Steamworks.TestWin32.csproj", "{3F6183AD-D966-44F2-A6EB-42E61E591B49}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Facepunch.Steamworks.TestWin64", "Facepunch.Steamworks.Test\Facepunch.Steamworks.TestWin64.csproj", "{165081E3-BD96-404B-B83E-A635F1AF7CDE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Debug|x64.ActiveCfg = Debug|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Debug|x64.Build.0 = Debug|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Debug|x86.ActiveCfg = Debug|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Debug|x86.Build.0 = Debug|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Release|Any CPU.Build.0 = Release|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Release|x64.ActiveCfg = Release|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Release|x64.Build.0 = Release|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Release|x86.ActiveCfg = Release|Any CPU + {B7225D11-2AAA-49D6-AE93-A73696EA35FE}.Release|x86.Build.0 = Release|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Debug|x64.ActiveCfg = Debug|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Debug|x64.Build.0 = Debug|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Debug|x86.ActiveCfg = Debug|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Debug|x86.Build.0 = Debug|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Release|Any CPU.Build.0 = Release|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Release|x64.ActiveCfg = Release|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Release|x64.Build.0 = Release|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Release|x86.ActiveCfg = Release|Any CPU + {8C73DA93-73AD-4445-9A2C-15D4A44337D3}.Release|x86.Build.0 = Release|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Debug|x64.ActiveCfg = Debug|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Debug|x64.Build.0 = Debug|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Debug|x86.ActiveCfg = Debug|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Debug|x86.Build.0 = Debug|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Release|Any CPU.Build.0 = Release|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Release|x64.ActiveCfg = Release|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Release|x64.Build.0 = Release|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Release|x86.ActiveCfg = Release|Any CPU + {2D6247F6-8AB2-405F-A00E-3A364B808A55}.Release|x86.Build.0 = Release|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Debug|x64.ActiveCfg = Debug|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Debug|x64.Build.0 = Debug|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Debug|x86.ActiveCfg = Debug|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Debug|x86.Build.0 = Debug|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Release|Any CPU.Build.0 = Release|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Release|x64.ActiveCfg = Release|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Release|x64.Build.0 = Release|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Release|x86.ActiveCfg = Release|Any CPU + {C62FF421-BE44-4DB0-B99A-E13E007A30B9}.Release|x86.Build.0 = Release|Any CPU + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Debug|x64.ActiveCfg = Debug|x64 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Debug|x64.Build.0 = Debug|x64 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Debug|x86.ActiveCfg = Debug|x86 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Debug|x86.Build.0 = Debug|x86 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Release|Any CPU.Build.0 = Release|Any CPU + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Release|x64.ActiveCfg = Release|x64 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Release|x64.Build.0 = Release|x64 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Release|x86.ActiveCfg = Release|x86 + {3F6183AD-D966-44F2-A6EB-42E61E591B49}.Release|x86.Build.0 = Release|x86 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Debug|x64.ActiveCfg = Debug|x64 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Debug|x64.Build.0 = Debug|x64 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Debug|x86.ActiveCfg = Debug|x86 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Debug|x86.Build.0 = Debug|x86 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Release|Any CPU.Build.0 = Release|Any CPU + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Release|x64.ActiveCfg = Release|x64 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Release|x64.Build.0 = Release|x64 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Release|x86.ActiveCfg = Release|x86 + {165081E3-BD96-404B-B83E-A635F1AF7CDE}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {506FC2EC-38D1-45E2-BAE8-D61584162F7D} + EndGlobalSection +EndGlobal diff --git a/Facepunch.Steamworks.sln b/Facepunch.Steamworks.sln index 26ca56b..b944b86 100644 --- a/Facepunch.Steamworks.sln +++ b/Facepunch.Steamworks.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29009.5 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33801.468 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generator", "Generator\Generator.csproj", "{B7225D11-2AAA-49D6-AE93-A73696EA35FE}" EndProject diff --git a/Facepunch.Steamworks/SteamClient.cs b/Facepunch.Steamworks/SteamClient.cs index f8f1db2..9005103 100644 --- a/Facepunch.Steamworks/SteamClient.cs +++ b/Facepunch.Steamworks/SteamClient.cs @@ -59,6 +59,7 @@ public static void Init( uint appid, bool asyncCallbacks = true ) AddInterface(); AddInterface(); AddInterface(); + AddInterface(); initialized = openInterfaces.Count > 0; diff --git a/Facepunch.Steamworks/SteamNetworkingFakeUDPPort.cs b/Facepunch.Steamworks/SteamNetworkingFakeUDPPort.cs new file mode 100644 index 0000000..da11e5e --- /dev/null +++ b/Facepunch.Steamworks/SteamNetworkingFakeUDPPort.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Steamworks +{ + internal class SteamNetworkingFakeUDPPort + { + } +} diff --git a/Facepunch.Steamworks/SteamNetworkingMessages.cs b/Facepunch.Steamworks/SteamNetworkingMessages.cs new file mode 100644 index 0000000..112646a --- /dev/null +++ b/Facepunch.Steamworks/SteamNetworkingMessages.cs @@ -0,0 +1,122 @@ +using Steamworks.Data; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Runtime.InteropServices; +using System.Text; + +namespace Steamworks +{ + public enum SteamNetworkingOptions + { + Unreliable = 0, + NoNagle = 1, + UnreliableNoNagle = Unreliable | NoNagle, + NoDelay = 4, + UnreliableNoDelay = Unreliable | NoDelay | NoNagle, + Reliable = 8, + ReliableNoNagle = Reliable | NoNagle + } + + public class SteamNetworkingMessages : SteamSharedClass + { + internal static ISteamNetworkingMessages Internal => Interface as ISteamNetworkingMessages; + + internal override bool InitializeInterface( bool server ) + { + SetInterface( server, new ISteamNetworkingMessages( server ) ); + if ( Interface.Self == IntPtr.Zero ) return false; + + InstallEvents( server ); + + return true; + } + + internal static void InstallEvents( bool server ) + { + Dispatch.Install( x => OnSessionRequest?.Invoke( x.DentityRemote), server ); + Dispatch.Install( x => OnSessionFailed?.Invoke( x.Nfo), server ); + } + + public static Action OnSessionRequest; + + public static Action OnSessionFailed; + + public bool AcceptSessionWithUser( ref NetIdentity identity ) => Internal.AcceptSessionWithUser( ref identity ); + public bool CloseSessionWithUser( ref NetIdentity identity ) => Internal.CloseSessionWithUser( ref identity ); + public bool CloseChannelWithUser( ref NetIdentity identity, int channel ) => Internal.CloseChannelWithUser( ref identity, channel ); + public ConnectionState GetSessionConnectionInfo( ref NetIdentity identity, ref ConnectionInfo info, ref ConnectionStatus status ) => Internal.GetSessionConnectionInfo( ref identity, ref info, ref status ); + + public static unsafe Result SendMessageToUser( ref NetIdentity identity, byte[] data, SteamNetworkingOptions flags, int channel) + { + uint length = (uint)data.Length; + fixed ( byte* p = data ) + { + IntPtr[] pubData = new IntPtr[] { (IntPtr)p }; + + return Internal.SendMessageToUser( ref identity, pubData, length, (int)flags, channel ); + } + } + + public unsafe static int ReceiveMessagesOnChannel( int channel, Action callback, int bufferSize = 32, bool receiveToEnd = true ) + { + if ( bufferSize < 1 || bufferSize > 256 ) throw new ArgumentOutOfRangeException( nameof( bufferSize ) ); + + int totalProcessed = 0; + NetMsg** messageBuffer = stackalloc NetMsg*[bufferSize]; + + while ( true ) + { + int processed = Internal.ReceiveMessagesOnChannel(channel, new IntPtr( &messageBuffer[0] ), bufferSize ); + totalProcessed += processed; + + try + { + for ( int i = 0; i < processed; i++ ) + { + ReceiveMessage( ref messageBuffer[i], callback ); + } + } + catch + { + for ( int i = 0; i < processed; i++ ) + { + if ( messageBuffer[i] != null ) + { + NetMsg.InternalRelease( messageBuffer[i] ); + } + } + + throw; + } + + + // + // Keep going if receiveToEnd and we filled the buffer + // + if ( !receiveToEnd || processed < bufferSize ) + break; + } + + return totalProcessed; + } + + internal unsafe static void ReceiveMessage( ref NetMsg* msg, Action callback ) + { + try + { + byte[] bytes = new byte[msg->DataSize]; + Marshal.Copy(msg->DataPtr, bytes, 0, msg->DataSize); + callback( bytes, msg->DataSize, msg->RecvTime, msg->MessageNumber, msg->Channel ); + } + finally + { + // + // Releases the message + // + NetMsg.InternalRelease( msg ); + msg = null; + } + } + } +} diff --git a/Facepunch.Steamworks/SteamNetworkingSockets.cs b/Facepunch.Steamworks/SteamNetworkingSockets.cs index 7fe3ace..4029ed6 100644 --- a/Facepunch.Steamworks/SteamNetworkingSockets.cs +++ b/Facepunch.Steamworks/SteamNetworkingSockets.cs @@ -287,6 +287,10 @@ public static bool RequestFakeIP( int numFakePorts = 1 ) return Internal.BeginAsyncRequestFakeIP( numFakePorts ); } + public static IntPtr CreateFakeUDPPort( int index ) + { + return Internal.CreateFakeUDPPort( index ); + } /// /// Return info about the FakeIP and port that we have been assigned, if any. /// diff --git a/Facepunch.Steamworks/SteamServer.cs b/Facepunch.Steamworks/SteamServer.cs index 7435cb8..2e148b2 100644 --- a/Facepunch.Steamworks/SteamServer.cs +++ b/Facepunch.Steamworks/SteamServer.cs @@ -107,6 +107,7 @@ public static void Init( AppId appid, SteamServerInit init, bool asyncCallbacks AddInterface(); AddInterface(); + AddInterface(); // // Initial settings @@ -291,6 +292,12 @@ public static void LogOnAnonymous() ForceHeartbeat(); } + public static void LogOn(string token) + { + Internal.LogOn( token ); + ForceHeartbeat(); + } + /// /// Log off of Steam. /// diff --git a/UpgradeLog.htm b/UpgradeLog.htm new file mode 100644 index 0000000..d4d8ff9 --- /dev/null +++ b/UpgradeLog.htm @@ -0,0 +1,280 @@ + + + + Rapport de migration +

+ Rapport de migration - Facepunch.Steamworks

Vue d'ensemble

ProjetChemin d'accèsErreursAvertissementsMessages
SolutionFacepunch.Steamworks.sln012
Facepunch.Steamworks.PosixFacepunch.Steamworks\Facepunch.Steamworks.Posix.csproj000
Facepunch.Steamworks.TestWin32Facepunch.Steamworks.Test\Facepunch.Steamworks.TestWin32.csproj000
Facepunch.Steamworks.TestWin64Facepunch.Steamworks.Test\Facepunch.Steamworks.TestWin64.csproj000
Facepunch.Steamworks.Win32Facepunch.Steamworks\Facepunch.Steamworks.Win32.csproj000
Facepunch.Steamworks.Win64Facepunch.Steamworks\Facepunch.Steamworks.Win64.csproj000
GeneratorGenerator\Generator.csproj000

Solution et projets

\ No newline at end of file