Fixed up last.fm scrobbling.

CQTexperiment
vspader 2009-03-27 20:00:45 -07:00
parent 7e3f257090
commit a0778d5c4f
2 changed files with 36 additions and 46 deletions

View File

@ -59,7 +59,7 @@ escapeForLastFM(NSString *string)
- (semaphore_t) semaphore; - (semaphore_t) semaphore;
- (void) processAudioScrobblerCommands:(AudioScrobbler *)myself; - (void) processAudioScrobblerCommands:(id)unused;
@end @end
@ -85,7 +85,7 @@ escapeForLastFM(NSString *string)
return nil; return nil;
} }
[NSThread detachNewThreadSelector:@selector(processAudioScrobblerCommands:) toTarget:self withObject:self]; [NSThread detachNewThreadSelector:@selector(processAudioScrobblerCommands:) toTarget:self withObject:nil];
} }
return self; return self;
} }
@ -190,7 +190,7 @@ escapeForLastFM(NSString *string)
return _semaphore; return _semaphore;
} }
- (void) processAudioScrobblerCommands:(AudioScrobbler *)myself - (void) processAudioScrobblerCommands:(id)unused
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AudioScrobblerClient *client = [[AudioScrobblerClient alloc] init]; AudioScrobblerClient *client = [[AudioScrobblerClient alloc] init];
@ -200,15 +200,15 @@ escapeForLastFM(NSString *string)
NSString *response = nil; NSString *response = nil;
in_port_t port = 33367; in_port_t port = 33367;
while([myself keepProcessingAudioScrobblerCommands]) { while([self keepProcessingAudioScrobblerCommands]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Get the first command to be sent // Get the first command to be sent
@synchronized([myself queue]) { @synchronized([self queue]) {
enumerator = [[myself queue] objectEnumerator]; enumerator = [[self queue] objectEnumerator];
command = [[enumerator nextObject] retain]; command = [[enumerator nextObject] retain];
[[myself queue] removeObjectIdenticalTo:command]; [[self queue] removeObjectIdenticalTo:command];
} }
if(nil != command) { if(nil != command) {
@ -234,14 +234,14 @@ escapeForLastFM(NSString *string)
} }
} }
semaphore_timedwait([myself semaphore], timeout); semaphore_timedwait([self semaphore], timeout);
[pool release]; [pool release];
} }
// Send a final stop command to cleanup // Send a final stop command to cleanup
@try { @try {
if([client connectToHost:@"localhost" port:port]) { if([client connectToHost:@"localhost" port:port]) {
[client send:[NSString stringWithFormat:@"STOP c=%@\n", [myself pluginID]]]; [client send:[NSString stringWithFormat:@"STOP c=%@\n", [self pluginID]]];
response = [client receive]; response = [client receive];
if(2 > [response length] || NSOrderedSame != [response compare:@"OK" options:NSLiteralSearch range:NSMakeRange(0,2)]) if(2 > [response length] || NSOrderedSame != [response compare:@"OK" options:NSLiteralSearch range:NSMakeRange(0,2)])
@ -256,7 +256,7 @@ escapeForLastFM(NSString *string)
} }
[client release]; [client release];
[myself setAudioScrobblerThreadCompleted:YES]; [self setAudioScrobblerThreadCompleted:YES];
[pool release]; [pool release];
} }

View File

@ -188,8 +188,11 @@ addressForHost(NSString *hostname)
{ {
NSParameterAssert(INADDR_NONE != remoteAddress); NSParameterAssert(INADDR_NONE != remoteAddress);
struct sockaddr_in socketAddress; _port = port;
int result; int result;
do {
struct sockaddr_in socketAddress;
_socket = socket(AF_INET, SOCK_STREAM, 0); _socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == _socket) { if(-1 == _socket) {
@ -197,33 +200,20 @@ addressForHost(NSString *hostname)
return NO; return NO;
} }
_port = port;
socketAddress.sin_family = AF_INET; socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = remoteAddress; socketAddress.sin_addr.s_addr = remoteAddress;
socketAddress.sin_port = htons(_port); socketAddress.sin_port = htons(_port);
result = connect(_socket, (const struct sockaddr *)&socketAddress, sizeof(struct sockaddr_in)); result = connect(_socket, (const struct sockaddr *)&socketAddress, sizeof(struct sockaddr_in));
// Don't check result yet
if(_doPortStepping) {
while(-1 == result && _port <= (port + kPortsToStep)) {
socketAddress.sin_port = htons(++_port);
result = connect(_socket, (const struct sockaddr *)&socketAddress, sizeof(struct sockaddr_in));
}
}
// Don't log failures, because the client may not be running
if(-1 == result) { if(-1 == result) {
close(_socket); close(_socket);
_socket = -1; _socket = -1;
_port = 0;
_doPortStepping = NO;
return NO; _port++;
} }
} while (YES == _doPortStepping && -1 == result && _port < (port + kPortsToStep));
return YES; return (-1 != result);
} }
@end @end