| 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "dap/io.h" |
| 16 | |
| 17 | #include <atomic> |
| 18 | #include <condition_variable> |
| 19 | #include <cstdarg> |
| 20 | #include <cstdio> |
| 21 | #include <cstring> // strlen |
| 22 | #include <deque> |
| 23 | #include <mutex> |
| 24 | #include <string> |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class Pipe : public dap::ReaderWriter { |
| 29 | public: |
| 30 | // dap::ReaderWriter compliance |
| 31 | bool isOpen() override { |
| 32 | std::unique_lock<std::mutex> lock(mutex); |
| 33 | return !closed; |
| 34 | } |
| 35 | |
| 36 | void close() override { |
| 37 | std::unique_lock<std::mutex> lock(mutex); |
| 38 | closed = true; |
| 39 | cv.notify_all(); |
| 40 | } |
| 41 | |
| 42 | size_t read(void* buffer, size_t bytes) override { |
| 43 | std::unique_lock<std::mutex> lock(mutex); |
| 44 | auto out = reinterpret_cast<uint8_t*>(buffer); |
| 45 | size_t n = 0; |
| 46 | while (true) { |
| 47 | cv.wait(lock, [&] { return closed || data.size() > 0; }); |
| 48 | if (closed) { |
| 49 | return n; |
| 50 | } |
| 51 | for (; n < bytes && data.size() > 0; n++) { |
| 52 | out[n] = data.front(); |
| 53 | data.pop_front(); |
| 54 | } |
| 55 | if (n == bytes) { |
| 56 | return n; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool write(const void* buffer, size_t bytes) override { |
| 62 | std::unique_lock<std::mutex> lock(mutex); |
| 63 | if (closed) { |
| 64 | return false; |
| 65 | } |
| 66 | if (bytes == 0) { |
| 67 | return true; |
| 68 | } |
| 69 | auto notify = data.size() == 0; |
| 70 | auto src = reinterpret_cast<const uint8_t*>(buffer); |
| 71 | for (size_t i = 0; i < bytes; i++) { |
| 72 | data.emplace_back(src[i]); |
| 73 | } |
| 74 | if (notify) { |
| 75 | cv.notify_all(); |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | std::mutex mutex; |
| 82 | std::condition_variable cv; |
| 83 | std::deque<uint8_t> data; |
| 84 | bool closed = false; |
| 85 | }; |
| 86 | |
| 87 | class RW : public dap::ReaderWriter { |
| 88 | public: |
| 89 | RW(const std::shared_ptr<Reader>& r, const std::shared_ptr<Writer>& w) |
| 90 | : r(r), w(w) {} |
| 91 | |
| 92 | // dap::ReaderWriter compliance |
| 93 | bool isOpen() override { return r->isOpen() && w->isOpen(); } |
| 94 | void close() override { |
| 95 | r->close(); |
| 96 | w->close(); |
| 97 | } |
| 98 | size_t read(void* buffer, size_t n) override { return r->read(buffer, n); } |
| 99 | bool write(const void* buffer, size_t n) override { |
| 100 | return w->write(buffer, n); |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | const std::shared_ptr<dap::Reader> r; |
| 105 | const std::shared_ptr<dap::Writer> w; |
| 106 | }; |
| 107 | |
| 108 | class File : public dap::ReaderWriter { |
| 109 | public: |
| 110 | File(FILE* f, bool closable) : f(f), closable(closable) {} |
| 111 | |
| 112 | ~File() { close(); } |
| 113 | |
| 114 | // dap::ReaderWriter compliance |
| 115 | bool isOpen() override { return !closed; } |
| 116 | void close() override { |
| 117 | if (closable) { |
| 118 | if (!closed.exchange(true)) { |
| 119 | fclose(f); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | size_t read(void* buffer, size_t n) override { |
| 124 | std::unique_lock<std::mutex> lock(readMutex); |
| 125 | auto out = reinterpret_cast<char*>(buffer); |
| 126 | for (size_t i = 0; i < n; i++) { |
| 127 | int c = fgetc(f); |
| 128 | if (c == EOF) { |
| 129 | return i; |
| 130 | } |
| 131 | out[i] = char(c); |
| 132 | } |
| 133 | return n; |
| 134 | } |
| 135 | bool write(const void* buffer, size_t n) override { |
| 136 | std::unique_lock<std::mutex> lock(writeMutex); |
| 137 | if (fwrite(buffer, 1, n, f) == n) { |
| 138 | fflush(f); |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | FILE* const f; |
| 146 | const bool closable; |
| 147 | std::mutex readMutex; |
| 148 | std::mutex writeMutex; |
| 149 | std::atomic<bool> closed = {false}; |
| 150 | }; |
| 151 | |
| 152 | class ReaderSpy : public dap::Reader { |
| 153 | public: |
| 154 | ReaderSpy(const std::shared_ptr<dap::Reader>& r, |
| 155 | const std::shared_ptr<dap::Writer>& s, |
| 156 | const std::string& prefix) |
| 157 | : r(r), s(s), prefix(prefix) {} |
| 158 | |
| 159 | // dap::Reader compliance |
| 160 | bool isOpen() override { return r->isOpen(); } |
| 161 | void close() override { r->close(); } |
| 162 | size_t read(void* buffer, size_t n) override { |
| 163 | auto c = r->read(buffer, n); |
| 164 | if (c > 0) { |
| 165 | auto chars = reinterpret_cast<const char*>(buffer); |
| 166 | std::string buf = prefix; |
| 167 | buf.append(chars, chars + c); |
| 168 | s->write(buf.data(), buf.size()); |
| 169 | } |
| 170 | return c; |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | const std::shared_ptr<dap::Reader> r; |
| 175 | const std::shared_ptr<dap::Writer> s; |
| 176 | const std::string prefix; |
| 177 | }; |
| 178 | |
| 179 | class WriterSpy : public dap::Writer { |
| 180 | public: |
| 181 | WriterSpy(const std::shared_ptr<dap::Writer>& w, |
| 182 | const std::shared_ptr<dap::Writer>& s, |
| 183 | const std::string& prefix) |
| 184 | : w(w), s(s), prefix(prefix) {} |
| 185 | |
| 186 | // dap::Writer compliance |
| 187 | bool isOpen() override { return w->isOpen(); } |
| 188 | void close() override { w->close(); } |
| 189 | bool write(const void* buffer, size_t n) override { |
| 190 | if (!w->write(buffer, n)) { |
| 191 | return false; |
| 192 | } |
| 193 | auto chars = reinterpret_cast<const char*>(buffer); |
| 194 | std::string buf = prefix; |
| 195 | buf.append(chars, chars + n); |
| 196 | s->write(buf.data(), buf.size()); |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | const std::shared_ptr<dap::Writer> w; |
| 202 | const std::shared_ptr<dap::Writer> s; |
| 203 | const std::string prefix; |
| 204 | }; |
| 205 | |
| 206 | } // anonymous namespace |
| 207 | |
| 208 | namespace dap { |
| 209 | |
| 210 | std::shared_ptr<ReaderWriter> ReaderWriter::create( |
| 211 | const std::shared_ptr<Reader>& r, |
| 212 | const std::shared_ptr<Writer>& w) { |
| 213 | return std::make_shared<RW>(r, w); |
| 214 | } |
| 215 | |
| 216 | std::shared_ptr<ReaderWriter> pipe() { |
| 217 | return std::make_shared<Pipe>(); |
| 218 | } |
| 219 | |
| 220 | std::shared_ptr<ReaderWriter> file(FILE* f, bool closable /* = true */) { |
| 221 | return std::make_shared<File>(f, closable); |
| 222 | } |
| 223 | |
| 224 | std::shared_ptr<ReaderWriter> file(const char* path) { |
| 225 | if (auto f = fopen(path, "wb" )) { |
| 226 | return std::make_shared<File>(f, true); |
| 227 | } |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | // spy() returns a Reader that copies all reads from the Reader r to the Writer |
| 232 | // s, using the given optional prefix. |
| 233 | std::shared_ptr<Reader> spy(const std::shared_ptr<Reader>& r, |
| 234 | const std::shared_ptr<Writer>& s, |
| 235 | const char* prefix /* = "\n<-" */) { |
| 236 | return std::make_shared<ReaderSpy>(r, s, prefix); |
| 237 | } |
| 238 | |
| 239 | // spy() returns a Writer that copies all writes to the Writer w to the Writer |
| 240 | // s, using the given optional prefix. |
| 241 | std::shared_ptr<Writer> spy(const std::shared_ptr<Writer>& w, |
| 242 | const std::shared_ptr<Writer>& s, |
| 243 | const char* prefix /* = "\n->" */) { |
| 244 | return std::make_shared<WriterSpy>(w, s, prefix); |
| 245 | } |
| 246 | |
| 247 | bool writef(const std::shared_ptr<Writer>& w, const char* msg, ...) { |
| 248 | char buf[2048]; |
| 249 | |
| 250 | va_list vararg; |
| 251 | va_start(vararg, msg); |
| 252 | vsnprintf(buf, sizeof(buf), msg, vararg); |
| 253 | va_end(vararg); |
| 254 | |
| 255 | return w->write(buf, strlen(buf)); |
| 256 | } |
| 257 | |
| 258 | } // namespace dap |
| 259 | |