In SocketWraper, don't use the notification posting system to send errors to the...
[macgdbp.git] / Source / SocketWrapper.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "SocketWrapper.h"
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <unistd.h>
23
24 NSString *sockNotificationDebuggerConnection = @"DebuggerConnection";
25 NSString *sockNotificationReceiver = @"SEL-del-SocketWrapper_dataReceived";
26 NSString *NsockDidAccept = @"SocketWrapper_DidAccept";
27 NSString *NsockDataReceived = @"SocketWrapper_DataReceived";
28 NSString *NsockDataSent = @"SocketWrapper_DataSent";
29
30 @interface SocketWrapper (Private)
31
32 - (void)error:(NSString *)msg;
33
34 - (void)connect:(id)obj;
35 - (void)postNotification:(NSString *)name withObject:(id)obj;
36 - (void)postNotification:(NSString *)name withObject:(id)obj withDict:(NSMutableDictionary *)dict;
37
38 @end
39
40 @implementation SocketWrapper
41
42 /**
43 * Initializes the socket wrapper with a host and port
44 */
45 - (id)initWithConnection:(DebuggerConnection *)cnx
46 {
47 if (self = [super init])
48 {
49 connection = cnx;
50 port = [connection port];
51
52 // the delegate notifications work funky because of threads. we register ourselves as the
53 // observer and then pass up the messages that are actually from this object (as we can't only observe self due to threads)
54 // to our delegate, and not to all delegates
55 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendMessageToDelegate:) name:nil object:nil];
56 }
57 return self;
58 }
59
60 /**
61 * Close our socket and clean up anything else
62 */
63 - (void)close
64 {
65 [[NSNotificationCenter defaultCenter] removeObserver:self];
66 close(sock);
67 }
68
69 /**
70 * Returns the delegate
71 */
72 - (id)delegate
73 {
74 return delegate;
75 }
76
77 /**
78 * Sets the delegate but does *not* retain it
79 */
80 - (void)setDelegate:(id)aDelegate
81 {
82 delegate = aDelegate;
83 }
84
85 /**
86 * Returns the name of the host to whom we are currently connected.
87 */
88 - (NSString *)remoteHost
89 {
90 struct sockaddr_in addr;
91 socklen_t addrLength;
92
93 if (getpeername(sock, (struct sockaddr *)&addr, &addrLength) < 0)
94 {
95 [self error:@"Could not get remote hostname."];
96 }
97
98 char *name = inet_ntoa(addr.sin_addr);
99
100 return [NSString stringWithUTF8String:name];
101 }
102
103 /**
104 * This is the notification listener for all types of notifications. If the notifications are from a SocketWrapper
105 * class, it checks that the value of _delegate in the NSNotification's userInfo matches that of this object. If it does,
106 * then the notification was sent from the same object in another thread and it passes the message along to the object's
107 * delegate. Complicated enough?
108 */
109 - (void)sendMessageToDelegate:(NSNotification *)notif
110 {
111 // this isn't us, so there's no point in continuing
112 if ([[notif userInfo] objectForKey:sockNotificationDebuggerConnection] != delegate)
113 {
114 return;
115 }
116
117 NSString *name = [notif name];
118
119 if (name == NsockDidAccept)
120 {
121 [delegate socketDidAccept];
122 }
123 else if (name == NsockDataReceived)
124 {
125 [delegate dataReceived:[notif object] deliverTo:NSSelectorFromString([[notif userInfo] objectForKey:sockNotificationReceiver])];
126 }
127 else if (name == NsockDataSent)
128 {
129 [delegate dataSent:[notif object]];
130 }
131 }
132
133 /**
134 * Connects to a socket on the port specified during init. This will dispatch another thread to do the
135 * actual waiting. Delegate notifications are posted along the way to let the client know what is going on.
136 */
137 - (void)connect
138 {
139 [NSThread detachNewThreadSelector:@selector(connect:) toTarget:self withObject:nil];
140 }
141
142 /**
143 * This does the actual dirty work (in a separate thread) of connecting to a socket
144 */
145 - (void)connect:(id)obj
146 {
147 // create an INET socket that we'll be listen()ing on
148 int socketOpen = socket(PF_INET, SOCK_STREAM, 0);
149
150 // create our address given the port
151 struct sockaddr_in address;
152 address.sin_family = AF_INET;
153 address.sin_port = htons(port);
154 address.sin_addr.s_addr = htonl(INADDR_ANY);
155 memset(address.sin_zero, '\0', sizeof(address.sin_zero));
156
157 // allow an already-opened socket to be reused
158 int yes = 1;
159 setsockopt(socketOpen, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
160
161 // bind the socket... and don't give up until we've tried for a while
162 int tries = 0;
163 while (bind(socketOpen, (struct sockaddr *)&address, sizeof(address)) < 0)
164 {
165 if (tries >= 5)
166 {
167 close(socketOpen);
168 [self error:@"Could not bind to socket"];
169 return;
170 }
171 NSLog(@"couldn't bind to the socket... trying again in 5");
172 sleep(5);
173 tries++;
174 }
175
176 // now we just have to keep our ears open
177 if (listen(socketOpen, 0) == -1)
178 {
179 [self error:@"Could not use bound socket for listening"];
180 }
181
182 // accept a connection
183 struct sockaddr_in remoteAddress;
184 socklen_t remoteAddressLen = sizeof(remoteAddress);
185 sock = accept(socketOpen, (struct sockaddr *)&remoteAddress, &remoteAddressLen);
186 if (sock < 0)
187 {
188 close(socketOpen);
189 [self error:@"Client failed to accept remote socket"];
190 return;
191 }
192
193 // we're done listening now that we have a connection
194 close(socketOpen);
195
196 [self postNotification:NsockDidAccept withObject:nil];
197 }
198
199 /**
200 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
201 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
202 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
203 * will return something (which we almost always do).
204 *
205 * The paramater is an optional selector which the delegate method dataReceived:deliverTo: should forward to
206 */
207 - (void)receive:(SEL)selector
208 {
209 // create a buffer
210 char buffer[1024];
211
212 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
213 int recvd = recv(sock, &buffer, sizeof(buffer), 0);
214
215 // take the received data and put it into an NSData
216 NSMutableData *data = [NSMutableData data];
217
218 // strip the length from the packet, and clear the null byte then add it to the NSData
219 char packetLength[8];
220 memset(packetLength, '\0', sizeof(packetLength));
221 int i = 0;
222 while (buffer[i] != '\0')
223 {
224 packetLength[i] = buffer[i];
225 i++;
226 }
227
228 // we also want the null byte, so move us up 1
229 i++;
230
231 // the total length of the full transmission
232 int length = atoi(packetLength);
233
234 // move the packet part of the received data into it's own char[]
235 char packet[sizeof(buffer)];
236 memset(packet, '\0', sizeof(packet));
237 memmove(packet, &buffer[i], recvd - i);
238
239 // convert bytes to NSData
240 [data appendBytes:packet length:recvd - i];
241
242 // check if we have a partial packet
243 if (length + i > sizeof(buffer))
244 {
245 while (recvd < length)
246 {
247 int latest = recv(sock, &buffer, sizeof(buffer), 0);
248 if (latest < 1)
249 {
250 [self error:@"Socket closed or could not be read"];
251 return;
252 }
253 [data appendBytes:buffer length:latest];
254 recvd += latest;
255 }
256 }
257
258 //NSLog(@"data = %@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
259
260 if (selector != nil)
261 {
262 [self postNotification:NsockDataReceived
263 withObject:data
264 withDict:[NSMutableDictionary dictionaryWithObject:NSStringFromSelector(selector) forKey:sockNotificationReceiver]];
265 }
266 else
267 {
268 [self postNotification:NsockDataReceived withObject:data];
269 }
270 }
271
272 /**
273 * Sends a given NSString over the socket
274 */
275 - (void)send:(NSString *)data
276 {
277 data = [NSString stringWithFormat:@"%@\0", data];
278 int sent = send(sock, [data UTF8String], [data length], 0);
279 if (sent < 0)
280 {
281 [self error:@"Failed to write data to socket"];
282 return;
283 }
284 if (sent < [data length])
285 {
286 // TODO - do we really need to worry about partial sends with the lenght of our commands?
287 NSLog(@"FAIL: only partial packet was sent; sent %d bytes", sent);
288 }
289
290 [self postNotification:NsockDataSent withObject:[data substringToIndex:sent]];
291 }
292
293 /**
294 * Helper method to simply post a notification to the default notification center with a given name and object
295 */
296 - (void)postNotification:(NSString *)name withObject:(id)obj
297 {
298 [self postNotification:name withObject:obj withDict:[NSMutableDictionary dictionary]];
299 }
300
301 /**
302 * Another helper method to aid in the posting of notifications. This one should be used if you have additional
303 * things for the userInfo. This automatically adds the sockNotificationDebuggerConnection key.
304 */
305 - (void)postNotification:(NSString *)name withObject:(id)obj withDict:(NSMutableDictionary *)dict
306 {
307 [dict setValue:delegate forKey:sockNotificationDebuggerConnection];
308 [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj userInfo:dict];
309 }
310
311 /**
312 * Helper method that just calls -[DebuggerWindowController setError:] on the main thread
313 */
314 - (void)error:(NSString *)msg
315 {
316 [delegate performSelectorOnMainThread:@selector(errorEncountered:) withObject:msg waitUntilDone:NO];
317 }
318
319 @end