//
//  RYUDPBrowser.h
//  RYCommunication iOS
//
//  Created by ldc on 2020/10/10.
//  Copyright © 2020 Xiamen Hanin. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@protocol RYUDPPort <NSObject>

@property (nonatomic, copy) NSString *ip;

@property (nonatomic, strong) NSData *advertisementData;

@end

@interface RYUDPPortAccessory : NSObject <RYUDPPort>

@end

@interface RYUDPBrowser : NSObject

@property (nonatomic, copy) void (^ _Nullable receiveDataBlock)(NSString *, NSData *);

@property (nonatomic, assign) BOOL enableBroadcast;


/// 设置发现广播间隔
/// - Parameter interval: 间隔
+ (void)setBroadcastInterval:(NSTimeInterval)interval;

+ (NSString *)broadcastIp;


/// 创建实例
/// - Parameters:
///   - port: 端口
///   - data: 搜索广播数据
- (instancetype)initWithPort:(uint16_t)port broadcast:(NSData * _Nullable)data;


/// 开始搜索
/// - Parameters:
///   - timeout: 搜索超时时间
///   - discovePortBlock: 发现设备回调
///   - searchTimeoutBlock: 搜索超时回调
///   - filterBlock: 过滤条件
///   - portGenerateBlock: 设备对象生成回调
///   - socketDisconnectedBlock: socket连接断开回调
///   - error: 开始搜索错误
- (NSString * _Nullable)startSearch:(NSTimeInterval)timeout
                   discovePortBlock:(void (^ _Nullable)(id<RYUDPPort>))discovePortBlock
                 searchTimeoutBlock:(void (^ _Nullable )(void))searchTimeoutBlock
                  portGenerateBlock:(id<RYUDPPort> _Nullable (^ _Nullable )(NSData *, NSString *))portGenerateBlock
            socketDisconnectedBlock:(void (^ _Nullable)(NSError * _Nullable))socketDisconnectedBlock
                              error:(NSError * _Nullable *)error;


/// 停止搜索
/// - Parameter identity: 任务标识
- (void)stopSearch:(NSString *)identity;


/// 发起广播
/// - Parameter data: 广播数据
- (void)broadcast:(NSData *)data;

@end

NS_ASSUME_NONNULL_END
