Panda3D

ringbuffer.I
1
2 /**
3 * This will get a pointer to the fist undelivered data in buffer
4 */
5 inline char * RingBuffer::GetMessageHead(void)
6 {
7 return _Buffer+_StartPos;
8 }
9 /**
10 * This will get the first writabe section of the buffer space
11 */
12 inline char * RingBuffer::GetBufferOpen(void)
13 {
14 return _Buffer+_EndPos;
15 }
16 /**
17 * Will force a compression of data // shift left to start position
18 */
19 inline void RingBuffer::ForceWindowSlide(void)
20 {
21 size_t len = AmountBuffered();
22 if(len > 0 && _StartPos != 0) // basic flush left..
23 {
24 memmove(_Buffer,GetMessageHead(),len);
25 _StartPos = 0;
26 _EndPos = len;
27 }
28 }
29 /**
30 * Will report the number of unread chars in buffer
31 */
32 inline size_t RingBuffer::AmountBuffered(void)
33 {
34 return _EndPos - _StartPos;
35 }
36
37
38 /**
39 * Will report amount of data that is contiguas that can be writen at the
40 * location returned by GetBufferOpen
41 */
42 inline size_t RingBuffer::BufferAvailabe(void)
43 {
44 return GetBufferSize() - _EndPos;
45 }
46
47
48 /**
49 * Throw away all inread information
50 */
51 void RingBuffer::ResetContent(void)
52 {
53 _StartPos = 0;
54 _EndPos = 0;
55 }
56 /**
57 *
58 */
59 inline RingBuffer::RingBuffer(size_t in_size) : MemBuffer(in_size)
60 {
61 _EndPos = 0;
62 _StartPos = 0;
63 }
64 /**
65 * Force a compress of the data
66 */
67 inline void RingBuffer::FullCompress(void)
68 {
69 if(_StartPos == _EndPos)
70 {
71 _StartPos = 0;
72 _EndPos = 0;
73 }
74 else
75 {
76 ForceWindowSlide();
77 }
78 }
79 /**
80 * Try and do a intelegent compress of the data space the algorithem is really
81 * stupid right know.. just say if i have read past 1/2 my space do a
82 * compress...Im open for sugestions
83 *
84
85 *
86 */
87 inline void RingBuffer::Compress(void)
88 {
89 if(_StartPos == _EndPos)
90 {
91 _StartPos = 0;
92 _EndPos = 0;
93 }
94 else if(_StartPos >= GetBufferSize() / 2)
95 {
96 ForceWindowSlide();
97 }
98 }
99 /**
100 * Adds Data to a ring Buffer Will do a compress if needed so pointers suplied
101 * by Get Call are no longer valide
102 *
103 */
104 inline bool RingBuffer::Put(const char * data, size_t len)
105 {
106 bool answer = false;
107
108 if(len > BufferAvailabe() )
109 Compress();
110
111 if(len <= BufferAvailabe() )
112 {
113 memcpy(GetBufferOpen(),data,len);
114 _EndPos += len;
115 answer = true;
116 }
117 return answer;
118 }
119 /**
120 *
121
122 *
123 */
124 inline bool RingBuffer::PutFast(const char * data, size_t len)
125 {
126 // no checking be carefull
127 memcpy(GetBufferOpen(),data,len); // should i be using memcopy..
128 _EndPos += len;
129 return true;
130 }
131
132 /**
133 * will copy the data .. false indicates not enogh data to read .. sorry...
134 *
135 */
136 inline bool RingBuffer::Get(char * data, size_t len)
137 {
138 bool answer = false;
139
140 if(len <= AmountBuffered() )
141 {
142 memcpy(data,GetMessageHead(),len);
143 _StartPos += len;
144 Compress();
145 answer = true;
146 }
147 return answer;
148 }
MemBuffer
Definition: membuffer.h:4
MemBuffer::GetBufferSize
size_t GetBufferSize(void) const
Access to the BUffer Size Information.
Definition: membuffer.I:106
RingBuffer::Compress
void Compress(void)
Try and do a intelegent compress of the data space the algorithem is really stupid right know.
Definition: ringbuffer.I:87
RingBuffer::Put
bool Put(const char *data, size_t len)
Adds Data to a ring Buffer Will do a compress if needed so pointers suplied by Get Call are no longer...
Definition: ringbuffer.I:104
RingBuffer::BufferAvailabe
size_t BufferAvailabe(void)
Will report amount of data that is contiguas that can be writen at the location returned by GetBuffer...
Definition: ringbuffer.I:42
RingBuffer::AmountBuffered
size_t AmountBuffered(void)
Will report the number of unread chars in buffer.
Definition: ringbuffer.I:32
RingBuffer::Get
bool Get(char *data, size_t len)
will copy the data .
Definition: ringbuffer.I:136
RingBuffer::ResetContent
void ResetContent(void)
Throw away all inread information.
Definition: ringbuffer.I:51
RingBuffer::FullCompress
void FullCompress(void)
Force a compress of the data.
Definition: ringbuffer.I:67