Search code examples
c#network-programminglidgren

Sending Int32 equal to 4, received as equal to 67108864


What's going on, I do this on the server:

var msg = Server.Api.CreateMessage();           
msg.Write(2);
msg.Write(FreshChunks.Count());
Server.Api.SendMessage(msg, peer.Connection, NetDeliveryMethod.ReliableUnordered);

then on the client it succesfuly reads the byte = 2 and the switch then routes to function which reads Int32 (FreshChunks.Count) which was equal 4 but when received it equals 67108864. I've tried with Int16-64 and UInt16-64, none of them work out the correct value.


Solution

  • Given that:

    • In your usage of msg.Write(2), the compiler reads the 2 as an int (Int32)
    • You mentioned that you "successfully read the byte = 2".

    It seems that one of these options is happening:

    1. msg.Write is writing only bytes that have at least one-bit set (=1) in them. (to save space)
    2. msg.Write is always casting the given argument to a byte.

    When asking for 4 bytes (Int32), You got: 0x04 00 00 00. The first byte is exactly the 4 you passed.
    It seems that when asking from msg.Read more bytes than it has (you requested 4bytes and it has only 1 due to msg.Write logic) It does one of these:

    1. Appends the remaining bytes with zeros
    2. Keeps on reading, and in your case, there were 3 0's bytes in the message's metadata that was returned to you.

    For solving your problem, you should read the documentation of the Write and Read methods and understand how they behave.