Skip to content

Commit

Permalink
Optimize Receive byte[] allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Metater committed Jan 8, 2025
1 parent 7380e04 commit c056561
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
10 changes: 5 additions & 5 deletions com.mirror.steamworks.net/NextClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class NextClient : NextCommon, IClient

private TimeSpan ConnectionTimeout;

private event Action<byte[], int> OnReceivedData;
private event Action<ArraySegment<byte>, int> OnReceivedData;
private event Action OnConnected;
private event Action OnDisconnected;
private Callback<SteamNetConnectionStatusChangedCallback_t> c_onConnectionChange = null;
Expand All @@ -40,7 +40,7 @@ public static NextClient CreateClient(FizzySteamworks transport, string host)

c.OnConnected += () => transport.OnClientConnected.Invoke();
c.OnDisconnected += () => transport.OnClientDisconnected.Invoke();
c.OnReceivedData += (data, ch) => transport.OnClientDataReceived.Invoke(new ArraySegment<byte>(data), ch);
c.OnReceivedData += (segment, channelId) => transport.OnClientDataReceived.Invoke(segment, channelId);

try
{
Expand Down Expand Up @@ -207,14 +207,14 @@ public void ReceiveData()
{
for (int i = 0; i < messageCount; i++)
{
(byte[] data, int ch) = ProcessMessage(messagePtrs[i]);
(var segment, int channelId) = ProcessMessage(messagePtrs[i]);
if (Connected)
{
OnReceivedData(data, ch);
OnReceivedData(segment, channelId);
}
else
{
BufferedData.Add(() => OnReceivedData(data, ch));
BufferedData.Add(() => OnReceivedData(segment, channelId));
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions com.mirror.steamworks.net/NextCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected EResult SendSocket(HSteamNetConnection conn, ArraySegment<byte> segmen

if (packetSize > MAX_PACKET_SIZE)
{
Debug.LogError($"Attempted to send oversize packet with {segment.Count} bytes on channel with ID {channelId}.");
Debug.LogError($"Attempted to send oversize packet with {packetSize} bytes on channel with ID {channelId}.");
return EResult.k_EResultFail;
}

Expand All @@ -54,17 +54,24 @@ protected EResult SendSocket(HSteamNetConnection conn, ArraySegment<byte> segmen
return res;
}

protected (byte[], int) ProcessMessage(IntPtr ptrs)
protected (ArraySegment<byte>, int) ProcessMessage(IntPtr ptrs)
{
SteamNetworkingMessage_t data = Marshal.PtrToStructure<SteamNetworkingMessage_t>(ptrs);
byte[] managedArray = new byte[data.m_cbSize];
Marshal.Copy(data.m_pData, managedArray, 0, data.m_cbSize);
int packetSize = data.m_cbSize;

// Not sure if this is a good way to handle oversize packets
//if (packetSize > MAX_PACKET_SIZE)
//{
// Debug.LogWarning($"Attempted to process oversize packet with {packetSize} bytes. Receiving an unreliable packet of size zero.");
// return (ArraySegment<byte>.Empty, Channels.Unreliable);
//}

Marshal.Copy(data.m_pData, buffer, 0, data.m_cbSize);
SteamNetworkingMessage_t.Release(ptrs);

int channel = managedArray[managedArray.Length - 1];
Array.Resize(ref managedArray, managedArray.Length - 1);
return (managedArray, channel);
var segment = new ArraySegment<byte>(buffer, 0, packetSize - 1);
int channelId = buffer[packetSize - 1];
return (segment, channelId);
}

public virtual void Dispose()
Expand Down
8 changes: 4 additions & 4 deletions com.mirror.steamworks.net/NextServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mirror.FizzySteam
public class NextServer : NextCommon, IServer
{
private event Action<int, string> OnConnectedWithAddress;
private event Action<int, byte[], int> OnReceivedData;
private event Action<int, ArraySegment<byte>, int> OnReceivedData;
private event Action<int> OnDisconnected;
private event Action<int, TransportError, string> OnReceivedError;

Expand Down Expand Up @@ -44,7 +44,7 @@ public static NextServer CreateServer(FizzySteamworks transport, int maxConnecti

server.OnConnectedWithAddress += (id, addres) => transport.OnServerConnectedWithAddress.Invoke(id, addres);
server.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id);
server.OnReceivedData += (id, data, ch) => transport.OnServerDataReceived.Invoke(id, new ArraySegment<byte>(data), ch);
server.OnReceivedData += (id, segment, channelId) => transport.OnServerDataReceived.Invoke(id, segment, channelId);
server.OnReceivedError += (id, error, reason) => transport.OnServerError.Invoke(id, error, reason);

try
Expand Down Expand Up @@ -193,8 +193,8 @@ public void ReceiveData()
{
for (int i = 0; i < messageCount; i++)
{
(byte[] data, int ch) = ProcessMessage(messagePtrs[i]);
OnReceivedData?.Invoke(connId, data, ch);
(var segment, int channelId) = ProcessMessage(messagePtrs[i]);
OnReceivedData?.Invoke(connId, segment, channelId);
}
}
}
Expand Down

0 comments on commit c056561

Please sign in to comment.