From 2f6f5b098085f36298e23e690cf27b61b4e2ec36 Mon Sep 17 00:00:00 2001 From: Alexander Strange Date: Fri, 14 Feb 2003 12:04:07 +0000 Subject: [PATCH] Finished the server sockets, almost. Not touched clients yet. Cleanups for other classes --- ITInetServerSocket.h | 31 +++++++++ ITInetServerSocket.m | 151 ++++++++++++++++++++++++++++++++++++++++++ ITInetSocket.h | 16 +++++ ITInetSocket.m | 17 +++++ ITVirtualMemoryInfo.m | 10 +-- 5 files changed, 217 insertions(+), 8 deletions(-) create mode 100755 ITInetServerSocket.h create mode 100755 ITInetServerSocket.m create mode 100755 ITInetSocket.h create mode 100755 ITInetSocket.m diff --git a/ITInetServerSocket.h b/ITInetServerSocket.h new file mode 100755 index 0000000..650748a --- /dev/null +++ b/ITInetServerSocket.h @@ -0,0 +1,31 @@ +// +// ITInetServerSocket.h +// ITFoundation +// +// Created by Alexander Strange on Thu Feb 13 2003. +// Copyright (c) 2003 __MyCompanyName__. All rights reserved. +// + +#import + +@class ITInetSocket; + +@protocol ITInetServerSocketOwner +- (void)newClientJoined:(ITInetSocket*)client; +@end + +@interface ITInetServerSocket : NSObject { + int sockfd; + NSMutableSet *clients; + NSNetService *service; + id delegate; +} + +- (id)init; +- (id)initWithServiceName:(NSString*)name delegate:(id)d; +- (id)initWithPort:(NSNumber*)port rendezvousName:(NSString*)name delegate:(id)d; + +- (int)sockfd; +- (NSSet*)clients; +- (id)delegate; +@end diff --git a/ITInetServerSocket.m b/ITInetServerSocket.m new file mode 100755 index 0000000..dcc8805 --- /dev/null +++ b/ITInetServerSocket.m @@ -0,0 +1,151 @@ +// +// ITInetServerSocket.m +// ITFoundation +// +// Created by Alexander Strange on Thu Feb 13 2003. +// Copyright (c) 2003 __MyCompanyName__. All rights reserved. +// + +#import "ITInetServerSocket.h" +#import "ITInetSocket.h" +#import +#import +#import +#import +#import +#import + +@interface ITInetServerSocket(Private) +-(void)setupConnectionWithServiceName:(NSString*)name; +-(void)setupConnectionWithPortNumber:(NSNumber*)port; +-(void)setupRendezvousAdvertising:(NSString*)name; +-(void)setupTimer; +-(void)timerFunc:(NSTimer*)timer; +@end + +@implementation ITInetServerSocket +- (id)init +{ + if (self = [super init]) + { + sockfd = 0; + delegate = clients = nil; + } + return self; +} + +- (id)initWithServiceName:(NSString*)name delegate:(id)d +{ + if (self = [super init]) + { + delegate = [d retain]; + clients = [[NSMutableSet alloc] init]; + [self setupConnectionWithServiceName:name]; + } + return self; +} + +- (id)initWithPort:(NSNumber*)port rendezvousName:(NSString*)name delegate:(id)d +{ + if (self = [super init]) + { + delegate = [d retain]; + clients = [[NSMutableSet alloc] init]; + [self setupConnectionWithPortNumber:port]; + } + return self; +} + +- (void)dealloc +{ + [service stop]; + [service release]; + [clients release]; + [delegate release]; + shutdown(sockfd,2); +} + +- (int)sockfd +{ + return sockfd; +} + +- (NSSet*)clients +{ + return clients; +} + +- (id)delegate +{ + return delegate; +} +@end + +@implementation ITInetServerSocket(Private) +-(void)setupConnectionWithServiceName:(NSString*)name +{ + const char *_name = [name cString]; + struct addrinfo hints,*res; + + hints.ai_flags = AI_PASSIVE; + hints.ai_family = PF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_addrlen = 0; + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + + getaddrinfo(NULL,_name,&hints,&res); + sockfd = socket(res->ai_family, res->ai_socktype,res->ai_protocol); + bind(sockfd, res->ai_addr, res->ai_addrlen); + listen(sockfd, SOMAXCONN); + fcntl(sockfd,F_SETFL,O_NONBLOCK); + freeaddrinfo(res); +} + +-(void)setupConnectionWithPortNumber:(NSNumber*)port +{ + short _port = [port shortValue]; + struct sockaddr_in sa; + + sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); + sa.sin_addr.s_addr = INADDR_ANY; + sa.sin_family = AF_INET; + sa.sin_port = htons(_port); + bind(sockfd,(struct sockaddr *)&sa,sizeof(sa)); + listen(sockfd, SOMAXCONN); + fcntl(sockfd,F_SETFL,O_NONBLOCK); +} + +- (void)setupRendezvousAdvertising:(NSString*)name port:(NSNumber*)port +{ + service = [[NSNetService alloc] initWithDomain:@"" type:[NSString stringWithFormat:@"_%@._tcp.",name] port:htons([port shortValue])]; + [service publish]; +} + +- (void)setupTimer +{ + NSTimer *timer = [NSTimer timerWithTimeInterval:0 target:self selector:@selector(timerFunc:) userInfo:nil repeats:YES]; + [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; +} + +- (void)timerFunc:(NSTimer*)timer +{ + struct sockaddr_in csa; + int csalen; + signed int cfd; + (void) timer; + cfd = accept(sockfd,(struct sockaddr*)&csa,&csalen); + if (cfd == -1) { + if (errno == EWOULDBLOCK) return; + else {perror("Too bad I haven't implemented error checking yet");} + } + else { + ITInetSocket *csocket = [[[ITInetSocket alloc] initWithFD:cfd delegate:self] autorelease]; + [clients addObject:csocket]; + [delegate newClientJoined:csocket]; + + } +} +@end \ No newline at end of file diff --git a/ITInetSocket.h b/ITInetSocket.h new file mode 100755 index 0000000..367c31e --- /dev/null +++ b/ITInetSocket.h @@ -0,0 +1,16 @@ +// +// ITInetSocket.h +// ITFoundation +// +// Created by Alexander Strange on Tue Feb 11 2003. +// Copyright (c) 2003 __MyCompanyName__. All rights reserved. +// + +#import + + +@interface ITInetSocket : NSObject { + +} +-(id)initWithFD:(int)fd delegate:(id)d; +@end diff --git a/ITInetSocket.m b/ITInetSocket.m new file mode 100755 index 0000000..ad781b7 --- /dev/null +++ b/ITInetSocket.m @@ -0,0 +1,17 @@ +// +// ITInetSocket.m +// ITFoundation +// +// Created by Alexander Strange on Tue Feb 11 2003. +// Copyright (c) 2003 __MyCompanyName__. All rights reserved. +// + +#import "ITInetSocket.h" + + +@implementation ITInetSocket +-(id)initWithFD:(int)fd delegate:(id)d +{ + return nil; +} +@end diff --git a/ITVirtualMemoryInfo.m b/ITVirtualMemoryInfo.m index a5d696d..3c6a96a 100755 --- a/ITVirtualMemoryInfo.m +++ b/ITVirtualMemoryInfo.m @@ -1,4 +1,5 @@ #import "ITVirtualMemoryInfo.h" +#import @interface ITVirtualMemoryInfo (Private) - (BOOL)refreshStats:(struct vm_statistics *)myStat; @@ -18,14 +19,7 @@ - (int)pageSize { - int pageSize = 0; - - if ( host_page_size(mach_host_self(), &pageSize) != KERN_SUCCESS ) { - NSLog(@"Failed to get page size, defaulting to 4096/4k"); - pageSize = DEFAULT_PAGE_SIZE; - } - - return pageSize; + return getpagesize(); } - (int)freePages -- 2.20.1