Skip to content

Restore socket rocket #14427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions CoreOnly/NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,64 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------
SocketRocket
--------------------------
Copyright 2012 Square Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

$OpenBSD: base64.c,v 1.5 2006/10/21 09:55:03 otto Exp $

Copyright (c) 1996 by Internet Software Consortium.

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

Portions Copyright (c) 1995 by International Business Machines, Inc.

International Business Machines, Inc. (hereinafter called IBM) grants
permission under its copyrights to use, copy, modify, and distribute this
Software with or without fee, provided that the above copyright notice and
all paragraphs of this notice appear in all copies, and that the name of IBM
not be used in connection with the marketing of any product incorporating
the Software or modifications thereof, without specific, written prior
permission.

To the extent it has a right to do so, IBM grants an immunity from suit
under its patents, if any, for the use, sale or manufacture of products to
the extent that such products are used for performing Domain Name System
dynamic updates in TCP/IP networks by means of the Software. No immunity is
granted for any product per se or for any other function of any product.

THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.


FirebaseFirestore
FirebaseFirestoreInternal

Expand Down
1 change: 1 addition & 0 deletions FirebaseDatabase.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
s.source_files = [
base_dir + '**/*.[mh]',
base_dir + 'third_party/Wrap-leveldb/APLevelDB.mm',
base_dir + 'third_party/SocketRocket/fbase64.c',
'FirebaseDatabase/Swift/Sources/**/*.swift',
'FirebaseAuth/Interop/**/*.h',
'FirebaseCore/Extension/*.h',
Expand Down
6 changes: 6 additions & 0 deletions FirebaseDatabase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Unreleased
- [fixed] Fix connection failure issue introduced in 10.27.0 by restoring the
Socket Rocket implementation instead of `NSURLSessionWebSocket`. Note that
this may expose a Thread Performance Checker Warning (#12883).
(#14188, #13877, #13855, #13529)

# 11.2.0
- [fixed] Fix temporary disconnects when the app goes inactive. The issue was
introduced in 10.27.0. (#13529)
Expand Down
58 changes: 58 additions & 0 deletions FirebaseDatabase/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,61 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------
SocketRocket
--------------------------
Copyright 2012 Square Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

$OpenBSD: base64.c,v 1.5 2006/10/21 09:55:03 otto Exp $

Copyright (c) 1996 by Internet Software Consortium.

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

Portions Copyright (c) 1995 by International Business Machines, Inc.

International Business Machines, Inc. (hereinafter called IBM) grants
permission under its copyrights to use, copy, modify, and distribute this
Software with or without fee, provided that the above copyright notice and
all paragraphs of this notice appear in all copies, and that the name of IBM
not be used in connection with the marketing of any product incorporating
the Software or modifications thereof, without specific, written prior
permission.

To the extent it has a right to do so, IBM grants an immunity from suit
under its patents, if any, for the use, sale or manufacture of products to
the extent that such products are used for performing Domain Name System
dynamic updates in TCP/IP networks by means of the Software. No immunity is
granted for any product per se or for any other function of any product.

THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
78 changes: 76 additions & 2 deletions FirebaseDatabase/Sources/Core/FPersistentConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleCallbackStatus.h"
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleOnDisconnect.h"
#if !TARGET_OS_WATCH
#if TARGET_OS_WATCH
#import <WatchKit/WatchKit.h>
#else
#import <SystemConfiguration/SystemConfiguration.h>
#endif // !TARGET_OS_WATCH
#endif // TARGET_OS_WATCH
#import <dlfcn.h>
#import <netinet/in.h>

Expand Down Expand Up @@ -166,6 +168,7 @@ - (id)initWithRepoInfo:(FRepoInfo *)repoInfo
retryExponent:kPersistentConnReconnectMultiplier
jitterFactor:0.7];

[self setupNotifications];
// Make sure we don't actually connect until open is called
[self interruptForReason:kFInterruptReasonWaitingForOpen];
}
Expand Down Expand Up @@ -550,6 +553,77 @@ - (void)openNetworkConnectionWithContext:
[self.realtime open];
}

#if !TARGET_OS_WATCH
static void reachabilityCallback(SCNetworkReachabilityRef ref,
SCNetworkReachabilityFlags flags, void *info) {
if (flags & kSCNetworkReachabilityFlagsReachable) {
FFLog(@"I-RDB034014",
@"Network became reachable. Trigger a connection attempt");
FPersistentConnection *self = (__bridge FPersistentConnection *)info;
// Reset reconnect delay
[self.retryHelper signalSuccess];
if (self->connectionState == ConnectionStateDisconnected) {
[self tryScheduleReconnect];
}
} else {
FFLog(@"I-RDB034015", @"Network is not reachable");
}
}
#endif // !TARGET_OS_WATCH

- (void)enteringForeground {
dispatch_async(self.dispatchQueue, ^{
// Reset reconnect delay
[self.retryHelper signalSuccess];
if (self->connectionState == ConnectionStateDisconnected) {
[self tryScheduleReconnect];
}
});
}

- (void)setupNotifications {
#if TARGET_OS_WATCH
if (@available(watchOS 7.0, *)) {
__weak FPersistentConnection *weakSelf = self;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserverForName:WKApplicationWillEnterForegroundNotification
object:nil
queue:nil
usingBlock:^(NSNotification *_Nonnull note) {
[weakSelf enteringForeground];
}];
}
#else
NSString *const *foregroundConstant = (NSString *const *)dlsym(
RTLD_DEFAULT, "UIApplicationWillEnterForegroundNotification");
if (foregroundConstant) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(enteringForeground)
name:*foregroundConstant
object:nil];
}
// An empty address is interpreted a generic internet access
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
reachability = SCNetworkReachabilityCreateWithAddress(
kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
SCNetworkReachabilityContext ctx = {0, (__bridge void *)(self), NULL, NULL,
NULL};
if (SCNetworkReachabilitySetCallback(reachability, reachabilityCallback,
&ctx)) {
SCNetworkReachabilitySetDispatchQueue(reachability, self.dispatchQueue);
} else {
FFLog(@"I-RDB034016",
@"Failed to set up network reachability monitoring");
CFRelease(reachability);
reachability = NULL;
}
#endif // !TARGET_OS_WATCH
}

