1 // Copyright 2020 The Dawn Authors 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 // http://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 #ifndef DAWNWIRE_CHUNKEDCOMMANDHANDLER_H_ 16 #define DAWNWIRE_CHUNKEDCOMMANDHANDLER_H_ 17 18 #include "common/Assert.h" 19 #include "dawn_wire/Wire.h" 20 #include "dawn_wire/WireCmd_autogen.h" 21 22 #include <cstdint> 23 #include <memory> 24 25 namespace dawn_wire { 26 27 class ChunkedCommandHandler : public CommandHandler { 28 public: 29 const volatile char* HandleCommands(const volatile char* commands, size_t size) override; 30 ~ChunkedCommandHandler() override; 31 32 protected: 33 enum class ChunkedCommandsResult { 34 Passthrough, 35 Consumed, 36 Error, 37 }; 38 39 // Returns |true| if the commands were entirely consumed into the chunked command vector 40 // and should be handled later once we receive all the command data. 41 // Returns |false| if commands should be handled now immediately. HandleChunkedCommands(const volatile char * commands,size_t size)42 ChunkedCommandsResult HandleChunkedCommands(const volatile char* commands, size_t size) { 43 uint64_t commandSize64 = 44 reinterpret_cast<const volatile CmdHeader*>(commands)->commandSize; 45 46 if (commandSize64 > std::numeric_limits<size_t>::max()) { 47 return ChunkedCommandsResult::Error; 48 } 49 size_t commandSize = static_cast<size_t>(commandSize64); 50 if (size < commandSize) { 51 return BeginChunkedCommandData(commands, commandSize, size); 52 } 53 return ChunkedCommandsResult::Passthrough; 54 } 55 56 private: 57 virtual const volatile char* HandleCommandsImpl(const volatile char* commands, 58 size_t size) = 0; 59 60 ChunkedCommandsResult BeginChunkedCommandData(const volatile char* commands, 61 size_t commandSize, 62 size_t initialSize); 63 64 size_t mChunkedCommandRemainingSize = 0; 65 size_t mChunkedCommandPutOffset = 0; 66 std::unique_ptr<char[]> mChunkedCommandData; 67 }; 68 69 } // namespace dawn_wire 70 71 #endif // DAWNWIRE_CHUNKEDCOMMANDHANDLER_H_ 72