#import "SimSocket.h"
void simReceivedDataCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
// NOTE: I don't know if I'm accessing the address argument properly here, or if it contains useful information!
NSLog(@"SimSocket: simReceivedDataCallback function entry. Socket=%d, address=%s, port=%d", CFSocketGetNative(s), inet_ntoa(((struct sockaddr_in *)address)->sin_addr), htons(((struct sockaddr_in *)address)->sin_port));
// send data to SimSocket delegate here
}
@implementation SimSocket
@synthesize readyToUse, delegate, errors;
- (id)initWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 {
if (self = [super init]) {
hostStr = host1;
port = port1;
isPublish = YES;
connectionType = connectionType1;
readyToUse = NO;
int sockType = (connectionType == SimSocketConnectionTcp) ? SOCK_STREAM : SOCK_DGRAM;
sockfd = socket(AF_INET, sockType, 0);
if (sockfd == -1) {
NSLog(@"SimSocket:initWithType: Error with socket() - %s", strerror(errno));
return self;
}
memset((void *)&address_in, 0, sizeof(address_in));
address_in.sin_family = AF_INET;
address_in.sin_port = htons(port);
address_in.sin_addr.s_addr = inet_addr([hostStr UTF8String]);
addressData = [NSData dataWithBytes:&address_in length:sizeof(address_in)];
readyToUse = YES;
return self;
}
else {
return nil;
}
}
- (id)initPublishWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 {
if ([self initWithType:connectionType1 host:host1 port:port1]) {
readyToUse = NO;
NSLog(@"SimSocket:initPublishWithType: Successfully created BSD socket - %d", sockfd);
cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketConnectCallBack, simCallback, NULL);
if (cfsocket == NULL) {
NSLog(@"SimSocket:initPublishWithType: Error with CFSocketCreateWithNative()");
return self;
}
readyToUse = YES;
}
return self;
}
- (id)initSubscribeWithType:(SimSocketConnection)connectionType1 host:(NSString *)host1 port:(int)port1 delegate:(id )delegate1 {
NSLog(@"SimSocket:initSubscribeWithType: starting initialization with delegate %@", delegate1);
if ([self initWithType:connectionType1 host:host1 port:port1]) {
readyToUse = NO;
NSLog(@"SimSocket:initSubscribeWithType: Successfully created BSD socket - %d", sockfd);
delegate = delegate1;
address_in.sin_addr.s_addr = htonl(INADDR_ANY);
addressData = [NSData dataWithBytes:&address_in length:sizeof(address_in)];
int yes = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
NSLog(@"SimSocket:initSubscribeWithType: Error with setsockopt() with SO_REUSEADDR - %s", strerror(errno));
}
if (bind(sockfd, (struct sockaddr*) &address_in, sizeof(address_in)) == -1) {
NSLog(@"SimSocket:initSubscribeWithType: Error with bind() - %s", strerror(errno));
errors = [NSString stringWithFormat:@"Bind Failed - %s. Try using a Different Port number.", strerror(errno)];
close (sockfd);
return self;
}
CFSocketContext context = { 0, (void *)self, NULL, NULL, NULL };
// TCP
if (connectionType == SimSocketConnectionTcp) {
if (listen(sockfd, 10) == -1) {
NSLog(@"SimSocket:initSubscribeWithType: Error with listen() - %s", strerror(errno));
return self;
}
cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketAcceptCallBack, simAcceptCallback, &context);
}
// UDP
else if (connectionType == SimSocketConnectionUdp) {
cfsocket = CFSocketCreateWithNative(NULL, sockfd, kCFSocketDataCallBack, simReceivedDataCallback, &context);
}
if (cfsocket == NULL) {
NSLog(@"SimSocket:initSubscribeWithType: Error with CFSocketCreateWithNative()");
return self;
}
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cfsocket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
NSLog(@"SimSocket:initSubscribeWithType: Current run loop = %x. Main thread run loop = %x.", CFRunLoopGetCurrent(), CFRunLoopGetMain());
readyToUse = YES;
NSLog(@"SimSocket:initSubscribeWithType: Completed socket initialization successfully.");
}
return self;
}
- (void)dealloc {
if (cfsocket) CFRelease(cfsocket);
if (sockfd != -1) close(sockfd);
[errors release];
[super dealloc];
}
-(void)shutdown
{
NSLog(@"SimSocket: shutdown: Am shutting down the socket. sockfd = %d", sockfd);
if (cfsocket) CFRelease(cfsocket);
if (sockfd != -1) close(sockfd);
}
- (void)sendBytes:(const void *)bytes length:(int)length {
if (!readyToUse) {
NSLog(@"SimSocket:sendBytes: socket not ready to use for sending");
return;
}
int bytesSent;
if (connectionType == SimSocketConnectionTcp) {
bytesSent = send(sockfd, bytes, length, 0);
}
else {
bytesSent = sendto(sockfd, bytes, length, 0, (struct sockaddr *)&address_in, sizeof(address_in));
}
if (bytesSent < 0) {
NSLog(@"SimSocket:sendBytes: Oops, error- %s. No bytes sent.", strerror(errno));
}
else if (bytesSent < length) {
NSLog(@"SimSocket:sendBytes: Oops, error- %s. Only %i sent out of %i", strerror(errno), bytesSent, length);
}
else {
NSLog(@"SimSocket:sendBytes: Successfully sent the packet (%d bytes) to %s", length, inet_ntoa(address_in.sin_addr));
}
NSLog(@"SimSocket:sendBytes: Current run loop = %x. Main thread run loop = %x.", CFRunLoopGetCurrent(), CFRunLoopGetMain());
}
@end