Starting to thread-ify SocketWrapper class.
[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 @implementation SocketWrapper
25
26 /**
27 * Initializes the socket wrapper with a host and port
28 */
29 - (id)initWithPort: (int)port
30 {
31 if (self = [super init])
32 {
33 _port = port;
34 }
35 return self;
36 }
37
38 /**
39 * Close our socket and clean up anything else
40 */
41 - (void)dealloc
42 {
43 close(_socket);
44
45 [super dealloc];
46 }
47
48 /**
49 * Returns the delegate
50 */
51 - (id)delegate
52 {
53 return _delegate;
54 }
55
56 /**
57 * Sets the delegate but does *not* retain it
58 */
59 - (void)setDelegate: (id)delegate
60 {
61 _delegate = delegate;
62 }
63
64 /**
65 * Connects to a socket on the port specified during init. This will dispatch another thread to do the
66 * actual waiting. Delegate notifications are posted along the way to let the client know what is going on.
67 */
68 - (void)connect
69 {
70 [NSThread detachNewThreadSelector: @selector(_connect:) toTarget: self withObject: nil];
71 }
72
73 /**
74 * This does the actual dirty work (in a separate thread) of connecting to a socket
75 */
76 - (void)_connect: (id)obj
77 {
78 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
79
80 // create an INET socket that we'll be listen()ing on
81 int socketOpen = socket(PF_INET, SOCK_STREAM, 0);
82
83 // create our address given the port
84 struct sockaddr_in address;
85 address.sin_family = AF_INET;
86 address.sin_port = htons(_port);
87 address.sin_addr.s_addr = htonl(INADDR_ANY);
88 memset(address.sin_zero, '\0', sizeof(address.sin_zero));
89
90 // bind the socket... and don't give up until we've tried for a while
91 int tries = 0;
92 while (bind(socketOpen, (struct sockaddr *)&address, sizeof(address)) < 0)
93 {
94 if (tries >= 5)
95 {
96 close(socketOpen);
97 [_delegate errorEncountered: nil];
98 }
99 NSLog(@"couldn't bind to the socket... trying again in 5");
100 sleep(5);
101 tries++;
102 }
103
104 // now we just have to keep our ears open
105 if (listen(socketOpen, 0) == -1)
106 {
107 [_delegate errorEncountered: nil];
108 }
109
110 // accept a connection
111 struct sockaddr_in remoteAddress;
112 socklen_t remoteAddressLen = sizeof(remoteAddress);
113 _socket = accept(socketOpen, (struct sockaddr *)&remoteAddress, &remoteAddressLen);
114 if (_socket < 0)
115 {
116 close(socketOpen);
117 [_delegate errorEncountered: nil];
118 }
119
120 // we're done listening now that we have a connection
121 close(socketOpen);
122
123 [_delegate performSelectorOnMainThread: @selector(socketDidAccept:) withObject: nil waitUntilDone: NO];
124 //[_delegate socketDidAccept];
125
126 [pool release];
127 }
128
129 /**
130 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
131 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
132 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
133 * will return something (which we almost always do).
134 *
135 * Data string returned is autorelease'd
136 */
137 - (void)receive
138 {
139 // create a buffer
140 char buffer[1024];
141
142 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
143 int recvd = recv(_socket, &buffer, sizeof(buffer), 0);
144
145 // take the received data and put it into an NSData
146 NSMutableData *data = [NSMutableData data];
147
148 // strip the length from the packet, and clear the null byte then add it to the NSData
149 char packetLength[8];
150 int i = 0;
151 while (buffer[i] != '\0')
152 {
153 packetLength[i] = buffer[i];
154 i++;
155 }
156
157 // we also want the null byte, so move us up 1
158 i++;
159
160 // the total length of the full transmission
161 int length = atoi(packetLength);
162
163 // move the packet part of the received data into it's own char[]
164 char packet[sizeof(buffer)];
165 memmove(packet, &buffer[i], recvd - i);
166
167 // convert bytes to NSData
168 [data appendBytes: packet length: recvd];
169
170 // check if we have a partial packet
171 if (length + i > sizeof(buffer))
172 {
173 while (recvd < length)
174 {
175 int latest = recv(_socket, &buffer, sizeof(buffer), 0);
176 if (latest < 1)
177 {
178 NSLog(@"socket closed or error");
179 }
180 [data appendBytes: buffer length: latest];
181 recvd += latest;
182 }
183 }
184
185 // convert the NSData into a NSString
186 [_delegate dataReceived: [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease]];
187 }
188
189 /**
190 * Sends a given NSString over the socket
191 */
192 - (void)send: (NSString *)data
193 {
194 data = [NSString stringWithFormat: @"%@\0", data];
195 int sent = send(_socket, [data UTF8String], [data length], 0);
196 if (sent < 0)
197 {
198 NSLog(@"error in sending");
199 }
200 if (sent < [data length])
201 {
202 // TODO - do we really need to worry about partial sends with the lenght of our commands?
203 NSLog(@"FAIL: only partial packet was sent; sent %d bytes", sent);
204 }
205
206 [_delegate dataSent];
207 }
208
209 /**
210 * Helper method to simply post a notification to the default notification center with a given name and object
211 */
212 - (void)_postNotification: (NSString *)name withObject: (id)obj
213 {
214 [[NSNotificationCenter defaultCenter] postNotificationName: name object: obj];
215 }
216
217 @end