Thu, 11 Dec 2014 05:59:43 +0200
- udp -> udpsocket
5 | 1 | #pragma once |
2 | #include "../main.h" | |
3 | ||
4 | class String; | |
5 | ||
6 | enum { MAX_NETWORK_STRING = 0x800 }; | |
7 | ||
8 | // TODO: Make able to handle big-endian too | |
9 | class Bytestream | |
10 | { | |
11 | public: | |
12 | static bool sink; | |
13 | ||
14 | Bytestream (unsigned long length = 0x800); | |
15 | Bytestream (const unsigned char* data, unsigned long length); | |
16 | Bytestream (const Vector<unsigned char>& bytes); | |
17 | ~Bytestream(); | |
18 | ||
19 | inline METHOD allocated_size() const -> unsigned long; | |
20 | inline METHOD bytes_left() const -> unsigned long; | |
21 | METHOD clear() -> void; | |
22 | inline METHOD data() -> unsigned char*; | |
23 | inline METHOD data() const -> const unsigned char*; | |
24 | METHOD grow_to_fit (unsigned long bytes) -> void; | |
25 | inline METHOD position() const -> unsigned long; | |
26 | METHOD read (unsigned char* buffer, unsigned long length, bool* ok = &sink) -> void; | |
27 | METHOD read_byte (bool* ok = &sink) -> char; | |
28 | METHOD read_short (bool* ok = &sink) -> short int; | |
29 | METHOD read_long (bool* ok = &sink) -> long int; | |
30 | METHOD read_string (bool* ok = &sink) -> String; | |
31 | METHOD read_float (bool* ok = &sink) -> float; | |
32 | METHOD resize (unsigned long length) -> void; | |
33 | inline METHOD rewind() -> void; | |
34 | inline METHOD seek (unsigned long pos) -> void; | |
35 | METHOD write (const unsigned char* val, unsigned int length) -> void; | |
36 | METHOD write_buffer (const Bytestream& other) -> void; | |
37 | METHOD write_buffer (const Vector<unsigned char>& other) -> void; | |
38 | METHOD write_byte (char val) -> void; | |
39 | METHOD write_double (double val) -> void; | |
40 | METHOD write_float (float val) -> void; | |
41 | METHOD write_long (long int val) -> void; | |
42 | METHOD write_short (short int val) -> void; | |
43 | METHOD write_string (const String& val) -> void; | |
44 | inline METHOD written_length() const -> unsigned long; | |
45 | ||
46 | inline METHOD operator[] (unsigned long idx) -> unsigned char&; | |
47 | inline METHOD operator[] (unsigned long idx) const -> unsigned char; | |
48 | ||
49 | private: | |
50 | unsigned char* m_data; | |
51 | unsigned char* m_cursor; | |
52 | unsigned long m_allocatedSize; | |
53 | unsigned long m_writtenLength; | |
54 | ||
55 | METHOD init (const unsigned char* data, unsigned long length) -> void; | |
56 | METHOD write (unsigned char val) -> void; | |
57 | inline METHOD space_left() const -> unsigned long; | |
58 | }; | |
59 | ||
60 | // ------------------------------------------------------------------------------------------------- | |
61 | // | |
62 | inline METHOD | |
63 | Bytestream::allocated_size() const -> unsigned long | |
64 | { | |
65 | return m_allocatedSize; | |
66 | } | |
67 | ||
68 | // ------------------------------------------------------------------------------------------------- | |
69 | // | |
70 | inline METHOD | |
71 | Bytestream::written_length() const -> unsigned long | |
72 | { | |
73 | return m_writtenLength; | |
74 | } | |
75 | ||
76 | // ------------------------------------------------------------------------------------------------- | |
77 | // | |
78 | inline METHOD | |
79 | Bytestream::operator[] (unsigned long idx) -> unsigned char& | |
80 | { | |
81 | return m_data[idx]; | |
82 | } | |
83 | ||
84 | // ------------------------------------------------------------------------------------------------- | |
85 | // | |
86 | inline METHOD | |
87 | Bytestream::operator[] (unsigned long idx) const -> unsigned char | |
88 | { | |
89 | return m_data[idx]; | |
90 | } | |
91 | ||
92 | // ------------------------------------------------------------------------------------------------- | |
93 | // | |
94 | inline METHOD | |
95 | Bytestream::position() const -> unsigned long | |
96 | { | |
97 | return m_cursor - m_data; | |
98 | } | |
99 | ||
100 | // ------------------------------------------------------------------------------------------------- | |
101 | // | |
102 | inline METHOD | |
103 | Bytestream::seek (unsigned long pos) -> void | |
104 | { | |
105 | m_cursor = m_data + pos; | |
106 | } | |
107 | ||
108 | // ------------------------------------------------------------------------------------------------- | |
109 | // | |
110 | inline METHOD | |
111 | Bytestream::rewind() -> void | |
112 | { | |
113 | m_cursor = m_data; | |
114 | } | |
115 | ||
116 | // ------------------------------------------------------------------------------------------------- | |
117 | // | |
118 | inline METHOD | |
119 | Bytestream::bytes_left() const -> unsigned long | |
120 | { | |
121 | return (m_writtenLength - (m_cursor - &m_data[0])); | |
122 | } | |
123 | ||
124 | // ------------------------------------------------------------------------------------------------- | |
125 | // | |
126 | inline METHOD | |
127 | Bytestream::space_left() const -> unsigned long | |
128 | { | |
129 | return (m_allocatedSize - m_writtenLength); | |
130 | } | |
131 | ||
132 | // ------------------------------------------------------------------------------------------------- | |
133 | // | |
134 | inline METHOD | |
135 | Bytestream::data() -> unsigned char* | |
136 | { | |
137 | return m_data; | |
138 | } | |
139 | ||
140 | // ------------------------------------------------------------------------------------------------- | |
141 | // | |
142 | inline METHOD | |
143 | Bytestream::data() const -> const unsigned char* | |
144 | { | |
145 | return m_data; | |
146 | } |