Commit Graph

17 Commits

Author SHA1 Message Date
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
Lorenzo Blasa
e9c0a459dd Remove connection timeout
Summary:
This change mainly removes the connection timeout period as it may introduce a race condition in which slow server connections get terminated potentially creating connection loops.

Also, suspend and wait for all operations to complete once a socket is disconnected.

Reviewed By: ivanmisuno

Differential Revision: D43048252

fbshipit-source-id: 64c28a3d3d2fd4e065084d5f55a17444385c07e0
2023-02-07 05:55:11 -08:00
Lorenzo Blasa
2ba167f899 Back out "Clear handlers on disconnect"
Summary:
^

Revert as to validate this is not causing regressions: T143523262

Reviewed By: passy

Differential Revision: D42800560

fbshipit-source-id: 8db61454eabfdb259637bb97c2bb4754984ecf6f
2023-01-27 04:07:37 -08:00
Lorenzo Blasa
764e94503e Clear handlers on disconnect
Summary:
A few changes that should make our connect/disconnect more robust:

* Certificate provider handler should be set directly to and managed by the policy.
* Instantiate the socket once, synchronously on the connect method. Explicit deallocation once, synchronously on the disconnect method.
* Clear handlers on disconnect after clearing the delegate.
* Wait for the operation queue to drain before returning.

Reviewed By: passy

Differential Revision: D42664724

fbshipit-source-id: bd482acbb64a9bc9e36fb3418d4c81afa2109305
2023-01-23 03:45:59 -08:00
Lorenzo Blasa
63dde6e5cf Exceptions
Summary:
Flipper doesn really use library specific exceptions throughout, and that's OK.

Introducing SSLException as a replacement for the existing Folly Async Socket SSL exception.

This exception originally thrown by rsocket. Because we had rsocket and websockets using the same code, websockets were creating and throwing this same exception.

With rsocket gone, we can fully replace the usage of that exception. This is also needed as to decouple Flipper from folly async components.

Reviewed By: fabiomassimo

Differential Revision: D36245624

fbshipit-source-id: f5c97c5efe063280ce95be130008dee7f4e5d788
2022-05-12 09:16:13 -07:00
Lorenzo Blasa
f1fe66afd9 Use a dispatch queue for websocket operations
Summary:
This diff ensures that all operations on the socket are put into a serial background queue, including delegate callbacks.

All operations are executed asynchronously except disconnect, which is made synchronous as to guarantee no resources are accessed after the call.

Reviewed By: fabiomassimo

Differential Revision: D35254499

fbshipit-source-id: 33d93926f7bfc8948095c59f12ca31f0a932b8ae
2022-04-12 02:30:02 -07:00
Andres Suarez
79023ee190 Update copyright headers from Facebook to Meta
Reviewed By: bhamodi

Differential Revision: D33331422

fbshipit-source-id: 016e8dcc0c0c7f1fc353a348b54fda0d5e2ddc01
2021-12-27 14:31:45 -08:00
Lorenzo Blasa
b60eaf5fe6 Move the serialiser to Flipper
Summary:
This change moves the existing serialiser from FlipperWebSocket to FlipperURLSerializer.

The serialiser can be shared with Android as it no longer has any iOS dependencies.

Reviewed By: fabiomassimo

Differential Revision: D31571272

fbshipit-source-id: 0769b384d4143d7404fcfcf993d49dc1b00eeffd
2021-10-15 02:34:35 -07:00
Lorenzo Blasa
5944002c4c Base64 encoding using OpenSSL
Summary: This change adds a function to base64 encode. It is used to encode the CSR which gets added to the connection url.

Reviewed By: passy

Differential Revision: D31570706

fbshipit-source-id: 8356550fe87ae3ac6aae8616744a9339cf69b511
2021-10-14 11:23:29 -07:00
Lorenzo Blasa
799d88275e Remove WebSocket delegate before closing
Summary:
This change removes ourselves as a delegate before closing.

SocketRocket uses its own internal async queue to perform most operations.

After a disconnect, we don't expect to receive any more delegate calls as the handlers may contain references which may have become invalid.

So, removing ourselves as delegates will ensure that we don't get called after a disconnect.

For sanity, we are also taking a copy of the message handler instead of a reference to it.

Reviewed By: briantkelley

Differential Revision: D31360721

fbshipit-source-id: bae5a2423757cd9064ffac28afb8b78c28a20d87
2021-10-03 01:17:54 -07:00
Lorenzo Blasa
cf1d579462 Manual disconnect on deallocation and do not clear message handler or message received
Summary:
Trigger a manual disconnect on deallocation. This was done automatically for us when the underlying socket gets released. But, this gives a bit more visibility and control onto exactly when this is going to take place.

Additionally, do not clear the message handler when a message is received.

It is not required as sendExpectResponse is one time called only used for certificate exchange. If this takes place again, a new handler will be set anyway.

Reviewed By: passy

Differential Revision: D31231828

fbshipit-source-id: 36ad13564a358b88d1618e94195fe05433d80993
2021-09-29 07:44:11 -07:00
Lorenzo Blasa
12865fd0bc Connect and disconnect on the current thread and dispatch events on the connection events handler
Summary:
Addresses an issue with the connect and disconnect sequence.

Both, connect and disconnect should be performed 'synchronously' instead of being queued in the connection event thread.

Events should be queued in the connection event thread instead.

Reviewed By: passy

Differential Revision: D31195525

