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