Skip to content

Commit

Permalink
Drop LazyDevSupportManager (#4984)
Browse files Browse the repository at this point in the history
* Drop LazySupportManager

* Change files

* Remove obsolete comment

* Remove obsolete comment

* Rename buffer0 to buffer

* yarn format

* Remove unneeded string allocations

* Update DevSupportManager.cpp
  • Loading branch information
JunielKatarn authored May 22, 2020
1 parent 225df52 commit ed7ee2d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 212 deletions.
8 changes: 8 additions & 0 deletions change/react-native-windows-2020-05-21-16-49-04-dsmwinrt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Drop LazySupportManager",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-05-21T23:49:04.082Z"
}
140 changes: 62 additions & 78 deletions vnext/Desktop/DevSupportManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@

#include <DevSettings.h>
#include <InstanceManager.h>

#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <unicode.h>

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Networking.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Web.Http.Filters.h>
#include <winrt/Windows.Web.Http.Headers.h>
#include <winrt/Windows.Web.Http.h>
#include "Executors/WebSocketJSExecutor.h"
#include "Utils.h"

using namespace boost::asio;
using namespace boost::asio::ip;
using namespace boost::beast;
using namespace boost::beast::http;
using namespace Concurrency;
using namespace std;
using namespace winrt::Windows::Storage::Streams;
using namespace winrt::Windows::Web::Http;

using Microsoft::React::WebSocketJSExecutor;
using std::make_unique;
using std::ostringstream;
using std::shared_ptr;
using std::string;
using std::vector;
using winrt::Windows::Foundation::Uri;
using winrt::Windows::Networking::HostName;