fbshipit-source-id: c15487ea163dd277dacee0e5669944ac6971355f
2021-09-27 09:29:20 -07:00
Lorenzo Blasa
3dacf5a7c9 WebSocket as default socket provider
Summary:
This change makes WebSockets the default for Flipper on iOS.

Having said that, we are introducing some logic to deal with clients connecting to older Flipper Desktop versions.

The mobile client will first attempt to connect via WebSocket with the Desktop. This connection can either be secure or insecure. If that fails, it will attempt to connect via RSocket.

Connection failure logic:
The mobile client will attempt to connect up-to 3 times via a WebSocket. If it fails to connect, then the socket provider is switched to RSocket.

As before, the mobile client will attempt to connect up-to 3 times via a RSocket. If it fails to connect, then the socket provider is switched back to WebSocket.

Process repeats until a successful connection is established.

Some logs that can be seen from iOS:

   2021-09-15 14:31:51.193503+0100 Sample[92026:92107440] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
   2021-09-15 14:31:51.878257+0100 Sample[92026:92107440] [connection] nw_socket_handle_socket_event [C1.1:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:31:52.553729+0100 Sample[92026:92107440] [connection] nw_socket_handle_socket_event [C1.2:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:31:52.899511+0100 Sample[92026:92107442] [connection] nw_connection_get_connected_socket [C1] Client called nw_connection_get_connected_socket on unconnected nw_connection
   2021-09-15 14:31:52.899664+0100 Sample[92026:92107442] TCP Conn 0x600001d384d0 Failed : error 0:61 [61]
   2021-09-15 14:31:57.120120+0100 Sample[92026:92107439] [connection] nw_socket_handle_socket_event [C2.1:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:31:57.141785+0100 Sample[92026:92107439] [connection] nw_socket_handle_socket_event [C2.2:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:31:57.151604+0100 Sample[92026:92107483] [connection] nw_connection_get_connected_socket [C2] Client called nw_connection_get_connected_socket on unconnected nw_connection
   2021-09-15 14:31:57.154312+0100 Sample[92026:92107483] TCP Conn 0x600001d7c0b0 Failed : error 0:61 [61]
   2021-09-15 14:31:59.206079+0100 Sample[92026:92107483] [connection] nw_socket_handle_socket_event [C3.1:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:31:59.236824+0100 Sample[92026:92107483] [connection] nw_socket_handle_socket_event [C3.2:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:31:59.251927+0100 Sample[92026:92107439] [connection] nw_connection_get_connected_socket [C3] Client called nw_connection_get_connected_socket on unconnected nw_connection
   2021-09-15 14:31:59.255963+0100 Sample[92026:92107439] TCP Conn 0x600001d1c210 Failed : error 0:61 [61]
   2021-09-15 14:32:01.291303+0100 Sample[92026:92107439] [connection] nw_socket_handle_socket_event [C4.1:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:32:01.312406+0100 Sample[92026:92107439] [connection] nw_socket_handle_socket_event [C4.2:1] Socket SO_ERROR [61: Connection refused]
   2021-09-15 14:32:01.323099+0100 Sample[92026:92107483] [connection] nw_connection_get_connected_socket [C4] Client called nw_connection_get_connected_socket on unconnected nw_connection
   2021-09-15 14:32:01.326028+0100 Sample[92026:92107483] TCP Conn 0x600001d7c0b0 Failed : error 0:61 [61]
   flipper: Failed to connect with the current socket provider
   flipper: Use legacy socket provider
   flipper: FlipperClient::onConnected

Reviewed By: passy

Differential Revision: D30900471

fbshipit-source-id: 7c242ad71306803b050d0174fc22696bb74fdba5
2021-09-23 05:22:23 -07:00
Lorenzo Blasa
89886c972b Back out "Revert D30371791"
Summary: This change reverts a revert. The change was reverted for 'unrelated' issues addressed on this diff dependencies.

Reviewed By: passy

Differential Revision: D30696113

fbshipit-source-id: 8591d6ea79999597024c316e9927a346979e5219
2021-09-13 06:15:38 -07:00
Lorenzo Blasa
fe23ea038c Revert D30371791
Summary: Revert D30371791 (cac09d14aa) to address undefined symbols for a few sandcastle jobs

Reviewed By: fabiomassimo

Differential Revision: D30606610

fbshipit-source-id: 24a5c08bcf5456a96469650a4207b05970399181
2021-08-27 11:05:12 -07:00
Lorenzo Blasa
672979b5f8 Guard files with the FB_SONARKIT_ENABLED macro
Summary: Guard code with FB_SONARKIT_ENABLED

Reviewed By: fabiomassimo

Differential Revision: D30576765

fbshipit-source-id: b51d7367c5482add27a5cb18453aedf03001bdc7
2021-08-26 10:16:25 -07:00
Lorenzo Blasa
cac09d14aa FlipperWebSocket
Summary:
Contains the implementation of FlipperWebSocket with any necessary changes to use it but without switching it on.

About SocketRocket and Cocoapods

A decision had to be made about whether to define different sub-specs, one for RSocket and another for SocketRocket.

I've opted to keep the podspec as is because:
- Keeps pod consumption as is.
- Makes easier to switch implementations using GK.
- There's no intention to keep RSocket going into the future. So, there's no point in creating a sub-spec only to remove it in the future.
- SocketRocket is a relatively small library so is not contributing to a significant increase in binary size.

If, as reviewer, you consider a subspec makes more sense, then feel free to reach out to discuss.

Reviewed By: fabiomassimo

Differential Revision: D30371791

fbshipit-source-id: 225c5b1de76aff1a6e36640a41765b963aaa2796
2021-08-26 09:02:59 -07:00