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

Fix struct pack size #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Building

A managed assembly can be built using any available compiling platform that supports C# 3.0 or higher.

Must define `VALVE_CALLBACK_PACK_SMALL` on Linux/Apple/FreeBSD, or `VALVE_CALLBACK_PACK_LARGE` on other platforms.

Define `VALVESOCKETS_SPAN` to enable support for Span. Please, follow [these steps](https://github.com/nxrighthere/ValveSockets-CSharp/issues/3#issuecomment-491916163) to enable fast access to native memory blocks and improve performance.

Usage
Expand Down
30 changes: 22 additions & 8 deletions ValveSockets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public enum Result {
WGNetworkSendExceeded = 110
}

[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Address : IEquatable<Address> {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] ip;
Expand Down Expand Up @@ -289,7 +289,7 @@ public bool Equals(Address other) {
}
}

[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct Configuration {
public ConfigurationValue value;
public ConfigurationDataType dataType;
Expand All @@ -310,15 +310,26 @@ public struct ConfigurationData {
}
}

[StructLayout(LayoutKind.Sequential)]
static class StructConst
{
#if VALVE_CALLBACK_PACK_SMALL
public const int PACK_SIZE = 4;
#elif VALVE_CALLBACK_PACK_LARGE
public const int PACK_SIZE = 8;
#else
#error "Not define VALVE_CALLBACK_PACK_SMALL(Linux/Apple/FreeBSD) or VALVE_CALLBACK_PACK_LARGE(Anything else.)"
#endif
}

[StructLayout(LayoutKind.Sequential, Pack = StructConst.PACK_SIZE)]
public struct StatusInfo {
private const int callback = Library.socketsCallbacks + 1;
public Connection connection;
public ConnectionInfo connectionInfo;
private ConnectionState oldState;
}

[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential, Pack = StructConst.PACK_SIZE)]
public struct ConnectionInfo {
public NetworkingIdentity identity;
public long userData;
Expand All @@ -333,11 +344,12 @@ public struct ConnectionInfo {
public string endDebug;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string connectionDescription;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public int flags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 63)]
private uint[] reserved;
}

[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential, Pack = StructConst.PACK_SIZE)]
public struct ConnectionStatus {
public ConnectionState state;
public int ping;
Expand All @@ -356,7 +368,7 @@ public struct ConnectionStatus {
private uint[] reserved;
}

[StructLayout(LayoutKind.Explicit, Size = 136)]
[StructLayout(LayoutKind.Explicit, Size = 136, Pack = 1)]
public struct NetworkingIdentity {
[FieldOffset(0)]
public IdentityType type;
Expand All @@ -376,7 +388,7 @@ public void SetSteamID(ulong steamID) {
}
}

[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct NetworkingMessage {
public IntPtr data;
public int length;
Expand All @@ -390,6 +402,8 @@ public struct NetworkingMessage {
public int channel;
public int flags;
public long userData;
public ushort idxLane;
public ushort pad1__;

public void CopyTo(byte[] destination) {
if (destination == null)
Expand Down