IPCCompiler: More work towards AudioServer bringup

- Add IEndpoint::handle(IMessage), a big switch table on message type.
  handle() will return a response message for synchronous messages,
  and return nullptr otherwise.
- Use i32 instead of int for everything
- Make IMessage::encode() const
- Make IEndpoint::decode_message() static, this allows template code to
  decode messages without an endpoint instance on hand.
This commit is contained in:
Andreas Kling 2019-08-03 19:21:15 +02:00
parent 18aeda2e0d
commit 3519b6c201
Notes: sideshowbarker 2024-07-19 12:54:54 +09:00
3 changed files with 40 additions and 8 deletions

View file

@ -195,7 +195,7 @@ int main(int argc, char** argv)
HashMap<String, int> message_ids;
dbg() << "enum class MessageID : int {";
dbg() << "enum class MessageID : i32 {";
for (auto& message : endpoint.messages) {
message_ids.set(message.name, message_ids.size() + 1);
dbg() << " " << message.name << " = " << message_ids.size() << ",";
@ -247,7 +247,8 @@ int main(int argc, char** argv)
dbg() << " typedef class " << response_type << " ResponseType;";
dbg() << " " << constructor_for_message(name, parameters);
dbg() << " virtual ~" << name << "() override {}";
dbg() << " virtual int id() const override { return (int)MessageID::" << name << "; }";
dbg() << " virtual i32 id() const override { return (int)MessageID::" << name << "; }";
dbg() << " static i32 static_message_id() { return (int)MessageID::" << name << "; }";
dbg() << " virtual String name() const override { return \"" << endpoint.name << "::" << name << "\"; }";
dbg() << " static OwnPtr<" << name << "> decode(BufferStream& stream)";
dbg() << " {";
@ -261,8 +262,9 @@ int main(int argc, char** argv)
initial_value = "false";
dbg() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
dbg() << " stream >> " << parameter.name << ";";
dbg() << " if (stream.handle_read_failure())";
dbg() << " if (stream.handle_read_failure()) {";
dbg() << " return nullptr;";
dbg() << " }";
}
StringBuilder builder;
@ -274,7 +276,7 @@ int main(int argc, char** argv)
}
dbg() << " return make<" << name << ">(" << builder.to_string() << ");";
dbg() << " }";
dbg() << " virtual ByteBuffer encode() override";
dbg() << " virtual ByteBuffer encode() const override";
dbg() << " {";
// FIXME: Support longer messages:
dbg() << " auto buffer = ByteBuffer::create_uninitialized(1024);";
@ -312,10 +314,10 @@ int main(int argc, char** argv)
dbg() << " " << endpoint.name << "Endpoint() {}";
dbg() << " virtual ~" << endpoint.name << "Endpoint() override {}";
dbg() << " virtual String name() const override { return \"" << endpoint.name << "\"; };";
dbg() << " virtual OwnPtr<IMessage> decode_message(const ByteBuffer& buffer)";
dbg() << " static OwnPtr<IMessage> decode_message(const ByteBuffer& buffer)";
dbg() << " {";
dbg() << " BufferStream stream(const_cast<ByteBuffer&>(buffer));";
dbg() << " int message_id = 0;";
dbg() << " i32 message_id = 0;";
dbg() << " stream >> message_id;";
dbg() << " switch (message_id) {";
for (auto& message : endpoint.messages) {
@ -333,6 +335,28 @@ int main(int argc, char** argv)
dbg() << " }";
dbg() << " }";
dbg();
dbg() << " virtual OwnPtr<IMessage> handle(const IMessage& message) override";
dbg() << " {";
dbg() << " switch (message.id()) {";
for (auto& message : endpoint.messages) {
auto do_decode_message = [&](const String& name, bool returns_something) {
dbg() << " case (int)" << endpoint.name << "::MessageID::" << name << ":";
if (returns_something) {
dbg() << " return handle(static_cast<const " << endpoint.name << "::" << name << "&>(message));";
} else {
dbg() << " handle(static_cast<const " << endpoint.name << "::" << name << "&>(message));";
dbg() << " return nullptr;";
}
};
do_decode_message(message.name, message.is_synchronous);
if (message.is_synchronous)
do_decode_message(message.response_name(), false);
}
dbg() << " default:";
dbg() << " return nullptr;";
dbg() << " }";
dbg() << " }";
for (auto& message : endpoint.messages) {
String return_type = "void";
@ -344,7 +368,7 @@ int main(int argc, char** argv)
builder.append("Response");
return_type = builder.to_string();
}
dbg() << " virtual " << return_type << " handle(const " << endpoint.name << "::" << message.name << "&) = 0;";
dbg() << " virtual OwnPtr<" << return_type << "> handle(const " << endpoint.name << "::" << message.name << "&) = 0;";
}
dbg() << "private:";

View file

@ -1,12 +1,20 @@
#pragma once
#include <AK/AKString.h>
#include <AK/OwnPtr.h>
namespace AK {
class BufferStream;
}
class IMessage;
class IEndpoint {
public:
virtual ~IEndpoint();
virtual String name() const = 0;
virtual OwnPtr<IMessage> handle(const IMessage&) = 0;
protected:
IEndpoint();

View file

@ -9,7 +9,7 @@ public:
virtual int id() const = 0;
virtual String name() const = 0;
virtual ByteBuffer encode() = 0;
virtual ByteBuffer encode() const = 0;
protected:
IMessage();