Files
flipper/react-native/react-native-flipper/windows/ReactNativeFlipper/FlipperReactSocketClient.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

80 lines
2.7 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 <folly/dynamic.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Networking.Sockets.h>
#include <future>
#include <memory>
#include <mutex>
#include "../../../../xplat/Flipper/FlipperSocket.h"
#include "../../../../xplat/Flipper/FlipperSocketProvider.h"
#include "../../../../xplat/Flipper/FlipperTransportTypes.h"
#include "FlipperReactBaseSocket.h"
namespace facebook {
namespace flipper {
class FlipperConnectionManager;
class ConnectionContextStore;
class FlipperReactSocketClient : public FlipperReactBaseSocket {
public:
FlipperReactSocketClient(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
Scheduler* scheduler);
FlipperReactSocketClient(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
Scheduler* scheduler,
ConnectionContextStore* connectionContextStore);
FlipperReactSocketClient(const FlipperReactSocketClient&) = delete;
FlipperReactSocketClient& operator=(const FlipperReactSocketClient&) = delete;
virtual ~FlipperReactSocketClient();
virtual void connect(FlipperConnectionManager* manager) override;
virtual void disconnect() override;
virtual void send(const folly::dynamic& message, SocketSendHandler completion)
override;
virtual void send(const std::string& message, SocketSendHandler completion)
override;
virtual void sendExpectResponse(
const std::string& message,
SocketSendExpectResponseHandler completion) override;
void OnWebSocketMessageReceived(
winrt::Windows::Networking::Sockets::MessageWebSocket const& /* sender */,
winrt::Windows::Networking::Sockets::
MessageWebSocketMessageReceivedEventArgs const& args);
void OnWebSocketClosed(
winrt::Windows::Networking::Sockets::IWebSocket const& /* sender */,
winrt::Windows::Networking::Sockets::WebSocketClosedEventArgs const&
args);
private:
winrt::Windows::Networking::Sockets::MessageWebSocket socket_;
winrt::event_token messageReceivedEventToken_;
winrt::event_token closedEventToken_;
winrt::Windows::Security::Cryptography::Certificates::Certificate
findClientCertificateFromStore();
winrt::Windows::Security::Cryptography::Certificates::Certificate
installClientCertificate();
winrt::Windows::Security::Cryptography::Certificates::Certificate
getClientCertificate();
std::unique_ptr<SocketSendExpectResponseHandler> overrideHandler_;
};
} // namespace flipper
} // namespace facebook