Exemplo n.º 1
0
        /// <summary>
        /// Find all markers in the datagram
        /// </summary>
        /// <param name="data"></param>
        /// <param name="markers"></param>
        /// <returns></returns>
        private static FoundMarker FindDatagramMarker(IEnumerable <byte> data, IEnumerable <Tuple <byte[], Marker> > markers)
        {
            var patternMatchResults = new List <FoundMarker>();
            var aCollection         = data as byte[] ?? data.ToArray();
            var firstOffset         = 0;

            foreach (var marker in markers)
            {
                var offset = marker.Item2.OffsetInStream;
                var result = PatternMatch <byte> .MatchOrMatchPartiallyAtEnd(aCollection, marker.Item1, firstOffset + offset);

                if (!result.NoMatch)
                {
                    if (!patternMatchResults.Any())
                    {
                        firstOffset = result.MatchPos - offset;
                    }
                    patternMatchResults.Add(new FoundMarker(marker.Item1, result, marker.Item2));
                }
                else
                {
                    break; //if we have no marker, then
                }
            }

            if (patternMatchResults.Count == 0)
            {
                return(null);
            }

            if (patternMatchResults.Count > 1)
            {
                // sort results: first the full matches then the partial matches, then by position
                patternMatchResults.Sort((m1, m2) =>
                {
                    if (m1.Result.FullMatch && !m2.Result.FullMatch)
                    {
                        return(-1);
                    }
                    if (m2.Result.FullMatch && !m1.Result.FullMatch)
                    {
                        return(1);
                    }
                    return(m1.Result.MatchPos - m2.Result.MatchPos);
                });
            }

            return(patternMatchResults.First());
        }