示例#1
0
 /// <inheritdoc />
 protected override byte[] EncodeInternal()
 {
     return(ByteArrayHelper.Combine(
                ByteArrayHelper.ToBytes(_node.ConnectionName),
                ByteArrayHelper.ToBytes(_start.ToString()),
                ByteArrayHelper.ToBytes(_end.ToString())));
 }
示例#2
0
 /// <inheritdoc />
 protected override byte[] EncodeInternal()
 {
     return(ByteArrayHelper.Combine(
                ByteArrayHelper.ToBytes(_start1.ToString()),
                ByteArrayHelper.ToBytes(_end1.ToString()),
                ByteArrayHelper.ToBytes(_start2.ToString()),
                ByteArrayHelper.ToBytes(_end2.ToString())));
 }
示例#3
0
        /// <inheritdoc />
        protected override byte[] EncodeInternal()
        {
            byte[] typeBytes     = ByteArrayHelper.ToBytes((int)_type);
            byte[] nameBytes     = ByteArrayHelper.ToBytes(_name);
            byte[] portBytes     = ByteArrayHelper.ToBytes(_port);
            byte[] settingsBytes = ByteArrayHelper.ToBytes(_settings);
            byte[] primaryBytes  = ByteArrayHelper.ToBytes(_primary);

            return(ByteArrayHelper.Combine(typeBytes, nameBytes, portBytes, settingsBytes, primaryBytes));
        }
示例#4
0
        public async Task Serialize(ListNode head, Stream s)
        {
            Debug.Assert(head.Previous == null, "Head node should be passed to this method.");
            var nodePacker = new ListNodePacker();
            var tasks      = GetAllNodes(head)
                             .Select(nodePacker.ToBytesAsync);
            var conversionTask = await Task.WhenAll(tasks);

            var bytes = ByteArrayHelper.Combine(conversionTask);

            s.Write(bytes);
            s.Position = 0;
        }
示例#5
0
        public void Add(byte[] packet)
        {
            if ((packet == null) || (packet.Length == 0))
            {
                return;
            }

            if (buffer == null)
            {
                buffer = (byte[])packet.Clone();
            }
            else
            {
                buffer = ByteArrayHelper.Combine(buffer, packet);
            }

            Parse();
        }
示例#6
0
        public void Add(byte[] ciid, byte[] cRefID, byte[] buffer)
        {
            if (buffer == null)
            {
                return;
            }

            DownloadRecord dr = Find(ciid, cRefID);

            if (dr != null)
            {
                if (dr.Buffer != null)
                {
                    dr.Buffer = ByteArrayHelper.Combine(dr.Buffer, buffer);
                }
                else
                {
                    dr.Buffer = buffer;
                }
            }
        }
示例#7
0
 /// <inheritdoc />
 protected override byte[] EncodeInternal()
 {
     return(ByteArrayHelper.Combine(ByteArrayHelper.ToBytes(_result)));
 }
示例#8
0
 /// <summary>
 /// Encodes the message data into a byte array.
 /// </summary>
 /// <returns>The encoded data as a byte array.</returns>
 public byte[] Encode()
 {
     return(ByteArrayHelper.Combine(BitConverter.GetBytes(GetMessageTypeId()), EncodeInternal()));
 }
示例#9
0
        /// <summary>
        /// Processes TCP responses.
        /// </summary>
        /// <param name="buffer">Raw response data without TCP header.</param>
        /// <returns>Port number (if available).</returns>
        public Pair <int, List <WaveMessage> > Process(byte[] buffer)
        {
            int port = -1;
            List <WaveMessage> res = null;

            using (MemoryStream mem = new MemoryStream(buffer))
            {
                if (isFirst)
                {
                    isFirst = false;

                    mem.ReadByte();
                    mem.ReadByte();

                    port = mem.ReadShort();
                }

                if (port != 0)
                {
                    // reading eveything
                    while (1 == 1)
                    {
                        int  messageSize     = unfinishedRemainder;
                        bool isTransactional = unfinishedTransactional;

                        if (tempStore == null)
                        {
                            int byte1 = mem.ReadByte();
                            int byte2 = mem.ReadByte();

                            if ((byte1 == -1) || (byte2 == -1))
                            {
                                break;
                            }

                            messageSize     = byte1 | ((byte2 & 0x7F) << 8);
                            isTransactional = ((byte2 & 0x80) != 0x80);
                        }

                        // try reading the raw data of the message
                        byte[] msg = new byte[messageSize];

                        int readResult = mem.Read(msg, 0, msg.Length);

                        if (readResult == messageSize)
                        {
                            // everything is fine
                            byte[] chunk = null;

                            if (tempStore != null)
                            {
                                chunk     = ByteArrayHelper.Combine(tempStore, msg);
                                tempStore = null;
                            }
                            else
                            {
                                chunk = msg;
                            }

                            if (chunk != null)
                            {
                                // decrypt if needed
                                if ((Core.Network.TransactionalEncryption != null) && (Core.Network.StreamingEncryption != null))
                                {
                                    if (isTransactional)
                                    {
                                        Core.Network.TransactionalEncryption.Decrypt(chunk, chunk.Length);
                                    }
                                    else
                                    {
                                        Core.Network.StreamingEncryption.Decrypt(chunk, chunk.Length);
                                    }
                                }

                                // parse away
                                List <WaveMessage> parsedMessages = ParseBucket(chunk);

                                if (parsedMessages != null)
                                {
                                    if (res != null)
                                    {
                                        res.AddRange(parsedMessages);
                                    }
                                    else
                                    {
                                        res = parsedMessages;
                                    }
                                }
                            }

                            // reset temporary variabless
                            unfinishedRemainder     = 0;
                            unfinishedTransactional = false;
                            tempStore = null;
                        }
                        else
                        {
                            // not enough data received
                            int remainder = messageSize - readResult;

                            if (remainder > 0)
                            {
                                byte[] chunk = new byte[readResult];
                                Buffer.BlockCopy(msg, 0, chunk, 0, readResult);

                                if (tempStore != null)
                                {
                                    tempStore = ByteArrayHelper.Combine(tempStore, chunk);
                                }
                                else
                                {
                                    tempStore = chunk;
                                }

                                // set temporary variabless
                                unfinishedRemainder     = remainder;
                                unfinishedTransactional = isTransactional;
                            }

                            DebugHelper.Trace("Incomplete TCP packet: {0} bytes remaining.", unfinishedRemainder);
                            break;
                        }
                    }
                }
            }

            return(new Pair <int, List <WaveMessage> >(port, res));
        }