Skip to content

Commit

Permalink
fix(inspector): ensure socket message is copied and stored (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni authored Mar 18, 2022
1 parent f46c425 commit 3098976
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 4 additions & 1 deletion NativeScript/inspector/InspectorServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@
if (size) {
NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding];

onMessage([payload UTF8String]);
if (payload) {
std::string result = [payload UTF8String];
onMessage(result);
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
Expand Down
3 changes: 2 additions & 1 deletion NativeScript/inspector/JsV8InspectorClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
std::vector<std::string> messages_;
bool runningNestedLoops_;
dispatch_queue_t messagesQueue_;
dispatch_semaphore_t messageArrived_;
std::function<void (std::string)> sender_;
bool isWaitingForDebugger_;

void enableInspector(int argc, char** argv);
void notify(std::unique_ptr<StringBuffer> message);
void onFrontendConnected(std::function<void (std::string)> sender);
void onFrontendMessageReceived(std::string message);
void onFrontendMessageReceived(std::string &message);
template <class TypeName>
static v8::Local<TypeName> PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent);
std::string PumpMessage();
Expand Down
14 changes: 12 additions & 2 deletions NativeScript/inspector/JsV8InspectorClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
messages_(),
runningNestedLoops_(false) {
this->messagesQueue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
this->messageArrived_ = dispatch_semaphore_create(0);
}

void JsV8InspectorClient::onFrontendConnected(std::function<void (std::string)> sender) {
Expand All @@ -100,9 +101,11 @@
this->disconnect();
}

void JsV8InspectorClient::onFrontendMessageReceived(std::string message) {
void JsV8InspectorClient::onFrontendMessageReceived(std::string &message) {
__block std::string strCopy = message;
dispatch_sync(this->messagesQueue_, ^{
this->messages_.push_back(message);
this->messages_.push_back(strCopy);
dispatch_semaphore_signal(messageArrived_);
});

tns::ExecuteOnMainThread([this, message]() {
Expand Down Expand Up @@ -166,15 +169,22 @@

terminated_ = false;
this->runningNestedLoops_ = true;
bool shouldWait = false;
while (!terminated_) {
std::string message = this->PumpMessage();
if (!message.empty()) {
this->dispatchMessage(message);
shouldWait = false;
} else {
shouldWait = true;
}

std::shared_ptr<Platform> platform = tns::Runtime::GetPlatform();
Isolate* isolate = runtime_->GetIsolate();
platform::PumpMessageLoop(platform.get(), isolate, platform::MessageLoopBehavior::kDoNotWait);
if(shouldWait && !terminated_) {
dispatch_semaphore_wait(messageArrived_, dispatch_time(DISPATCH_TIME_NOW, 1000000)); // 1ms in ns
}
}

terminated_ = false;
Expand Down

0 comments on commit 3098976

Please sign in to comment.