2 * Copyright 2017 Google
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import <Foundation/Foundation.h>
19 @class FIRMessagingConnection;
20 @class FIRMessagingDataMessageManager;
21 @class FIRMessagingRmqManager;
23 @class GtalkDataMessageStanza;
26 typedef void (^FIRMessagingMessageHandler)(NSDictionary *);
28 typedef NS_ENUM(NSUInteger, FIRMessagingConnectionState) {
29 kFIRMessagingConnectionNotConnected = 0,
30 kFIRMessagingConnectionConnecting,
31 kFIRMessagingConnectionConnected,
32 kFIRMessagingConnectionSignedIn,
35 typedef NS_ENUM(NSUInteger, FIRMessagingConnectionCloseReason) {
36 kFIRMessagingConnectionCloseReasonSocketDisconnected = 0,
37 kFIRMessagingConnectionCloseReasonTimeout,
38 kFIRMessagingConnectionCloseReasonUserDisconnect,
41 @protocol FIRMessagingConnectionDelegate<NSObject>
43 - (void)connection:(FIRMessagingConnection *)fcmConnection
44 didCloseForReason:(FIRMessagingConnectionCloseReason)reason;
45 - (void)didLoginWithConnection:(FIRMessagingConnection *)fcmConnection;
46 - (void)connectionDidRecieveMessage:(GtalkDataMessageStanza *)message;
48 * Called when a stream ACK or a selective ACK are received - this indicates the
49 * message has been received by MCS.
50 * @return The count of rmqIds deleted from the client RMQ store.
52 - (int)connectionDidReceiveAckForRmqIds:(NSArray *)rmqIds;
58 * This class maintains the actual FIRMessaging connection that we use to receive and send messages
59 * while the app is in foreground. Once we have a registrationID from the FIRMessaging backend we
60 * are able to set up this connection which is used for any further communication with FIRMessaging
61 * backend. In case the connection breaks off while the app is still being used we try to rebuild
62 * the connection with an exponential backoff.
64 * This class also notifies the delegate about the main events happening in the lifcycle of the
65 * FIRMessaging connection (read FIRMessagingConnectionDelegate). All of the `on-the-wire`
66 * interactions with FIRMessaging are channelled through here.
68 @interface FIRMessagingConnection : NSObject
70 @property(nonatomic, readonly, assign) FIRMessagingConnectionState state;
71 @property(nonatomic, readonly, copy) NSString *host;
72 @property(nonatomic, readonly, assign) NSUInteger port;
73 @property(nonatomic, readwrite, weak) id<FIRMessagingConnectionDelegate> delegate;
75 - (instancetype)initWithAuthID:(NSString *)authId
76 token:(NSString *)token
79 runLoop:(NSRunLoop *)runLoop
80 rmq2Manager:(FIRMessagingRmqManager *)rmq2Manager
81 fcmManager:(FIRMessagingDataMessageManager *)dataMessageManager;
83 - (void)signIn; // connect
84 - (void)signOut; // disconnect
87 * Teardown the FIRMessaging connection and deallocate the resources being held up by the
93 * Send proto to the wire. The message will be cached before we try to send so that in case of
94 * failure we can send it again later on when we have connection.
96 - (void)sendProto:(GPBMessage *)proto;
99 * Send a message after the currently in progress connection succeeds, otherwise drop it.
101 * This should be used for TTL=0 messages that force a reconnect. They shouldn't be persisted
102 * in the RMQ, but they should be sent if the reconnect is successful.
104 - (void)sendOnConnectOrDrop:(GPBMessage *)message;