]> src.bluestatic.org Git - macgdbp.git/blob - Source/SocketWrapper.m
Implementing the new delegate system throughout SocketWrapper.
[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 *NsockError = @"SocketWrapper_Error";
26 NSString *NsockDidAccept = @"SocketWrapper_DidAccept";
27 NSString *NsockDataReceived = @"SocketWrapper_DataReceived";
28 NSString *NsockDataSent = @"SocketWrapper_DataSent";
29
30 @implementation SocketWrapper
31
32 /**
33 * Initializes the socket wrapper with a host and port
34 */
35 - (id)initWithPort: (int)port
36 {
37 if (self = [super init])
38 {
39 _port = port;
40
41 // the delegate notifications work funky because of threads. we register ourselves as the
42 // observer and then pass up the messages that are actually from this object (as we can't only observe self due to threads)
43 // to our delegate, and not to all delegates
44 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_sendMessageToDelegate:) name: nil object: nil];
45 }
46 return self;
47 }
48
49 /**
50 * Close our socket and clean up anything else
51 */
52 - (void)dealloc
53 {
54 close(_socket);
55
56 [super dealloc];
57 }
58
59 /**
60 * Returns the delegate
61 */
62 - (id)delegate
63 {
64 return _delegate;
65 }
66
67 /**
68 * Sets the delegate but does *not* retain it
69 */
70 - (void)setDelegate: (id)delegate
71 {
72 _delegate = delegate;
73 }
74
75 /**
76 * This is the notification listener for all types of notifications. If the notifications are from a SocketWrapper
77 * class, it checks that the value of _delegate in the NSNotification's userInfo matches that of this object. If it does,
78 * then the notification was sent from the same object in another thread and it passes the message along to the object's
79 * delegate. Complicated enough?
80 */
81 - (void)_sendMessageToDelegate: (NSNotification *)notif
82 {
83 // this isn't us, so there's no point in continuing
84 if ([[notif userInfo] objectForKey: sockNotificationDebuggerConnection] != _delegate)
85 {
86 return;
87 }
88
89 NSString *name = [notif name];
90
91 if (name == NsockDidAccept)
92 {
93 [_delegate socketDidAccept];
94 }
95 else if (name == NsockDataReceived)
96 {
97 [_delegate dataReceived: [notif object]];
98 }
99 else if (name == NsockDataSent)
100 {
101 [_delegate dataSent];
102 }
103 else if (name == NsockError)
104 {
105 [_delegate errorEncountered: [NSError errorWithDomain: [notif object] code: -1 userInfo: nil]];
106 }
107 }
108
109 /**
110 * Connects to a socket on the port specified during init. This will dispatch another thread to do the
111 * actual waiting. Delegate notifications are posted along the way to let the client know what is going on.
112 */
113 - (void)connect
114 {
115 [NSThread detachNewThreadSelector: @selector(_connect:) toTarget: self withObject: nil];
116 }
117
118 /**
119 * This does the actual dirty work (in a separate thread) of connecting to a socket
120 */
121 - (void)_connect: (id)obj
122 {
123 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
124
125 // create an INET socket that we'll be listen()ing on
126 int socketOpen = socket(PF_INET, SOCK_STREAM, 0);
127
128 // create our address given the port
129 struct sockaddr_in address;
130 address.sin_family = AF_INET;
131 address.sin_port = htons(_port);
132 address.sin_addr.s_addr = htonl(INADDR_ANY);
133 memset(address.sin_zero, '\0', sizeof(address.sin_zero));
134
135 // bind the socket... and don't give up until we've tried for a while
136 int tries = 0;
137 while (bind(socketOpen, (struct sockaddr *)&address, sizeof(address)) < 0)
138 {
139 if (tries >= 5)
140 {
141 close(socketOpen);
142 [self _postNotification: NsockError withObject: @"Could not bind to socket"];
143 }
144 NSLog(@"couldn't bind to the socket... trying again in 5");
145 sleep(5);
146 tries++;
147 }
148
149 // now we just have to keep our ears open
150 if (listen(socketOpen, 0) == -1)
151 {
152 [self _postNotification: NsockError withObject: @"Could not use bound socket for listening"];
153 }
154
155 // accept a connection
156 struct sockaddr_in remoteAddress;
157 socklen_t remoteAddressLen = sizeof(remoteAddress);
158 _socket = accept(socketOpen, (struct sockaddr *)&remoteAddress, &remoteAddressLen);
159 if (_socket < 0)
160 {
161 close(socketOpen);
162 [self _postNotification: NsockError withObject: @"Client failed to accept remote socket"];
163 }
164
165 // we're done listening now that we have a connection
166 close(socketOpen);
167
168 [self _postNotification: NsockDidAccept withObject: nil];
169
170 [pool release];
171 }
172
173 /**
174 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
175 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
176 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
177 * will return something (which we almost always do).
178 *
179 * Data string returned is autorelease'd
180 */
181 - (void)receive
182 {
183 // create a buffer
184 char buffer[1024];
185
186 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
187 int recvd = recv(_socket, &buffer, sizeof(buffer), 0);
188
189 // take the received data and put it into an NSData
190 NSMutableData *data = [NSMutableData data];
191
192 // strip the length from the packet, and clear the null byte then add it to the NSData
193 char packetLength[8];
194 int i = 0;
195 while (buffer[i] != '\0')
196 {
197 packetLength[i] = buffer[i];
198 i++;
199 }
200
201 // we also want the null byte, so move us up 1
202 i++;
203
204 // the total length of the full transmission
205 int length = atoi(packetLength);
206
207 // move the packet part of the received data into it's own char[]
208 char packet[sizeof(buffer)];
209 memmove(packet, &buffer[i], recvd - i);
210
211 // convert bytes to NSData
212 [data appendBytes: packet length: recvd];
213
214 // check if we have a partial packet
215 if (length + i > sizeof(buffer))
216 {
217 while (recvd < length)
218 {
219 int latest = recv(_socket, &buffer, sizeof(buffer), 0);
220 if (latest < 1)
221 {
222 [self _postNotification: NsockError withObject: @"Socket closed or could not be read"];
223 }
224 [data appendBytes: buffer length: latest];
225 recvd += latest;
226 }
227 }
228
229 // convert the NSData into a NSString
230 [self _postNotification: NsockDataReceived withObject: [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease]];
231 }
232
233 /**
234 * Sends a given NSString over the socket
235 */
236 - (void)send: (NSString *)data
237 {
238 data = [NSString stringWithFormat: @"%@\0", data];
239 int sent = send(_socket, [data UTF8String], [data length], 0);
240 if (sent < 0)
241 {
242 [self _postNotification: NsockError withObject: @"Failed to write data to socket"];
243 }
244 if (sent < [data length])
245 {
246 // TODO - do we really need to worry about partial sends with the lenght of our commands?
247 NSLog(@"FAIL: only partial packet was sent; sent %d bytes", sent);
248 }
249
250 [self _postNotification: NsockDataSent withObject: nil];
251 }
252
253 /**
254 * Helper method to simply post a notification to the default notification center with a given name and object
255 */
256 - (void)_postNotification: (NSString *)name withObject: (id)obj
257 {
258 NSDictionary *dict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: _delegate, nil]
259 forKeys: [NSArray arrayWithObjects: sockNotificationDebuggerConnection, nil]];
260 [[NSNotificationCenter defaultCenter] postNotificationName: name object: obj userInfo: dict];
261 }
262
263 @end