Exemplo n.º 1
0
        /// <summary>
        /// Constructs a <see cref="Packet"/> from a byte array
        /// </summary>
        /// <param name="bytes"><see cref="Packet"/> as byte array</param>
        /// <returns>
        /// <see cref="Packet"/> if deserialization was successful, else null
        /// </returns>
        public static Packet Deserialize(byte[] bytes)
        {
            BytesReader reader = new BytesReader(bytes);

            var flag   = reader.ReadString();
            var packet = new Packet();

            if (flag.Equals("PACKET_DATA"))
            {
                try {
                    packet.Tag = reader.ReadString();

                    var payloadLen = bytes.Length - reader.Index;
                    packet.Payload = reader.ReadBytes(payloadLen);
                }
                catch (Exception e) {
                    var msg = "Packet deserialization error: " + e.Message;
                    UnityEngine.Debug.LogError(msg);
                    packet = null;
                }
            }
            else
            {
                packet = null;
            }

            return(packet);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a <see cref="Message"/> from a byte array
        /// </summary>
        /// <param name="bytes">The byte array representing the data</param>
        /// <returns>
        /// <see cref="Message"/> if deserialization was successful, else null
        /// </returns>
        public static Message Deserialize(byte[] bytes)
        {
            BytesReader reader = new BytesReader(bytes);

            var message = new Message();
            var flag    = reader.ReadString();

            if (flag.Equals("MESSAGE_DATA"))
            {
                try {
                    message.sender     = reader.ReadShort();
                    message.recipients = reader.ReadShortArray();
                    message.bytes      = reader.ReadByteArray();
                }
                catch (Exception e) {
                    var msg = "Message deserialization error: " + e.Message;
                    UnityEngine.Debug.LogError(msg);
                    message = null;
                }
            }
            else
            {
                message = null;
            }

            return(message);
        }