OLD | NEW |
1 #include <Windows.h> | 1 #include <Windows.h> |
2 #include <Lmcons.h> | 2 #include <Lmcons.h> |
3 #include <Sddl.h> | 3 #include <Sddl.h> |
4 | 4 |
5 #include "Communication.h" | 5 #include "Communication.h" |
6 #include "Utils.h" | 6 #include "Utils.h" |
7 | 7 |
8 namespace | 8 namespace |
9 { | 9 { |
10 const int bufferSize = 1024; | 10 const int bufferSize = 1024; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 Communication::PipeConnectionError::PipeConnectionError() | 56 Communication::PipeConnectionError::PipeConnectionError() |
57 : std::runtime_error(AppendErrorCode("Unable to connect to a named pipe")) | 57 : std::runtime_error(AppendErrorCode("Unable to connect to a named pipe")) |
58 { | 58 { |
59 } | 59 } |
60 | 60 |
61 Communication::PipeBusyError::PipeBusyError() | 61 Communication::PipeBusyError::PipeBusyError() |
62 : std::runtime_error("Timeout while trying to connect to a named pipe, pipe is
busy") | 62 : std::runtime_error("Timeout while trying to connect to a named pipe, pipe is
busy") |
63 { | 63 { |
64 } | 64 } |
65 | 65 |
| 66 Communication::PipeDisconnectedError::PipeDisconnectedError() |
| 67 : std::runtime_error("Pipe disconnected") |
| 68 { |
| 69 } |
| 70 |
66 Communication::Pipe::Pipe(const std::wstring& pipeName, Communication::Pipe::Mod
e mode) | 71 Communication::Pipe::Pipe(const std::wstring& pipeName, Communication::Pipe::Mod
e mode) |
67 { | 72 { |
68 pipe = INVALID_HANDLE_VALUE; | 73 pipe = INVALID_HANDLE_VALUE; |
69 if (mode == MODE_CREATE) | 74 if (mode == MODE_CREATE) |
70 { | 75 { |
71 SECURITY_ATTRIBUTES sa; | 76 SECURITY_ATTRIBUTES sa; |
72 memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); | 77 memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); |
73 sa.nLength = sizeof(SECURITY_ATTRIBUTES); | 78 sa.nLength = sizeof(SECURITY_ATTRIBUTES); |
74 | 79 |
75 PSECURITY_DESCRIPTOR securitydescriptor; | 80 PSECURITY_DESCRIPTOR securitydescriptor; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 Communication::InputBuffer Communication::Pipe::ReadMessage() | 126 Communication::InputBuffer Communication::Pipe::ReadMessage() |
122 { | 127 { |
123 std::stringstream stream; | 128 std::stringstream stream; |
124 std::auto_ptr<char> buffer(new char[bufferSize]); | 129 std::auto_ptr<char> buffer(new char[bufferSize]); |
125 bool doneReading = false; | 130 bool doneReading = false; |
126 while (!doneReading) | 131 while (!doneReading) |
127 { | 132 { |
128 DWORD bytesRead; | 133 DWORD bytesRead; |
129 if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0)) | 134 if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0)) |
130 doneReading = true; | 135 doneReading = true; |
131 else if (GetLastError() != ERROR_MORE_DATA) | 136 else |
132 { | 137 { |
133 std::stringstream stream; | 138 DWORD lastError = GetLastError(); |
134 stream << "Error reading from pipe: " << GetLastError(); | 139 switch (lastError) |
135 throw std::runtime_error(stream.str()); | 140 { |
| 141 case ERROR_MORE_DATA: |
| 142 break; |
| 143 case ERROR_BROKEN_PIPE: |
| 144 throw PipeDisconnectedError(); |
| 145 default: |
| 146 std::stringstream stream; |
| 147 stream << "Error reading from pipe: " << lastError; |
| 148 throw std::runtime_error(stream.str()); |
| 149 } |
136 } | 150 } |
137 stream << std::string(buffer.get(), bytesRead); | 151 stream << std::string(buffer.get(), bytesRead); |
138 } | 152 } |
139 return Communication::InputBuffer(stream.str()); | 153 return Communication::InputBuffer(stream.str()); |
140 } | 154 } |
141 | 155 |
142 void Communication::Pipe::WriteMessage(Communication::OutputBuffer& message) | 156 void Communication::Pipe::WriteMessage(Communication::OutputBuffer& message) |
143 { | 157 { |
144 DWORD bytesWritten; | 158 DWORD bytesWritten; |
145 std::string data = message.Get(); | 159 std::string data = message.Get(); |
146 if (!data.length()) | 160 if (!data.length()) |
147 return; | 161 return; |
148 if (!WriteFile(pipe, data.c_str(), data.length(), &bytesWritten, 0)) | 162 if (!WriteFile(pipe, data.c_str(), data.length(), &bytesWritten, 0)) |
149 throw std::runtime_error("Failed to write to pipe"); | 163 throw std::runtime_error("Failed to write to pipe"); |
150 } | 164 } |
OLD | NEW |