//
//  ShearDeviceUDPManager.m
//  VclustersGemini
//
//  Copyright © 2021 APPLE. All rights reserved.
//

#import "RYShearDeviceUDPManager.h"
#import "GCDAsyncUdpSocket.h"

static RYShearDeviceUDPManager *shareInstance = nil;

@interface RYShearDeviceUDPManager()<GCDAsyncUdpSocketDelegate>{
    GCDAsyncUdpSocket *updSocket;
    GCDAsyncUdpSocket *serverSocket;
    
    BOOL isInShearch;
}

@end

@implementation RYShearDeviceUDPManager

+ (instancetype)shareInstance {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[RYShearDeviceUDPManager alloc] init];
    });
    return shareInstance;
}

- (void)startShearchDevice{
    updSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
    serverSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
    NSError *error = nil;
    [updSocket bindToPort:10000 error:&error];/*绑定端口*/

    [updSocket enableBroadcast:YES error:&error];
    
    [serverSocket bindToPort:10001 error:&error];
    [serverSocket enableBroadcast:YES error:&error];
    
    if (error){
        HLog(@"\n---error--%@-",error);
    }else{
//        [updSocket beginReceiving:&error];
        [serverSocket beginReceiving:&error];
    }
}

- (void)broadcast{
    NSString *str = @"getIpAddress";
    NSData *data = [str dataUsingEncoding:(NSUTF8StringEncoding)];
    NSString *host = @"255.255.255.255";
    [updSocket sendData:data toHost:host port:10000 withTimeout:-1 tag:100];
    HLog(@"\n----广播-%@---%@--",host,str);
    if (isInShearch){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self broadcast];
        });
    }
}

- (void)shearchDeviceLoop{
    isInShearch = YES;
    
    [self broadcast];
}

/**
 * 发送消息成功
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
    HLog(@"\n----广播发送成功--");
}

/**
 * 发送消息失败
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
    ;
}

/**
 * 接受到消息
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock
   didReceiveData:(NSData *)data
      fromAddress:(NSData *)address
withFilterContext:(id)filterContext{
    NSString *ip = [GCDAsyncUdpSocket hostFromAddress:address];/*ip*/
    uint16_t port = [GCDAsyncUdpSocket portFromAddress:address];/*端口*/
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    if (port == 10001){/*设备端回复的消息*/
        isInShearch = NO;
    }
}

//- (void)backBroadcast{
//    NSString *str = @"1213456789087968543546789";
//    NSData *data = [str dataUsingEncoding:(NSUTF8StringEncoding)];
//    NSString *host = @"255.255.255.255";
//    [serverSocket sendData:data toHost:host port:10001 withTimeout:-1 tag:100];
//}

/**
 * 关闭
**/
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error{
    ;
}

@end
