Add requestResponse handler for incoming calls
Summary: Flipper exposes a call() api to plugins which lets them call their sdk component, and it returns a promise with the response. Currently this is done by sending a fireAndForget request, noting the id of the request, and then receiving fireAndForget requests and matching up the ids to give the result back to the right plugin promise. Instead, it will be simpler to use rsocket requestResponse, instead of fireAndForget, which is for this exact use case. This diff adds a requestResponse handler to the SDK, so that it can deal with such requests and respond accordingly, while preserving the current functionality if it receives a fireAndForget. So this part is backwards compatible and should be safe to land in isolation. A later diff will change the desktop app to use requestResponse, which may not be backwards compatible, so that will have to be deployed more carefully. Reviewed By: passy Differential Revision: D13974049 fbshipit-source-id: b371d94a86b1f186375161ed8f2242a462ce418f
This commit is contained in:
committed by
Facebook Github Bot
parent
8f6138a41c
commit
4a3de26a88
@@ -16,8 +16,11 @@
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
#include "ConnectionContextStore.h"
|
||||
#include "FireAndForgetBasedFlipperResponder.h"
|
||||
#include "FlipperResponderImpl.h"
|
||||
#include "FlipperStep.h"
|
||||
#include "Log.h"
|
||||
#include "yarpl/Single.h"
|
||||
|
||||
#define WRONG_THREAD_EXIT_MSG \
|
||||
"ERROR: Aborting flipper initialization because it's not running in the flipper thread."
|
||||
@@ -30,6 +33,8 @@ static constexpr int maxPayloadSize = 0xFFFFFF;
|
||||
namespace facebook {
|
||||
namespace flipper {
|
||||
|
||||
rsocket::Payload toRSocketPayload(dynamic data);
|
||||
|
||||
class ConnectionEvents : public rsocket::RSocketConnectionEvents {
|
||||
private:
|
||||
FlipperConnectionManagerImpl* websocket_;
|
||||
@@ -72,7 +77,54 @@ class Responder : public rsocket::RSocketResponder {
|
||||
rsocket::Payload request,
|
||||
rsocket::StreamId streamId) {
|
||||
const auto payload = request.moveDataToString();
|
||||
websocket_->callbacks_->onMessageReceived(folly::parseJson(payload));
|
||||
std::unique_ptr<FireAndForgetBasedFlipperResponder> responder;
|
||||
auto message = folly::parseJson(payload);
|
||||
if (message.find("id") != message.items().end()) {
|
||||
auto id = message["id"].getInt();
|
||||
responder =
|
||||
std::make_unique<FireAndForgetBasedFlipperResponder>(websocket_, id);
|
||||
}
|
||||
|
||||
websocket_->callbacks_->onMessageReceived(
|
||||
folly::parseJson(payload), std::move(responder));
|
||||
}
|
||||
|
||||
std::shared_ptr<yarpl::single::Single<rsocket::Payload>>
|
||||
handleRequestResponse(rsocket::Payload request, rsocket::StreamId streamId) {
|
||||
const auto requestString = request.moveDataToString();
|
||||
|
||||
auto dynamicSingle = yarpl::single::Single<folly::dynamic>::create(
|
||||
[payload = std::move(requestString), this](auto observer) {
|
||||
auto responder = std::make_unique<FlipperResponderImpl>(observer);
|
||||
websocket_->callbacks_->onMessageReceived(
|
||||
folly::parseJson(payload), std::move(responder));
|
||||
});
|
||||
|
||||
auto rsocketSingle = yarpl::single::Single<rsocket::Payload>::create(
|
||||
[payload = std::move(requestString), dynamicSingle, this](
|
||||
auto observer) {
|
||||
observer->onSubscribe(
|
||||
yarpl::single::SingleSubscriptions::empty());
|
||||
dynamicSingle->subscribe(
|
||||
[observer, this](folly::dynamic d) {
|
||||
websocket_->connectionEventBase_->runInEventBaseThread(
|
||||
[observer, d]() {
|
||||
try {
|
||||
observer->onSuccess(toRSocketPayload(d));
|
||||
|
||||
} catch (std::exception& e) {
|
||||
log(e.what());
|
||||
observer->onError(e);
|
||||
}
|
||||
});
|
||||
},
|
||||
[observer, this](folly::exception_wrapper e) {
|
||||
websocket_->connectionEventBase_->runInEventBaseThread(
|
||||
[observer, e]() { observer->onError(e); });
|
||||
});
|
||||
});
|
||||
|
||||
return rsocketSingle;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -236,24 +288,18 @@ void FlipperConnectionManagerImpl::setCallbacks(Callbacks* callbacks) {
|
||||
|
||||
void FlipperConnectionManagerImpl::sendMessage(const folly::dynamic& message) {
|
||||
flipperEventBase_->add([this, message]() {
|
||||
std::string json = folly::toJson(message);
|
||||
rsocket::Payload payload = rsocket::Payload(json);
|
||||
auto payloadLength = payload.data->computeChainDataLength();
|
||||
|
||||
DCHECK_LE(payloadLength, maxPayloadSize);
|
||||
if (payloadLength > maxPayloadSize) {
|
||||
auto logMessage =
|
||||
std::string(
|
||||
"Error: Skipping sending message larger than max rsocket payload: ") +
|
||||
json;
|
||||
log(logMessage);
|
||||
try {
|
||||
rsocket::Payload payload = toRSocketPayload(message);
|
||||
if (client_) {
|
||||
client_->getRequester()
|
||||
->fireAndForget(std::move(payload))
|
||||
->subscribe([]() {});
|
||||
}
|
||||
} catch (std::length_error& e) {
|
||||
// Skip sending messages that are too large.
|
||||
log(e.what());
|
||||
return;
|
||||
}
|
||||
if (client_) {
|
||||
client_->getRequester()
|
||||
->fireAndForget(std::move(payload))
|
||||
->subscribe([]() {});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -341,5 +387,21 @@ bool FlipperConnectionManagerImpl::isRunningInOwnThread() {
|
||||
return flipperEventBase_->isInEventBaseThread();
|
||||
}
|
||||
|
||||
rsocket::Payload toRSocketPayload(dynamic data) {
|
||||
std::string json = folly::toJson(data);
|
||||
rsocket::Payload payload = rsocket::Payload(json);
|
||||
auto payloadLength = payload.data->computeChainDataLength();
|
||||
|
||||
DCHECK_LE(payloadLength, maxPayloadSize);
|
||||
if (payloadLength > maxPayloadSize) {
|
||||
auto logMessage =
|
||||
std::string(
|
||||
"Error: Skipping sending message larger than max rsocket payload: ") +
|
||||
json;
|
||||
throw new std::length_error(logMessage);
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
} // namespace flipper
|
||||
} // namespace facebook
|
||||
|
||||
Reference in New Issue
Block a user