Esempio n. 1
0
        public SessionMessageBase AssembleFragment(SessionMessageBase message, uint sequenceNumber)
        {
            FragmentMessage fragment = message as FragmentMessage;

            SessionMessageType messageType = fragment.Header.SessionMessageType;
            int sequenceBegin = (int)fragment.SequenceBegin;
            int sequenceEnd   = (int)fragment.SequenceEnd;

            _fragmentQueue[(int)sequenceNumber] = fragment.Data;

            IEnumerable <int> neededSequences = Enumerable.Range(sequenceBegin, sequenceEnd - sequenceBegin);

            foreach (var seq in neededSequences)
            {
                if (!_fragmentQueue.ContainsKey(seq))
                {
                    return(null);
                }
            }

            EndianWriter writer = new EndianWriter();

            foreach (int seq in neededSequences)
            {
                byte[] data = _fragmentQueue[seq];
                writer.Write(data);
                _fragmentQueue.Remove(seq);
            }

            SessionMessageBase assembled = SessionMessageTransport.CreateFromMessageType(messageType);

            assembled.Deserialize(new EndianReader(writer.ToBytes()));
            return(assembled);
        }
        public SessionMessageBase AssembleFragment(SessionMessageBase message, uint sequenceNumber)
        {
            FragmentMessage fragment = message as FragmentMessage;

            SessionMessageType messageType = fragment.Header.SessionMessageType;
            int sequenceBegin = (int)fragment.SequenceBegin;
            int sequenceEnd   = (int)fragment.SequenceEnd;

            _fragmentQueue[(int)sequenceNumber] = fragment.Data;

            IEnumerable <int> neededSequences = Enumerable.Range(sequenceBegin, sequenceEnd - sequenceBegin);

            BEWriter writer = new BEWriter();

            foreach (int seq in neededSequences)
            {
                try
                {
                    byte[] data = _fragmentQueue[seq];
                    writer.Write(data);
                }
                catch (KeyNotFoundException)
                {
                    return(null);
                }
            }

            // Pop obsolete fragment data
            foreach (int seq in neededSequences)
            {
                _fragmentQueue.Remove(seq);
            }

            SessionMessageBase assembled = SessionMessageTransport.CreateFromMessageType(messageType);

            assembled.Deserialize(new BEReader(writer.ToBytes()));
            return(assembled);
        }