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 <sstream> | 5 #include <sstream> |
6 #include <stdexcept> | |
5 #include <stdint.h> | 7 #include <stdint.h> |
6 #include <string> | 8 #include <string> |
7 #include <vector> | 9 #include <vector> |
8 #include <Windows.h> | 10 #include <Windows.h> |
9 | 11 |
10 namespace Communication | 12 namespace Communication |
11 { | 13 { |
12 extern const std::wstring pipeName; | 14 extern const std::wstring pipeName; |
Felix Dahlke
2013/06/03 12:18:47
Since we're really always using the same pipeName,
Wladimir Palant
2013/06/03 14:00:26
The tests use a different pipe name.
| |
13 const int bufferSize = 1024; | 15 const int bufferSize = 1024; |
Felix Dahlke
2013/06/03 12:18:47
This is only being used in Communication.cpp, I'd
Wladimir Palant
2013/06/03 14:00:26
True.
| |
14 | 16 |
15 enum ValueType {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; | 17 enum ValueType {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; |
16 | 18 |
17 class InputBuffer | 19 class InputBuffer |
18 { | 20 { |
19 public: | 21 public: |
20 InputBuffer(const std::string& data) : buffer(data) {} | 22 InputBuffer(const std::string& data) : buffer(data) {} |
21 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } | 23 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } |
22 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } | 24 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } |
23 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } | 25 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } |
24 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } | 26 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } |
25 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } | 27 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } |
26 private: | 28 private: |
27 std::istringstream buffer; | 29 std::istringstream buffer; |
30 int32_t currentType; | |
Felix Dahlke
2013/06/03 12:18:47
I'd like to have the typedef here I asked for in t
Wladimir Palant
2013/06/03 14:00:26
You are getting it in the other review :)
| |
31 bool hasType; | |
32 | |
33 void CheckType(ValueType expectedType) | |
Felix Dahlke
2013/06/03 12:18:47
I'd prefer to see this function in the cpp, it's a
| |
34 { | |
35 if (!hasType) | |
36 ReadBinary(currentType); | |
37 | |
38 if (currentType != expectedType) | |
39 { | |
40 // Make sure we don't attempt to read the type again | |
41 hasType = true; | |
42 throw new std::runtime_error("Unexpected type found in input buffer"); | |
43 } | |
44 else | |
45 hasType = false; | |
46 } | |
28 | 47 |
29 template<class T> | 48 template<class T> |
30 InputBuffer& ReadString(T& value, ValueType expectedType) | 49 InputBuffer& ReadString(T& value, ValueType expectedType) |
31 { | 50 { |
32 int32_t type; | 51 CheckType(expectedType); |
33 ReadBinary(type); | |
34 if (type != expectedType) | |
35 throw new std::runtime_error("Unexpected type found in input buffer"); | |
36 | 52 |
37 uint32_t length; | 53 uint32_t length; |
38 ReadBinary(length); | 54 ReadBinary(length); |
39 | 55 |
40 std::auto_ptr<T::value_type> data(new T::value_type[length]); | 56 std::auto_ptr<T::value_type> data(new T::value_type[length]); |
41 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength); | 57 buffer.read(reinterpret_cast<char*>(data.get()), sizeof(T::value_type) * l ength); |
42 if (buffer.fail()) | 58 if (buffer.fail()) |
43 throw new std::runtime_error("Unexpected end of input buffer"); | 59 throw new std::runtime_error("Unexpected end of input buffer"); |
44 | 60 |
45 value.assign(data.get(), length); | 61 value.assign(data.get(), length); |
46 return *this; | 62 return *this; |
47 } | 63 } |
48 | 64 |
49 template<class T> | 65 template<class T> |
50 InputBuffer& Read(T& value, ValueType expectedType) | 66 InputBuffer& Read(T& value, ValueType expectedType) |
51 { | 67 { |
52 int32_t type; | 68 CheckType(expectedType); |
53 ReadBinary(type); | |
54 if (type != expectedType) | |
55 throw new std::runtime_error("Unexpected type found in input buffer"); | |
56 | |
57 ReadBinary(value); | 69 ReadBinary(value); |
58 return *this; | 70 return *this; |
59 } | 71 } |
60 | 72 |
61 template<class T> | 73 template<class T> |
62 void ReadBinary(T& value) | 74 void ReadBinary(T& value) |
63 { | 75 { |
64 buffer.read(reinterpret_cast<char*>(&value), sizeof(T)); | 76 buffer.read(reinterpret_cast<char*>(&value), sizeof(T)); |
65 if (buffer.fail()) | 77 if (buffer.fail()) |
66 throw new std::runtime_error("Unexpected end of input buffer"); | 78 throw new std::runtime_error("Unexpected end of input buffer"); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 | 124 |
113 template<class T> | 125 template<class T> |
114 void WriteBinary(const T& value) | 126 void WriteBinary(const T& value) |
115 { | 127 { |
116 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T)); | 128 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T)); |
117 if (buffer.fail()) | 129 if (buffer.fail()) |
118 throw new std::runtime_error("Unexpected error writing to output buffer" ); | 130 throw new std::runtime_error("Unexpected error writing to output buffer" ); |
119 } | 131 } |
120 }; | 132 }; |
121 | 133 |
122 InputBuffer ReadMessage(HANDLE pipe); | 134 class PipeConnectionError : public std::runtime_error |
123 void WriteMessage(HANDLE pipe, OutputBuffer& message); | 135 { |
136 public: | |
137 PipeConnectionError(); | |
138 }; | |
139 | |
140 class Pipe | |
141 { | |
142 public: | |
143 enum Mode {MODE_CREATE, MODE_CONNECT}; | |
144 | |
145 Pipe(const std::wstring& name, Mode mode); | |
146 ~Pipe(); | |
147 | |
148 InputBuffer ReadMessage(); | |
149 void WriteMessage(OutputBuffer& message); | |
150 | |
151 protected: | |
152 HANDLE pipe; | |
153 }; | |
124 } | 154 } |
125 | 155 |
126 #endif | 156 #endif |
OLD | NEW |