* Source/SocketWrapper.m:
[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
23 @implementation SocketWrapper
24
25 /**
26 * Initializes the socket wrapper with a host and port
27 */
28 - (id)initWithPort: (int)port
29 {
30 if (self = [super init])
31 {
32 // create an INET socket that we'll be listen()ing on
33 int socketOpen = socket(PF_INET, SOCK_STREAM, 0);
34
35 // create our address given the port
36 struct sockaddr_in address;
37 address.sin_family = AF_INET;
38 address.sin_port = htons(port);
39 address.sin_addr.s_addr = htonl(INADDR_ANY);
40 memset(address.sin_zero, '\0', sizeof(address.sin_zero));
41
42 // bind the socket... and don't give up until we've tried for a while
43 int tries = 0;
44 while (bind(socketOpen, (struct sockaddr *)&address, sizeof(address)) < 0)
45 {
46 if (tries >= 5)
47 {
48 close(socketOpen);
49 NSLog(@"giving up now");
50 return nil;
51 }
52 NSLog(@"couldn't bind to the socket... trying again in 5");
53 sleep(5);
54 tries++;
55 }
56
57 // now we just have to keep our ears open
58 if (listen(socketOpen, 0) == -1)
59 {
60 NSLog(@"listen failed");
61 return nil;
62 }
63
64 // accept a connection
65 struct sockaddr_in remoteAddress;
66 socklen_t remoteAddressLen = sizeof(remoteAddress);
67 _socket = accept(socketOpen, (struct sockaddr *)&remoteAddress, &remoteAddressLen);
68 if (_socket < 0)
69 {
70 close(socketOpen);
71 NSLog(@"could not accept() the socket");
72 return nil;
73 }
74
75 // we're done listening now that we have a connection
76 close(socketOpen);
77 }
78 return self;
79 }
80
81 /**
82 * Close our socket and clean up anything else
83 */
84 - (void)dealloc
85 {
86 close(_socket);
87
88 [super dealloc];
89 }
90
91 /**
92 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
93 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
94 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
95 * will return something (which we almost always do).
96 *
97 * This function can only read a set amount of bytes (1024 to be exact). If anything is larger, then partial
98 * data will be returned. Don't shoot the messenger... even if it is our fault.
99 *
100 * Data string returned is autorelease'd
101 */
102 - (NSString *)receive
103 {
104 // create a buffer
105 char buffer[1024];
106
107 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
108 int recvd = recv(_socket, &buffer, sizeof(buffer), 0);
109
110 // the length of the packet
111 // packet is formatted in len<null>packet
112 int length = atoi(buffer);
113
114 // take the received data and put it into an NSData
115 NSMutableData *data = [NSMutableData data];
116
117 // strip the length from the packet, and clear the null byte then add it to the NSData
118 for (int i = sizeof(length) - 1; i >= 0; i--)
119 {
120 buffer[i] = ' ';
121 }
122 [data appendBytes: buffer length: recvd];
123
124 // check if we have a partial packet
125 if (length + sizeof(length) > sizeof(buffer))
126 {
127 while (recvd < length)
128 {
129 int latest = recv(_socket, &buffer, sizeof(buffer), 0);
130 if (latest < 1)
131 {
132 NSLog(@"socket closed or error");
133 }
134 [data appendBytes: buffer length: latest];
135 recvd += latest;
136 }
137 }
138
139 // convert the NSData into a NSString
140 return [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
141 }
142
143 /**
144 * Sends a given NSString over the socket
145 */
146 - (void)send: (NSString *)data
147 {
148 // TODO - implement me
149 }
150
151 @end