2007-02-25 02:43:56 +00:00
|
|
|
/*
|
2007-07-05 23:24:25 +00:00
|
|
|
* $Id: AudioScrobblerClient.m 869 2007-06-18 15:51:33Z stephen_booth $
|
2007-02-25 02:43:56 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 - 2007 Stephen F. Booth <me@sbooth.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-02-07 05:49:27 +00:00
|
|
|
* This is a port of the BlockingClient client class from
|
2007-02-25 02:43:56 +00:00
|
|
|
* the Last.fm ScrobSub library by sharevari
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "AudioScrobblerClient.h"
|
|
|
|
|
2013-10-03 08:00:58 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
#define kBufferSize 1024
|
|
|
|
#define kPortsToStep 5
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static in_addr_t
|
|
|
|
addressForHost(NSString *hostname) {
|
2007-02-25 02:43:56 +00:00
|
|
|
NSCParameterAssert(nil != hostname);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
in_addr_t address;
|
|
|
|
struct hostent *hostinfo;
|
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
address = inet_addr([hostname cStringUsingEncoding:NSASCIIStringEncoding]);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(INADDR_NONE == address) {
|
|
|
|
hostinfo = gethostbyname([hostname cStringUsingEncoding:NSASCIIStringEncoding]);
|
|
|
|
if(NULL == hostinfo) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Unable to resolve address for \"%@\".", hostname);
|
2007-02-25 02:43:56 +00:00
|
|
|
return INADDR_NONE;
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
address = *((in_addr_t *)hostinfo->h_addr_list[0]);
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface AudioScrobblerClient (Private)
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)connectToSocket:(in_addr_t)remoteAddress port:(in_port_t)port;
|
2007-02-25 02:43:56 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioScrobblerClient
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)init {
|
2007-02-25 02:43:56 +00:00
|
|
|
if((self = [super init])) {
|
2022-02-07 05:49:27 +00:00
|
|
|
_socket = -1;
|
|
|
|
_doPortStepping = YES;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)connectToHost:(NSString *)hostname port:(in_port_t)port {
|
2007-07-05 23:24:25 +00:00
|
|
|
NSParameterAssert(nil != hostname);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
in_addr_t remoteAddress = addressForHost(hostname);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
if(INADDR_NONE != remoteAddress)
|
|
|
|
return [self connectToSocket:remoteAddress port:port];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
return NO;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)isConnected {
|
2007-07-05 23:24:25 +00:00
|
|
|
return (-1 != _socket);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (in_port_t)connectedPort {
|
2007-07-05 23:24:25 +00:00
|
|
|
return _port;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)send:(NSString *)data {
|
|
|
|
const char *utf8data = [data UTF8String];
|
|
|
|
unsigned len = (unsigned int)strlen(utf8data);
|
|
|
|
unsigned bytesToSend = len;
|
|
|
|
unsigned totalBytesSent = 0;
|
|
|
|
ssize_t bytesSent = 0;
|
2007-07-05 23:24:25 +00:00
|
|
|
|
|
|
|
if(NO == [self isConnected]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Can't send data, client not connected");
|
2007-07-05 23:24:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
while(totalBytesSent < bytesToSend && -1 != bytesSent) {
|
|
|
|
bytesSent = send(_socket, utf8data + totalBytesSent, bytesToSend - totalBytesSent, 0);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == bytesSent || 0 == bytesSent)
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Unable to send data through socket: %s", strerror(errno));
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
totalBytesSent += bytesSent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSString *)receive {
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
int readSize = kBufferSize - 1;
|
|
|
|
ssize_t bytesRead = 0;
|
|
|
|
BOOL keepGoing = YES;
|
|
|
|
NSString *result = nil;
|
2007-07-05 23:24:25 +00:00
|
|
|
|
|
|
|
if(NO == [self isConnected]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Can't receive data, client not connected");
|
2007-07-05 23:24:25 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
do {
|
|
|
|
bytesRead = recv(_socket, buffer, readSize, 0);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == bytesRead || 0 == bytesRead) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Unable to receive data through socket: %s", strerror(errno));
|
2007-07-05 23:24:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if('\n' == buffer[bytesRead - 1]) {
|
2007-02-25 02:43:56 +00:00
|
|
|
--bytesRead;
|
|
|
|
keepGoing = NO;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
buffer[bytesRead] = '\0';
|
|
|
|
result = [[NSString alloc] initWithUTF8String:buffer];
|
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
} while(keepGoing);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2008-08-26 17:45:16 +00:00
|
|
|
return result;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)shutdown {
|
|
|
|
int result;
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
ssize_t bytesRead;
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
if(NO == [self isConnected]) {
|
2007-02-25 02:43:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
result = shutdown(_socket, SHUT_WR);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == result)
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Socket shutdown failed: %s", strerror(errno));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
bytesRead = recv(_socket, buffer, kBufferSize, 0);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == bytesRead)
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"AudioScrobblerClient error: Waiting for shutdown confirmation failed: %s", strerror(errno));
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-02-25 02:43:56 +00:00
|
|
|
if(0 != bytesRead) {
|
2007-07-05 23:24:25 +00:00
|
|
|
NSString *received = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Received unexpected bytes during shutdown: %@", received);
|
2022-02-07 05:49:27 +00:00
|
|
|
} else
|
2007-02-25 02:43:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = close(_socket);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == result)
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Couldn't close socket (%s)", strerror(errno));
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
_socket = -1;
|
|
|
|
_port = 0;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioScrobblerClient (Private)
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)connectToSocket:(in_addr_t)remoteAddress port:(in_port_t)port {
|
2007-07-05 23:24:25 +00:00
|
|
|
NSParameterAssert(INADDR_NONE != remoteAddress);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
_port = port;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
int result;
|
|
|
|
do {
|
2022-02-07 05:49:27 +00:00
|
|
|
struct sockaddr_in socketAddress;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(-1 == _socket) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Unable to create socket (%s)", strerror(errno));
|
2009-03-28 03:00:45 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
socketAddress.sin_family = AF_INET;
|
|
|
|
socketAddress.sin_addr.s_addr = remoteAddress;
|
|
|
|
socketAddress.sin_port = htons(_port);
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
result = connect(_socket, (const struct sockaddr *)&socketAddress, sizeof(struct sockaddr_in));
|
|
|
|
if(-1 == result) {
|
|
|
|
close(_socket);
|
|
|
|
_socket = -1;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
_port++;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
} while(YES == _doPortStepping && -1 == result && _port < (port + kPortsToStep));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
return (-1 != result);
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|