mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-03-14 22:20:51 +00:00
use pinned handle for interop
This commit is contained in:
parent
372ccc9906
commit
1acebc44cd
@ -90,16 +90,11 @@ public class Client
|
||||
|
||||
public void Send(ArraySegment<byte> segment, int channelId)
|
||||
{
|
||||
// use pointer to managed array instead of making copy. seems to work okay
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* pData = segment.Array)
|
||||
{
|
||||
var result = SteamNetworkingSockets.SendMessageToConnection(_conn, (IntPtr)(pData + segment.Offset), (uint)segment.Count, Util.MirrorChannel2SendFlag(channelId), out _);
|
||||
if (result != EResult.k_EResultOK) _transport.Log($"[warn] send returned {result}");
|
||||
_transport.OnClientDataSent?.Invoke(segment, channelId);
|
||||
}
|
||||
}
|
||||
var handle = GCHandle.Alloc(segment.Array, GCHandleType.Pinned); // prevent moving or gc when passing to native function
|
||||
var result = SteamNetworkingSockets.SendMessageToConnection(_conn, handle.AddrOfPinnedObject() + segment.Offset, (uint)segment.Count, Util.MirrorChannel2SendFlag(channelId), out _);
|
||||
handle.Free();
|
||||
if (result != EResult.k_EResultOK) _transport.Log($"[warn] send returned {result}");
|
||||
_transport.OnClientDataSent?.Invoke(segment, channelId);
|
||||
}
|
||||
|
||||
public void Receive()
|
||||
|
@ -85,16 +85,11 @@ public class Server
|
||||
{
|
||||
var conn = new HSteamNetConnection((uint)connectionId);
|
||||
|
||||
// use pointer to managed array instead of making copy. seems to work okay
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* pData = segment.Array)
|
||||
{
|
||||
var result = SteamNetworkingSockets.SendMessageToConnection(conn, (IntPtr)(pData + segment.Offset), (uint)segment.Count, Util.MirrorChannel2SendFlag(channelId), out _);
|
||||
if (result != EResult.k_EResultOK) _transport.Log($"[warn] send {conn.ToDebugString()} returned {result}");
|
||||
_transport.OnServerDataSent?.Invoke(connectionId, segment, channelId);
|
||||
}
|
||||
}
|
||||
var handle = GCHandle.Alloc(segment.Array, GCHandleType.Pinned); // prevent moving or gc when passing to native function
|
||||
var result = SteamNetworkingSockets.SendMessageToConnection(conn, handle.AddrOfPinnedObject() + segment.Offset, (uint)segment.Count, Util.MirrorChannel2SendFlag(channelId), out _);
|
||||
handle.Free();
|
||||
if (result != EResult.k_EResultOK) _transport.Log($"[warn] send {conn.ToDebugString()} returned {result}");
|
||||
_transport.OnServerDataSent?.Invoke(connectionId, segment, channelId);
|
||||
}
|
||||
|
||||
public void Receive()
|
||||
|
@ -2,6 +2,7 @@
|
||||
using Steamworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SteamTransport;
|
||||
|
||||
@ -69,6 +70,25 @@ public static class Util
|
||||
// 50% change of doing all, delay .5 seconds
|
||||
if (transport.DoFakeNetworkErrors)
|
||||
{
|
||||
var floatHandle = GCHandle.Alloc((float)50, GCHandleType.Pinned);
|
||||
var intHandle = GCHandle.Alloc((int)500, GCHandleType.Pinned);
|
||||
|
||||
// global scope = dont apply to connection
|
||||
SteamNetworkingUtils.SetConfigValue(
|
||||
ESteamNetworkingConfigValue.k_ESteamNetworkingConfig_FakePacketLoss_Send,
|
||||
ESteamNetworkingConfigScope.k_ESteamNetworkingConfig_Global,
|
||||
IntPtr.Zero,
|
||||
ESteamNetworkingConfigDataType.k_ESteamNetworkingConfig_Float,
|
||||
floatHandle.AddrOfPinnedObject()
|
||||
);
|
||||
SteamNetworkingUtils.SetConfigValue(
|
||||
ESteamNetworkingConfigValue.k_ESteamNetworkingConfig_FakePacketLoss_Recv,
|
||||
ESteamNetworkingConfigScope.k_ESteamNetworkingConfig_Global,
|
||||
IntPtr.Zero,
|
||||
ESteamNetworkingConfigDataType.k_ESteamNetworkingConfig_Float,
|
||||
floatHandle.AddrOfPinnedObject()
|
||||
);
|
||||
/*
|
||||
result.Add(new SteamNetworkingConfigValue_t
|
||||
{
|
||||
m_eValue = ESteamNetworkingConfigValue.k_ESteamNetworkingConfig_FakePacketLoss_Send,
|
||||
@ -87,6 +107,7 @@ public static class Util
|
||||
m_float = 50
|
||||
}
|
||||
});
|
||||
|
||||
result.Add(new SteamNetworkingConfigValue_t
|
||||
{
|
||||
m_eValue = ESteamNetworkingConfigValue.k_ESteamNetworkingConfig_FakePacketLag_Send,
|
||||
@ -105,6 +126,7 @@ public static class Util
|
||||
m_int32 = 500
|
||||
}
|
||||
});
|
||||
|
||||
result.Add(new SteamNetworkingConfigValue_t
|
||||
{
|
||||
m_eValue = ESteamNetworkingConfigValue.k_ESteamNetworkingConfig_FakePacketReorder_Send,
|
||||
@ -132,6 +154,7 @@ public static class Util
|
||||
m_int32 = 500
|
||||
}
|
||||
});
|
||||
|
||||
result.Add(new SteamNetworkingConfigValue_t
|
||||
{
|
||||
m_eValue = ESteamNetworkingConfigValue.k_ESteamNetworkingConfig_FakePacketDup_Send,
|
||||
@ -159,7 +182,11 @@ public static class Util
|
||||
m_int32 = 500
|
||||
}
|
||||
});
|
||||
*/
|
||||
floatHandle.Free();
|
||||
intHandle.Free();
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ public static class Program
|
||||
transport.TestIpAddress = "127.0.0.1:1234";
|
||||
// make timeout for client detecting server drop different than timeout for server detecting client drop
|
||||
transport.Timeout = 5000;
|
||||
transport.DoFakeNetworkErrors = true;
|
||||
transport.DoFakeNetworkErrors = false;
|
||||
|
||||
transport.OnServerError = (conn, error, s) => Console.Error.WriteLine($"ERROR {conn} {error} {s}");
|
||||
var theConn = -1;
|
||||
@ -122,7 +122,7 @@ public static class Program
|
||||
transport.Log = Console.WriteLine;
|
||||
transport.TestIpAddress = "127.0.0.1:1234";
|
||||
transport.Timeout = 20000;
|
||||
transport.DoFakeNetworkErrors = false;
|
||||
transport.DoFakeNetworkErrors = true;
|
||||
|
||||
transport.OnClientError = (error, s) => Console.Error.WriteLine($"ERROR {error} {s}");
|
||||
transport.OnClientDataReceived = (bytes, i) => Console.WriteLine($"RECV {bytes.Join()} {i}");
|
||||
|
Loading…
x
Reference in New Issue
Block a user