objective c - How to make my Thread waiting until GCDAsyncSocket or AsyncSocket response me? -
when finding tcp/ip communication lib objective-c, people suggested me use gcdasyncsocket. have tired it, can use gcdasyncsocket establish tcp connection, , send packets.
but, project has list of process. have wait connection established, send ip packet 1, read response data, send ip packet 2, read response data...... following code:
[self establishgcdasyncsockettcpconnection]; if ([self handshake1]) { nslog(@"handshake 1 done! "); } else { nslog(@"handshake 1 fail! *"); return no; } if ([self handshake2]) { nslog(@"handshake 2 done! "); } else { nslog(@"handshake 2 fail! *"); return no; }
i have wait ip delegate function return me something.
- (void)socket:(gcdasyncsocket *)sock didconnecttohost:(nsstring *)host port:(uint16)port { nslog(@"socket:didconnecttohost:%@ port:%d", host, port); tcpconnectionflag = yes; } - (void)socket:(gcdasyncsocket *)sock didreaddata:(nsdata *)data withtag:(long)tag { //return me something. }
question:
how make thread waiting gcdasyncsocket response? or there other solution? replace gcdasyncsocket or asyncsocket else?
i found solution. know not enough, way found solve problem:
in .h file.
@interface ipengine : nsobject{ nsoperationqueue *operationqueue; } @property (nonatomic, retain) nsoperationqueue *operationqueue;
in .m file
-(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]; }
for tcp
#pragma mark gcdasyncsocket delegate 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; }
for udp
#pragma mark gcdasyncudpsocket delegate udp /** * called when datagram given tag has been sent. **/ - (void)udpsocket:(gcdasyncudpsocket *)sock didsenddatawithtag:(long)tag{ self.operationqueue.suspended = yes; } /** * called if error occurs while trying send datagram. * due timeout, or more serious such data being large fit in sigle packet. **/ - (void)udpsocket:(gcdasyncudpsocket *)sock didnotsenddatawithtag:(long)tag duetoerror:(nserror *)error{ self.operationqueue.suspended = no; } /** * called when socket has received requested datagram. **/ - (void)udpsocket:(gcdasyncudpsocket *)sock didreceivedata:(nsdata *)data fromaddress:(nsdata *)address withfiltercontext:(id)filtercontext{ responsedata = data; self.operationqueue.suspended = no; } /** * called when socket closed. **/ - (void)udpsocketdidclose:(gcdasyncudpsocket *)sock witherror:(nserror *)error{ self.operationqueue.suspended = no; }
Comments
Post a Comment