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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
65e515bdaa
commit
e42db220ee
@@ -134,9 +134,9 @@ FlipperReactSocketClient::getClientCertificate() {
|
||||
return installClientCertificate();
|
||||
}
|
||||
|
||||
bool FlipperReactSocketClient::connect(FlipperConnectionManager* manager) {
|
||||
void FlipperReactSocketClient::connect(FlipperConnectionManager* manager) {
|
||||
if (status_ != Status::Unconnected) {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
status_ = Status::Connecting;
|
||||
@@ -176,30 +176,34 @@ bool FlipperReactSocketClient::connect(FlipperConnectionManager* manager) {
|
||||
socket_.Closed({this, &FlipperReactSocketClient::OnWebSocketClosed});
|
||||
|
||||
try {
|
||||
this->socket_.ConnectAsync(winrt::Windows::Foundation::Uri(uri))
|
||||
.wait_for(std::chrono::seconds(10));
|
||||
|
||||
status_ = Status::Initializing;
|
||||
scheduler_->schedule(
|
||||
[eventHandler = eventHandler_]() { eventHandler(SocketEvent::OPEN); });
|
||||
|
||||
return true;
|
||||
Windows::Foundation::IAsyncAction ^ connectAction;
|
||||
connectAction =
|
||||
this->socket_.ConnectAsync(winrt::Windows::Foundation::Uri(uri));
|
||||
connectAction->Completed = ref new AsyncActionCompletedHandler(
|
||||
[eventHandler = eventHandler_](
|
||||
Windows::Foundation::IAsyncAction ^ asyncAction,
|
||||
Windows::Foundation::AsyncStatus asyncStatus) {
|
||||
if (asyncStatus == Windows::Foundation::AsyncStatus::Completed) {
|
||||
eventHandler(SocketEvent::OPEN);
|
||||
} else {
|
||||
eventHandler(SocketEvent::ERROR);
|
||||
}
|
||||
});
|
||||
|
||||
} catch (winrt::hresult_error const& ex) {
|
||||
winrt::Windows::Web::WebErrorStatus webErrorStatus{
|
||||
winrt::Windows::Networking::Sockets::WebSocketError::GetStatus(
|
||||
ex.to_abi())};
|
||||
socket_ = nullptr;
|
||||
status_ = Status::Unconnected;
|
||||
eventHandler(SocketEvent::ERROR);
|
||||
}
|
||||
|
||||
disconnect();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void FlipperReactSocketClient::disconnect() {
|
||||
status_ = Status::Closed;
|
||||
scheduler_->schedule(
|
||||
[eventHandler = eventHandler_]() { eventHandler(SocketEvent::CLOSE); });
|
||||
// socket_.Close();
|
||||
socket_ = nullptr;
|
||||
}
|
||||
|
||||
@@ -269,14 +273,10 @@ void FlipperReactSocketClient::OnWebSocketMessageReceived(
|
||||
const std::string payload = winrt::to_string(message);
|
||||
|
||||
if (overrideHandler_ != nullptr) {
|
||||
scheduler_->schedule([payload, messageHandler = *overrideHandler_]() {
|
||||
messageHandler(payload, false);
|
||||
});
|
||||
messageHandler(payload, false);
|
||||
overrideHandler_ = nullptr;
|
||||
} else if (messageHandler_) {
|
||||
scheduler_->schedule([payload, messageHandler = messageHandler_]() {
|
||||
messageHandler(payload);
|
||||
});
|
||||
messageHandler(payload);
|
||||
}
|
||||
} catch (winrt::hresult_error const& ex) {
|
||||
// winrt::Windows::Web::WebErrorStatus webErrorStatus{
|
||||
@@ -291,8 +291,7 @@ void FlipperReactSocketClient::OnWebSocketClosed(
|
||||
return;
|
||||
}
|
||||
status_ = Status::Closed;
|
||||
scheduler_->schedule(
|
||||
[eventHandler = eventHandler_]() { eventHandler(SocketEvent::CLOSE); });
|
||||
eventHandler(SocketEvent::CLOSE);
|
||||
}
|
||||
|
||||
} // namespace flipper
|
||||
|
||||
Reference in New Issue
Block a user