3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.dogcows.com/chaz/wiki/TheCheat
6 * Copyright (c) 2003-2010, Charles McGarvey et al.
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
12 #import "CheatServer.h"
15 @interface CheatServer ( PrivateAPI
)
18 - (void)_performDelegateSelector
:(SEL)selector
;
19 - (void)_performDelegateSelector
:(SEL)selector withObject
:(id)object
;
24 @implementation CheatServer
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
28 #pragma mark Initialization
29 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32 - (id)initWithDelegate
:(id)delegate
34 if ( self = [super init
] ) {
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 #pragma mark Starting/Stopping
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 - (BOOL)listenOnPort
:(int)port broadcast
:(NSString
*)name
53 if ( [self isListening
] ) {
59 _socket
= [[MySocket alloc
] initWithDelegate
:self];
60 if ( ![_socket listenOnPort
:port
] ) {
66 _children
= [[NSMutableArray alloc
] init
];
69 _name
= [name retain
];
71 // start the rendezvous broadcast
72 if ( _name
) { // domain: @"local."
73 _netService
= [[NSNetService alloc
] initWithDomain
:@
"" type
:@
"_cheat._tcp." name
:_name port
:port
];
74 [_netService setDelegate
:self];
75 [_netService publish
];
88 // if broadcasting, stop
101 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
102 #pragma mark Accessing Children
103 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
108 return [_children count
];
111 - (NSArray
*)children
113 return [NSArray arrayWithArray
:_children
];
116 - (void)removeChildAtIndex
:(unsigned)index
118 [_children removeObjectAtIndex
:index
];
122 - (void)serverChildConnected
:(ServerChild
*)theChild
124 if ( [self isListening
] ) {
125 //[_children addObject:theChild];
126 [self _performDelegateSelector
:@selector(serverChildrenChanged
:)];
130 - (void)serverChildDisconnected
:(ServerChild
*)theChild
132 if ( [self isListening
] ) {
133 [_children removeObject
:theChild
];
134 [self _performDelegateSelector
:@selector(serverChildrenChanged
:)];
138 - (void)serverChildChanged
:(ServerChild
*)theChild
140 [self _performDelegateSelector
:@selector(serverChildrenChanged
:)];
144 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
145 #pragma mark MySocketDelegate
146 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
149 - (void)socket
:(MySocket
*)mySocket didAcceptSocket
:(MySocket
*)newSocket
151 ChazLog( @
"CheatServer - MySocket accepted another one!!" );
155 // create a new child
156 child
= [[ServerChild alloc
] initWithSocket
:newSocket delegate
:self];
157 [_children addObject
:child
];
161 - (void)socketDidDisconnect
:(MySocket
*)mySocket
163 ChazLog( @
"CheatServer - MySocket disconnected: %p", mySocket
);
164 [self _performDelegateSelector
:@selector(serverDisconnectedUnexpectedly
:)];
168 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
169 #pragma mark NetService Delegate
170 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
173 - (void)netServiceWillPublish
:(NSNetService
*)sender
175 ChazLog( @
"service will publish" );
179 - (void)netService
:(NSNetService
*)sender didNotPublish
:(NSDictionary
*)errorDict
181 ChazLog( @
"service did not publish" );
182 // just inform the delegate and let it decide what to do
183 [self _performDelegateSelector
:@selector(server
:failedToBroadcastName
:) withObject
:[sender name
]];
186 - (void)netServiceDidStop
:(NSNetService
*)sender
188 ChazLog( @
"service stopped" );
193 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
194 #pragma mark Delegate Methods
195 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
198 - (void)_performDelegateSelector
:(SEL)selector
200 if ( [_delegate respondsToSelector
:selector
] ) {
201 [_delegate performSelector
:selector withObject
:self];
205 - (void)_performDelegateSelector
:(SEL)selector withObject
:(id)object
207 if ( [_delegate respondsToSelector
:selector
] ) {
208 [_delegate performSelector
:selector withObject
:self withObject
:object
];
213 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
214 #pragma mark Accessors
215 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
219 return _socket
!= nil;
224 return [_socket localHost
];
232 - (NSString
*)broadcast
242 - (void)setDelegate
:(id)delegate
244 _delegate
= delegate
;