| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #ifndef COMMUNICATION_H | 1 #ifndef COMMUNICATION_H |
| 2 #define COMMUNICATION_H | 2 #define COMMUNICATION_H |
| 3 | 3 |
| 4 #include <memory> | 4 #include <memory> |
| 5 #include <sstream> | 5 #include <sstream> |
| 6 #include <stdexcept> | 6 #include <stdexcept> |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 #include <Windows.h> | 10 #include <Windows.h> |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 }; | 29 }; |
| 30 enum ValueType : uint32_t { | 30 enum ValueType : uint32_t { |
| 31 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL | 31 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL |
| 32 }; | 32 }; |
| 33 typedef uint32_t SizeType; | 33 typedef uint32_t SizeType; |
| 34 | 34 |
| 35 class InputBuffer | 35 class InputBuffer |
| 36 { | 36 { |
| 37 public: | 37 public: |
| 38 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} | 38 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} |
| 39 InputBuffer(const InputBuffer& copy) { hasType = copy.hasType; buffer = std: :istringstream(copy.buffer.str()); currentType = copy.currentType; } | |
| 39 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); } | 40 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); } |
| 40 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } | 41 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } |
| 41 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } | 42 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } |
| 42 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } | 43 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } |
| 43 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } | 44 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } |
| 44 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } | 45 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } |
| 46 ValueType GetType(); | |
| 47 bool IsEmpty() { return buffer.eof(); } | |
|
Wladimir Palant
2013/07/22 06:43:11
This is just wrong :-(
eof() doesn't mean that the
Oleksandr
2013/07/22 09:53:31
I guess the name was just wrong. Should've been Is
| |
| 45 private: | 48 private: |
| 46 std::istringstream buffer; | 49 std::istringstream buffer; |
| 47 ValueType currentType; | 50 ValueType currentType; |
| 48 bool hasType; | 51 bool hasType; |
| 49 | 52 |
| 50 void CheckType(ValueType expectedType); | 53 void CheckType(ValueType expectedType); |
| 51 | 54 |
| 52 template<class T> | 55 template<class T> |
| 53 InputBuffer& ReadString(T& value, ValueType expectedType) | 56 InputBuffer& ReadString(T& value, ValueType expectedType) |
| 54 { | 57 { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 73 ReadBinary(value); | 76 ReadBinary(value); |
| 74 return *this; | 77 return *this; |
| 75 } | 78 } |
| 76 | 79 |
| 77 template<class T> | 80 template<class T> |
| 78 void ReadBinary(T& value) | 81 void ReadBinary(T& value) |
| 79 { | 82 { |
| 80 buffer.read(reinterpret_cast<char*>(&value), sizeof(T)); | 83 buffer.read(reinterpret_cast<char*>(&value), sizeof(T)); |
| 81 if (buffer.fail()) | 84 if (buffer.fail()) |
| 82 throw new std::runtime_error("Unexpected end of input buffer"); | 85 throw new std::runtime_error("Unexpected end of input buffer"); |
| 86 hasType = false; | |
| 83 } | 87 } |
| 84 }; | 88 }; |
| 85 | 89 |
| 86 class OutputBuffer | 90 class OutputBuffer |
| 87 { | 91 { |
| 88 public: | 92 public: |
| 89 OutputBuffer() {} | 93 OutputBuffer() {} |
| 90 | 94 |
| 91 // Explicit copy constructor to allow returning OutputBuffer by value | 95 // Explicit copy constructor to allow returning OutputBuffer by value |
| 92 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} | 96 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 | 165 |
| 162 InputBuffer ReadMessage(); | 166 InputBuffer ReadMessage(); |
| 163 void WriteMessage(OutputBuffer& message); | 167 void WriteMessage(OutputBuffer& message); |
| 164 | 168 |
| 165 protected: | 169 protected: |
| 166 HANDLE pipe; | 170 HANDLE pipe; |
| 167 }; | 171 }; |
| 168 } | 172 } |
| 169 | 173 |
| 170 #endif | 174 #endif |
| OLD | NEW |