You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.0 KiB

6 years ago
  1. /*
  2. * Copyright 2017 Google
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #import <Foundation/Foundation.h>
  17. @class FIRMessagingConnection;
  18. @class FIRMessagingDataMessageManager;
  19. @class FIRMessagingRmqManager;
  20. @class GtalkDataMessageStanza;
  21. @class GPBMessage;
  22. typedef void (^FIRMessagingMessageHandler)(NSDictionary *);
  23. typedef NS_ENUM(NSUInteger, FIRMessagingConnectionState) {
  24. kFIRMessagingConnectionNotConnected = 0,
  25. kFIRMessagingConnectionConnecting,
  26. kFIRMessagingConnectionConnected,
  27. kFIRMessagingConnectionSignedIn,
  28. };
  29. typedef NS_ENUM(NSUInteger, FIRMessagingConnectionCloseReason) {
  30. kFIRMessagingConnectionCloseReasonSocketDisconnected = 0,
  31. kFIRMessagingConnectionCloseReasonTimeout,
  32. kFIRMessagingConnectionCloseReasonUserDisconnect,
  33. };
  34. @protocol FIRMessagingConnectionDelegate<NSObject>
  35. - (void)connection:(FIRMessagingConnection *)fcmConnection
  36. didCloseForReason:(FIRMessagingConnectionCloseReason)reason;
  37. - (void)didLoginWithConnection:(FIRMessagingConnection *)fcmConnection;
  38. - (void)connectionDidRecieveMessage:(GtalkDataMessageStanza *)message;
  39. /**
  40. * Called when a stream ACK or a selective ACK are received - this indicates the
  41. * message has been received by MCS.
  42. * @return The count of rmqIds deleted from the client RMQ store.
  43. */
  44. - (int)connectionDidReceiveAckForRmqIds:(NSArray *)rmqIds;
  45. @end
  46. /**
  47. * This class maintains the actual FIRMessaging connection that we use to receive and send messages
  48. * while the app is in foreground. Once we have a registrationID from the FIRMessaging backend we
  49. * are able to set up this connection which is used for any further communication with FIRMessaging
  50. * backend. In case the connection breaks off while the app is still being used we try to rebuild
  51. * the connection with an exponential backoff.
  52. *
  53. * This class also notifies the delegate about the main events happening in the lifcycle of the
  54. * FIRMessaging connection (read FIRMessagingConnectionDelegate). All of the `on-the-wire`
  55. * interactions with FIRMessaging are channelled through here.
  56. */
  57. @interface FIRMessagingConnection : NSObject
  58. @property(nonatomic, readwrite, assign) int64_t lastHeartbeatPingTimestamp;
  59. @property(nonatomic, readonly, assign) FIRMessagingConnectionState state;
  60. @property(nonatomic, readonly, copy) NSString *host;
  61. @property(nonatomic, readonly, assign) NSUInteger port;
  62. @property(nonatomic, readwrite, weak) id<FIRMessagingConnectionDelegate> delegate;
  63. - (instancetype)initWithAuthID:(NSString *)authId
  64. token:(NSString *)token
  65. host:(NSString *)host
  66. port:(NSUInteger)port
  67. runLoop:(NSRunLoop *)runLoop
  68. rmq2Manager:(FIRMessagingRmqManager *)rmq2Manager
  69. fcmManager:(FIRMessagingDataMessageManager *)dataMessageManager;
  70. - (void)signIn; // connect
  71. - (void)signOut; // disconnect
  72. /**
  73. * Teardown the FIRMessaging connection and deallocate the resources being held up by the
  74. * connection.
  75. */
  76. - (void)teardown;
  77. /**
  78. * Send proto to the wire. The message will be cached before we try to send so that in case of
  79. * failure we can send it again later on when we have connection.
  80. */
  81. - (void)sendProto:(GPBMessage *)proto;
  82. /**
  83. * Send a message after the currently in progress connection succeeds, otherwise drop it.
  84. *
  85. * This should be used for TTL=0 messages that force a reconnect. They shouldn't be persisted
  86. * in the RMQ, but they should be sent if the reconnect is successful.
  87. */
  88. - (void)sendOnConnectOrDrop:(GPBMessage *)message;
  89. @end