namespace facebook {
namespace react {
namespace Microsoft::React {

DevSupportManager::DevSupportManager() : m_resolver{m_context} {}
DevSupportManager::DevSupportManager() {}

DevSupportManager::~DevSupportManager() {
StopPollingLiveReload();
Expand All @@ -55,60 +59,49 @@ string DevSupportManager::GetJavaScriptFromServer(
const string &platform) {
auto bundleUrl =
DevServerHelper::get_BundleUrl(debugHost, jsBundleName, platform /*platform*/, "true" /*dev*/, "false" /*hot*/);

Url url(bundleUrl);
auto const resolveResult = m_resolver.resolve(url.host, url.port);
tcp::socket socket{m_context};
boost::system::error_code ec;
connect(socket, resolveResult, ec);
if (ec) {
Uri uri(Common::Unicode::Utf8ToUtf16(bundleUrl));
Filters::HttpBaseProtocolFilter filter;
filter.CacheControl().ReadBehavior(Filters::HttpCacheReadBehavior::NoCache);
HttpClient client(filter);
client.DefaultRequestHeaders().Host(HostName(uri.Host().c_str()));

HttpRequestMessage request(HttpMethod::Get(), uri);
HttpResponseMessage response = client.SendRequestAsync(request).get();

IBuffer buffer = response.Content().ReadAsBufferAsync().get();
auto reader = DataReader::FromBuffer(buffer);
reader.UnicodeEncoding(UnicodeEncoding::Utf8);
auto length = reader.UnconsumedBufferLength();
string result;
if (length > 0 || response.IsSuccessStatusCode()) {
vector<uint8_t> data(length);
reader.ReadBytes(data);
result = string(reinterpret_cast<char *>(data.data()), data.size());
} else {
m_exceptionCaught = true;
return R"({"error:")"s + ec.message() + R"("})"s;
ostringstream stream;
stream << R"({"error":")" << static_cast<int>(response.StatusCode()) << " downloading " << bundleUrl << R"("})";
result = stream.str();
}

request<string_body> request{verb::get, url.Target(), 11};
request.set(field::host, url.host);
request.set(field::user_agent, BOOST_BEAST_VERSION_STRING);

write(socket, request);

flat_buffer buffer;
response<string_body> response;

parser<false, string_body> p{std::move(response)};
p.body_limit(25 * 1024 * 1024); // 25MB (boost default of 1MB is too small for dev bundles)

read(socket, buffer, p.base());
response = p.release();
std::stringstream jsStringStream;
jsStringStream << response.body();
// TODO: Check if UTF-8 processing is required.

return jsStringStream.str();
return result;
}

void DevSupportManager::StartPollingLiveReload(const string &debugHost, std::function<void()> onChangeCallback) {
auto t = create_task([this, debugHost, onChangeCallback = move(onChangeCallback)] {
cancellation_token token = m_liveReloadCts.get_token();
while (!token.is_canceled()) {
try {
Url url(DevServerHelper::get_OnChangeEndpointUrl(debugHost));
auto const resolveResult = m_resolver.resolve(url.host, url.port);
tcp::socket socket{m_context};
connect(socket, resolveResult);

request<string_body> request{verb::get, url.Target(), 11 /*HTTP 1.1*/};
request.set(field::host, url.host);
request.set(field::user_agent, BOOST_BEAST_VERSION_STRING);
request.set(field::connection, "keep-alive");

write(socket, request);
flat_buffer buffer;
response<dynamic_body> response;
read(socket, buffer, response);

if (response.result_int() == 205 /*ResetContent*/ && !token.is_canceled())
Uri uri(Common::Unicode::Utf8ToUtf16(DevServerHelper::get_OnChangeEndpointUrl(debugHost)));
HttpClient client;
client.DefaultRequestHeaders().Host(HostName(uri.Host().c_str()));
client.DefaultRequestHeaders().Connection().TryParseAdd(L"keep-alive");

HttpResponseMessage response = client.GetAsync(uri, HttpCompletionOption::ResponseHeadersRead).get();

if (response.StatusCode() == HttpStatusCode::ResetContent /*ResetContent*/ && !token.is_canceled())
onChangeCallback();

} catch (const std::exception & /*e*/) {
// Just let the live reload stop working when the connection fails,
// rather than bringing down the app.
Expand All @@ -121,28 +114,19 @@ task<void> DevSupportManager::LaunchDevToolsAsync(const string &debugHost, const
auto t = task_from_result();

return t.then([=]() {
Url url(DevServerHelper::get_LaunchDevToolsCommandUrl(debugHost));
auto const resolveResult = m_resolver.resolve(url.host, url.port);
tcp::socket socket{m_context};
boost::system::error_code ec;
connect(socket, resolveResult, ec);
if (ec) {
m_exceptionCaught = true;
return;
}

request<string_body> request{verb::get, url.Target(), 11};
request.set(field::host, url.host);
request.set(field::user_agent, BOOST_BEAST_VERSION_STRING);
Uri uri(Common::Unicode::Utf8ToUtf16(DevServerHelper::get_LaunchDevToolsCommandUrl(debugHost)));
HttpClient client;
client.DefaultRequestHeaders().Host(HostName(uri.Host().c_str()));
HttpResponseMessage response = client.GetAsync(uri, HttpCompletionOption::ResponseHeadersRead).get();
});
}

write(socket, request);
} // namespace Microsoft::React

flat_buffer buffer;
response<dynamic_body> response;
namespace facebook::react {

read(socket, buffer, response);
});
shared_ptr<IDevSupportManager> CreateDevSupportManager() {
return std::make_shared<Microsoft::React::DevSupportManager>();
}

} // namespace react
} // namespace facebook
} // namespace facebook::react
23 changes: 7 additions & 16 deletions vnext/Desktop/DevSupportManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@
#undef U

#include <IDevSupportManager.h>

#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/system/error_code.hpp>
#include <cxxreact/JSExecutor.h>

namespace facebook {
namespace react {

using JSECreator =
std::function<std::unique_ptr<JSExecutor>(std::shared_ptr<ExecutorDelegate>, std::shared_ptr<MessageQueueThread>)>;
namespace Microsoft::React {

struct DevSettings;
using JSECreator = std::function<std::unique_ptr<facebook::react::JSExecutor>(
std::shared_ptr<facebook::react::ExecutorDelegate>,
std::shared_ptr<facebook::react::MessageQueueThread>)>;

class DevSupportManager : public IDevSupportManager {
class DevSupportManager : public facebook::react::IDevSupportManager {
public:
DevSupportManager();
~DevSupportManager();

virtual JSECreator LoadJavaScriptInProxyMode(const DevSettings &settings) override;
virtual facebook::react::JSECreator LoadJavaScriptInProxyMode(const facebook::react::DevSettings &settings) override;
virtual std::string GetJavaScriptFromServer(
const std::string &debugHost,
const std::string &jsBundleName,
Expand All @@ -43,10 +37,7 @@ class DevSupportManager : public IDevSupportManager {
const Concurrency::cancellation_token &token = Concurrency::cancellation_token::none());

bool m_exceptionCaught = false;
boost::asio::io_context m_context;
boost::asio::ip::tcp::resolver m_resolver;
Concurrency::cancellation_token_source m_liveReloadCts;
};

} // namespace react
} // namespace facebook
} // namespace Microsoft::React
67 changes: 0 additions & 67 deletions vnext/Desktop/LazyDevSupportManager.cpp

This file was deleted.

37 changes: 0 additions & 37 deletions vnext/Desktop/LazyDevSupportManager.h

This file was deleted.

9 changes: 1 addition & 8 deletions vnext/Desktop/React.Windows.Desktop.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge>
<CppWinRTGenerateWindowsMetadata>true</CppWinRTGenerateWindowsMetadata>
<CppWinRTParameters>-lib $(MSBuildProjectName)</CppWinRTParameters>

<!--
Cppwinrt.exe derives the names of the source files it generates from the $(RootNamespace)
property. Currently, the value of this property differs between the 'Microsoft.ReactNative' and
Expand All @@ -34,7 +33,6 @@
homogenize the root namespace for C++/WinRT code generation.
-->
<CppWinRTRootNamespace>Microsoft.ReactNative</CppWinRTRootNamespace>

<MinimalCoreWin>true</MinimalCoreWin>
<ProjectGuid>{95048601-C3DC-475F-ADF8-7C0C764C10D5}</ProjectGuid>
<ProjectName>React.Windows.Desktop</ProjectName>
Expand Down Expand Up @@ -161,7 +159,6 @@
<ClCompile Include="Executors\WebSocketJSExecutor.cpp" />
<ClCompile Include="Executors\WebSocketJSExecutorFactory.cpp" />
<ClCompile Include="JSBigStringResourceDll.cpp" />
<ClCompile Include="LazyDevSupportManager.cpp" />
<ClCompile Include="module.g.cpp" />
<ClCompile Include="Modules\NetworkingModule.cpp" />
<ClCompile Include="Modules\TimingModule.cpp" />
Expand Down Expand Up @@ -192,7 +189,6 @@
</ClInclude>
<ClInclude Include="DevSupportManager.h" />
<ClInclude Include="Executors\WebSocketJSExecutor.h" />
<ClInclude Include="LazyDevSupportManager.h" />
<ClInclude Include="Modules\NetworkingModule.h" />
<ClInclude Include="JSBigStringResourceDll.h" />
<ClInclude Include="Modules\TimingModule.h" />
Expand Down Expand Up @@ -235,23 +231,20 @@
<Import Project="$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(SolutionDir)packages\Microsoft.Windows.CppWinRT.2.0.200316.3\build\native\Microsoft.Windows.CppWinRT.targets')" />
<Import Project="$(V8_Package)\build\native\ReactNative.V8JSI.Windows.targets" Condition="Exists('$(V8_Package)\build\native\ReactNative.V8JSI.Windows.targets') AND '$(USE_V8)' == 'true'" />
</ImportGroup>

<!-- See $(CppWinRTRootNamespace) comments above. -->
<Target Name="CppWinRTSetRootNamespace" BeforeTargets="CppWinRTMakeComponentProjection" Condition="'$(CppWinRTRootNamespace)' != ''">
<PropertyGroup>
<_CppWinRTOriginalRootNamespace>$(RootNamespace)</_CppWinRTOriginalRootNamespace>
<RootNamespace>$(CppWinRTRootNamespace)</RootNamespace>
</PropertyGroup>
</Target>

<!-- See $(CppWinRTRootNamespace) comments above. -->
<Target Name="CppWinRTRestoreRootNamespace" AfterTargets="CppWinRTMakeComponentProjection" Condition="'$(CppWinRTRootNamespace)' != ''">
<PropertyGroup>
<RootNamespace>$(_CppWinRTOriginalRootNamespace)</RootNamespace>
<_CppWinRTOriginalRootNamespace/>
<_CppWinRTOriginalRootNamespace />
</PropertyGroup>
</Target>

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
Expand Down
Loading

0 comments on commit ed7ee2d

Please sign in to comment.