Skip to content

Commit 2e11fc1

Browse files
authored
Restore socket rocket (#14427)
1 parent 8430a3c commit 2e11fc1

19 files changed

+2811
-79
lines changed

CoreOnly/NOTICES

+58
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,64 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10071007
THE SOFTWARE.
10081008

10091009

1010+
--------------------------
1011+
SocketRocket
1012+
--------------------------
1013+
Copyright 2012 Square Inc.
1014+
1015+
Licensed under the Apache License, Version 2.0 (the "License");
1016+
you may not use this file except in compliance with the License.
1017+
You may obtain a copy of the License at
1018+
1019+
http://www.apache.org/licenses/LICENSE-2.0
1020+
1021+
Unless required by applicable law or agreed to in writing, software
1022+
distributed under the License is distributed on an "AS IS" BASIS,
1023+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1024+
See the License for the specific language governing permissions and
1025+
limitations under the License.
1026+
1027+
$OpenBSD: base64.c,v 1.5 2006/10/21 09:55:03 otto Exp $
1028+
1029+
Copyright (c) 1996 by Internet Software Consortium.
1030+
1031+
Permission to use, copy, modify, and distribute this software for any
1032+
purpose with or without fee is hereby granted, provided that the above
1033+
copyright notice and this permission notice appear in all copies.
1034+
1035+
THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
1036+
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1037+
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
1038+
CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1039+
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1040+
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1041+
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1042+
SOFTWARE.
1043+
1044+
Portions Copyright (c) 1995 by International Business Machines, Inc.
1045+
1046+
International Business Machines, Inc. (hereinafter called IBM) grants
1047+
permission under its copyrights to use, copy, modify, and distribute this
1048+
Software with or without fee, provided that the above copyright notice and
1049+
all paragraphs of this notice appear in all copies, and that the name of IBM
1050+
not be used in connection with the marketing of any product incorporating
1051+
the Software or modifications thereof, without specific, written prior
1052+
permission.
1053+
1054+
To the extent it has a right to do so, IBM grants an immunity from suit
1055+
under its patents, if any, for the use, sale or manufacture of products to
1056+
the extent that such products are used for performing Domain Name System
1057+
dynamic updates in TCP/IP networks by means of the Software. No immunity is
1058+
granted for any product per se or for any other function of any product.
1059+
1060+
THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
1061+
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1062+
PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
1063+
DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
1064+
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
1065+
IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
1066+
1067+
10101068
FirebaseFirestore
10111069
FirebaseFirestoreInternal
10121070

FirebaseDatabase.podspec

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
3636
s.source_files = [
3737
base_dir + '**/*.[mh]',
3838
base_dir + 'third_party/Wrap-leveldb/APLevelDB.mm',
39+
base_dir + 'third_party/SocketRocket/fbase64.c',
3940
'FirebaseDatabase/Swift/Sources/**/*.swift',
4041
'FirebaseAuth/Interop/**/*.h',
4142
'FirebaseCore/Extension/*.h',

FirebaseDatabase/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Unreleased
2+
- [fixed] Fix connection failure issue introduced in 10.27.0 by restoring the
3+
Socket Rocket implementation instead of `NSURLSessionWebSocket`. Note that
4+
this may expose a Thread Performance Checker Warning (#12883).
5+
(#14188, #13877, #13855, #13529)
6+
17
# 11.2.0
28
- [fixed] Fix temporary disconnects when the app goes inactive. The issue was
39
introduced in 10.27.0. (#13529)

FirebaseDatabase/LICENSE

+58
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,61 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
277277
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
278278
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
279279
THE SOFTWARE.
280+
281+
282+
--------------------------
283+
SocketRocket
284+
--------------------------
285+
Copyright 2012 Square Inc.
286+
287+
Licensed under the Apache License, Version 2.0 (the "License");
288+
you may not use this file except in compliance with the License.
289+
You may obtain a copy of the License at
290+
291+
http://www.apache.org/licenses/LICENSE-2.0
292+
293+
Unless required by applicable law or agreed to in writing, software
294+
distributed under the License is distributed on an "AS IS" BASIS,
295+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
296+
See the License for the specific language governing permissions and
297+
limitations under the License.
298+
299+
$OpenBSD: base64.c,v 1.5 2006/10/21 09:55:03 otto Exp $
300+
301+
Copyright (c) 1996 by Internet Software Consortium.
302+
303+
Permission to use, copy, modify, and distribute this software for any
304+
purpose with or without fee is hereby granted, provided that the above
305+
copyright notice and this permission notice appear in all copies.
306+
307+
THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
308+
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
309+
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
310+
CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
311+
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
312+
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
313+
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
314+
SOFTWARE.
315+
316+
Portions Copyright (c) 1995 by International Business Machines, Inc.
317+
318+
International Business Machines, Inc. (hereinafter called IBM) grants
319+
permission under its copyrights to use, copy, modify, and distribute this
320+
Software with or without fee, provided that the above copyright notice and
321+
all paragraphs of this notice appear in all copies, and that the name of IBM
322+
not be used in connection with the marketing of any product incorporating
323+
the Software or modifications thereof, without specific, written prior
324+
permission.
325+
326+
To the extent it has a right to do so, IBM grants an immunity from suit
327+
under its patents, if any, for the use, sale or manufacture of products to
328+
the extent that such products are used for performing Domain Name System
329+
dynamic updates in TCP/IP networks by means of the Software. No immunity is
330+
granted for any product per se or for any other function of any product.
331+
332+
THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
333+
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
334+
PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
335+
DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
336+
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
337+
IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.

FirebaseDatabase/Sources/Core/FPersistentConnection.m

+76-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
3535
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleCallbackStatus.h"
3636
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleOnDisconnect.h"
37-
#if !TARGET_OS_WATCH
37+
#if TARGET_OS_WATCH
38+
#import <WatchKit/WatchKit.h>
39+
#else
3840
#import <SystemConfiguration/SystemConfiguration.h>
39-
#endif // !TARGET_OS_WATCH
41+
#endif // TARGET_OS_WATCH
4042
#import <dlfcn.h>
4143
#import <netinet/in.h>
4244

@@ -166,6 +168,7 @@ - (id)initWithRepoInfo:(FRepoInfo *)repoInfo
166168
retryExponent:kPersistentConnReconnectMultiplier
167169
jitterFactor:0.7];
168170

171+
[self setupNotifications];
169172
// Make sure we don't actually connect until open is called
170173
[self interruptForReason:kFInterruptReasonWaitingForOpen];
171174
}
@@ -550,6 +553,77 @@ - (void)openNetworkConnectionWithContext:
550553
[self.realtime open];
551554
}
552555

556+
#if !TARGET_OS_WATCH
557+
static void reachabilityCallback(SCNetworkReachabilityRef ref,
558+
SCNetworkReachabilityFlags flags, void *info) {
559+
if (flags & kSCNetworkReachabilityFlagsReachable) {
560+
FFLog(@"I-RDB034014",
561+
@"Network became reachable. Trigger a connection attempt");
562+
FPersistentConnection *self = (__bridge FPersistentConnection *)info;
563+
// Reset reconnect delay
564+
[self.retryHelper signalSuccess];
565+
if (self->connectionState == ConnectionStateDisconnected) {
566+
[self tryScheduleReconnect];
567+
}
568+
} else {
569+
FFLog(@"I-RDB034015", @"Network is not reachable");
570+
}
571+
}
572+
#endif // !TARGET_OS_WATCH
573+
574+
- (void)enteringForeground {
575+
dispatch_async(self.dispatchQueue, ^{
576+
// Reset reconnect delay
577+
[self.retryHelper signalSuccess];
578+
if (self->connectionState == ConnectionStateDisconnected) {
579+
[self tryScheduleReconnect];
580+
}
581+
});
582+
}
583+
584+
- (void)setupNotifications {
585+
#if TARGET_OS_WATCH
586+
if (@available(watchOS 7.0, *)) {
587+
__weak FPersistentConnection *weakSelf = self;
588+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
589+
[center addObserverForName:WKApplicationWillEnterForegroundNotification
590+
object:nil
591+
queue:nil
592+
usingBlock:^(NSNotification *_Nonnull note) {
593+
[weakSelf enteringForeground];
594+
}];
595+
}
596+
#else
597+
NSString *const *foregroundConstant = (NSString *const *)dlsym(
598+
RTLD_DEFAULT, "UIApplicationWillEnterForegroundNotification");
599+
if (foregroundConstant) {
600+
[[NSNotificationCenter defaultCenter]
601+
addObserver:self
602+
selector:@selector(enteringForeground)
603+
name:*foregroundConstant
604+
object:nil];
605+
}
606+
// An empty address is interpreted a generic internet access
607+
struct sockaddr_in zeroAddress;
608+
bzero(&zeroAddress, sizeof(zeroAddress));
609+
zeroAddress.sin_len = sizeof(zeroAddress);
610+
zeroAddress.sin_family = AF_INET;
611+
reachability = SCNetworkReachabilityCreateWithAddress(
612+
kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
613+
SCNetworkReachabilityContext ctx = {0, (__bridge void *)(self), NULL, NULL,
614+
NULL};
615+
if (SCNetworkReachabilitySetCallback(reachability, reachabilityCallback,
616+
&ctx)) {
617+
SCNetworkReachabilitySetDispatchQueue(reachability, self.dispatchQueue);
618+
} else {
619+
FFLog(@"I-RDB034016",
620+
@"Failed to set up network reachability monitoring");
621+
CFRelease(reachability);
622+
reachability = NULL;
623+
}
624+
#endif // !TARGET_OS_WATCH
625+
}
626+
553627
- (void)sendAuthAndRestoreStateAfterComplete:(BOOL)restoreStateAfterComplete {
554628
NSAssert([self connected], @"Must be connected to send auth");
555629
NSAssert(self.authToken != nil,

FirebaseDatabase/Sources/Public/FirebaseDatabase/FIRDatabase.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ NS_ASSUME_NONNULL_BEGIN
2727
* read or write data, use `FIRDatabase.reference()`.
2828
*/
2929
NS_SWIFT_NAME(Database) __attribute__((availability(
30-
watchos, introduced = 7.0, deprecated = 9.0,
30+
watchos, introduced = 6.0, deprecated = 9.0,
3131
message = "Socket connections are not supported on watchOS 9.0 and higher. "
3232
"Use the Firebase Database REST API instead. See "
3333
"github.com/firebase/firebase-ios-sdk/issues/10195 "
3434
"for more details.")))
35-
API_AVAILABLE(ios(12.0), macos(10.15), macCatalyst(13), tvos(13.0),
36-
watchos(7.0)) @interface FIRDatabase : NSObject
35+
@interface FIRDatabase : NSObject
3736

3837
/**
3938
* The NSObject initializer that has been marked as unavailable. Use the

FirebaseDatabase/Sources/Realtime/FWebSocketConnection.h

+21
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
*/
1616

1717
#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
18+
#if !TARGET_OS_WATCH
19+
#import "FirebaseDatabase/Sources/third_party/SocketRocket/FSRWebSocket.h"
20+
#endif // !TARGET_OS_WATCH
1821
#import <Foundation/Foundation.h>
1922

2023
@protocol FWebSocketDelegate;
2124

25+
#if !TARGET_OS_WATCH
26+
@interface FWebSocketConnection : NSObject <FSRWebSocketDelegate>
27+
#else
2228
@interface FWebSocketConnection : NSObject <NSURLSessionWebSocketDelegate>
29+
#endif // else !TARGET_OS_WATCH
2330

2431
@property(nonatomic, weak) id<FWebSocketDelegate> delegate;
2532

@@ -34,6 +41,20 @@
3441
- (void)start;
3542
- (void)send:(NSDictionary *)dictionary;
3643

44+
// Ignore FSRWebSocketDelegate calls on watchOS.
45+
#if !TARGET_OS_WATCH
46+
- (void)webSocket:(FSRWebSocket *)webSocket didReceiveMessage:(id)message;
47+
48+
// Exclude the `webSocket` argument since it isn't used in this codebase and it
49+
// allows for better code sharing with watchOS.
50+
- (void)webSocketDidOpen;
51+
- (void)webSocket:(FSRWebSocket *)webSocket didFailWithError:(NSError *)error;
52+
- (void)webSocket:(FSRWebSocket *)webSocket
53+
didCloseWithCode:(NSInteger)code
54+
reason:(NSString *)reason
55+
wasClean:(BOOL)wasClean;
56+
#endif // !TARGET_OS_WATCH
57+
3758
@end
3859

3960
@protocol FWebSocketDelegate <NSObject>

0 commit comments

Comments
 (0)