2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #import "CheatServer.h"
24 @interface CheatServer ( PrivateAPI
)
27 - (void)_performDelegateSelector
:(SEL)selector
;
28 - (void)_performDelegateSelector
:(SEL)selector withObject
:(id)object
;
33 @implementation CheatServer
36 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 #pragma mark Initialization
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 - (id)initWithDelegate
:(id)delegate
43 if ( self = [super init
] ) {
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 #pragma mark Starting/Stopping
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 - (BOOL)listenOnPort
:(int)port broadcast
:(NSString
*)name
62 if ( [self isListening
] ) {
68 _socket
= [[MySocket alloc
] initWithDelegate
:self];
69 if ( ![_socket listenOnPort
:port
] ) {
75 _children
= [[NSMutableArray alloc
] init
];
78 _name
= [name retain
];
80 // start the rendezvous broadcast
81 if ( _name
) { // domain: @"local."
82 _netService
= [[NSNetService alloc
] initWithDomain
:@
"" type
:@
"_cheat._tcp." name
:_name port
:port
];
83 [_netService setDelegate
:self];
84 [_netService publish
];
97 // if broadcasting, stop
110 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
111 #pragma mark Accessing Children
112 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
117 return [_children count
];
120 - (NSArray
*)children
122 return [NSArray arrayWithArray
:_children
];
125 - (void)removeChildAtIndex
:(unsigned)index
127 [_children removeObjectAtIndex
:index
];
131 - (void)serverChildConnected
:(ServerChild
*)theChild
133 if ( [self isListening
] ) {
134 //[_children addObject:theChild];
135 [self _performDelegateSelector
:@selector(serverChildrenChanged
:)];
139 - (void)serverChildDisconnected
:(ServerChild
*)theChild
141 if ( [self isListening
] ) {
142 [_children removeObject
:theChild
];
143 [self _performDelegateSelector
:@selector(serverChildrenChanged
:)];
147 - (void)serverChildChanged
:(ServerChild
*)theChild
149 [self _performDelegateSelector
:@selector(serverChildrenChanged
:)];
153 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
154 #pragma mark MySocketDelegate
155 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
158 - (void)socket
:(MySocket
*)mySocket didAcceptSocket
:(MySocket
*)newSocket
160 ChazLog( @
"CheatServer - MySocket accepted another one!!" );
164 // create a new child
165 child
= [[ServerChild alloc
] initWithSocket
:newSocket delegate
:self];
166 [_children addObject
:child
];
170 - (void)socketDidDisconnect
:(MySocket
*)mySocket
172 ChazLog( @
"CheatServer - MySocket disconnected: %p", mySocket
);
173 [self _performDelegateSelector
:@selector(serverDisconnectedUnexpectedly
:)];
177 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
178 #pragma mark NetService Delegate
179 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
182 - (void)netServiceWillPublish
:(NSNetService
*)sender
184 ChazLog( @
"service will publish" );
188 - (void)netService
:(NSNetService
*)sender didNotPublish
:(NSDictionary
*)errorDict
190 ChazLog( @
"service did not publish" );
191 // just inform the delegate and let it decide what to do
192 [self _performDelegateSelector
:@selector(server
:failedToBroadcastName
:) withObject
:[sender name
]];
195 - (void)netServiceDidStop
:(NSNetService
*)sender
197 ChazLog( @
"service stopped" );
202 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
203 #pragma mark Delegate Methods
204 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
207 - (void)_performDelegateSelector
:(SEL)selector
209 if ( [_delegate respondsToSelector
:selector
] ) {
210 [_delegate performSelector
:selector withObject
:self];
214 - (void)_performDelegateSelector
:(SEL)selector withObject
:(id)object
216 if ( [_delegate respondsToSelector
:selector
] ) {
217 [_delegate performSelector
:selector withObject
:self withObject
:object
];
222 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
223 #pragma mark Accessors
224 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
228 return _socket
!= nil;
233 return [_socket localHost
];
241 - (NSString
*)broadcast
251 - (void)setDelegate
:(id)delegate
253 _delegate
= delegate
;