Jump to content

Inconsistent Unmanaged Memory C# behaviour

Guest
Go to solution Solved by Guest,

I'm very drunk but something along the lines of
 

IntPtr ptrData = Marshal.AllocHGlobal(Marshal.SizeOf(clientToServerControllerInputPacket));
        Marshal.StructureToPtr(clientToServerControllerInputPacket, ptrData, false);//

fixed it somewhere in the code with the pointer thingy...

Whaddup:
 

void MessageServerControllerInput(ClientToServerControllerInput input, bool pressed) {
        ClientToServerControllerInputPacket clientToServerControllerInputPacket = new ClientToServerControllerInputPacket();
        
        clientToServerControllerInputPacket.packet = PacketType.ClientToServerControllerInput;
        clientToServerControllerInputPacket.input = input;
        clientToServerControllerInputPacket.pointInTime = 0f;
        clientToServerControllerInputPacket.pressed = pressed;

        IntPtr ptrData = Marshal.AllocHGlobal(Marshal.SizeOf(clientToServerControllerInputPacket));
        int sizeOfMessage = Marshal.SizeOf<ClientToServerControllerInputPacket>(clientToServerControllerInputPacket);
        NetworkManager.instance.currentConnectionToServer.MessageServerData(ptrData, sizeOfMessage);

        ClientToServerControllerInputPacket clientToServerControllerInputPacket1 = (ClientToServerControllerInputPacket)Marshal.PtrToStructure(ptrData, typeof(ClientToServerControllerInputPacket));
        GD.Print("Testing SendingDatum Packet not networked: Pressed?: " + clientToServerControllerInputPacket1.pressed + " Button: " + clientToServerControllerInputPacket1.input.ToString());
        GD.Print("Real Data: Pressed?: " + clientToServerControllerInputPacket.pressed + " Button: " + clientToServerControllerInputPacket.input.ToString());
        Marshal.FreeHGlobal(ptrData);
    }


So, I am working with the "Godot" Game Engine using C#, however, the code I am using is C#. The only Godot commands are the Print statements which are self-explanatory.

Additionally the

ClientToServerControllerInput

type is an enumeration. There are no problems with this enumeration.

 

Problem:
I am obligated to send a structure of data as an IntPtr because I'm working with Steamworks (Steam's multiplayer networking solution) and I am trying to figure out why data is inconsistent when received by the server.
I decided to "de-serialize" the data so to speak without a network transmission to eliminate network transmission as a cause.

This data was then producing inconsistent "pressed" values.
Basically, there's an event system that says "Was this key just pressed? true/false" which works perfectly." (Which is repeatable with the "Real Data" print statement)
Something is up with reading the IntPtr that makes it read inconsistently.
I have the data in the same spot every time as far as I can tell based on how I wrote the structure...

public struct ClientToServerControllerInputPacket {
    public PacketType packet;
    public ClientToServerControllerInput input;
    public float pointInTime;
    /// <summary>
    /// Pressed = True when just pressed. 
    /// Pressed = False when just released.
    /// </summary>
    public bool pressed; 

}


I also decided to print out the packet type and that seems to be inconsistent as well...

the variable types are not variable in length to my knowledge... despite this, the "packet" would still have zero offset in memory since it's the first element... right?
The "ClientToServerControllerInput" is a byte enumeration so should only take 8 bits of memory.
 

public enum ClientToServerControllerInput : byte { 
    MoveForward,
    MoveBackward,
    MoveLeft,
    MoveRight,
    LeanLeft,
    LeanRight,
    Shoot
}

Floats are 4 bytes of data... maybe decimals in floats can move but... after I commented it out, the consistency of packet data became more erratic. The "pressed" variable was no more consistent than it was before... so if anything, removing the float made data less consistent.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm very drunk but something along the lines of
 

IntPtr ptrData = Marshal.AllocHGlobal(Marshal.SizeOf(clientToServerControllerInputPacket));
        Marshal.StructureToPtr(clientToServerControllerInputPacket, ptrData, false);//

fixed it somewhere in the code with the pointer thingy...

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×