Files
flipper/xplat/FlipperWebSocket/FlipperWebSocket.cpp
Lorenzo Blasa e44cad5e99 Partially remove dependency on folly async
Summary:
This change isolates the usage of folly async from Flipper. Is now self-contained in Flipper Folly schedulers.

Users of Flipper can decide not to use the types defined in that header and implement their own.

NOTE: changes are minimal, we are just replacing direct calls to folly event base with a scheduler which simply relays this on to folly.

Reviewed By: fabiomassimo

Differential Revision: D36626483

fbshipit-source-id: add0241caf4af0aa5c3b5c2e7efc2e725f5400ab
2022-05-25 15:58:05 -07:00

98 lines
2.6 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifdef FB_SONARKIT_ENABLED
#include "FlipperWebSocket.h"
#include <Flipper/ConnectionContextStore.h>
#include <Flipper/FlipperTransportTypes.h>
#include <Flipper/FlipperURLSerializer.h>
#include <Flipper/Log.h>
#include <folly/String.h>
#include <folly/futures/Future.h>
#include <folly/json.h>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include "WebSocketClient.h"
#include "WebSocketTLSClient.h"
namespace facebook {
namespace flipper {
FlipperWebSocket::FlipperWebSocket(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
Scheduler* scheduler)
: FlipperWebSocket(
std::move(endpoint),
std::move(payload),
scheduler,
nullptr) {}
FlipperWebSocket::FlipperWebSocket(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
Scheduler* scheduler,
ConnectionContextStore* connectionContextStore) {
if (endpoint.secure) {
socket_ = std::make_unique<WebSocketTLSClient>(
endpoint, std::move(payload), scheduler, connectionContextStore);
} else {
socket_ = std::make_unique<WebSocketClient>(
endpoint, std::move(payload), scheduler, connectionContextStore);
}
}
FlipperWebSocket::~FlipperWebSocket() {}
void FlipperWebSocket::setEventHandler(SocketEventHandler eventHandler) {
socket_->setEventHandler(eventHandler);
}
void FlipperWebSocket::setMessageHandler(SocketMessageHandler messageHandler) {
socket_->setMessageHandler(messageHandler);
}
bool FlipperWebSocket::connect(FlipperConnectionManager* manager) {
return socket_->connect(manager);
}
void FlipperWebSocket::disconnect() {
socket_->disconnect();
}
void FlipperWebSocket::send(
const folly::dynamic& message,
SocketSendHandler completion) {
socket_->send(message, completion);
}
void FlipperWebSocket::send(
const std::string& message,
SocketSendHandler completion) {
socket_->send(message, completion);
}
/**
Only ever used for insecure connections to receive the device_id from a
signCertificate request. If the intended usage ever changes, then a better
approach needs to be put in place.
*/
void FlipperWebSocket::sendExpectResponse(
const std::string& message,
SocketSendExpectResponseHandler completion) {
socket_->sendExpectResponse(message, completion);
}
} // namespace flipper
} // namespace facebook
#endif