C++ WebSocket client for Flipper

Summary:
Introducing a Flipper WebSocket client implemented in C++.
The requirement came from Spark AR (Skylight) as they have a macOS/Linux/Windows clients.

For reviewers:

- This is an implementation of the existing FlipperSocket interface. Effectively, the only type that needs to be reviewed is WebSocketTLSClient.

- BaseClient defined a base class for WebSocketClient and WebSocketTLSClient.

- WebSocketClient is a simplified version of WebSocketTLSClient as there's no TLS configuration.

Reviewed By: mweststrate

Differential Revision: D34081943

fbshipit-source-id: 619a83f5a6783a21069d0f5111d139bb180f9e97
This commit is contained in:
Lorenzo Blasa
2022-02-10 05:23:40 -08:00
committed by Facebook GitHub Bot
parent 8696349593
commit b3cf7e1ad1
7 changed files with 1011 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
/*
* 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/io/async/AsyncSocketException.h>
#include <folly/io/async/SSLContext.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,
folly::EventBase* eventBase)
: FlipperWebSocket(
std::move(endpoint),
std::move(payload),
eventBase,
nullptr) {}
FlipperWebSocket::FlipperWebSocket(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase,
ConnectionContextStore* connectionContextStore) {
if (endpoint.secure) {
socket_ = std::make_unique<WebSocketTLSClient>(
endpoint, std::move(payload), eventBase, connectionContextStore);
} else {
socket_ = std::make_unique<WebSocketClient>(
endpoint, std::move(payload), eventBase, 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