diff --git a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m index 09b14a3..960ba1c 100644 --- a/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m +++ b/Ifish/controllers/leftcontrollers/IFishHotpotUDPHelper.m @@ -21,6 +21,9 @@ NSString *_pwd; } +@property (nonatomic, strong) NSTimer *loopTimer; + +@property (nonatomic, assign) int tag; @end @@ -46,21 +49,36 @@ NSError *error; //本地接收端口 [self.clientSocket bindToPort:bindPort error:&error]; - [self.clientSocket receiveWithTimeout:-1 tag:0]; } return self; } +- (void)loopTimerTick { + if (self.tag == 0) { + [self sendConnectHotspotSoketdata]; + } else if (self.tag == 1) { + [self sendRestartSocketData]; + } else if (self.tag == 2) { + [self sendResetSocketData]; + } +} + +- (void)startLoopTimer { + self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(loopTimerTick) userInfo:nil repeats:YES]; +} + - (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd { _ssidName = ssidName; _pwd = pwd; [self postmsg:@"命令已发出"]; [self sendConnectHotspotSoketdata]; + [self startLoopTimer]; } - (void)broadCastRestCommand { [self postmsg:@"命令已发出"]; [self sendResetSocketData]; + [self startLoopTimer]; } - (void)sendResetSocketData { @@ -71,13 +89,16 @@ NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; - - [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0]; + self.tag = 2; + [self.clientSocket receiveWithTimeout:-1 tag:self.tag]; + + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag]; } - (void)broadCastRestartCommand { [self postmsg:@"命令已发出"]; [self sendRestartSocketData]; + [self startLoopTimer]; } - (void)sendRestartSocketData { @@ -89,8 +110,10 @@ NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; - - [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0]; + self.tag = 1; + [self.clientSocket receiveWithTimeout:-1 tag:self.tag]; + + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag]; } -(void)sendConnectHotspotSoketdata{ @@ -105,11 +128,17 @@ NSData *data=[dataContorl stringToHexData:request]; UInt16 port = 333; - [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0]; + self.tag = 0; + [self.clientSocket receiveWithTimeout:-1 tag:self.tag]; + + [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag]; } -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port { + [self.loopTimer invalidate]; + self.loopTimer = nil; + NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data]; if ([model.result isEqualToString:@"01"]) {