Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/shared/Communication.h

Issue 10786016: Moved all pipe functionality into a self-containing Communication::Pipe class (Closed)
Left Patch Set: Created May 31, 2013, 11:48 a.m.
Right Patch Set: Created June 3, 2013, 2:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/shared/Communication.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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;
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.
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.
16 15
17 enum ValueType {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL}; 16 enum ProcType : uint32_t {
17 PROC_MATCHES,
18 PROC_GET_ELEMHIDE_SELECTORS,
19 PROC_AVAILABLE_SUBSCRIPTIONS,
20 PROC_LISTED_SUBSCRIPTIONS,
21 PROC_SET_SUBSCRIPTION,
22 PROC_UPDATE_ALL_SUBSCRIPTIONS,
23 PROC_GET_EXCEPTION_DOMAINS,
24 PROC_ADD_FILTER,
25 };
26 enum ValueType : uint32_t {
27 TYPE_PROC, TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL
28 };
29 typedef uint32_t SizeType;
18 30
19 class InputBuffer 31 class InputBuffer
20 { 32 {
21 public: 33 public:
22 InputBuffer(const std::string& data) : buffer(data) {} 34 InputBuffer(const std::string& data) : buffer(data), hasType(false) {}
35 InputBuffer& operator>>(ProcType& value) { return Read(value, TYPE_PROC); }
23 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); } 36 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); }
24 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); } 37 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); }
25 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); } 38 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); }
26 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); } 39 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); }
27 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); } 40 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); }
28 private: 41 private:
29 std::istringstream buffer; 42 std::istringstream buffer;
30 int32_t currentType; 43 ValueType 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; 44 bool hasType;
32 45
33 void CheckType(ValueType expectedType) 46 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 }
47 47
48 template<class T> 48 template<class T>
49 InputBuffer& ReadString(T& value, ValueType expectedType) 49 InputBuffer& ReadString(T& value, ValueType expectedType)
50 { 50 {
51 CheckType(expectedType); 51 CheckType(expectedType);
52 52
53 uint32_t length; 53 SizeType length;
54 ReadBinary(length); 54 ReadBinary(length);
55 55
56 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]);
57 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);
58 if (buffer.fail()) 58 if (buffer.fail())
59 throw new std::runtime_error("Unexpected end of input buffer"); 59 throw new std::runtime_error("Unexpected end of input buffer");
60 60
61 value.assign(data.get(), length); 61 value.assign(data.get(), length);
62 return *this; 62 return *this;
63 } 63 }
(...skipping 20 matching lines...) Expand all
84 public: 84 public:
85 OutputBuffer() {} 85 OutputBuffer() {}
86 86
87 // Explicit copy constructor to allow returning OutputBuffer by value 87 // Explicit copy constructor to allow returning OutputBuffer by value
88 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {} 88 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {}
89 89
90 std::string Get() 90 std::string Get()
91 { 91 {
92 return buffer.str(); 92 return buffer.str();
93 } 93 }
94 OutputBuffer& operator<<(ProcType value) { return Write(value, TYPE_PROC); }
94 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); } 95 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); } 96 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); }
96 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); } 97 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); }
97 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); } 98 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); }
98 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); } 99 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); }
99 private: 100 private:
100 std::ostringstream buffer; 101 std::ostringstream buffer;
101 102
103 // Disallow copying
104 const OutputBuffer& operator=(const OutputBuffer&);
105
102 template<class T> 106 template<class T>
103 OutputBuffer& WriteString(const T& value, int32_t type) 107 OutputBuffer& WriteString(const T& value, ValueType type)
104 { 108 {
105 WriteBinary(type); 109 WriteBinary(type);
106 110
107 uint32_t length = value.size(); 111 SizeType length = value.size();
108 WriteBinary(length); 112 WriteBinary(length);
109 113
110 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length); 114 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length);
111 if (buffer.fail()) 115 if (buffer.fail())
112 throw new std::runtime_error("Unexpected error writing to output buffer" ); 116 throw new std::runtime_error("Unexpected error writing to output buffer" );
113 117
114 return *this; 118 return *this;
115 } 119 }
116 120
117 template<class T> 121 template<class T>
118 OutputBuffer& Write(const T value, int32_t type) 122 OutputBuffer& Write(const T value, ValueType type)
119 { 123 {
120 WriteBinary(type); 124 WriteBinary(type);
121 WriteBinary(value); 125 WriteBinary(value);
122 return *this; 126 return *this;
123 } 127 }
124 128
125 template<class T> 129 template<class T>
126 void WriteBinary(const T& value) 130 void WriteBinary(const T& value)
127 { 131 {
128 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T)); 132 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T));
(...skipping 18 matching lines...) Expand all
147 151
148 InputBuffer ReadMessage(); 152 InputBuffer ReadMessage();
149 void WriteMessage(OutputBuffer& message); 153 void WriteMessage(OutputBuffer& message);
150 154
151 protected: 155 protected:
152 HANDLE pipe; 156 HANDLE pipe;
153 }; 157 };
154 } 158 }
155 159
156 #endif 160 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld