Beispiel #1
0
        public static byte[] GetBytesToSend(PackageData package)
        {
            var deviceId = package.DeviceId;

            if (string.IsNullOrEmpty(deviceId))
            {
                return(new byte[] {});
            }

            var payloadBytes       = Encoding.UTF8.GetBytes(deviceId);
            var payloadLengthBytes = BitConverter.GetBytes(payloadBytes.Length);

            payloadBytes = payloadLengthBytes.Concat(payloadBytes).ToArray();

            return(payloadBytes);
        }
Beispiel #2
0
        public static bool TryGetValue(byte[] bytes, out PackageData value)
        {
            if (bytes?.Any() != true)
            {
                value = PackageData.Empty;
                return(false);
            }

            var sizeFlagBytes = sizeof(int);
            var length        = BitConverter.ToInt32(bytes.Take(sizeFlagBytes).ToArray(), 0);

            if (bytes.Length - sizeFlagBytes != length)
            {
                value = PackageData.Empty;
                return(false);
            }

            var encoded = Encoding.UTF8.GetString(bytes.Skip(sizeFlagBytes).ToArray());

            value = new PackageData(encoded);
            return(true);
        }