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

Inconsistent message data #5

Open
AriaTheCurseling opened this issue Aug 14, 2024 · 1 comment
Open

Inconsistent message data #5

AriaTheCurseling opened this issue Aug 14, 2024 · 1 comment

Comments

@AriaTheCurseling
Copy link
Owner

Describe the bug
Seemingly randomly receives the wrong value
image

Calling Code

// Is not consistent, but it seems to happen about 1 in 20 times.
void BUG() {
        server = SteamNetworkingSockets.CreateRelaySocket<SocketManager>(0);
        manager = SteamNetworkingSockets.ConnectRelay<ConnectionManager>(SteamClient.SteamId, 0);

        server.onConnecting += OnServerConnectionAttempt;
        server.onMessage += OnMessage;

		var data = MemoryMarshal.AsBytes<int>(new int[1] {42});

		manager.Connection.SendMessage(data);
}

void OnMessage(ReadOnlySpan<byte> data, Connection connection, NetIdentity identity, long messageNum, long recvTime, int channel){
        int value = MemoryMarshal.Read<int>(data);

        UnityEngine.Debug.Log($"Received message: Message data size: {data.Length}, {value}");
}
@AriaTheCurseling
Copy link
Owner Author

AriaTheCurseling commented Aug 14, 2024

Issue located:
GCHandle.Alloc in NetMsg forces a move of the data, but C# only moves the first byte of the span.

The issue is fully reproducible when using a value that requires multiple bytes to store.

Possible solutions:

  • Send T[] instead of Span
  • Use fixed instead of addrOfFixedObject to prevent the move
  • The Memory may also work as a replacement for Span, as it is designed for thread safety

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

No branches or pull requests

1 participant