Delegate methods work far better than notifications, though I'm not sure how threads...
[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 *SocketWrapperDidErrorNotification = @"errorOccurred";
25 NSString *SocketWrapperSocketDidBindNotification = @"bindSuccess";
26 NSString *SocketWrapperSocketDidAcceptNotification = @"acceptSuccess";
27 NSString *SocketWrapperDataReceivedNotification = @"dataReceived";
28 NSString *SocketWrapperDataSentNotification = @"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 // create an INET socket that we'll be listen()ing on
40 int socketOpen = socket(PF_INET, SOCK_STREAM, 0);
41
42 // create our address given the port
43 struct sockaddr_in address;
44 address.sin_family = AF_INET;
45 address.sin_port = htons(port);
46 address.sin_addr.s_addr = htonl(INADDR_ANY);
47 memset(address.sin_zero, '\0', sizeof(address.sin_zero));
48
49 // bind the socket... and don't give up until we've tried for a while
50 int tries = 0;
51 while (bind(socketOpen, (struct sockaddr *)&address, sizeof(address)) < 0)
52 {
53 if (tries >= 5)
54 {
55 close(socketOpen);
56 [_delegate errorEncountered: nil];
57 return nil;
58 }
59 NSLog(@"couldn't bind to the socket... trying again in 5");
60 sleep(5);
61 tries++;
62 }
63 [_delegate socketDidAccept];
64
65 // now we just have to keep our ears open
66 if (listen(socketOpen, 0) == -1)
67 {
68 [_delegate errorEncountered: nil];
69 return nil;
70 }
71
72 // accept a connection
73 struct sockaddr_in remoteAddress;
74 socklen_t remoteAddressLen = sizeof(remoteAddress);
75 _socket = accept(socketOpen, (struct sockaddr *)&remoteAddress, &remoteAddressLen);
76 if (_socket < 0)
77 {
78 close(socketOpen);
79 [_delegate errorEncountered: nil];
80 return nil;
81 }
82 [_delegate socketDidAccept];
83
84 // we're done listening now that we have a connection
85 close(socketOpen);
86 }
87 return self;
88 }
89
90 /**
91 * Close our socket and clean up anything else
92 */
93 - (void)dealloc
94 {
95 close(_socket);
96
97 [super dealloc];
98 }
99
100 /**
101 * Returns the delegate
102 */
103 - (id)delegate
104 {
105 return _delegate;
106 }
107
108 /**
109 * Sets the delegate but does *not* retain it
110 */
111 - (void)setDelegate: (id)delegate
112 {
113 _delegate = delegate;
114 }
115
116 /**
117 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
118 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
119 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
120 * will return something (which we almost always do).
121 *
122 * Data string returned is autorelease'd
123 */
124 - (void)receive
125 {
126 // create a buffer
127 char buffer[1024];
128
129 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
130 int recvd = recv(_socket, &buffer, sizeof(buffer), 0);
131
132 // take the received data and put it into an NSData
133 NSMutableData *data = [NSMutableData data];
134
135 // strip the length from the packet, and clear the null byte then add it to the NSData
136 char packetLength[32];
137 int i = 0;
138 do
139 {
140 packetLength[i] = buffer[i];
141 i++;
142 } while (buffer[i] != '\0');
143 // the length of the packet
144 // packet is formatted in len<null>packet
145 int length = atoi(packetLength);
146
147 // take our bytes and convert them to NSData
148 char packet[sizeof(buffer)];
149 strcpy(packet, &buffer[i + 1]);
150 [data appendBytes: packet length: recvd];
151
152 // check if we have a partial packet
153 if (length + sizeof(length) > sizeof(buffer))
154 {
155 while (recvd < length)
156 {
157 int latest = recv(_socket, &buffer, sizeof(buffer), 0);
158 if (latest < 1)
159 {
160 NSLog(@"socket closed or error");
161 }
162 [data appendBytes: buffer length: latest];
163 recvd += latest;
164 }
165 }
166
167 // convert the NSData into a NSString
168 NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
169
170 [_delegate dataReceived: string];
171
172 //return string;
173 }
174
175 /**
176 * Sends a given NSString over the socket
177 */
178 - (void)send: (NSString *)data
179 {
180 data = [NSString stringWithFormat: @"%@\0", data];
181 int sent = send(_socket, [data UTF8String], [data length], 0);
182 if (sent < 0)
183 {
184 NSLog(@"error in sending");
185 }
186 if (sent < [data length])
187 {
188 // TODO - do we really need to worry about partial sends with the lenght of our commands?
189 NSLog(@"FAIL: only partial packet was sent; sent %d bytes", sent);
190 }
191
192 [_delegate dataSent];
193 }
194
195 @end