sources/network/bytestream.h

branch
protocol5
changeset 159
970d58a01e8b
parent 158
de7574d292ad
child 180
2e7225dbd9b2
equal deleted inserted replaced
155:9f71f854474a 159:970d58a01e8b
38 enum 38 enum
39 { 39 {
40 MAX_NETWORK_STRING = 0x800 40 MAX_NETWORK_STRING = 0x800
41 }; 41 };
42 42
43 // TODO: Make able to handle big-endian too 43 class IOError : public std::exception
44 {
45 String m_message;
46
47 public:
48 IOError(String message) :
49 m_message(message) {}
50
51 const char* what() const throw()
52 {
53 return m_message;
54 }
55 };
56
44 class Bytestream 57 class Bytestream
45 { 58 {
46 public: 59 public:
47 class IOError : public std::exception 60 Bytestream(ByteArray& data);
48 {
49 String m_message;
50 61
51 public: 62 int bytesLeft() const;
52 IOError (String message) : 63 ByteArray::Iterator getCurrentIterator();
53 m_message (message) {} 64 int position() const;
54 65 ByteArray readBuffer(int length);
55 const char* what() const throw() 66 int8_t readByte();
56 { 67 int32_t readLong();
57 return m_message; 68 int16_t readShort();
58 } 69 String readString();
59 }; 70 float readFloat();
60 71 void rewind();
61 Bytestream (unsigned long length = 0x800); 72 void seek(int position);
62 Bytestream (const unsigned char* data, unsigned long length); 73 void write(const unsigned char* val, unsigned int length);
63 Bytestream (const Vector<unsigned char>& bytes); 74 void writeBuffer(const ByteArray& other);
64 Bytestream (const Bytestream& other); 75 void writeByte(int8_t value);
65 ~Bytestream(); 76 void writeDouble(double val);
66 77 void writeFloat(float value);
67 void clear(); 78 void writeLong(int32_t value);
68 void grow_to_fit (unsigned long bytes); 79 void writeShort(int16_t value);
69 void read (unsigned char* buffer, unsigned long length); 80 void writeString(const String& string);
70 int8_t read_byte();
71 int32_t read_long();
72 int16_t read_short();
73 String read_string();
74 float read_float();
75 void resize (unsigned long length);
76 void write (const unsigned char* val, unsigned int length);
77 void write_buffer (const Bytestream& other);
78 void write_buffer (const Vector<unsigned char>& other);
79 void write_byte (int8_t val);
80 void write_double (double val);
81 void write_float (float val);
82 void write_long (int32_t val);
83 void write_short (int16_t val);
84 void write_string (const String& val);
85
86 Bytestream& operator= (const Bytestream& other);
87
88 unsigned long allocated_size() const
89 {
90 return m_allocatedSize;
91 }
92
93 unsigned long bytes_left() const
94 {
95 return m_writtenLength - (m_cursor - &m_data[0]);
96 }
97
98 inline unsigned char* data()
99 {
100 return m_data;
101 }
102 inline const unsigned char* data() const
103 {
104 return m_data;
105 }
106
107 unsigned long position() const
108 {
109 return m_cursor - m_data;
110 }
111
112 void rewind()
113 {
114 m_cursor = m_data;
115 }
116
117 void seek (unsigned long pos)
118 {
119 m_cursor = m_data + pos;
120 }
121
122 Vector<unsigned char> to_vector() const
123 {
124 return Vector<unsigned char> (m_data, m_writtenLength);
125 }
126
127 unsigned long written_length() const
128 {
129 return m_writtenLength;
130 }
131
132 unsigned char& operator[] (unsigned long idx)
133 {
134 return m_data[idx];
135 }
136
137 unsigned char operator[] (unsigned long idx) const
138 {
139 return m_data[idx];
140 }
141 81
142 private: 82 private:
143 unsigned char* m_data; 83 ByteArray& m_data;
144 unsigned char* m_cursor; 84 int m_position;
145 unsigned long m_allocatedSize;
146 unsigned long m_writtenLength;
147 85
148 void init (const unsigned char* data, unsigned long length); 86 int8_t read();
149 void write (unsigned char val); 87 void ensureReadSpace(int bytes);
150 void ensure_read_space (unsigned int bytes);
151
152 unsigned long space_left() const
153 {
154 return m_allocatedSize - m_writtenLength;
155 }
156 }; 88 };
157 89
158 END_ZFC_NAMESPACE 90 END_ZFC_NAMESPACE

mercurial