2017-02-23 17:54:37 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-02-23 17:54:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTReconnectingWebSocket.h"
|
|
|
|
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTDefines.h>
|
2017-08-08 15:10:51 +00:00
|
|
|
#import <fishhook/fishhook.h>
|
2017-02-23 17:54:37 +00:00
|
|
|
|
2018-05-11 21:13:24 +00:00
|
|
|
#if __has_include(<os/log.h>) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100300 /* __IPHONE_10_3 */
|
2017-09-21 19:57:29 +00:00
|
|
|
#import <os/log.h>
|
2018-05-11 21:13:24 +00:00
|
|
|
#endif /* __IPHONE_10_3 */
|
2017-09-21 19:57:29 +00:00
|
|
|
|
2017-02-23 17:54:37 +00:00
|
|
|
#import "RCTSRWebSocket.h"
|
|
|
|
|
|
|
|
#if RCT_DEV // Only supported in dev mode
|
|
|
|
|
2018-05-11 21:13:24 +00:00
|
|
|
#if __has_include(<os/log.h>) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100300 /* __IPHONE_10_3 */
|
|
|
|
|
|
|
|
// From https://github.com/apple/swift/blob/ad40c770bfe372f879b530443a3d94761fe258a6/stdlib/public/SDK/os/os_log.m
|
|
|
|
typedef struct os_log_pack_s {
|
|
|
|
uint64_t olp_continuous_time;
|
|
|
|
struct timespec olp_wall_time;
|
|
|
|
const void *olp_mh;
|
|
|
|
const void *olp_pc;
|
|
|
|
const char *olp_format;
|
|
|
|
uint8_t olp_data[0];
|
|
|
|
} os_log_pack_s, *os_log_pack_t;
|
|
|
|
|
|
|
|
static void (*orig__nwlog_pack)(os_log_pack_t pack, os_log_type_t logType);
|
|
|
|
|
|
|
|
static void my__nwlog_pack(os_log_pack_t pack, os_log_type_t logType)
|
|
|
|
{
|
|
|
|
if (logType == OS_LOG_TYPE_ERROR && strstr(pack->olp_format, "Connection has no connected handler") == NULL) {
|
|
|
|
orig__nwlog_pack(pack, logType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __IPHONE_10_3 */
|
|
|
|
|
2017-08-08 15:10:51 +00:00
|
|
|
static void (*orig_nwlog_legacy_v)(int, char*, va_list);
|
|
|
|
|
|
|
|
static void my_nwlog_legacy_v(int level, char *format, va_list args) {
|
|
|
|
static const uint buffer_size = 256;
|
|
|
|
static char buffer[buffer_size];
|
|
|
|
va_list copy;
|
|
|
|
va_copy(copy, args);
|
|
|
|
vsnprintf(buffer, buffer_size, format, copy);
|
|
|
|
va_end(copy);
|
|
|
|
|
2017-09-21 19:57:29 +00:00
|
|
|
if (strstr(buffer, "nw_connection_get_connected_socket_block_invoke") == NULL &&
|
|
|
|
strstr(buffer, "Connection has no connected handler") == NULL) {
|
2017-08-08 15:10:51 +00:00
|
|
|
orig_nwlog_legacy_v(level, format, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 19:57:29 +00:00
|
|
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
|
|
|
|
|
|
|
static void (*orig_os_log_error_impl)(void *dso, os_log_t log, os_log_type_t type, const char *format, uint8_t *buf, uint32_t size);
|
|
|
|
|
|
|
|
static void my_os_log_error_impl(void *dso, os_log_t log, os_log_type_t type, const char *format, uint8_t *buf, uint32_t size)
|
|
|
|
{
|
|
|
|
if (strstr(format, "TCP Conn %p Failed : error %ld:%d") == NULL) {
|
|
|
|
orig_os_log_error_impl(dso, log, type, format, buf, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __IPHONE_11_0 */
|
|
|
|
|
2017-02-23 17:54:37 +00:00
|
|
|
@interface RCTReconnectingWebSocket () <RCTSRWebSocketDelegate>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTReconnectingWebSocket {
|
|
|
|
NSURL *_url;
|
|
|
|
RCTSRWebSocket *_socket;
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:10:51 +00:00
|
|
|
+ (void)load
|
|
|
|
{
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
rebind_symbols((struct rebinding[1]){
|
|
|
|
{"nwlog_legacy_v", my_nwlog_legacy_v, (void *)&orig_nwlog_legacy_v}
|
|
|
|
}, 1);
|
2018-05-11 21:13:24 +00:00
|
|
|
#if __has_include(<os/log.h>) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 100300 /* __IPHONE_10_3 */
|
|
|
|
rebind_symbols((struct rebinding[1]){
|
|
|
|
{"__nwlog_pack", my__nwlog_pack, (void *)&orig__nwlog_pack}
|
|
|
|
}, 1);
|
|
|
|
#endif /* __IPHONE_10_3 */
|
2017-09-21 19:57:29 +00:00
|
|
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
|
|
|
rebind_symbols((struct rebinding[1]){
|
|
|
|
{"_os_log_error_impl", my_os_log_error_impl, (void *)&orig_os_log_error_impl}
|
|
|
|
}, 1);
|
|
|
|
#endif /* __IPHONE_11_0 */
|
2017-08-08 15:10:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:13 +00:00
|
|
|
- (instancetype)initWithURL:(NSURL *)url queue:(dispatch_queue_t)queue
|
2017-02-23 17:54:37 +00:00
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_url = url;
|
2017-11-21 02:12:13 +00:00
|
|
|
_delegateDispatchQueue = queue;
|
2017-02-23 17:54:37 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:13 +00:00
|
|
|
- (instancetype)initWithURL:(NSURL *)url
|
|
|
|
{
|
|
|
|
return [self initWithURL:url queue:dispatch_get_main_queue()];
|
|
|
|
}
|
|
|
|
|
2017-04-17 14:28:46 +00:00
|
|
|
- (void)send:(id)data
|
|
|
|
{
|
|
|
|
[_socket send:data];
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:54:37 +00:00
|
|
|
- (void)start
|
|
|
|
{
|
|
|
|
[self stop];
|
|
|
|
_socket = [[RCTSRWebSocket alloc] initWithURL:_url];
|
|
|
|
_socket.delegate = self;
|
2017-11-21 02:12:13 +00:00
|
|
|
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
|
2017-02-23 17:54:37 +00:00
|
|
|
[_socket open];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stop
|
|
|
|
{
|
|
|
|
_socket.delegate = nil;
|
|
|
|
[_socket closeWithCode:1000 reason:@"Invalidated"];
|
|
|
|
_socket = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
|
|
|
|
{
|
2017-11-21 02:12:13 +00:00
|
|
|
[_delegate reconnectingWebSocket:self didReceiveMessage:message];
|
2017-02-23 17:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reconnect
|
|
|
|
{
|
|
|
|
__weak RCTSRWebSocket *socket = _socket;
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
|
|
// Only reconnect if the observer wasn't stoppped while we were waiting
|
|
|
|
if (socket) {
|
|
|
|
[self start];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:59:40 +00:00
|
|
|
- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
|
|
|
|
{
|
2017-11-21 02:12:13 +00:00
|
|
|
[_delegate reconnectingWebSocketDidOpen:self];
|
2017-08-29 14:59:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 17:54:37 +00:00
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
|
|
|
|
{
|
2017-11-21 02:12:13 +00:00
|
|
|
[_delegate reconnectingWebSocketDidClose:self];
|
2017-02-23 17:54:37 +00:00
|
|
|
[self reconnect];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean
|
|
|
|
{
|
2017-11-21 02:12:13 +00:00
|
|
|
[_delegate reconnectingWebSocketDidClose:self];
|
2017-02-23 17:54:37 +00:00
|
|
|
[self reconnect];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#endif
|