Example #1
0
        internal byte[] GetPayloadBytes()
        {
            if (_token == null)
            {
                throw new Exception("Token is missing");
            }

            if (_deviceId == null)
            {
                throw new Exception("DeviceID is missing");
            }

            // Encrypt the payload using the token
            var jsonBody         = CreateJson();
            var encryptedPayload = AESHelper.Encrypt(_token.Key, _token.InitializationVector, jsonBody);

            using (var content = new ByteList())
            {
                // Create the header
                content.Add(0x21, 0x31);                                // Magic number
                content.WriteUInt16BE(32 + encryptedPayload.Length);    // Content+header length
                content.Repeat(0x00, 4);                                // Unknown1
                content.AddRange(_deviceId);                            // Device ID (received from handshake)

                // Add seconds passed to server timestamp
                var secondsPassed = (DateTime.Now - _serverStamp.ReceivedTime).TotalSeconds;

                // Write Stamp to payload
                content.WriteUInt32BE((UInt32)Math.Floor(_serverStamp.Timestamp + secondsPassed));

                // Header is done and we have the payload and the token. We can now write the MD5 checksum
                // Get MD5 checksum of [header, token payload]
                content.AddRange(ByteList.Join(content.Take(16).ToArray(), _token.Token, encryptedPayload).ToMd5());

                // Then add the encrypted payload itself
                content.AddRange(encryptedPayload);

                return(content.ToBinaryASCIIArray());
            }
        }
 internal void InserByteArray(byte[] data)
 {
     if (SecondIn)
     {
         ByteList.Clear();
         SecondIn = false;
     }
     if (FirstIn)
     {
         SecondIn = true;
         FirstIn  = false;
     }
     ByteList.AddRange(data);
 }
Example #3
0
        public bool InsertByteArray(byte[] Data, int ml, out List <byte[]> datax)
        {
            lock (locklist)
            {
                datax = new List <byte[]>();

                ByteList.AddRange(Data);

                Interlocked.Add(ref Vlent, Data.Length);


                if (lengt == -1 && Vlent > ml)
                {
                    int res = 0;

                    for (int i = 0; i < ml; i++)
                    {
                        int temp = ((int)ByteList[current + i]) & 0xff;
                        temp <<= i * 8;
                        res    = temp + res;
                    }

                    if (res > MaxSize)
                    {
                        Reset();
                        throw new Exception("数据包大于预设长度,如果你传入的数据比较大,请设置重新 maxSize 值");
                    }

                    if (res <= 0)
                    {
                        Reset();

                        return(false);
                    }

                    Interlocked.Exchange(ref lengt, res);
                }


                if ((Vlent - current) >= lengt)
                {
                    int lengx = lengt;
                    Interlocked.Exchange(ref lengt, -1);

                    byte[] data = new byte[lengx];
                    ByteList.CopyTo(current, data, 0, lengx);
                    datax.Add(data);

                    Interlocked.Add(ref current, lengx);


recopy:

                    if (current == ByteList.Count)
                    {
                        Reset();
                        return(true);
                    }

                    if (ByteList.Count - current > ml)
                    {
                        int res = 0;
                        for (int i = 0; i < ml; i++)
                        {
                            int temp = ((int)ByteList[current + i]) & 0xff;
                            temp <<= i * 8;
                            res    = temp + res;
                        }

                        if (res > MaxSize)
                        {
                            Reset();
                            throw new Exception("数据包大于预设长度,如果你传入的数据比较大,请设置重新 maxSize 值");
                        }

                        if (res <= 0)
                        {
                            Reset();
                            return(true);
                        }

                        if (ByteList.Count - current < res)
                        {
                            return(true);
                        }
                        data = new byte[res];
                        ByteList.CopyTo(current, data, 0, res);
                        datax.Add(data);

                        Interlocked.Add(ref current, res);

                        goto recopy;
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Example #4
0
 public void InsertByteArray(byte[] Data)
 {
     ByteList.AddRange(Data);
 }