sources/network/bytestream.cpp

changeset 13
09dcaeaa216b
parent 11
cffa2777d917
child 18
56a1ac7d931b
equal deleted inserted replaced
12:8d0d1b368de0 13:09dcaeaa216b
29 */ 29 */
30 30
31 #include "bytestream.h" 31 #include "bytestream.h"
32 #include <string.h> 32 #include <string.h>
33 33
34 bool Bytestream::sink;
35
36 // ------------------------------------------------------------------------------------------------- 34 // -------------------------------------------------------------------------------------------------
37 // 35 //
38 Bytestream::Bytestream (unsigned long length) : 36 Bytestream::Bytestream (unsigned long length) :
39 m_data (nullptr) 37 m_data (nullptr)
40 { 38 {
120 m_writtenLength = 0; 118 m_writtenLength = 0;
121 } 119 }
122 120
123 // ------------------------------------------------------------------------------------------------- 121 // -------------------------------------------------------------------------------------------------
124 // 122 //
125 char Bytestream::read_byte (bool* ok) 123 METHOD
126 { 124 Bytestream::ensure_read_space (unsigned int bytes) -> void
127 *ok = bytes_left() > 0; 125 {
128 return *ok ? *m_cursor++ : -1; 126 if (bytes_left() < bytes)
129 }
130
131 // -------------------------------------------------------------------------------------------------
132 //
133 short int Bytestream::read_short (bool* ok)
134 {
135 if (bytes_left() < 2)
136 { 127 {
137 *ok = false; 128 throw IOError (format ("attempted to read %1 byte(s) past the end of bytestream",
138 return false; 129 bytes - bytes_left()));
139 } 130 }
140 131 }
132
133 // -------------------------------------------------------------------------------------------------
134 //
135 char Bytestream::read_byte()
136 {
137 ensure_read_space (1);
138 return *m_cursor++;
139 }
140
141 // -------------------------------------------------------------------------------------------------
142 //
143 short int Bytestream::read_short()
144 {
145 ensure_read_space (2);
141 short int result = 0; 146 short int result = 0;
142 147
143 for (int i = 0; i < 2; ++i) 148 for (int i = 0; i < 2; ++i)
144 result |= m_cursor[i] << (i * 8); 149 result |= m_cursor[i] << (i * 8);
145 150
147 return result; 152 return result;
148 } 153 }
149 154
150 // ------------------------------------------------------------------------------------------------- 155 // -------------------------------------------------------------------------------------------------
151 // 156 //
152 long int Bytestream::read_long (bool* ok) 157 long int Bytestream::read_long()
153 { 158 {
154 if (bytes_left() < 4) 159 ensure_read_space (4);
155 {
156 *ok = false;
157 return -1;
158 }
159
160 long int result = 0; 160 long int result = 0;
161 161
162 for (int i = 0; i < 4; ++i) 162 for (int i = 0; i < 4; ++i)
163 result |= m_cursor[i] << (i * 8); 163 result |= m_cursor[i] << (i * 8);
164 164
166 return result; 166 return result;
167 } 167 }
168 168
169 // ------------------------------------------------------------------------------------------------- 169 // -------------------------------------------------------------------------------------------------
170 // 170 //
171 float Bytestream::read_float (bool* ok) 171 float Bytestream::read_float()
172 { 172 {
173 int value = read_long (ok); 173 int value = read_long();
174
175 if (*ok == false)
176 return -1.0f;
177
178 return reinterpret_cast<float&> (value); 174 return reinterpret_cast<float&> (value);
179 } 175 }
180 176
181 // ------------------------------------------------------------------------------------------------- 177 // -------------------------------------------------------------------------------------------------
182 // 178 //
183 String Bytestream::read_string (bool* ok) 179 String Bytestream::read_string()
184 { 180 {
185 // Zandronum sends strings of maximum 2048 characters, though it only 181 // Zandronum sends strings of maximum 2048 characters, though it only
186 // reads 2047-character long ones so I guess we can follow up and do 182 // reads 2047-character long ones so I guess we can follow up and do
187 // the same :-) 183 // the same :-)
188 static char buffer[MAX_NETWORK_STRING]; 184 static char buffer[MAX_NETWORK_STRING];
192 188
193 // where's the end of the string? 189 // where's the end of the string?
194 for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd) 190 for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd)
195 { 191 {
196 if (stringEnd == end) 192 if (stringEnd == end)
197 {
198 // past the end of the buffer! Argh! 193 // past the end of the buffer! Argh!
199 *ok = false; 194 throw IOError ("unterminated string in packet");
200 return "";
201 }
202 } 195 }
203 196
204 m_cursor = stringEnd + 1; 197 m_cursor = stringEnd + 1;
205 unsigned int length = stringEnd - m_cursor; 198 unsigned int length = stringEnd - m_cursor;
206 199
215 } 208 }
216 209
217 // ------------------------------------------------------------------------------------------------- 210 // -------------------------------------------------------------------------------------------------
218 // 211 //
219 METHOD 212 METHOD
220 Bytestream::read (unsigned char* buffer, unsigned long length, bool* ok) -> void 213 Bytestream::read (unsigned char* buffer, unsigned long length) -> void
221 { 214 {
222 if (bytes_left() < length) 215 ensure_read_space (length);
223 {
224 *ok = false;
225 return;
226 }
227
228 memcpy (buffer, m_cursor, length); 216 memcpy (buffer, m_cursor, length);
229 m_cursor += length; 217 m_cursor += length;
230 } 218 }
231 219
232 // ------------------------------------------------------------------------------------------------- 220 // -------------------------------------------------------------------------------------------------

mercurial