sources/network/bytestream.h

Fri, 22 Jul 2016 17:52:23 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Fri, 22 Jul 2016 17:52:23 +0300
changeset 158
de7574d292ad
parent 157
42bb29924218
child 180
2e7225dbd9b2
permissions
-rw-r--r--

Added the ByteArray typedef for Vector<unsigned char>

9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
1 /*
109
e4966d7e615d Happy new year 2016
Teemu Piippo <crimsondusk64@gmail.com>
parents: 98
diff changeset
2 Copyright 2014 - 2016 Teemu Piippo
9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
3 All rights reserved.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
4
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
5 Redistribution and use in source and binary forms, with or without
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
6 modification, are permitted provided that the following conditions
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
7 are met:
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
8
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
9 1. Redistributions of source code must retain the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
10 notice, this list of conditions and the following disclaimer.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
12 notice, this list of conditions and the following disclaimer in the
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
13 documentation and/or other materials provided with the distribution.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
15 contributors may be used to endorse or promote products derived from
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
16 this software without specific prior written permission.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
17
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
29 */
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
30
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #pragma once
13
09dcaeaa216b - to hell with that 'ok' field. now throws an exception if attempts to read past the end
Teemu Piippo <crimsondusk64@gmail.com>
parents: 11
diff changeset
32 #include <stdexcept>
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "../main.h"
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
34 BEGIN_ZFC_NAMESPACE
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 class String;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
38 enum
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
39 {
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
40 MAX_NETWORK_STRING = 0x800
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
41 };
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
43 class IOError : public std::exception
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
44 {
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
45 String m_message;
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
46
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
47 public:
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
48 IOError(String message) :
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
49 m_message(message) {}
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
50
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
51 const char* what() const throw()
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
52 {
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
53 return m_message;
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
54 }
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
55 };
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
56
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 class Bytestream
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 public:
158
de7574d292ad Added the ByteArray typedef for Vector<unsigned char>
Teemu Piippo <teemu@compsta2.com>
parents: 157
diff changeset
60 Bytestream(ByteArray& data);
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
61
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
62 int bytesLeft() const;
158
de7574d292ad Added the ByteArray typedef for Vector<unsigned char>
Teemu Piippo <teemu@compsta2.com>
parents: 157
diff changeset
63 ByteArray::Iterator getCurrentIterator();
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
64 int position() const;
158
de7574d292ad Added the ByteArray typedef for Vector<unsigned char>
Teemu Piippo <teemu@compsta2.com>
parents: 157
diff changeset
65 ByteArray readBuffer(int length);
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
66 int8_t readByte();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
67 int32_t readLong();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
68 int16_t readShort();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
69 String readString();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
70 float readFloat();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
71 void rewind();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
72 void seek(int position);
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
73 void write(const unsigned char* val, unsigned int length);
158
de7574d292ad Added the ByteArray typedef for Vector<unsigned char>
Teemu Piippo <teemu@compsta2.com>
parents: 157
diff changeset
74 void writeBuffer(const ByteArray& other);
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
75 void writeByte(int8_t value);
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
76 void writeDouble(double val);
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
77 void writeFloat(float value);
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
78 void writeLong(int32_t value);
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
79 void writeShort(int16_t value);
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
80 void writeString(const String& string);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 private:
158
de7574d292ad Added the ByteArray typedef for Vector<unsigned char>
Teemu Piippo <teemu@compsta2.com>
parents: 157
diff changeset
83 ByteArray& m_data;
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
84 int m_position;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
157
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
86 int8_t read();
42bb29924218 Bytestream now behaves more like a cursor. It does not store the data anymore, rather it leaves the user to specify a vector to use for storage.
Teemu Piippo <teemu@compsta2.com>
parents: 109
diff changeset
87 void ensureReadSpace(int bytes);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 };
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 73
diff changeset
90 END_ZFC_NAMESPACE

mercurial