]> Dogcows Code - chaz/thecheat/blob - CheatServer.m
The Cheat 1.2
[chaz/thecheat] / CheatServer.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
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)
9 // any later version.
10 //
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.
15 //
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.
19 //
20
21 #import "CheatServer.h"
22
23
24 @interface CheatServer ( PrivateAPI )
25
26 // delegate methods
27 - (void)_performDelegateSelector:(SEL)selector;
28 - (void)_performDelegateSelector:(SEL)selector withObject:(id)object;
29
30 @end
31
32
33 @implementation CheatServer
34
35
36 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 #pragma mark Initialization
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40
41 - (id)initWithDelegate:(id)delegate
42 {
43 if ( self = [super init] ) {
44 _delegate = delegate;
45 }
46 return self;
47 }
48
49 - (void)dealloc
50 {
51 [self stop];
52 [super dealloc];
53 }
54
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 #pragma mark Starting/Stopping
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 - (BOOL)listenOnPort:(int)port broadcast:(NSString *)name
61 {
62 if ( [self isListening] ) {
63 // already running
64 return NO;
65 }
66
67 // start the server
68 _socket = [[MySocket alloc] initWithDelegate:self];
69 if ( ![_socket listenOnPort:port] ) {
70 [_socket release];
71 _socket = nil;
72 return NO;
73 }
74
75 _children = [[NSMutableArray alloc] init];
76
77 _port = port;
78 _name = [name retain];
79
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];
85 }
86
87 return YES;
88 }
89
90 - (void)stop
91 {
92 // close the listener
93 [_socket disconnect];
94 [_socket release];
95 _socket = nil;
96
97 // if broadcasting, stop
98 [_netService stop];
99 _netService = nil;
100
101 [_children release];
102 _children = nil;
103
104 _port = 0;
105 [_name release];
106 _name = nil;
107 }
108
109
110 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
111 #pragma mark Accessing Children
112 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113
114
115 - (int)childCount
116 {
117 return [_children count];
118 }
119
120 - (NSArray *)children
121 {
122 return [NSArray arrayWithArray:_children];
123 }
124
125 - (void)removeChildAtIndex:(unsigned)index
126 {
127 [_children removeObjectAtIndex:index];
128 }
129
130
131 - (void)serverChildConnected:(ServerChild *)theChild
132 {
133 if ( [self isListening] ) {
134 //[_children addObject:theChild];
135 [self _performDelegateSelector:@selector(serverChildrenChanged:)];
136 }
137 }
138
139 - (void)serverChildDisconnected:(ServerChild *)theChild
140 {
141 if ( [self isListening] ) {
142 [_children removeObject:theChild];
143 [self _performDelegateSelector:@selector(serverChildrenChanged:)];
144 }
145 }
146
147 - (void)serverChildChanged:(ServerChild *)theChild
148 {
149 [self _performDelegateSelector:@selector(serverChildrenChanged:)];
150 }
151
152
153 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
154 #pragma mark MySocketDelegate
155 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
156
157
158 - (void)socket:(MySocket *)mySocket didAcceptSocket:(MySocket *)newSocket
159 {
160 ChazLog( @"CheatServer - MySocket accepted another one!!" );
161
162 ServerChild *child;
163
164 // create a new child
165 child = [[ServerChild alloc] initWithSocket:newSocket delegate:self];
166 [_children addObject:child];
167 [child release];
168 }
169
170 - (void)socketDidDisconnect:(MySocket *)mySocket
171 {
172 ChazLog( @"CheatServer - MySocket disconnected: %p", mySocket );
173 [self _performDelegateSelector:@selector(serverDisconnectedUnexpectedly:)];
174 }
175
176
177 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
178 #pragma mark NetService Delegate
179 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
180
181
182 - (void)netServiceWillPublish:(NSNetService *)sender
183 {
184 ChazLog( @"service will publish" );
185 //[sender resolve];
186 }
187
188 - (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict
189 {
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]];
193 }
194
195 - (void)netServiceDidStop:(NSNetService *)sender
196 {
197 ChazLog( @"service stopped" );
198 [sender release];
199 }
200
201
202 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
203 #pragma mark Delegate Methods
204 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
205
206
207 - (void)_performDelegateSelector:(SEL)selector
208 {
209 if ( [_delegate respondsToSelector:selector] ) {
210 [_delegate performSelector:selector withObject:self];
211 }
212 }
213
214 - (void)_performDelegateSelector:(SEL)selector withObject:(id)object
215 {
216 if ( [_delegate respondsToSelector:selector] ) {
217 [_delegate performSelector:selector withObject:self withObject:object];
218 }
219 }
220
221
222 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
223 #pragma mark Accessors
224 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
225
226 - (BOOL)isListening
227 {
228 return _socket != nil;
229 }
230
231 - (NSString *)host
232 {
233 return [_socket localHost];
234 }
235
236 - (int)port
237 {
238 return _port;
239 }
240
241 - (NSString *)broadcast
242 {
243 return _name;
244 }
245
246 - (id)delegate
247 {
248 return _delegate;
249 }
250
251 - (void)setDelegate:(id)delegate
252 {
253 _delegate = delegate;
254 }
255
256
257 @end
This page took 0.043397 seconds and 5 git commands to generate.