dont copy on send

This commit is contained in:
JohnCorby 2025-02-24 15:57:54 -08:00
parent 83a912e7c4
commit d711202dae
3 changed files with 9 additions and 10 deletions

View File

@ -69,13 +69,12 @@ public class Client
public void Send(ArraySegment<byte> segment, int channelId)
{
var data = new byte[segment.Count];
Array.Copy(segment.Array, segment.Offset, data, 0, data.Length);
// use pointer to managed array instead of making copy. is this okay?
unsafe
{
fixed (byte* pData = data)
fixed (byte* pData = segment.Array)
{
var result = SteamNetworkingSockets.SendMessageToConnection(_conn, (IntPtr)pData, (uint)data.Length, Util.MirrorChannel2SendFlag(channelId), out _);
var result = SteamNetworkingSockets.SendMessageToConnection(_conn, (IntPtr)(pData + segment.Offset), (uint)segment.Count, Util.MirrorChannel2SendFlag(channelId), out _);
_transport.OnClientDataSent?.Invoke(segment, channelId);
}
}

View File

@ -92,7 +92,7 @@ public static class Program
running = false;
break;
case 's':
transport.ServerSend(theConn, new ArraySegment<byte>(new byte[] { 1, 2, 3 }));
transport.ServerSend(theConn, new ArraySegment<byte>(new byte[] { 1, 2, 3, 4, 5 }, 1, 5 - 1));
break;
case 'd':
transport.ServerDisconnect(theConn);
@ -136,7 +136,7 @@ public static class Program
running = false;
break;
case 's':
transport.ClientSend(new ArraySegment<byte>(new byte[] { 1, 2, 3 }));
transport.ClientSend(new ArraySegment<byte>(new byte[] { 1, 2, 3, 4, 5 }, 1, 5 - 1));
break;
}
}

View File

@ -46,6 +46,7 @@ public class Server
public bool IsListening;
private HSteamListenSocket _listenSocket;
// connection id is derived from uint to int cast here. is that okay???
private readonly List<HSteamNetConnection> _conns = new();
public void StartListening()
@ -62,13 +63,12 @@ public class Server
{
var conn = new HSteamNetConnection((uint)connectionId);
var data = new byte[segment.Count];
Array.Copy(segment.Array, segment.Offset, data, 0, data.Length);
// use pointer to managed array instead of making copy. is this okay?
unsafe
{
fixed (byte* pData = data)
fixed (byte* pData = segment.Array)
{
var result = SteamNetworkingSockets.SendMessageToConnection(conn, (IntPtr)pData, (uint)data.Length, Util.MirrorChannel2SendFlag(channelId), out _);
var result = SteamNetworkingSockets.SendMessageToConnection(conn, (IntPtr)(pData + segment.Offset), (uint)segment.Count, Util.MirrorChannel2SendFlag(channelId), out _);
_transport.OnServerDataSent?.Invoke(connectionId, segment, channelId);
}
}