make rpc shutdown thread safe

This commit is contained in:
Ari Lazier 2015-11-12 17:16:24 -08:00
parent 8e9a14e1c0
commit 271c02c2c1
1 changed files with 80 additions and 47 deletions

View File

@ -48,8 +48,19 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre
} }
} }
#if DEBUG
#import <GCDWebServers/GCDWebServers.h>
#import <RealmJS/RealmRPC.hpp> #import <RealmJS/RealmRPC.hpp>
#import <RealmJS/RJSUtil.hpp> @interface RealmReact () {
GCDWebServer *_webServer;
std::unique_ptr<realm_js::RPCServer> _rpcServer;
std::mutex _rpcMutex;
}
@end
static __weak RealmReact *s_currentRealmModule = nil;
#endif
@interface RealmReact () <RCTBridgeModule> @interface RealmReact () <RCTBridgeModule>
@end @end
@ -73,33 +84,17 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre
return @"Realm"; return @"Realm";
} }
- (void)setBridge:(RCTBridge *)bridge {
_bridge = bridge;
Ivar executorIvar = class_getInstanceVariable([bridge class], "_javaScriptExecutor");
id executor = object_getIvar(bridge, executorIvar);
bool chromeDebugMode = [executor isMemberOfClass:NSClassFromString(@"RCTWebSocketExecutor")];
#if DEBUG #if DEBUG
static GCDWebServer *s_webServer; - (void)startRPC {
static realm_js::RPCServer *rpcServer; std::lock_guard<std::mutex> lock(_rpcMutex);
if (s_webServer) {
[s_webServer stop];
[s_webServer removeAllHandlers];
s_webServer = nil;
delete rpcServer;
}
// The executor could be a RCTWebSocketExecutor, in which case it won't have a JS context.
if (chromeDebugMode) {
[GCDWebServer setLogLevel:3]; [GCDWebServer setLogLevel:3];
GCDWebServer *webServer = [[GCDWebServer alloc] init]; _webServer = [[GCDWebServer alloc] init];
rpcServer = new realm_js::RPCServer(); _rpcServer = std::make_unique<realm_js::RPCServer>();
__weak __typeof__(self) weakSelf = self;
// Add a handler to respond to POST requests on any URL // Add a handler to respond to POST requests on any URL
[webServer addDefaultHandlerForMethod:@"POST" [_webServer addDefaultHandlerForMethod:@"POST"
requestClass:[GCDWebServerDataRequest class] requestClass:[GCDWebServerDataRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) { processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
GCDWebServerResponse *response; GCDWebServerResponse *response;
@ -107,9 +102,18 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre
// perform all realm ops on the main thread // perform all realm ops on the main thread
__block NSData *responseData; __block NSData *responseData;
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
RealmReact *self = weakSelf;
if (self) {
std::lock_guard<std::mutex> lock(_rpcMutex);
if (_rpcServer) {
realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]); realm_js::json args = realm_js::json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]);
std::string responseText = rpcServer->perform_request(request.path.UTF8String, args).dump(); std::string responseText = _rpcServer->perform_request(request.path.UTF8String, args).dump();
responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()]; responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()];
return;
}
}
// we have been deallocated
responseData = [NSData data];
}); });
response = [[GCDWebServerDataResponse alloc] initWithData:responseData contentType:@"application/json"]; response = [[GCDWebServerDataResponse alloc] initWithData:responseData contentType:@"application/json"];
} }
@ -124,14 +128,43 @@ JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool cre
return response; return response;
}]; }];
[webServer startWithPort:8082 bonjourName:nil]; [_webServer startWithPort:8082 bonjourName:nil];
return;
}
s_webServer = webServer; - (void)shutdownRPC {
std::lock_guard<std::mutex> lock(_rpcMutex);
[_webServer stop];
[_webServer removeAllHandlers];
_webServer = nil;
_rpcServer.reset();
}
- (void)dealloc {
[self shutdownRPC];
}
#endif
- (void)setBridge:(RCTBridge *)bridge {
_bridge = bridge;
Ivar executorIvar = class_getInstanceVariable([bridge class], "_javaScriptExecutor");
id executor = object_getIvar(bridge, executorIvar);
bool isDebugExecutor = [executor isMemberOfClass:NSClassFromString(@"RCTWebSocketExecutor")];
#if DEBUG
if (s_currentRealmModule) {
[s_currentRealmModule shutdownRPC];
}
s_currentRealmModule = self;
if (isDebugExecutor) {
[self startRPC];
return; return;
} }
#endif #endif
if (chromeDebugMode) { if (isDebugExecutor) {
@throw [NSException exceptionWithName:@"Invalid Executor" reason:@"Chrome debug mode not supported in Release builds" userInfo:nil]; @throw [NSException exceptionWithName:@"Invalid Executor" reason:@"Chrome debug mode not supported in Release builds" userInfo:nil];
} }