0

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

0

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.

OnZi12
  • 86
  • 3
  • thank you very much, such an unfortunate stupid mistake, I didn't check if it was surely taking 2 as a byte because of it working anyway – Mike Azurek Jul 12 '21 at 20:18
  • My pleasure, my friend. Don't be hard on yourself. It seems like a very reasonable mistake to make. I would expect the API to return what it receives to be the default behavior. The API designer apparently had choosen memory optimization as the default behavior, which makes API calls like ``ReadInt32``behave differently based on the size of the number sent. – OnZi12 Jul 12 '21 at 20:40