Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RunLoop/AsyncUdpSocket.m #49

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions RunLoop/AsyncUdpSocket.m
Expand Up @@ -205,10 +205,12 @@ - (id)initWithDelegate:(id)delegate userData:(long)userData enableIPv4:(BOOL)ena
theSendQueue = [[NSMutableArray alloc] initWithCapacity:SENDQUEUE_CAPACITY];
theCurrentSend = nil;
theSendTimer = nil;
theSendQueueLock = [[NSLock alloc] init];

theReceiveQueue = [[NSMutableArray alloc] initWithCapacity:RECEIVEQUEUE_CAPACITY];
theCurrentReceive = nil;
theReceiveTimer = nil;
theReceiveQueueLock = [[NSLock alloc] init];

// Socket context
theContext.version = 0;
Expand Down Expand Up @@ -308,7 +310,8 @@ - (id)initIPv6
- (void) dealloc
{
[self close];

[theSendQueueLock release];
[theReceiveQueueLock release];
[NSObject cancelPreviousPerformRequestsWithTarget:theDelegate selector:@selector(onUdpSocketDidClose:) object:self];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
Expand Down Expand Up @@ -1662,8 +1665,9 @@ - (BOOL)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)ta
if(![self isConnected]) return NO;

AsyncSendPacket *packet = [[AsyncSendPacket alloc] initWithData:data address:nil timeout:timeout tag:tag];

[theSendQueueLock lock];
[theSendQueue addObject:packet];
[theSendQueueLock unlock];
[self scheduleDequeueSend];

return YES;
Expand Down Expand Up @@ -1693,8 +1697,9 @@ - (BOOL)sendData:(NSData *)data
packet = [[AsyncSendPacket alloc] initWithData:data address:address6 timeout:timeout tag:tag];
else
return NO;

[theSendQueueLock lock];
[theSendQueue addObject:packet];
[theSendQueueLock unlock];
[self scheduleDequeueSend];

return YES;
Expand All @@ -1716,8 +1721,9 @@ - (BOOL)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTi
return NO;

AsyncSendPacket *packet = [[AsyncSendPacket alloc] initWithData:data address:remoteAddr timeout:timeout tag:tag];

[theSendQueueLock lock];
[theSendQueue addObject:packet];
[theSendQueueLock unlock];
[self scheduleDequeueSend];

return YES;
Expand Down Expand Up @@ -1790,7 +1796,9 @@ - (void)maybeDequeueSend
{
// Dequeue next send packet
theCurrentSend = [theSendQueue objectAtIndex:0];
[theSendQueueLock lock];
[theSendQueue removeObjectAtIndex:0];
[theSendQueueLock unlock];

// Start time-out timer.
if(theCurrentSend->timeout >= 0.0)
Expand Down Expand Up @@ -1941,8 +1949,9 @@ - (void)receiveWithTimeout:(NSTimeInterval)timeout tag:(long)tag
if(theFlags & kDidClose) return;

AsyncReceivePacket *packet = [[AsyncReceivePacket alloc] initWithTimeout:timeout tag:tag];

[theReceiveQueueLock lock];
[theReceiveQueue addObject:packet];
[theReceiveQueueLock unlock];
[self scheduleDequeueReceive];
}

Expand Down Expand Up @@ -2002,8 +2011,9 @@ - (void)maybeDequeueReceive
{
// Dequeue next receive packet
theCurrentReceive = [theReceiveQueue objectAtIndex:0];
[theReceiveQueueLock lock];
[theReceiveQueue removeObjectAtIndex:0];

[theReceiveQueueLock unlock];
// Start time-out timer.
if (theCurrentReceive->timeout >= 0.0)
{
Expand Down