Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SteamNetworkingMessage_t isn't an unmanaged type, blocking optimized API calls #600

Closed
Idles opened this issue Mar 7, 2024 · 1 comment · Fixed by #601
Closed

SteamNetworkingMessage_t isn't an unmanaged type, blocking optimized API calls #600

Idles opened this issue Mar 7, 2024 · 1 comment · Fixed by #601

Comments

@Idles
Copy link

Idles commented Mar 7, 2024

The C# definition for SteamNetworkingMessage_t is (accidentally?) a managed type, which necessitates an unneeded GC allocation and some copies when using it to call SteamNetworkingSockets.SendMessages. See the sample code provided in #598.

The root cause making it a managed type is its SteamNetworkingIdentity field, which is itself a managed type because of its field m_reserved. Until struct inline arrays arrive and can be broadly used (incl. Unity), replacing m_reserved with 32 individual uint fields transitively makes SteamNetworkingMessage_t an unmanaged type.

This one (tedious) code change in SteamNetworkingIdentity allows an optimized call to SteamNetworkingSockets.SendMessages shown in the (unsafe) sample code below. I've tested it successfully at runtime using my other local modifications to Steamworks.NET discussed in #598.

var ptr = SteamNetworkingUtils.AllocateMessage(messageBytes.Length);
var msg = (SteamNetworkingMessage_t*)ptr.ToPointer();

Marshal.Copy(messageBytes, 0, msg->m_pData, messageBytes.Length);
msg->m_nFlags = flags;
msg->m_idxLane = lane;
msg->m_conn = conn; 

var msgPtrsToSend = new IntPtr[] { ptr };
SteamNetworkingSockets.SendMessages(1, msgPtrsToSend, null);
@rlabrecque
Copy link
Owner

That makes complete sense, I wonder if there's any other shenanigans we can pull instead 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants