我找到了解决方案。我知道这还不够好,但这是我发现解决问题的唯一方法:
在 .h 文件中。
@interface IPEngine : NSObject{
NSOperationQueue *operationQueue;
}
@property (nonatomic, retain) NSOperationQueue *operationQueue;
在 .m 文件中
-(NSData *) sendIpPacket:(NSData *)data {
switch (protocolType) {
case TCPCommunication:
[gcdAsyncTCPSocket writeData:data
withTimeout:-1.0
tag:0];
break;
case UDPCommunication:{
[gcdAsyncUdpSocket sendData:data
toHost:ipAddressString
port:[ipPortString integerValue]
withTimeout:3
tag:0];
}
break;
default:
break;
}
[self waitForResponse];
return responseData;
}
-(void) waitForResponse
{
self.operationQueue = [NSOperationQueue new];
self.operationQueue.suspended=true; //Okay make true
[self.operationQueue addOperationWithBlock:^{}];
[self.operationQueue waitUntilAllOperationsAreFinished];
}
对于 TCP
#pragma mark GCDAsyncSocket Delegate for TCP
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
self.operationQueue.suspended = NO;
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
self.operationQueue.suspended = YES;
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
responseData = data;
self.operationQueue.suspended = NO;
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
self.operationQueue.suspended = NO;
}
对于 UDP
#pragma mark GCDAsyncUdpSocket Delegate for UDP
/**
* Called when the datagram with the given tag has been sent.
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
self.operationQueue.suspended = YES;
}
/**
* Called if an error occurs while trying to send a datagram.
* This could be due to a timeout, or something more serious such as the data being too large to fit in a sigle packet.
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
self.operationQueue.suspended = NO;
}
/**
* Called when the socket has received the requested datagram.
**/
- (void)udpSocket:(GCDAsyncUdpSocket *)sock
didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext{
responseData = data;
self.operationQueue.suspended = NO;
}
/**
* Called when the socket is closed.
**/
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error{
self.operationQueue.suspended = NO;
}