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.

105 lines
3.8 KiB

6 years ago
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. */
  43. - (void)connectionDidReceiveAckForRmqIds:(NSArray *)rmqIds;
  44. @end
  45. /**
  46. * This class maintains the actual FIRMessaging connection that we use to receive and send messages
  47. * while the app is in foreground. Once we have a registrationID from the FIRMessaging backend we
  48. * are able to set up this connection which is used for any further communication with FIRMessaging
  49. * backend. In case the connection breaks off while the app is still being used we try to rebuild
  50. * the connection with an exponential backoff.
  51. *
  52. * This class also notifies the delegate about the main events happening in the lifcycle of the
  53. * FIRMessaging connection (read FIRMessagingConnectionDelegate). All of the `on-the-wire`
  54. * interactions with FIRMessaging are channelled through here.
  55. */
  56. @interface FIRMessagingConnection : NSObject
  57. @property(nonatomic, readonly, assign) FIRMessagingConnectionState state;
  58. @property(nonatomic, readonly, copy) NSString *host;
  59. @property(nonatomic, readonly, assign) NSUInteger port;
  60. @property(nonatomic, readwrite, weak) id<FIRMessagingConnectionDelegate> delegate;
  61. - (instancetype)initWithAuthID:(NSString *)authId
  62. token:(NSString *)token
  63. host:(NSString *)host
  64. port:(NSUInteger)port
  65. runLoop:(NSRunLoop *)runLoop
  66. rmq2Manager:(FIRMessagingRmqManager *)rmq2Manager
  67. fcmManager:(FIRMessagingDataMessageManager *)dataMessageManager;
  68. - (void)signIn; // connect
  69. - (void)signOut; // disconnect
  70. /**
  71. * Teardown the FIRMessaging connection and deallocate the resources being held up by the
  72. * connection.
  73. */
  74. - (void)teardown;
  75. /**
  76. * Send proto to the wire. The message will be cached before we try to send so that in case of
  77. * failure we can send it again later on when we have connection.
  78. */
  79. - (void)sendProto:(GPBMessage *)proto;
  80. /**
  81. * Send a message after the currently in progress connection succeeds, otherwise drop it.
  82. *
  83. * This should be used for TTL=0 messages that force a reconnect. They shouldn't be persisted
  84. * in the RMQ, but they should be sent if the reconnect is successful.
  85. */
  86. - (void)sendOnConnectOrDrop:(GPBMessage *)message;
  87. @end