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