| 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> |
| 11 | 11 |
| 12 namespace Communication | 12 namespace Communication |
| 13 { | 13 { |
| 14 extern const std::wstring pipeName; | 14 extern const std::wstring pipeName; |
| 15 const int bufferSize = 1024; | 15 const int bufferSize = 1024; |
| 16 | 16 |
| 17 enum ValueType {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; | 17 enum {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; |
| 18 typedef int32_t ValueType; | |
| 19 typedef uint32_t SizeType; | |
| 18 | 20 |
| 19 class InputBuffer | 21 class InputBuffer |
| 20 { | 22 { |
| 21 public: | 23 public: |
| 22 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} | 24 InputBuffer(const std::string& data) : buffer(data), hasType(false) {} |
| 23 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } | 25 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } |
| 24 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } | 26 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } |
| 25 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } | 27 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } |
| 26 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } | 28 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } |
| 27 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } | 29 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } |
| 28 private: | 30 private: |
| 29 std::istringstream buffer; | 31 std::istringstream buffer; |
| 30 int32_t currentType; | 32 ValueType currentType; |
| 31 bool hasType; | 33 bool hasType; |
| 32 | 34 |
| 33 void CheckType(ValueType expectedType) | 35 void CheckType(ValueType expectedType) |
| 34 { | 36 { |
| 35 if (!hasType) | 37 if (!hasType) |
| 36 ReadBinary(currentType); | 38 ReadBinary(currentType); |
| 37 | 39 |
| 38 if (currentType != expectedType) | 40 if (currentType != expectedType) |
| 39 { | 41 { |
| 40 // Make sure we don't attempt to read the type again | 42 // Make sure we don't attempt to read the type again |
| 41 hasType = true; | 43 hasType = true; |
| 42 throw new std::runtime_error("Unexpected type found in input buffer"); | 44 throw new std::runtime_error("Unexpected type found in input buffer"); |
| 43 } | 45 } |
| 44 else | 46 else |
| 45 hasType = false; | 47 hasType = false; |
| 46 } | 48 } |
| 47 | 49 |
| 48 template<class T> | 50 template<class T> |
| 49 InputBuffer& ReadString(T& value, ValueType expectedType) | 51 InputBuffer& ReadString(T& value, ValueType expectedType) |
| 50 { | 52 { |
| 51 CheckType(expectedType); | 53 CheckType(expectedType); |
| 52 | 54 |
| 53 uint32_t length; | 55 SizeType length; |
| 54 ReadBinary(length); | 56 ReadBinary(length); |
| 55 | 57 |
| 56 std::auto_ptr<T::value_type> data(new T::value_type[length]); | 58 std::auto_ptr<T::value_type> data(new T::value_type[length]); |
| 57 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength); | 59 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength); |
| 58 if (buffer.fail()) | 60 if (buffer.fail()) |
| 59 throw new std::runtime_error("Unexpected end of input buffer"); | 61 throw new std::runtime_error("Unexpected end of input buffer"); |
| 60 | 62 |
| 61 value.assign(data.get(), length); | 63 value.assign(data.get(), length); |
| 62 return *this; | 64 return *this; |
| 63 } | 65 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 92 return buffer.str(); | 94 return buffer.str(); |
| 93 } | 95 } |
| 94 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); } | 96 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); } |
| 95 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); } | 97 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); } |
| 96 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } | 98 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } |
| 97 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } | 99 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } |
| 98 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } | 100 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } |
| 99 private: | 101 private: |
| 100 std::ostringstream buffer; | 102 std::ostringstream buffer; |
| 101 | 103 |
| 104 // Disallow copying | |
| 105 const OutputBuffer& operator=(const OutputBuffer&); | |
| 106 | |
| 102 template<class T> | 107 template<class T> |
| 103 OutputBuffer& WriteString(const T& value, int32_t type) | 108 OutputBuffer& WriteString(const T& value, ValueType type) |
| 104 { | 109 { |
| 105 WriteBinary(type); | 110 WriteBinary(type); |
| 106 | 111 |
| 107 uint32_t length = value.size(); | 112 SizeType length = value.size(); |
| 108 WriteBinary(length); | 113 WriteBinary(length); |
| 109 | 114 |
| 110 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length); | 115 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length); |
| 111 if (buffer.fail()) | 116 if (buffer.fail()) |
| 112 throw new std::runtime_error("Unexpected error writing to output buffer" ); | 117 throw new std::runtime_error("Unexpected error writing to output buffer" ); |
| 113 | 118 |
| 114 return *this; | 119 return *this; |
| 115 } | 120 } |
| 116 | 121 |
| 117 template<class T> | 122 template<class T> |
| 118 OutputBuffer& Write(const T value, int32_t type) | 123 OutputBuffer& Write(const T value, ValueType type) |
|
Felix Dahlke
2013/06/03 12:23:54
Shouldn't value be passed by reference?
Wladimir Palant
2013/06/03 12:25:55
Given that this function is meant for primitive ty
Felix Dahlke
2013/06/04 06:41:38
Right, didn't think of that. Won't hurt, but it wo
| |
| 119 { | 124 { |
| 120 WriteBinary(type); | 125 WriteBinary(type); |
| 121 WriteBinary(value); | 126 WriteBinary(value); |
| 122 return *this; | 127 return *this; |
| 123 } | 128 } |
| 124 | 129 |
| 125 template<class T> | 130 template<class T> |
| 126 void WriteBinary(const T& value) | 131 void WriteBinary(const T& value) |
| 127 { | 132 { |
| 128 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T)); | 133 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T)); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 147 | 152 |
| 148 InputBuffer ReadMessage(); | 153 InputBuffer ReadMessage(); |
| 149 void WriteMessage(OutputBuffer& message); | 154 void WriteMessage(OutputBuffer& message); |
| 150 | 155 |
| 151 protected: | 156 protected: |
| 152 HANDLE pipe; | 157 HANDLE pipe; |
| 153 }; | 158 }; |
| 154 } | 159 } |
| 155 | 160 |
| 156 #endif | 161 #endif |
| OLD | NEW |