summaryrefslogtreecommitdiff
path: root/indra/llmessage/llpacketring.h
blob: 5315e5a5bda68242e4076dafc55ec0667608f5eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * @file llpacketring.h
 * @brief LLPacketRing: a simple ring buffer for LLPacketBuffers.
 *
 * LLPacketRing stores incoming UDP packets that have already been received
 * from the network socket. It has no socket or proxy awareness; callers
 * push packets in with pushPacket() and retrieve them in FIFO order with
 * popPacket().
 *
 * The ring starts at DEFAULT_BUFFER_RING_SIZE slots and grows in increments
 * of BUFFER_RING_EXPANSION up to MAX_BUFFER_RING_SIZE.  Once at the ceiling,
 * pushPacket() silently overwrites the oldest queued packet to make room for
 * the incoming one.
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#pragma once

#include <vector>

#include "llpacketbuffer.h"


class LLPacketRing
{
public:
    LLPacketRing();
    ~LLPacketRing();

    // Copy 'packet' onto the tail of the ring, growing the ring or
    // overwriting the oldest entry when the ring is at capacity.
    void pushPacket(const LLPacketBuffer& packet);

    // Copy the head (oldest) packet into 'packet'.
    // Returns true if a packet was available, false if the ring was empty.
    bool popPacket(LLPacketBuffer& packet);

    S32 getNumBufferedPackets() const { return (S32)(mNumBufferedPackets); }
    S32 getNumBufferedBytes()   const { return mNumBufferedBytes; }

    // Ratio of buffered packets to DEFAULT_BUFFER_RING_SIZE (0 = empty, 1 = nominal full).
    F32 getBufferLoadRate() const;

private:
    // Returns true if the ring was expanded, false if already at the ceiling.
    bool expandRing();

    std::vector<LLPacketBuffer*> mRing;
    S16 mHeadIndex          { 0 };
    S16 mNumBufferedPackets { 0 };
    S32 mNumBufferedBytes   { 0 };
};