Files
flipper/xplat/Flipper/FlipperConnectionManagerImpl.h
Lorenzo Blasa e42db220ee Socket connect no longer synchronous and blocking
Summary:
Never really liked this code. Before this change, calls to connect were blocking.

Because of this, we had to make use of promises and a bit of really not that good-looking code.

So, this change makes connect non-blocking meaning that we make full use of our event handler.

These changes contain:
- CSR is not getting generated after each failed attempt.
- Connect is no longer blocking.
- Do not report events via the handler when explicitly disconnecting.

Reviewed By: jknoxville

Differential Revision: D46853228

fbshipit-source-id: 00e6a9c7c039a756175fe14982959e078d92bacb
2023-06-28 12:09:58 -07:00

101 lines
2.5 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.
*/
#pragma once
#include <mutex>
#include "FlipperConnectionManager.h"
#include "FlipperInitConfig.h"
#include "FlipperScheduler.h"
#include "FlipperSocket.h"
#include "FlipperState.h"
namespace facebook {
namespace flipper {
class ConnectionEvents;
class ConnectionContextStore;
class FlipperRSocketResponder;
class FlipperConnectionManagerWrapper;
class FlipperConnectionManagerImpl : public FlipperConnectionManager {
friend ConnectionEvents;
public:
FlipperConnectionManagerImpl(
FlipperInitConfig config,
std::shared_ptr<FlipperState> state,
std::shared_ptr<ConnectionContextStore> contextStore);
~FlipperConnectionManagerImpl();
void start() override;
void stop() override;
bool isConnected() const override;
void setCallbacks(Callbacks* callbacks) override;
void sendMessage(const folly::dynamic& message) override;
void sendMessageRaw(const std::string& message) override;
void onMessageReceived(
const folly::dynamic& message,
std::unique_ptr<FlipperResponder> responder) override;
void reconnect();
void setCertificateProvider(
const std::shared_ptr<FlipperCertificateProvider> provider) override;
std::shared_ptr<FlipperCertificateProvider> getCertificateProvider() override;
private:
bool isConnected_ = false;
bool started_ = false;
bool isConnectionTrusted_ = false;
std::shared_ptr<FlipperCertificateProvider> certificateProvider_ = nullptr;
Callbacks* callbacks_;
DeviceData deviceData_;
std::shared_ptr<FlipperState> state_;
int insecurePort;
int securePort;
int altInsecurePort;
int altSecurePort;
Scheduler* scheduler_;
Scheduler* connectionScheduler_;
std::unique_ptr<FlipperSocket> socket_;
int failedConnectionAttempts_ = 0;
std::shared_ptr<ConnectionContextStore> store_;
std::shared_ptr<FlipperConnectionManagerWrapper> implWrapper_;
void startSync();
void connectAndExchangeCertificate();
void connectSecurely();
void handleSocketEvent(const SocketEvent event);
bool isCertificateExchangeNeeded();
void requestSignedCertificate();
void processSignedCertificateResponse(
std::shared_ptr<FlipperStep> gettingCertificateStep,
std::string response,
bool isError);
bool isRunningInOwnThread();
void reevaluateSocketProvider();
std::string getDeviceId();
};
} // namespace flipper
} // namespace facebook