1 // Copyright (c) 2011-2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #ifndef WINPTY_SHARED_BUFFER_H
22 #define WINPTY_SHARED_BUFFER_H
32 #include "WinptyException.h"
36 std::vector<char> m_buf;
41 template <typename T> void putRawValue(const T &t) {
42 putRawData(&t, sizeof(t));
44 template <typename T> void replaceRawValue(size_t pos, const T &t) {
45 replaceRawData(pos, &t, sizeof(t));
48 void putRawData(const void *data, size_t len);
49 void replaceRawData(size_t pos, const void *data, size_t len);
50 void putInt32(int32_t i);
51 void putInt64(int64_t i);
52 void putWString(const wchar_t *str, size_t len);
53 void putWString(const wchar_t *str) { putWString(str, wcslen(str)); }
54 void putWString(const std::wstring &str) { putWString(str.data(), str.size()); }
55 std::vector<char> &buf() { return m_buf; }
57 // MSVC 2013 does not generate these automatically, so help it out.
58 WriteBuffer(WriteBuffer &&other) : m_buf(std::move(other.m_buf)) {}
59 WriteBuffer &operator=(WriteBuffer &&other) {
60 m_buf = std::move(other.m_buf);
67 class DecodeError : public WinptyException {
68 virtual const wchar_t *what() const WINPTY_NOEXCEPT override {
69 return L"DecodeError: RPC message decoding error";
74 std::vector<char> m_buf;
78 explicit ReadBuffer(std::vector<char> &&buf) : m_buf(std::move(buf)) {}
80 template <typename T> T getRawValue() {
82 getRawData(&ret, sizeof(ret));
86 void getRawData(void *data, size_t len);
89 std::wstring getWString();
92 // MSVC 2013 does not generate these automatically, so help it out.
93 ReadBuffer(ReadBuffer &&other) :
94 m_buf(std::move(other.m_buf)), m_off(other.m_off) {}
95 ReadBuffer &operator=(ReadBuffer &&other) {
96 m_buf = std::move(other.m_buf);
102 #endif // WINPTY_SHARED_BUFFER_H