Permalink
Browse files

Merge RCTPackagerClient into RCTPackagerConnection

Reviewed By: mmmulani

Differential Revision: D4988204

fbshipit-source-id: 78e0df5268bfc11e4e0edf8e60494e55022cd9f2
  • Loading branch information...
javache authored and facebook-github-bot committed May 5, 2017
1 parent 71e84e6 commit e7680131d7dc841bcdde7f0dbba95bdeedefa126
@@ -161,41 +161,13 @@ - (void)returnConsumer:(RCTSRIOConsumer *)consumer;
@interface RCTSRWebSocket () <NSStreamDelegate>
- (void)_writeData:(NSData *)data;
- (void)_closeWithProtocolError:(NSString *)message;
- (void)_failWithError:(NSError *)error;
- (void)_disconnect;
- (void)_readFrameNew;
- (void)_readFrameContinue;
- (void)_pumpScanner;
- (void)_pumpWriting;
- (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback;
- (void)_addConsumerWithDataLength:(size_t)dataLength callback:(data_callback)callback readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
- (void)_addConsumerWithScanner:(stream_scanner)consumer callback:(data_callback)callback dataLength:(size_t)dataLength;
- (void)_readUntilBytes:(const void *)bytes length:(size_t)length callback:(data_callback)dataHandler;
- (void)_readUntilHeaderCompleteWithCallback:(data_callback)dataHandler;
- (void)_sendFrameWithOpcode:(RCTSROpCode)opcode data:(id)data;
- (BOOL)_checkHandshake:(CFHTTPMessageRef)httpMessage;
- (void)_RCTSR_commonInit;
- (void)_initializeStreams;
- (void)_connect;
@property (nonatomic, assign) RCTSRReadyState readyState;
@property (nonatomic, strong) NSOperationQueue *delegateOperationQueue;
@property (nonatomic, strong) dispatch_queue_t delegateDispatchQueue;
@end
@implementation RCTSRWebSocket
{
NSInteger _webSocketVersion;
@@ -255,13 +227,6 @@ @implementation RCTSRWebSocket
RCTSRIOConsumerPool *_consumerPool;
}
static __strong NSData *CRLFCRLF;
+ (void)initialize;
{
CRLFCRLF = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4];
}
- (instancetype)initWithURLRequest:(NSURLRequest *)request protocols:(NSArray<NSString *> *)protocols
{
RCTAssertParam(request);
@@ -434,7 +399,7 @@ - (void)_HTTPHeadersDidFinish;
return;
}
if(![self _checkHandshake:_receivedHTTPHeaders]) {
if (![self _checkHandshake:_receivedHTTPHeaders]) {
[self _failWithError:[NSError errorWithDomain:RCTSRWebSocketErrorDomain code:2133 userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Invalid Sec-WebSocket-Accept response"]}]];
return;
}
@@ -8,23 +8,26 @@
*/
#import <React/RCTDefines.h>
#import <React/RCTPackagerClientResponder.h>
#if RCT_DEV // Only supported in dev mode
@class RCTPackagerClientResponder;
@class RCTSRWebSocket;
extern const int RCT_PACKAGER_CLIENT_PROTOCOL_VERSION;
@protocol RCTPackagerClientMethod
- (void)handleRequest:(id)params withResponder:(RCTPackagerClientResponder *)responder;
- (void)handleNotification:(id)params;
@end
@interface RCTPackagerClient : NSObject
@interface RCTPackagerClientResponder : NSObject
- (instancetype)initWithURL:(NSURL *)url;
- (void)addHandler:(id<RCTPackagerClientMethod>)handler forMethod:(NSString *)name;
- (void)start;
- (void)stop;
- (instancetype)initWithId:(id)msgId socket:(RCTSRWebSocket *)socket;
- (void)respondWithResult:(id)result;
- (void)respondWithError:(id)error;
@end
@@ -9,93 +9,57 @@
#import "RCTPackagerClient.h"
#import <React/RCTConvert.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
#import <React/RCTReconnectingWebSocket.h>
#import <React/RCTSRWebSocket.h>
#import <React/RCTUtils.h>
#import "RCTPackagerClientResponder.h"
#if RCT_DEV // Only supported in dev mode
@interface RCTPackagerClient () <RCTWebSocketProtocolDelegate>
@end
const int RCT_PACKAGER_CLIENT_PROTOCOL_VERSION = 2;
@implementation RCTPackagerClient {
RCTReconnectingWebSocket *_socket;
NSMutableDictionary<NSString *, id<RCTPackagerClientMethod>> *_handlers;
@implementation RCTPackagerClientResponder {
id _msgId;
__weak RCTSRWebSocket *_socket;
}
- (instancetype)initWithURL:(NSURL *)url
- (instancetype)initWithId:(id)msgId socket:(RCTSRWebSocket *)socket
{
if (self = [super init]) {
_socket = [[RCTReconnectingWebSocket alloc] initWithURL:url];
_socket.delegate = self;
_handlers = [NSMutableDictionary new];
_msgId = msgId;
_socket = socket;
}
return self;
}
- (void)addHandler:(id<RCTPackagerClientMethod>)handler forMethod:(NSString *)name
{
_handlers[name] = handler;
}
- (void)start
{
_socket.delegate = self;
[_socket start];
}
- (void)stop
{
[_socket stop];
}
- (BOOL)isSupportedVersion:(NSNumber *)version
- (void)respondWithResult:(id)result
{
NSArray<NSNumber *> *const kSupportedVersions = @[ @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION) ];
return [kSupportedVersions containsObject:version];
NSDictionary<NSString *, id> *msg = @{
@"version": @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION),
@"id": _msgId,
@"result": result,
};
NSError *jsError = nil;
NSString *message = RCTJSONStringify(msg, &jsError);
if (jsError) {
RCTLogError(@"%@ failed to stringify message with error %@", [self class], jsError);
} else {
[_socket send:message];
}
}
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
- (void)respondWithError:(id)error
{
if (!_handlers) {
return;
}
NSError *error = nil;
NSDictionary<NSString *, id> *msg = RCTJSONParse(message, &error);
if (error) {
RCTLogError(@"%@ failed to parse message with error %@\n<message>\n%@\n</message>", [self class], error, msg);
return;
}
if (![self isSupportedVersion:msg[@"version"]]) {
RCTLogError(@"%@ received message with not supported version %@", [self class], msg[@"version"]);
return;
}
id<RCTPackagerClientMethod> methodHandler = _handlers[msg[@"method"]];
if (!methodHandler) {
if (msg[@"id"]) {
NSString *errorMsg = [NSString stringWithFormat:@"%@ no handler found for method %@", [self class], msg[@"method"]];
RCTLogError(errorMsg, msg[@"method"]);
[[[RCTPackagerClientResponder alloc] initWithId:msg[@"id"]
socket:webSocket] respondWithError:errorMsg];
}
return; // If it was a broadcast then we ignore it gracefully
}
if (msg[@"id"]) {
[methodHandler handleRequest:msg[@"params"]
withResponder:[[RCTPackagerClientResponder alloc] initWithId:msg[@"id"]
socket:webSocket]];
NSDictionary<NSString *, id> *msg = @{
@"version": @(RCT_PACKAGER_CLIENT_PROTOCOL_VERSION),
@"id": _msgId,
@"error": error,
};
NSError *jsError = nil;
NSString *message = RCTJSONStringify(msg, &jsError);
if (jsError) {
RCTLogError(@"%@ failed to stringify message with error %@", [self class], jsError);
} else {
[methodHandler handleNotification:msg[@"params"]];
[_socket send:message];
}
}
@end

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -14,14 +14,15 @@
#if RCT_DEV
@class RCTBridge;
@protocol RCTPackagerClientMethod;
/**
* Encapsulates connection to React Native packager
*/
@interface RCTPackagerConnection : NSObject
- (instancetype)initWithBridge:(RCTBridge *)bridge;
- (void)connect;
- (void)addHandler:(id<RCTPackagerClientMethod>)handler forMethod:(NSString *)name;
@end
Oops, something went wrong.

0 comments on commit e768013

Please sign in to comment.