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.

142 lines
3.8 KiB

//
// RTKPackageCommunication.h
// RTKLEFoundation
//
// Created by jerome_gu on 2019/1/10.
// Copyright © 2019 Realtek. All rights reserved.
//
#import <Foundation/Foundation.h>
#ifdef RTK_SDK_IS_STATIC_LIBRARY
#import "RTKLEGeneralDefines.h"
#import "RTKAttemptAction.h"
#else
#import <RTKLEFoundation/RTKLEGeneralDefines.h>
#import <RTKLEFoundation/RTKAttemptAction.h>
#endif
NS_ASSUME_NONNULL_BEGIN
/**
*
*/
typedef void (^RTKCommunicationSendResult)(BOOL success, NSError *_Nullable error, NSData*_Nullable supply);
/**
*
*/
typedef void (^RTKCommunicationRequestResult)(BOOL success, NSError *_Nullable err, NSData*_Nullable data);
typedef NS_ENUM(NSUInteger, RTKPackageCommunicationStatus) {
RTKPackageCommunicationStatusNotOpen,
RTKPackageCommunicationStatusOpening,
RTKPackageCommunicationStatusOpen,
RTKPackageCommunicationStatusClosing,
RTKPackageCommunicationStatusClosed,
RTKPackageCommunicationStatusErrorOccurred, /* can not reopen*/
};
@class RTKPackageCommunication;
/**
* Communication处理的状态变化和接收数据
*/
@protocol RTKPackageCommunicationClient <NSObject>
- (void)communicationDidChangeState:(RTKPackageCommunication *)communication;
- (void)communication:(RTKPackageCommunication *)transmission didReceive:(NSData *)data;
@end
/**
*
* @discussion
*/
@interface RTKPackageCommunication : NSObject <RTKPackageCommunicationClient> {
@protected
RTKPackageCommunicationStatus _state;
}
@property (readonly, nullable) RTKPackageCommunication *underlyingCommunication;
@property (readonly) RTKPackageCommunicationStatus state;
- (instancetype)initWithUnderlyingCommunication:(RTKPackageCommunication *)communication;
/**
* Communication对象的上层客户
*/
@property (readonly) NSArray <RTKPackageCommunicationClient> *upperCommunications;
- (void)addUpperCommunication:(id<RTKPackageCommunicationClient>)communication;
- (void)removeUpperCommunication:(id<RTKPackageCommunicationClient>)communication;
/**
* Communication
* @discussion
*/
- (void)open;
- (void)openWithCompletion:(RTKLECompletionBlock)handler;
/**
* Communication
* @discussion
*/
- (void)close;
/**
*
* @param handler Block
* @discussion handler中得到发送结果handler第一个参数为YES; handler第一个参数为NO. handler不一定被调用
*/
- (void)send:(NSData *)data completionHandler:(RTKCommunicationSendResult)handler;
// will not invoke the -send: completion handler.
- (void)cancelSendingOfData:(NSData *)data;
@end
/**
* ACK确认的通信机制
*/
@interface RTKPackageACKCommunication : RTKPackageCommunication
@property (readonly) NSMutableArray <RTKAttemptAction*> *pendingSends;
// protected
- (nullable RTKAttemptAction *)pendingSendOfID:(NSInteger)ID;
- (nullable RTKAttemptAction *)pendingSendOfID:(NSInteger)ID subID:(NSInteger)subID;
- (void)ackPendingSends:(RTKLEACKPackage *)ackPackage;
@end
/**
* Request形式的通信
*/
@interface RTKRequestCommunication : RTKPackageCommunication
@property (readonly) NSMutableArray <RTKAttemptAction*> *pendingRequests;
- (void)sendRequest:(NSData *)data completionHandler:(RTKCommunicationRequestResult)handler;
- (void)sendRequest:(NSData *)data waitInterval:(NSTimeInterval)interval retryCount:(NSUInteger)count completionHandler:(RTKCommunicationRequestResult)handler;
// protected
- (nullable RTKAttemptAction *)pendingRequestOfID:(NSInteger)ID subID:(NSInteger)subID;
- (BOOL)canResponsePendingRequests:(RTKLEPackage *)package;
@end
NS_ASSUME_NONNULL_END