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

Side by Side Diff: src/shared/Communication.h

Issue 10824027: Implement better marshaling (Closed)
Patch Set: Created May 31, 2013, 8:26 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/shared/AutoHandle.cpp ('k') | src/shared/Communication.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #ifndef COMMUNICATION_H 1 #ifndef COMMUNICATION_H
2 #define COMMUNICATION_H 2 #define COMMUNICATION_H
3 3
4 #include <sstream>
5 #include <stdint.h>
4 #include <string> 6 #include <string>
5 #include <vector> 7 #include <vector>
6 #include <Windows.h> 8 #include <Windows.h>
7 9
8 namespace Communication 10 namespace Communication
9 { 11 {
10 extern const std::wstring pipeName; 12 extern const std::wstring pipeName;
11 const int bufferSize = 1024; 13 const int bufferSize = 1024;
12 14
13 std::string MarshalStrings(const std::vector<std::string>& strings); 15 enum ValueType {TYPE_STRING, TYPE_WSTRING, TYPE_INT64, TYPE_INT32, TYPE_BOOL};
14 std::vector<std::string> UnmarshalStrings(const std::string& message); 16
15 std::string ReadMessage(HANDLE pipe); 17 class InputBuffer
16 void WriteMessage(HANDLE pipe, const std::string& message); 18 {
19 public:
20 InputBuffer(const std::string& data) : buffer(data) {}
21 InputBuffer& operator>>(std::string& value) { return ReadString(value, TYPE_ STRING); }
22 InputBuffer& operator>>(std::wstring& value) { return ReadString(value, TYPE _WSTRING); }
23 InputBuffer& operator>>(int64_t& value) { return Read(value, TYPE_INT64); }
24 InputBuffer& operator>>(int32_t& value) { return Read(value, TYPE_INT32); }
25 InputBuffer& operator>>(bool& value) { return Read(value, TYPE_BOOL); }
26 private:
27 std::istringstream buffer;
28
29 template<class T>
30 InputBuffer& ReadString(T& value, ValueType expectedType)
Felix Dahlke 2013/06/03 11:25:41 I'd just call this "ReadArray", since it would wor
Wladimir Palant 2013/06/03 11:53:00 Nope - this is expecting std::base_string rather t
31 {
32 int32_t type;
Felix Dahlke 2013/06/03 11:25:41 Should be ValueType, not int32_t I guess?
Wladimir Palant 2013/06/03 11:53:00 I'm not sure whether the internal representation o
Felix Dahlke 2013/06/03 12:02:09 Fair enough. But I'd want a typedef then :)
33 ReadBinary(type);
34 if (type != expectedType)
35 throw new std::runtime_error("Unexpected type found in input buffer");
36
37 uint32_t length;
Felix Dahlke 2013/06/03 11:25:41 Would prefer a typedef for this.
38 ReadBinary(length);
39
40 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);
42 if (buffer.fail())
43 throw new std::runtime_error("Unexpected end of input buffer");
44
45 value.assign(data.get(), length);
Felix Dahlke 2013/06/03 11:25:41 Based on some quick searching, only std::basic_str
Wladimir Palant 2013/06/03 11:53:00 See above - this function is used both for std::st
Felix Dahlke 2013/06/03 12:02:09 I thought basic_string was a base class, but it's
46 return *this;
47 }
48
49 template<class T>
50 InputBuffer& Read(T& value, ValueType expectedType)
51 {
52 int32_t type;
Felix Dahlke 2013/06/03 11:25:41 Like above, I think this should be ValueType
53 ReadBinary(type);
54 if (type != expectedType)
55 throw new std::runtime_error("Unexpected type found in input buffer");
56
57 ReadBinary(value);
58 return *this;
59 }
60
61 template<class T>
62 void ReadBinary(T& value)
63 {
64 buffer.read(reinterpret_cast<char*>(&value), sizeof(T));
65 if (buffer.fail())
66 throw new std::runtime_error("Unexpected end of input buffer");
67 }
68 };
69
70 class OutputBuffer
71 {
72 public:
73 OutputBuffer() {}
74
75 // Explicit copy constructor to allow returning OutputBuffer by value
Felix Dahlke 2013/06/03 11:25:41 I find this kind of obvious :P
Wladimir Palant 2013/06/03 11:53:00 It wasn't to me :)
76 OutputBuffer(const OutputBuffer& copy) : buffer(copy.buffer.str()) {}
Felix Dahlke 2013/06/03 11:25:41 We should implement the copy assign operator as we
Wladimir Palant 2013/06/03 11:53:00 I think I'm fine with copy assignment failing - I
Felix Dahlke 2013/06/03 12:02:09 Assignment won't fail if the copy assignment opera
77
78 std::string Get()
79 {
80 return buffer.str();
81 }
82 OutputBuffer& operator<<(const std::string& value) { return WriteString(valu e, TYPE_STRING); }
83 OutputBuffer& operator<<(const std::wstring& value) { return WriteString(val ue, TYPE_WSTRING); }
84 OutputBuffer& operator<<(int64_t value) { return Write(value, TYPE_INT64); }
85 OutputBuffer& operator<<(int32_t value) { return Write(value, TYPE_INT32); }
86 OutputBuffer& operator<<(bool value) { return Write(value, TYPE_BOOL); }
87 private:
88 std::ostringstream buffer;
89
90 template<class T>
91 OutputBuffer& WriteString(const T& value, int32_t type)
Felix Dahlke 2013/06/03 11:25:41 Should be ValueType instead of int32_t I guess.
92 {
93 WriteBinary(type);
94
95 uint32_t length = value.size();
96 WriteBinary(length);
97
98 buffer.write(reinterpret_cast<const char*>(value.c_str()), sizeof(T::value _type) * length);
Felix Dahlke 2013/06/03 11:25:41 AFAIK, only std::basic_string has c_str(). So no n
Wladimir Palant 2013/06/03 11:53:00 See above - both std::string and std::wstring are
99 if (buffer.fail())
100 throw new std::runtime_error("Unexpected error writing to output buffer" );
101
102 return *this;
103 }
104
105 template<class T>
106 OutputBuffer& Write(const T value, int32_t type)
Felix Dahlke 2013/06/03 11:25:41 Should be ValueType instead of int32_t I think. Al
107 {
108 WriteBinary(type);
109 WriteBinary(value);
110 return *this;
111 }
112
113 template<class T>
114 void WriteBinary(const T& value)
115 {
116 buffer.write(reinterpret_cast<const char*>(&value), sizeof(T));
117 if (buffer.fail())
118 throw new std::runtime_error("Unexpected error writing to output buffer" );
119 }
120 };
121
122 InputBuffer ReadMessage(HANDLE pipe);
123 void WriteMessage(HANDLE pipe, OutputBuffer& message);
17 } 124 }
18 125
19 #endif 126 #endif
OLDNEW
« no previous file with comments | « src/shared/AutoHandle.cpp ('k') | src/shared/Communication.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld