How to read arbitrary length of ByteArray from token?
Options
SPF
✭
in Photon Bolt
I'm trying send event with token, with complex data. This is what I do:
"var objectBytes = packet.ReadByteArray(5);" This line uses 5 as example, but actually the length of the ByteArray is not fixed. So how should I read arbitrary length of ByteArray from token?
using UnityEngine; using Bolt; using UdpKit; //using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.IO; public class ListOfIntToken : IProtocolToken { public List<int> intList; public void Read(UdpPacket packet) { var objectBytes = packet.ReadByteArray(5); var mStream = new MemoryStream(); var binFormatter = new BinaryFormatter(); mStream.Write(objectBytes, 0, objectBytes.Length); mStream.Position = 0; var myObject = binFormatter.Deserialize(mStream) as List<int>; } public void Write(UdpPacket packet) { var binFormatter = new BinaryFormatter(); var mStream = new MemoryStream(); binFormatter.Serialize(mStream, intList); //byte[] bytes = userId.Select(x => (byte)x).ToArray(); packet.WriteByteArray(mStream.ToArray()); } }
"var objectBytes = packet.ReadByteArray(5);" This line uses 5 as example, but actually the length of the ByteArray is not fixed. So how should I read arbitrary length of ByteArray from token?
0
Comments
-
Besides the main question, I'm not very familiar with Bytes. So for a List of data, or Dictionary of data, or custom class, how should I decide the size to pass on to "ReadByteArray()"?0
-
UdpPacket has a ton of methods/extension methods for writing binary data directly into the packet stream. And understanding what a byte is, is kind of an entry level requirement for networking.0
-
@DirtyHippy That's for sure. I understand what byte is and basic theory related staff. I'm just looking for more Bolt related docs and examples that I can study from.0
-
@DirtyHippy I changed the codes to the following, could you kindly check on it please? It works but there's a little issue. I have two clients running A and B. A is server. Both are sending the event with this token but with different data for testing. In `Write` method I print the `byteArraySize` out, and when the data are received on server I print them out too. The `byteArraySize` for A's data is 0, and the time it's printed is before the printing line in`Write` method, where the size was 221. However for B's data the size was correct. What may causes this problem?
using System.Collections; using System.Collections.Generic; using UnityEngine; using Bolt; using UdpKit; //using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.IO; public class ListOfIntToken : IProtocolToken { public List<int> intList; public int byteArraySize; public void Read(UdpPacket packet) { byteArraySize = packet.ReadInt(); var objectBytes = packet.ReadByteArray(byteArraySize); var mStream = new MemoryStream(); var binFormatter = new BinaryFormatter(); mStream.Write(objectBytes, 0, objectBytes.Length); mStream.Position = 0; intList = binFormatter.Deserialize(mStream) as List<int>; } public void Write(UdpPacket packet) { var binFormatter = new BinaryFormatter(); var mStream = new MemoryStream(); binFormatter.Serialize(mStream, intList); //byte[] bytes = userId.Select(x => (byte)x).ToArray(); var byteArray = mStream.ToArray(); byteArraySize = byteArray.Length; packet.WriteInt(byteArraySize); packet.WriteByteArray(byteArray); } }
0