sources/network/bytestream.h

branch
protocol5
changeset 195
be953e1621d9
parent 191
2e6cbacafdc7
equal deleted inserted replaced
176:060a13878ca0 195:be953e1621d9
1 /* 1 /*
2 Copyright 2014 - 2016 Teemu Piippo 2 Copyright 2014 - 2021 Teemu Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions 6 modification, are permitted provided that the following conditions
7 are met: 7 are met:
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #pragma once 31 #pragma once
32 #include <stdexcept> 32 #include <stdexcept>
33 #include <string>
33 #include "../main.h" 34 #include "../main.h"
34 BEGIN_ZFC_NAMESPACE 35 BEGIN_ZFC_NAMESPACE
35
36 class String;
37 36
38 enum 37 enum
39 { 38 {
40 MAX_NETWORK_STRING = 0x800 39 MAX_NETWORK_STRING = 0x800
41 }; 40 };
42 41
43 class IOError : public std::exception 42 class IOError : public std::exception
44 { 43 {
45 String m_message; 44 std::string m_message;
46 45
47 public: 46 public:
48 IOError(String message) : 47 IOError(std::string message) :
49 m_message(message) {} 48 m_message(message) {}
50 49
51 const char* what() const throw() 50 const char* what() const throw()
52 { 51 {
53 return m_message; 52 return m_message.data();
54 } 53 }
55 }; 54 };
56 55
57 class Bytestream 56 class Bytestream
58 { 57 {
59 public: 58 public:
60 Bytestream(ByteArray& data); 59 Bytestream(std::vector<unsigned char>& data);
61 60
62 int bytesLeft() const; 61 int bytesLeft() const;
63 ByteArray::Iterator getCurrentIterator(); 62 std::vector<unsigned char>::iterator getCurrentIterator();
64 int position() const; 63 int position() const;
65 ByteArray readBuffer(int length); 64 std::vector<unsigned char> readBuffer(int length);
66 int8_t readByte(); 65 int8_t readByte();
67 int32_t readLong(); 66 int32_t readLong();
68 int16_t readShort(); 67 int16_t readShort();
69 String readString(); 68 std::string readString();
70 float readFloat(); 69 float readFloat();
71 void rewind(); 70 void rewind();
72 void seek(int position); 71 void seek(int position);
73 void write(const unsigned char* val, unsigned int length); 72 void write(const unsigned char* val, unsigned int length);
74 void writeBuffer(const ByteArray& other); 73 void writeBuffer(const std::vector<unsigned char>& other);
75 void writeByte(int8_t value); 74 void writeByte(int8_t value);
76 void writeDouble(double val); 75 void writeDouble(double val);
77 void writeFloat(float value); 76 void writeFloat(float value);
78 void writeLong(int32_t value); 77 void writeLong(int32_t value);
79 void writeShort(int16_t value); 78 void writeShort(int16_t value);
80 void writeString(const String& string); 79 void writeString(const std::string& string);
81 80
82 private: 81 private:
83 ByteArray& m_data; 82 std::vector<unsigned char>& m_data;
84 int m_position; 83 int m_position;
85 84
86 int8_t read(); 85 int8_t read();
87 void ensureReadSpace(int bytes); 86 void ensureReadSpace(int bytes);
88 }; 87 };

mercurial