jsonrpcx / json-rpc-cxx

JSON-RPC for modern C++
MIT License
240 stars 43 forks source link

Transport agnostic interfaces #34

Open tinu73 opened 2 years ago

tinu73 commented 2 years ago

First of all this is really a great and useful framework.

I would like to raise the question or rather a discussion weather the interfaces are transport agnostic yes or no. If i am right in my conclusion they are not. Why?

iclientconnector.hpp

using MessageHandler = std::function<void(const std::string)>;
virtual void Post(const std::string &request) = 0;
MessageHandler OnMessage;

client.hpp

using MessageHandler = std::function<void(const JsonRpcResponse)>;
JsonRpcClient(IClientConnector &connector, version v) : connector(connector), v(v)
{
    connector.OnMessage = [this](const std::string &message)
    {
        json response = json::parse(message);
        JsonRpcResponse rpc_response{};
        ...
        OnMessage(rpc_response);
    }
}
...
MessageHandler OnMessage;

client.cpp

std::future<JsonRpcResponse> future;
std::promise<JsonRpcResponse> promise;
JsonRpcResponse response;
client.OnMessage = [&] (const JsonRpcResponse &response) mutable
{
    promise.set_value(response);
};
future = promise.get_future();
client.CallMethod(1, "GetProduct", {"0xff"});
response = future.get();
std::cout << response.result.dump() << std::endl;
cinemast commented 2 years ago

Hi!

I am not sure I fully understood your issue with the synchronous interface. If you actually need async semantics, you can always wrap it into future/promises, right? Just introducing them on the connector level does not really make sense in my opinions, since the calling semantics through the RPC client will be synchronous again.

Could you explain in a bit more detail, maybe even using the TCP connector example what exactly would be easier/better with async interfaces?