- (void)sendAuthAndRestoreStateAfterComplete:(BOOL)restoreStateAfterComplete {
NSAssert([self connected], @"Must be connected to send auth");
NSAssert(self.authToken != nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ NS_ASSUME_NONNULL_BEGIN
* read or write data, use `FIRDatabase.reference()`.
*/
NS_SWIFT_NAME(Database) __attribute__((availability(
watchos, introduced = 7.0, deprecated = 9.0,
watchos, introduced = 6.0, deprecated = 9.0,
message = "Socket connections are not supported on watchOS 9.0 and higher. "
"Use the Firebase Database REST API instead. See "
"github.com/firebase/firebase-ios-sdk/issues/10195 "
"for more details.")))
API_AVAILABLE(ios(12.0), macos(10.15), macCatalyst(13), tvos(13.0),
watchos(7.0)) @interface FIRDatabase : NSObject
@interface FIRDatabase : NSObject

/**
* The NSObject initializer that has been marked as unavailable. Use the
Expand Down
21 changes: 21 additions & 0 deletions FirebaseDatabase/Sources/Realtime/FWebSocketConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
*/

#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
#if !TARGET_OS_WATCH
#import "FirebaseDatabase/Sources/third_party/SocketRocket/FSRWebSocket.h"
#endif // !TARGET_OS_WATCH
#import <Foundation/Foundation.h>

@protocol FWebSocketDelegate;

#if !TARGET_OS_WATCH
@interface FWebSocketConnection : NSObject <FSRWebSocketDelegate>
#else
@interface FWebSocketConnection : NSObject <NSURLSessionWebSocketDelegate>
#endif // else !TARGET_OS_WATCH

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

Expand All @@ -34,6 +41,20 @@
- (void)start;
- (void)send:(NSDictionary *)dictionary;

// Ignore FSRWebSocketDelegate calls on watchOS.
#if !TARGET_OS_WATCH
- (void)webSocket:(FSRWebSocket *)webSocket didReceiveMessage:(id)message;

// Exclude the `webSocket` argument since it isn't used in this codebase and it
// allows for better code sharing with watchOS.
- (void)webSocketDidOpen;
- (void)webSocket:(FSRWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocket:(FSRWebSocket *)webSocket
didCloseWithCode:(NSInteger)code
reason:(NSString *)reason
wasClean:(BOOL)wasClean;
#endif // !TARGET_OS_WATCH

@end

@protocol FWebSocketDelegate <NSObject>
Expand Down
Loading
Loading