• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //* Copyright 2019 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 #include "common/Assert.h"
16 #include "dawn_wire/client/Client.h"
17 
18 #include <string>
19 
20 namespace dawn_wire { namespace client {
21     {% for command in cmd_records["return command"] %}
22         bool Client::Handle{{command.name.CamelCase()}}(DeserializeBuffer* deserializeBuffer) {
23             Return{{command.name.CamelCase()}}Cmd cmd;
24             WireResult deserializeResult = cmd.Deserialize(deserializeBuffer, &mAllocator);
25 
26             if (deserializeResult == WireResult::FatalError) {
27                 return false;
28             }
29 
30             {% for member in command.members if member.handle_type %}
31                 {% set Type = member.handle_type.name.CamelCase() %}
32                 {% set name = as_varName(member.name) %}
33 
34                 {% if member.type.dict_name == "ObjectHandle" %}
35                     {{Type}}* {{name}} = {{Type}}Allocator().GetObject(cmd.{{name}}.id);
36                     uint32_t {{name}}Generation = {{Type}}Allocator().GetGeneration(cmd.{{name}}.id);
37                     if ({{name}}Generation != cmd.{{name}}.generation) {
38                         {{name}} = nullptr;
39                     }
40                 {% endif %}
41             {% endfor %}
42 
43             return Do{{command.name.CamelCase()}}(
44                 {%- for member in command.members -%}
45                     {%- if member.handle_type -%}
46                         {{as_varName(member.name)}}
47                     {%- else -%}
48                         cmd.{{as_varName(member.name)}}
49                     {%- endif -%}
50                     {%- if not loop.last -%}, {% endif %}
51                 {%- endfor -%}
52             );
53         }
54     {% endfor %}
55 
56     const volatile char* Client::HandleCommandsImpl(const volatile char* commands, size_t size) {
57         DeserializeBuffer deserializeBuffer(commands, size);
58 
59         while (deserializeBuffer.AvailableSize() >= sizeof(CmdHeader) + sizeof(ReturnWireCmd)) {
60             // Start by chunked command handling, if it is done, then it means the whole buffer
61             // was consumed by it, so we return a pointer to the end of the commands.
62             switch (HandleChunkedCommands(deserializeBuffer.Buffer(), deserializeBuffer.AvailableSize())) {
63                 case ChunkedCommandsResult::Consumed:
64                     return commands + size;
65                 case ChunkedCommandsResult::Error:
66                     return nullptr;
67                 case ChunkedCommandsResult::Passthrough:
68                     break;
69             }
70 
71             ReturnWireCmd cmdId = *static_cast<const volatile ReturnWireCmd*>(static_cast<const volatile void*>(
72                 deserializeBuffer.Buffer() + sizeof(CmdHeader)));
73             bool success = false;
74             switch (cmdId) {
75                 {% for command in cmd_records["return command"] %}
76                     {% set Suffix = command.name.CamelCase() %}
77                     case ReturnWireCmd::{{Suffix}}:
78                         success = Handle{{Suffix}}(&deserializeBuffer);
79                         break;
80                 {% endfor %}
81                 default:
82                     success = false;
83             }
84 
85             if (!success) {
86                 return nullptr;
87             }
88             mAllocator.Reset();
89         }
90 
91         if (deserializeBuffer.AvailableSize() != 0) {
92             return nullptr;
93         }
94 
95         return commands;
96     }
97 }}  // namespace dawn_wire::client
98