Esempio n. 1
0
        public static VisualBubble FetchNewestBubbleIfNotWaiting(Stream stream, Service service,
                                                                 int searchDepth = 10)
        {
            VisualBubble firstBubble = null;

            lock (OperationLock)
            {
                stream.Seek(stream.Length, SeekOrigin.Begin);

                for (var i = 0; i < searchDepth; i++)
                {
                    if (stream.Position == 0)
                    {
                        break;
                    }

                    byte[] headerBytes;
                    int    headerBytesLength;
                    int    endPosition;

                    BubbleGroupDatabasePrimitives.ReadBubbleHeaderType(stream, out headerBytes,
                                                                       out headerBytesLength, out endPosition);

                    var guid = BubbleGroupDatabasePrimitives.ReadBubbleHeaderStruct(headerBytes, headerBytesLength,
                                                                                    endPosition + 1, out endPosition);

                    var bubbleData = BubbleGroupDatabasePrimitives.ReadBubbleData(stream);

                    var visualBubble = Deserialize(bubbleData);
                    if (visualBubble == null)
                    {
                        continue;
                    }

                    // if any of the bubbles are waiting, then make sure to perform a full load
                    if (!(visualBubble is NewBubble) &&
                        (visualBubble.Status == Bubble.BubbleStatus.Waiting && visualBubble.Direction == Bubble.BubbleDirection.Outgoing))
                    {
                        return(null);
                    }

                    if (firstBubble == null)
                    {
                        visualBubble.Service = service;
                        visualBubble.ID      = guid;
                        firstBubble          = visualBubble;
                    }
                }
            }

            return(firstBubble);
        }
Esempio n. 2
0
        public static long FetchBubblesOnDay(BubbleGroup group, Stream stream, Action <VisualBubble> updateCallback,
                                             int day, long cursor = -1, string[] bubbleTypes = null, Func <VisualBubble, bool> comparer = null)
        {
            var lowerTime = Time.GetNowUnixTimestamp() - (day * 86400);
            var upperTime = lowerTime + 86400 + 600;

            lock (OperationLock)
            {
                stream.Seek(cursor == -1 ? stream.Length : cursor, SeekOrigin.Begin);
                long streamPosition;

                while (true)
                {
                    streamPosition = stream.Position;

                    if (stream.Position == 0)
                    {
                        return(-2);
                    }

                    var    found = false;
                    byte[] headerBytes;
                    int    headerBytesLength;
                    int    endPosition;

                    if (bubbleTypes == null)
                    {
                        BubbleGroupDatabasePrimitives.ReadBubbleHeader(stream, out headerBytes, out headerBytesLength);
                        BubbleGroupDatabasePrimitives.FindBubbleHeaderDelimiter(headerBytes, headerBytesLength, 0, out endPosition);
                        found = true;
                    }
                    else
                    {
                        var bubbleType = BubbleGroupDatabasePrimitives.ReadBubbleHeaderType(stream, out headerBytes,
                                                                                            out headerBytesLength, out endPosition);
                        for (var x = 0; x < bubbleTypes.Length; x++)
                        {
                            if (bubbleTypes[x] != bubbleType)
                            {
                                continue;
                            }
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        var guid = BubbleGroupDatabasePrimitives.ReadBubbleHeaderStruct(headerBytes, headerBytesLength,
                                                                                        endPosition + 1, out endPosition);
                        var time = BubbleGroupDatabasePrimitives.AsciiBytesToString(headerBytes, endPosition + 1,
                                                                                    headerBytesLength);
                        long longTime;
                        Int64.TryParse(time, out longTime);

                        if (longTime < lowerTime)
                        {
                            break;
                        }

                        if (longTime > upperTime)
                        {
                            BubbleGroupDatabasePrimitives.JumpBubbleData(stream);
                            continue;
                        }

                        var bubbleData = BubbleGroupDatabasePrimitives.ReadBubbleData(stream);

                        var visualBubble = Deserialize(bubbleData);
                        if (visualBubble == null)
                        {
                            continue;
                        }
                        if (comparer != null && !comparer(visualBubble))
                        {
                            continue;
                        }
                        visualBubble.Service = @group.Service;
                        visualBubble.ID      = guid;
                        visualBubble.BubbleGroupReference = @group;
                        updateCallback(visualBubble);
                    }
                    else
                    {
                        BubbleGroupDatabasePrimitives.JumpBubbleData(stream);
                    }
                }

                return(streamPosition);
            }
        }